File: simple.rs

package info (click to toggle)
rust-rspec 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 312 kB
  • sloc: makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download
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
extern crate rspec;

pub fn main() {
    // The easiest way to open a suite is by calling the `rspec::run(…)` function,
    // passing it the result of one of these functions:
    //
    // - `rspec::suite`,
    // - `rspec::describe`
    // - `rspec::given`
    //
    // which all behave the same and only differ in the label
    // that is printed the the est suite's log.
    //
    // One then passes the following arguments to aforementioned function:
    //
    // - a name (to add some more meaning to the runner's output)
    // - an initial value (to base the tests on)
    // - a closure (to provide the suite's test logic)
    rspec::run(&rspec::given("a value of zero", 0, |ctx| {
        ctx.then("it is zero", |value| {
            assert_eq!(*value, 0);
        });

        ctx.when("multiplying by itself", |ctx| {
            // Any time one wants to mutate the value being tested
            // one does so by calling `ctx.before(…)` (or `ctx.after(…)`),
            // which will execute the provided closure before any other
            // sub-context (e.g. `ctx.when(…)`) or example (e.g. `ctx.then(…)`)
            // is executed:
            ctx.before(|value| {
                *value *= *value;
            });

            ctx.then("it remains zero", |value| {
                assert_eq!(*value, 0);
            });
        });

        ctx.when("adding a value to it", |ctx| {
            ctx.before(|value| {
                *value += 42;
            });

            ctx.then("it becomes said value", |value| {
                assert_eq!(*value, 42);
            });
        });
    }));
}