asserting/char/
mod.rs

1use crate::assertions::AssertChar;
2use crate::colored::{mark_missing_string, mark_unexpected_char};
3use crate::expectations::{
4    is_alphabetic, is_alphanumeric, is_ascii, is_control_char, is_digit, is_lower_case,
5    is_upper_case, is_whitespace, IsAlphabetic, IsAlphanumeric, IsAscii, IsControlChar, IsDigit,
6    IsLowerCase, IsUpperCase, IsWhitespace,
7};
8use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
9use crate::std::format;
10use crate::std::string::{String, ToString};
11
12impl<R> AssertChar for Spec<'_, char, R>
13where
14    R: FailingStrategy,
15{
16    fn is_lowercase(self) -> Self {
17        self.expecting(is_lower_case())
18    }
19
20    fn is_uppercase(self) -> Self {
21        self.expecting(is_upper_case())
22    }
23
24    fn is_ascii(self) -> Self {
25        self.expecting(is_ascii())
26    }
27
28    fn is_alphabetic(self) -> Self {
29        self.expecting(is_alphabetic())
30    }
31
32    fn is_alphanumeric(self) -> Self {
33        self.expecting(is_alphanumeric())
34    }
35
36    fn is_control_char(self) -> Self {
37        self.expecting(is_control_char())
38    }
39
40    fn is_digit(self, radix: u32) -> Self {
41        self.expecting(is_digit(radix))
42    }
43
44    fn is_whitespace(self) -> Self {
45        self.expecting(is_whitespace())
46    }
47}
48
49impl<R> AssertChar for Spec<'_, &char, R>
50where
51    R: FailingStrategy,
52{
53    fn is_lowercase(self) -> Self {
54        self.expecting(is_lower_case())
55    }
56
57    fn is_uppercase(self) -> Self {
58        self.expecting(is_upper_case())
59    }
60
61    fn is_ascii(self) -> Self {
62        self.expecting(is_ascii())
63    }
64
65    fn is_alphabetic(self) -> Self {
66        self.expecting(is_alphabetic())
67    }
68
69    fn is_alphanumeric(self) -> Self {
70        self.expecting(is_alphanumeric())
71    }
72
73    fn is_control_char(self) -> Self {
74        self.expecting(is_control_char())
75    }
76
77    fn is_digit(self, radix: u32) -> Self {
78        self.expecting(is_digit(radix))
79    }
80
81    fn is_whitespace(self) -> Self {
82        self.expecting(is_whitespace())
83    }
84}
85
86impl Expectation<char> for IsLowerCase {
87    fn test(&mut self, subject: &char) -> bool {
88        subject.is_lowercase()
89    }
90
91    fn message(
92        &self,
93        expression: &Expression<'_>,
94        actual: &char,
95        inverted: bool,
96        format: &DiffFormat,
97    ) -> String {
98        let (not, expected) = if inverted {
99            ("not ", actual.to_uppercase().to_string())
100        } else {
101            ("", actual.to_lowercase().to_string())
102        };
103        let marked_actual = mark_unexpected_char(*actual, format);
104        let marked_expected = mark_missing_string(&expected, format);
105        format!("expected {expression} to be {not}lowercase\n   but was: {marked_actual}\n  expected: {marked_expected}")
106    }
107}
108
109impl Invertible for IsLowerCase {}
110
111impl Expectation<&char> for IsLowerCase {
112    fn test(&mut self, subject: &&char) -> bool {
113        <Self as Expectation<char>>::test(self, subject)
114    }
115
116    fn message(
117        &self,
118        expression: &Expression<'_>,
119        actual: &&char,
120        inverted: bool,
121        format: &DiffFormat,
122    ) -> String {
123        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
124    }
125}
126
127impl Expectation<char> for IsUpperCase {
128    fn test(&mut self, subject: &char) -> bool {
129        subject.is_uppercase()
130    }
131
132    fn message(
133        &self,
134        expression: &Expression<'_>,
135        actual: &char,
136        inverted: bool,
137        format: &DiffFormat,
138    ) -> String {
139        let (not, expected) = if inverted {
140            ("not ", actual.to_lowercase().to_string())
141        } else {
142            ("", actual.to_uppercase().to_string())
143        };
144        let marked_actual = mark_unexpected_char(*actual, format);
145        let marked_expected = mark_missing_string(&expected, format);
146        format!("expected {expression} to be {not}uppercase\n   but was: {marked_actual}\n  expected: {marked_expected}")
147    }
148}
149
150impl Invertible for IsUpperCase {}
151
152impl Expectation<&char> for IsUpperCase {
153    fn test(&mut self, subject: &&char) -> bool {
154        <Self as Expectation<char>>::test(self, subject)
155    }
156
157    fn message(
158        &self,
159        expression: &Expression<'_>,
160        actual: &&char,
161        inverted: bool,
162        format: &DiffFormat,
163    ) -> String {
164        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
165    }
166}
167
168impl Expectation<char> for IsAscii {
169    fn test(&mut self, subject: &char) -> bool {
170        subject.is_ascii()
171    }
172
173    fn message(
174        &self,
175        expression: &Expression<'_>,
176        actual: &char,
177        inverted: bool,
178        format: &DiffFormat,
179    ) -> String {
180        let not = if inverted { "not " } else { "" };
181        let marked_actual = mark_unexpected_char(*actual, format);
182        format!("expected {expression} to be {not}an ASCII character\n   but was: {marked_actual}\n  expected: {not}an ASCII character")
183    }
184}
185
186impl Invertible for IsAscii {}
187
188impl Expectation<&char> for IsAscii {
189    fn test(&mut self, subject: &&char) -> bool {
190        <Self as Expectation<char>>::test(self, subject)
191    }
192
193    fn message(
194        &self,
195        expression: &Expression<'_>,
196        actual: &&char,
197        inverted: bool,
198        format: &DiffFormat,
199    ) -> String {
200        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
201    }
202}
203
204impl Expectation<char> for IsAlphabetic {
205    fn test(&mut self, subject: &char) -> bool {
206        subject.is_alphabetic()
207    }
208
209    fn message(
210        &self,
211        expression: &Expression<'_>,
212        actual: &char,
213        inverted: bool,
214        format: &DiffFormat,
215    ) -> String {
216        let not = if inverted { "not " } else { "" };
217        let marked_actual = mark_unexpected_char(*actual, format);
218        format!("expected {expression} to be {not}an alphabetic character\n   but was: {marked_actual}\n  expected: {not}an alphabetic character")
219    }
220}
221
222impl Invertible for IsAlphabetic {}
223
224impl Expectation<&char> for IsAlphabetic {
225    fn test(&mut self, subject: &&char) -> bool {
226        <Self as Expectation<char>>::test(self, subject)
227    }
228
229    fn message(
230        &self,
231        expression: &Expression<'_>,
232        actual: &&char,
233        inverted: bool,
234        format: &DiffFormat,
235    ) -> String {
236        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
237    }
238}
239
240impl Expectation<char> for IsAlphanumeric {
241    fn test(&mut self, subject: &char) -> bool {
242        subject.is_alphanumeric()
243    }
244
245    fn message(
246        &self,
247        expression: &Expression<'_>,
248        actual: &char,
249        inverted: bool,
250        format: &DiffFormat,
251    ) -> String {
252        let not = if inverted { "not " } else { "" };
253        let marked_actual = mark_unexpected_char(*actual, format);
254        format!("expected {expression} to be {not}an alphanumeric character\n   but was: {marked_actual}\n  expected: {not}an alphanumeric character")
255    }
256}
257
258impl Invertible for IsAlphanumeric {}
259
260impl Expectation<&char> for IsAlphanumeric {
261    fn test(&mut self, subject: &&char) -> bool {
262        <Self as Expectation<char>>::test(self, subject)
263    }
264
265    fn message(
266        &self,
267        expression: &Expression<'_>,
268        actual: &&char,
269        inverted: bool,
270        format: &DiffFormat,
271    ) -> String {
272        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
273    }
274}
275
276impl Expectation<char> for IsControlChar {
277    fn test(&mut self, subject: &char) -> bool {
278        subject.is_control()
279    }
280
281    fn message(
282        &self,
283        expression: &Expression<'_>,
284        actual: &char,
285        inverted: bool,
286        format: &DiffFormat,
287    ) -> String {
288        let not = if inverted { "not " } else { "" };
289        let marked_actual = mark_unexpected_char(*actual, format);
290        format!("expected {expression} to be {not}a control character\n   but was: {marked_actual}\n  expected: {not}a control character")
291    }
292}
293
294impl Invertible for IsControlChar {}
295
296impl Expectation<&char> for IsControlChar {
297    fn test(&mut self, subject: &&char) -> bool {
298        <Self as Expectation<char>>::test(self, subject)
299    }
300
301    fn message(
302        &self,
303        expression: &Expression<'_>,
304        actual: &&char,
305        inverted: bool,
306        format: &DiffFormat,
307    ) -> String {
308        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
309    }
310}
311
312impl Expectation<char> for IsDigit {
313    fn test(&mut self, subject: &char) -> bool {
314        subject.is_digit(self.radix)
315    }
316
317    fn message(
318        &self,
319        expression: &Expression<'_>,
320        actual: &char,
321        inverted: bool,
322        format: &DiffFormat,
323    ) -> String {
324        let not = if inverted { "not " } else { "" };
325        let radix = self.radix;
326        let marked_actual = mark_unexpected_char(*actual, format);
327        format!("expected {expression} to be {not}a digit in the radix {radix}\n   but was: {marked_actual}\n  expected: {not}a digit in the radix {radix}")
328    }
329}
330
331impl Invertible for IsDigit {}
332
333impl Expectation<&char> for IsDigit {
334    fn test(&mut self, subject: &&char) -> bool {
335        <Self as Expectation<char>>::test(self, subject)
336    }
337
338    fn message(
339        &self,
340        expression: &Expression<'_>,
341        actual: &&char,
342        inverted: bool,
343        format: &DiffFormat,
344    ) -> String {
345        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
346    }
347}
348
349impl Expectation<char> for IsWhitespace {
350    fn test(&mut self, subject: &char) -> bool {
351        subject.is_whitespace()
352    }
353
354    fn message(
355        &self,
356        expression: &Expression<'_>,
357        actual: &char,
358        inverted: bool,
359        format: &DiffFormat,
360    ) -> String {
361        let not = if inverted { "not " } else { "" };
362        let marked_actual = mark_unexpected_char(*actual, format);
363        format!("expected {expression} to be {not}whitespace\n   but was: {marked_actual}\n  expected: {not}whitespace")
364    }
365}
366
367impl Invertible for IsWhitespace {}
368
369impl Expectation<&char> for IsWhitespace {
370    fn test(&mut self, subject: &&char) -> bool {
371        <Self as Expectation<char>>::test(self, subject)
372    }
373
374    fn message(
375        &self,
376        expression: &Expression<'_>,
377        actual: &&char,
378        inverted: bool,
379        format: &DiffFormat,
380    ) -> String {
381        <Self as Expectation<char>>::message(self, expression, actual, inverted, format)
382    }
383}
384
385#[cfg(test)]
386mod tests;