simple/
simple.rs

1extern crate rspec;
2
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}