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
/*!
An assertion utility focused on unit testing.

This library provides the variant of assertion macros that does not panic
and continues test cases whenever assertions are passed or not.

# Example

```
use expected::{expected, expect};

let name = "Alice";
let age = 14;

let ((), disappoints) = expected(|| {
    expect!(name == "Alice");
    expect!(age == 18);
    // ...
});

if let Some(disappoints) = disappoints {
    eprintln!("{}", disappoints);
}
```
!*/

#![doc(html_root_url = "https://docs.rs/expected/0.0.1")]
#![deny(missing_docs)]
#![forbid(clippy::todo, clippy::unimplemented)]
#![cfg_attr(test, deny(warnings))]

mod context;
mod disappoint;
mod macros;

cfg_if::cfg_if! {
    if #[cfg(feature = "futures")] {
        mod futures;
        pub use futures::{FutureExpectedExt, Expected};
    }
}

pub use crate::disappoint::{Disappoint, Disappoints};

use crate::context::Context;

/// Run the provided closure and checks to see if all expectation have been satisfied.
pub fn expected<F, R>(f: F) -> (R, Option<Disappoints>)
where
    F: FnOnce() -> R,
{
    let mut ctx = Context::default();
    let value = ctx.set(f);
    (value, ctx.take_disappoints())
}

#[doc(hidden)] // private API
#[inline(never)]
pub fn disappoint(
    payload: Box<dyn std::any::Any + Send>,
    file: &'static str,
    line: u32,
    column: u32,
) {
    Context::with(|ctx| {
        ctx.add_disappoint(Disappoint::new(payload, file, line, column));
    })
    .unwrap_or_else(|| {
        eprintln!("warning: expect!() should be invoked inside of `expected`.");
    });
}