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#[derive(Clone, Debug)]
22pub enum CheckResult {
23 Pass,
25 Fail(String),
27}
28
29pub trait Expectation<T: Debug> {
31 fn check(&self, value: &T) -> CheckResult;
35}
36
37pub trait ExpectationBuilder<'e> {
39 type Value: Debug + 'e;
41
42 fn to_pass(self, expectation: impl Expectation<Self::Value> + 'e) -> Self;
45}
46
47pub fn expect<'e, T: Debug>(value: T) -> RootExpectations<'e, T> {
56 RootExpectations::new(value)
57}
58
59pub 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}