asserting/
char_count.rs

1//! Implementations of the character count assertions.
2
3use crate::assertions::AssertHasCharCount;
4use crate::colored::{mark_missing, mark_unexpected};
5use crate::expectations::{
6    HasAtLeastCharCount, HasAtMostCharCount, HasCharCount, HasCharCountGreaterThan,
7    HasCharCountInRange, HasCharCountLessThan,
8};
9use crate::properties::CharCountProperty;
10use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Spec};
11use crate::std::fmt::Debug;
12use crate::std::format;
13use crate::std::ops::RangeBounds;
14use crate::std::string::String;
15
16impl<S, R> AssertHasCharCount<usize> for Spec<'_, S, R>
17where
18    S: CharCountProperty + Debug,
19    R: FailingStrategy,
20{
21    fn has_char_count(self, expected_char_count: usize) -> Self {
22        self.expecting(HasCharCount {
23            expected_char_count,
24        })
25    }
26
27    fn has_char_count_in_range<U>(self, expected_range: U) -> Self
28    where
29        U: RangeBounds<usize> + Debug,
30    {
31        self.expecting(HasCharCountInRange::new(expected_range))
32    }
33
34    fn has_char_count_less_than(self, expected_char_count: usize) -> Self {
35        self.expecting(HasCharCountLessThan {
36            expected_char_count,
37        })
38    }
39
40    fn has_char_count_greater_than(self, expected_char_count: usize) -> Self {
41        self.expecting(HasCharCountGreaterThan {
42            expected_char_count,
43        })
44    }
45
46    fn has_at_most_char_count(self, expected_char_count: usize) -> Self {
47        self.expecting(HasAtMostCharCount {
48            expected_char_count,
49        })
50    }
51
52    fn has_at_least_char_count(self, expected_char_count: usize) -> Self {
53        self.expecting(HasAtLeastCharCount {
54            expected_char_count,
55        })
56    }
57}
58
59impl<S> Expectation<S> for HasCharCount<usize>
60where
61    S: CharCountProperty + Debug,
62{
63    fn test(&mut self, subject: &S) -> bool {
64        subject.char_count_property() == self.expected_char_count
65    }
66
67    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
68        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
69        let marked_expected = mark_missing(&self.expected_char_count, format);
70        format!(
71            "expected {expression} has a char count of {:?}\n   but was: {marked_actual}\n  expected: {marked_expected}",
72            self.expected_char_count
73        )
74    }
75}
76
77impl<S, R> Expectation<S> for HasCharCountInRange<R, usize>
78where
79    S: CharCountProperty + Debug,
80    R: RangeBounds<usize> + Debug,
81{
82    fn test(&mut self, subject: &S) -> bool {
83        self.expected_range.contains(&subject.char_count_property())
84    }
85
86    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
87        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
88        let marked_expected = mark_missing(&self.expected_range, format);
89        format!(
90            "expected {expression} has a char count of {:?}\n   but was: {marked_actual}\n  expected: {marked_expected}",
91            self.expected_range,
92        )
93    }
94}
95
96impl<S> Expectation<S> for HasCharCountLessThan<usize>
97where
98    S: CharCountProperty + Debug,
99{
100    fn test(&mut self, subject: &S) -> bool {
101        subject.char_count_property() < self.expected_char_count
102    }
103
104    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
105        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
106        let marked_expected = mark_missing(&self.expected_char_count, format);
107        format!(
108            "expected {expression} has a char count less than {:?}\n   but was: {marked_actual}\n  expected: < {marked_expected}",
109            self.expected_char_count,
110        )
111    }
112}
113
114impl<S> Expectation<S> for HasCharCountGreaterThan<usize>
115where
116    S: CharCountProperty + Debug,
117{
118    fn test(&mut self, subject: &S) -> bool {
119        subject.char_count_property() > self.expected_char_count
120    }
121
122    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
123        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
124        let marked_expected = mark_missing(&self.expected_char_count, format);
125        format!(
126            "expected {expression} has a char count greater than {:?}\n   but was: {marked_actual}\n  expected: > {marked_expected}",
127            self.expected_char_count,
128        )
129    }
130}
131
132impl<S> Expectation<S> for HasAtMostCharCount<usize>
133where
134    S: CharCountProperty + Debug,
135{
136    fn test(&mut self, subject: &S) -> bool {
137        subject.char_count_property() <= self.expected_char_count
138    }
139
140    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
141        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
142        let marked_expected = mark_missing(&self.expected_char_count, format);
143        format!(
144            "expected {expression} has at most a char count of {:?}\n   but was: {marked_actual}\n  expected: <= {marked_expected}",
145            self.expected_char_count,
146        )
147    }
148}
149
150impl<S> Expectation<S> for HasAtLeastCharCount<usize>
151where
152    S: CharCountProperty,
153{
154    fn test(&mut self, subject: &S) -> bool {
155        subject.char_count_property() >= self.expected_char_count
156    }
157
158    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
159        let marked_actual = mark_unexpected(&actual.char_count_property(), format);
160        let marked_expected = mark_missing(&self.expected_char_count, format);
161        format!(
162            "expected {expression} has at least a char count of {:?}\n   but was: {marked_actual}\n  expected: >= {marked_expected}",
163            self.expected_char_count,
164        )
165    }
166}