advanced/
advanced.rs

1extern crate rspec;
2
3use std::collections::BTreeSet;
4
5pub fn main() {
6    #[derive(Clone, Debug)]
7    struct Environment {
8        set: BTreeSet<usize>,
9        len_before: usize,
10    }
11
12    let environment = Environment {
13        set: BTreeSet::new(),
14        len_before: 0,
15    };
16
17    rspec::run(&rspec::given("a BTreeSet", environment, |ctx| {
18        ctx.when("not having added any items", |ctx| {
19            ctx.then("it is empty", |env| assert!(env.set.is_empty()));
20        });
21
22        ctx.when("adding an new item", |ctx| {
23            ctx.before_all(|env| {
24                env.len_before = env.set.len();
25                env.set.insert(42);
26            });
27
28            ctx.then("it is not empty any more", |env| {
29                assert!(!env.set.is_empty());
30            });
31
32            ctx.then("its len increases by 1", |env| {
33                assert_eq!(env.set.len(), env.len_before + 1);
34            });
35
36            ctx.when("adding it again", |ctx| {
37                ctx.before_all(|env| {
38                    env.len_before = env.set.len();
39                    env.set.insert(42);
40                });
41
42                ctx.then("its len remains the same", |env| {
43                    assert_eq!(env.set.len(), env.len_before);
44                });
45            });
46        });
47
48        ctx.when("returning to outer context", |ctx| {
49            ctx.then("it is still empty", |env| assert!(env.set.is_empty()));
50        });
51
52        ctx.then("panic!(…) fails", |_env| {
53            panic!("Some reason for failure.")
54        });
55    }));
56}