speculate!() { /* proc-macro */ }
Expand description
Creates a test
module using a friendly syntax.
Inside this block, the following elements can be used:
-
describe
(or its aliascontext
) - to group tests in a hierarchy, for readability. Can be arbitrarily nested. -
before
- contains setup code that’s inserted before every sibling and nestedit
andbench
blocks. -
after
- contains teardown code that’s inserted after every sibling and nestedit
andbench
blocks. -
it
(or its aliastest
) - contains tests.For example:
#[macro_use] extern crate speculate as other_speculate; it "can add 1 and 2" { assert_eq!(1 + 2, 3); }
You can optionally add attributes to this block:
#[macro_use] extern crate speculate as other_speculate; #[ignore] test "ignore" { assert_eq!(1, 2); } #[should_panic] test "should panic" { assert_eq!(1, 2); } #[should_panic(expected = "foo")] test "should panic with foo" { panic!("foo"); }
-
bench
- contains benchmarks (usingBencher
).For example:
#[macro_use] extern crate speculate as other_speculate; bench "xor 1 to 1000" |b| { // Here, `b` is a `test::Bencher`. b.iter(|| (0..1000).fold(0, |a, b| a ^ b)); }
-
Any other Rust “Item”, such as
static
,const
,fn
, etc.
§Example
#[macro_use] extern crate speculate as other_speculate;
speculate! {
const ZERO: i32 = 0;
fn add(a: i32, b: i32) -> i32 {
a + b
}
describe "math" {
const ONE: i32 = 1;
fn sub(a: i32, b: i32) -> i32 {
a - b
}
before {
let two = ONE + ONE;
}
it "can add stuff" {
assert_eq!(ONE, add(ZERO, ONE));
assert_eq!(two, add(ONE, ONE));
}
it "can subtract stuff" {
assert_eq!(ZERO, sub(ONE, ONE));
assert_eq!(ONE, sub(two, ONE));
}
}
}