Function run

Source
pub fn run<T>(suite: &Suite<T>)
where T: Clone + Send + Sync + Debug,
Expand description

A wrapper for conveniently running a test suite with the default configuration with considerebly less glue-code.

§Examples

rspec::run(&rspec::given("a scenario", (), |ctx| {
    ctx.when("...", |ctx| {
        // ...
    });

    ctx.then("...", |env| { /* ... */ });
}));
Examples found in repository?
examples/advanced.rs (lines 17-55)
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}
More examples
Hide additional examples
examples/simple.rs (lines 19-48)
3pub fn main() {
4    // The easiest way to open a suite is by calling the `rspec::run(…)` function,
5    // passing it the result of one of these functions:
6    //
7    // - `rspec::suite`,
8    // - `rspec::describe`
9    // - `rspec::given`
10    //
11    // which all behave the same and only differ in the label
12    // that is printed the the est suite's log.
13    //
14    // One then passes the following arguments to aforementioned function:
15    //
16    // - a name (to add some more meaning to the runner's output)
17    // - an initial value (to base the tests on)
18    // - a closure (to provide the suite's test logic)
19    rspec::run(&rspec::given("a value of zero", 0, |ctx| {
20        ctx.then("it is zero", |value| {
21            assert_eq!(*value, 0);
22        });
23
24        ctx.when("multiplying by itself", |ctx| {
25            // Any time one wants to mutate the value being tested
26            // one does so by calling `ctx.before(…)` (or `ctx.after(…)`),
27            // which will execute the provided closure before any other
28            // sub-context (e.g. `ctx.when(…)`) or example (e.g. `ctx.then(…)`)
29            // is executed:
30            ctx.before(|value| {
31                *value *= *value;
32            });
33
34            ctx.then("it remains zero", |value| {
35                assert_eq!(*value, 0);
36            });
37        });
38
39        ctx.when("adding a value to it", |ctx| {
40            ctx.before(|value| {
41                *value += 42;
42            });
43
44            ctx.then("it becomes said value", |value| {
45                assert_eq!(*value, 42);
46            });
47        });
48    }));
49}