pub struct TestCase { /* private fields */ }Expand description
A handle to the current test case.
This is passed to #[hegel::test] functions and provides methods
for drawing values, making assumptions, and recording notes.
§Example
use hegel::generators as gs;
#[hegel::test]
fn my_test(tc: hegel::TestCase) {
let x: i32 = tc.draw(gs::integers());
tc.assume(x > 0);
tc.note(&format!("x = {}", x));
}Implementations§
Source§impl TestCase
impl TestCase
Sourcepub fn draw<T: Debug>(&self, generator: impl Generator<T>) -> T
pub fn draw<T: Debug>(&self, generator: impl Generator<T>) -> T
Draw a value from a generator.
§Example
use hegel::generators as gs;
#[hegel::test]
fn my_test(tc: hegel::TestCase) {
let x: i32 = tc.draw(gs::integers());
let s: String = tc.draw(gs::text());
}Examples found in repository?
More examples
examples/stack.rs (line 15)
13 fn push(&mut self, tc: TestCase) {
14 let integers = gs::integers::<i32>;
15 let element = tc.draw(integers());
16 self.stack.push(element);
17 }
18
19 #[rule]
20 fn pop(&mut self, _: TestCase) {
21 self.stack.pop();
22 }
23
24 #[rule]
25 fn pop_push(&mut self, tc: TestCase) {
26 let integers = gs::integers::<i32>;
27 let element = tc.draw(integers());
28 let initial = self.stack.clone();
29 self.stack.push(element);
30 let popped = self.stack.pop().unwrap();
31 assert_eq!(popped, element);
32 assert_eq!(self.stack, initial);
33 }examples/ledger.rs (line 51)
50 fn create_account(&mut self, tc: TestCase) {
51 let account = tc.draw(gs::text().min_size(1));
52 tc.note(&format!("create account '{}'", account.clone()));
53 self.accounts.add(account);
54 }
55
56 #[rule]
57 fn credit(&mut self, tc: TestCase) {
58 let account = self.accounts.draw().clone();
59 let amount = tc.draw(gs::integers::<i64>().min_value(0).max_value(LIMIT));
60 tc.note(&format!("credit '{}' with {}", account.clone(), amount));
61 self.ledger.credit(account, amount);
62 }
63
64 #[rule]
65 fn transfer(&mut self, tc: TestCase) {
66 let from = self.accounts.draw().clone();
67 let to = self.accounts.draw().clone();
68 let amount = tc.draw(gs::integers::<i64>().min_value(0).max_value(LIMIT));
69 tc.note(&format!(
70 "transfer '{}' from {} to {}",
71 amount,
72 from.clone(),
73 to.clone()
74 ));
75 self.ledger.transfer(from, to, amount);
76 }Sourcepub fn draw_silent<T>(&self, generator: impl Generator<T>) -> T
pub fn draw_silent<T>(&self, generator: impl Generator<T>) -> T
Draw a value from a generator without recording it in the output.
Unlike draw, this does not require T: Debug and
will not print the value in the failing-test summary.
Sourcepub fn assume(&self, condition: bool)
pub fn assume(&self, condition: bool)
Assume a condition is true. If false, reject the current test input.
§Example
use hegel::generators as gs;
#[hegel::test]
fn my_test(tc: hegel::TestCase) {
let age: u32 = tc.draw(gs::integers());
tc.assume(age >= 18);
}Sourcepub fn note(&self, message: &str)
pub fn note(&self, message: &str)
Note a message which will be displayed with the reported failing test case.
Only prints during the final replay of a failing test case.
§Example
use hegel::generators as gs;
#[hegel::test]
fn my_test(tc: hegel::TestCase) {
let x: i32 = tc.draw(gs::integers());
tc.note(&format!("Generated x = {}", x));
}Examples found in repository?
More examples
examples/ledger.rs (line 52)
50 fn create_account(&mut self, tc: TestCase) {
51 let account = tc.draw(gs::text().min_size(1));
52 tc.note(&format!("create account '{}'", account.clone()));
53 self.accounts.add(account);
54 }
55
56 #[rule]
57 fn credit(&mut self, tc: TestCase) {
58 let account = self.accounts.draw().clone();
59 let amount = tc.draw(gs::integers::<i64>().min_value(0).max_value(LIMIT));
60 tc.note(&format!("credit '{}' with {}", account.clone(), amount));
61 self.ledger.credit(account, amount);
62 }
63
64 #[rule]
65 fn transfer(&mut self, tc: TestCase) {
66 let from = self.accounts.draw().clone();
67 let to = self.accounts.draw().clone();
68 let amount = tc.draw(gs::integers::<i64>().min_value(0).max_value(LIMIT));
69 tc.note(&format!(
70 "transfer '{}' from {} to {}",
71 amount,
72 from.clone(),
73 to.clone()
74 ));
75 self.ledger.transfer(from, to, amount);
76 }Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TestCase
impl !RefUnwindSafe for TestCase
impl !Send for TestCase
impl !Sync for TestCase
impl Unpin for TestCase
impl UnsafeUnpin for TestCase
impl !UnwindSafe for TestCase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more