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