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