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
// Here we will define a struct with
// two members, an associated function,
// and two methods.
struct Foo {
line: String,
count: i32
}
impl Foo {
pub fn new() -> Foo {
Foo { line: String::new(), count: 0 }
}
pub fn append(&mut self, str: &str) {
self.line += str;
}
pub fn increase(&mut self) {
self.count += 1;
}
}
fn main () {
let mut foo = Foo::new();
foo.append("fizzbuzz");
foo.increase();
}
#[cfg(test)]
mod tests {
// Pull the Foo struct into scope
use super::*;
// Now pull in the lab tools
use laboratory::{LabResult, describe, expect, NullState};
// Define a single test
#[test]
fn test() -> LabResult {
// Now we can describe Foo.
// Notice how the callback closure takes an argument name "suite".
// the suite variable is the suite's context and many child suites, specs
// and options can be defined using that struct.
// The suite context struct has a method named describe where a developer
// can append child suites that are relevant to the parent such as
// a method or member of a struct.
describe("Foo", |suite| {
// Here we can describe the "new" associated function
suite.describe("#new()", |suite| {
suite.it("should return an instance of Foo with two members", |_| {
let foo = Foo::new();
expect(foo.line).to_be(String::new())?;
expect(foo.count).to_equal(0)
});
})
// Now we will describe the "append" method
.describe("#append()", |suite| {
suite.it("should append \"fizzbuzz\" to Foo#line", |_| {
let mut foo = Foo::new();
foo.append("fizzbuzz");
expect(foo.line).to_be("fizzbuzz".to_string())
});
})
// Finally, we will describe the "increase" method
.describe("#increase()", |suite| {
suite.it("should increase Foo#count by 1", |_| {
let mut foo = Foo::new();
foo.increase();
expect(foo.count).to_equal(1)
});
});
}).state(NullState).milis().run()
}
}