1use crate::borrow::BorrowedOrOwned;
2use crate::expectation_list::ExpectationList;
3use crate::{CheckResult, Expectation, ExpectationBuilder};
4use std::fmt::Debug;
5
6pub struct RootExpectations<'e, T: Debug> {
15 value: BorrowedOrOwned<'e, T>,
16 expectations: ExpectationList<'e, T>,
17}
18
19impl<'e, T: Debug> RootExpectations<'e, T> {
20 pub fn new(value: T) -> Self {
22 RootExpectations {
23 expectations: ExpectationList::new(),
24 value: BorrowedOrOwned::Owned(value),
25 }
26 }
27
28 pub fn new_ref(value: &'e T) -> Self {
30 RootExpectations {
31 expectations: ExpectationList::new(),
32 value: BorrowedOrOwned::Borrowed(value),
33 }
34 }
35
36 pub fn check(self) {
38 drop(self)
39 }
40}
41
42impl<'e, T: Debug + 'e> ExpectationBuilder<'e> for RootExpectations<'e, T> {
43 type Value = T;
45
46 fn to_pass(mut self, expectation: impl Expectation<T> + 'e) -> Self {
48 self.expectations.push(expectation);
49 self
50 }
51}
52
53impl<'e, T: Debug> Drop for RootExpectations<'e, T> {
54 fn drop(&mut self) {
55 let value = self.value.borrow_self();
56 if let CheckResult::Fail(message) = self.expectations.check(value) {
57 panic!("{}", message);
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use crate::tests::TestExpectation;
65 use crate::{CheckResult, ExpectationBuilder, expect, expect_ref};
66
67 #[test]
68 pub fn that_assert_runs_an_expectation() {
69 let (expectation, expected) = TestExpectation::new(CheckResult::Pass);
71
72 let expectations = expect(true).to_pass(expectation);
74
75 expectations.check();
77
78 assert!(*expected.lock().unwrap());
80 }
81
82 #[test]
83 pub fn that_assert_works_on_references() {
84 let (expectation, _) = TestExpectation::new(CheckResult::Pass);
86
87 let value = true;
89 expect_ref(&value).to_pass(expectation);
90 }
91
92 #[test]
93 pub fn that_check_runs_all_expectations() {
94 let (expectation1, expected1) = TestExpectation::new(CheckResult::Pass);
96 let (expectation2, expected2) = TestExpectation::new(CheckResult::Pass);
97
98 let expectations = expect(true).to_pass(expectation1).to_pass(expectation2);
100
101 expectations.check();
103
104 assert!(*expected1.lock().unwrap());
106 assert!(*expected2.lock().unwrap());
107 }
108
109 #[test]
110 #[should_panic]
111 pub fn that_failure_panics() {
112 let (expectation, _) = TestExpectation::new(CheckResult::Fail("message".to_owned()));
114
115 let expectations = expect(true).to_pass(expectation);
117
118 expectations.check();
120 }
121}