1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
fn main() {
add_one::add_one(0);
multiply_by_two::multiply_by_two(1);
}
// In this crate we have two
// public modules:
// - add_one, and
// - multiply_by_two
// here is the first module
pub mod add_one {
// and here is its function that we want to test
pub fn add_one (x: u64) -> u64 { x + 1 }
#[cfg(test)]
pub mod tests {
use super::*;
use laboratory::{describe, expect, Suite};
// Here is where we will define our first suite.
// Notice that this function returns a Suite struct.
// Also notice that no other methods are called on this suite.
pub fn suite<T>() -> Suite<T> {
describe("add_one()", |suite| {
suite.it("should return 1", |_| {
expect(add_one(0)).to_equal(1)
})
.it("should return 2", |_| {
expect(add_one(1)).to_equal(2)
});
})
}
}
}
// here is our second module
pub mod multiply_by_two {
// ...and the function we want to test
pub fn multiply_by_two (x: u64) -> u64 { x * 2 }
#[cfg(test)]
pub mod tests {
use super::*;
use laboratory::{describe, expect, Suite};
// Again, we will define a function that returns a Suite struct
pub fn suite<T>() -> Suite<T> {
describe("multiply_by_two()", |suite| {
suite.it("should return 2", |_| {
expect(multiply_by_two(1)).to_equal(2)
})
.it("should return 4", |_| {
expect(multiply_by_two(2)).to_equal(4)
});
})
}
}
}
// Now here is where we will import and run our
// tests under one umbrella of the crate.
#[cfg(test)]
mod tests {
// pull our modules into scope
use super::*;
// pull in our lab tools
use laboratory::{describe, LabResult, NullState};
#[test]
fn test() -> LabResult {
// Describe the crate.
// And using the describe_import() method we make the two
// modules child suites to be tested
describe("My Crate", |suite| {
suite
.describe_import(add_one::tests::suite())
.describe_import(multiply_by_two::tests::suite());
}).state(NullState)
// Now we can run our tests with any other options
.run()
}
}