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
72
73
74
75
76
77
78
79
80
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]

//! # Expect
//!
//! A flexible library for adding assertions to types.
//!
//! ```
//! # use expect::{Expect, Assertion};
//! # use std::default::Default;
//!
//! #[derive(Default)]
//! struct Point {
//!     x: f64,
//!     y: f64
//! }
//!
//! // Clockwise from the top left.
//! #[derive(Default)]
//! struct Square(Point, Point, Point, Point);
//!
//! #[derive(Default)]
//! struct Contains(Point);
//!
//! impl Assertion<Square> for Contains {
//!     fn assert(self, square: &Square) {
//!         assert!(self.0.x > square.0.x && self.0.x < square.1.x
//!              && self.0.y > square.2.y && self.0.y < square.1.y);
//!     }
//! }
//!
//! let square = Square(
//!     Point { x: 0.0, y: 1.0 },
//!     Point { x: 1.0, y: 1.0 },
//!     Point { x: 1.0, y: 0.0 },
//!     Point { x: 0.0, y: 0.0 },
//! );
//!
//! square
//!     .expect(Contains(Point { x: 0.5, y: 0.18 }))
//!     .expect(Contains(Point { x: 0.12, y: 0.9 }))
//!
//!     // You can also use tuples of Assertions.
//!     .expect((Contains(Point { x: 0.63, y: 0.4 }),
//!              Contains(Point { x: 0.7, y: 0.85 })));
//! ```
//!

/// An assertion that can be applied to a type.
pub trait Assertion<T: ?Sized>: Sized {
    /// Assert some property about T.
    ///
    /// ## Panics
    ///
    /// This method SHOULD panic if the property is not fulfilled.
    #[inline]
    fn assert(self, _: &T) { }
}

/// Extension trait for applying assertions.
pub trait Expect {
    /// Check an assertion on Self.
    #[inline]
    fn expect<A: Assertion<Self>>(self, assertion: A) -> Self where Self: Sized {
        assertion.assert(&self);
        self
    }

    /// Check an assertion on a reference to Self.
    #[inline]
    fn expect_ref<A: Assertion<Self>>(&self, assertion: A) -> &Self {
        assertion.assert(self);
        self
    }
}

impl<E> Expect for E {}

mod impls;