Skip to main content

rxpect/
lib.rs

1#![doc=include_str!("../README.md")]
2mod borrow;
3mod expectation_list;
4pub mod expectations;
5mod projection;
6mod root;
7
8pub use borrow::BorrowedOrOwned;
9pub use expectation_list::ExpectationList;
10pub use projection::ExpectProjection;
11pub use projection::ProjectedExpectationsBuilder;
12pub use root::RootExpectations;
13
14use std::fmt::Debug;
15
16#[doc = include_str!("../README.md")]
17#[cfg(doctest)]
18pub struct ReadmeDoctests;
19
20/// Result of an expectation check
21#[derive(Clone, Debug)]
22pub enum CheckResult {
23    /// The expectation passed
24    Pass,
25    /// The expectation failed, contains a message describing the failure
26    Fail(String),
27}
28
29/// An expectation on a value
30pub trait Expectation<T: Debug> {
31    /// Check this expectation
32    /// Returns CheckResult::Pass if the expectation pass
33    /// and CheckResult::Fail with a descriptive message if it didn't
34    fn check(&self, value: &T) -> CheckResult;
35}
36
37/// Trait to enable fluent building of expectations
38pub trait ExpectationBuilder<'e> {
39    /// Target value type for this builder
40    type Value: Debug + 'e;
41
42    /// Expect the value to pass an expectation
43    /// This is intended to be used in extension methods to add expectations to the builder
44    fn to_pass(self, expectation: impl Expectation<Self::Value> + 'e) -> Self;
45}
46
47/// Create expectations for a value.
48/// Used as an entrypoint for fluently building expectations
49/// ```
50/// use rxpect::expect;
51/// use rxpect::expectations::EqualityExpectations;
52///
53/// expect(1).to_equal(1);
54/// ```
55pub fn expect<'e, T: Debug>(value: T) -> RootExpectations<'e, T> {
56    RootExpectations::new(value)
57}
58
59/// Create expectations for a reference to a value.
60/// Used as an entrypoint for fluently building expectations
61/// ```
62/// use rxpect::expect_ref;
63/// use rxpect::expectations::EqualityExpectations;
64///
65/// expect_ref(&1).to_equal(1);
66/// ```
67pub fn expect_ref<T: Debug>(value: &'_ T) -> RootExpectations<'_, T> {
68    RootExpectations::new_ref(value)
69}
70
71#[cfg(test)]
72pub(crate) mod tests {
73    use crate::{CheckResult, Expectation};
74    use std::fmt::Debug;
75    use std::rc::Rc;
76    use std::sync::Mutex;
77
78    pub(crate) struct TestExpectation {
79        pub asserted: Rc<Mutex<bool>>,
80        result: CheckResult,
81    }
82
83    impl TestExpectation {
84        pub fn new(result: CheckResult) -> (TestExpectation, Rc<Mutex<bool>>) {
85            let asserted = Rc::new(Mutex::new(false));
86            (
87                TestExpectation {
88                    asserted: asserted.clone(),
89                    result,
90                },
91                asserted,
92            )
93        }
94    }
95
96    impl<T: Debug> Expectation<T> for TestExpectation {
97        fn check(&self, _: &T) -> CheckResult {
98            let mut asserted = self.asserted.lock().unwrap();
99            *asserted = true;
100            self.result.clone()
101        }
102    }
103}