Debian Codegraph @

debian/missing-sources/pretty_assertions/tests/macros.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![no_implicit_prelude]
3
4 #[cfg(feature = "alloc")]
5 extern crate alloc;
6
7 #[allow(clippy::eq_op)]
8 mod assert_str_eq {
9 use ::core::{cmp::PartialEq, convert::AsRef};
10
11 #[cfg(feature = "alloc")]
12 use ::alloc::string::{String, ToString};
13 #[cfg(feature = "std")]
14 use ::std::string::{String, ToString};
15
16 #[test]
17 fn passes_str() {
18 let a = "some value";
19 ::pretty_assertions::assert_str_eq!(a, a);
20 }
21
22 #[test]
23 fn passes_string() {
24 let a: String = "some value".to_string();
25 ::pretty_assertions::assert_str_eq!(a, a);
26 }
27
28 #[test]
29 fn passes_comparable_types() {
30 let s0: &'static str = "foo";
31 let s1: String = "foo".to_string();
32 ::pretty_assertions::assert_str_eq!(s0, s1);
33 }
34
35 #[derive(PartialEq)]
36 struct MyString(String);
37
38 impl AsRef<str> for MyString {
39 fn as_ref(&self) -> &str {
40 &self.0
41 }
42 }
43
44 impl PartialEq<String> for MyString {
45 fn eq(&self, other: &String) -> bool {
46 &self.0 == other
47 }
48 }
49
50 #[test]
51 fn passes_as_ref_types() {
52 let s0 = MyString("foo".to_string());
53 let s1 = "foo".to_string();
54 ::pretty_assertions::assert_str_eq!(s0, s1);
55 }
56
57 #[test]
58 #[should_panic(expected = r#"assertion failed: `(left == right)`
59
60 Diff < left / right > :
61 foo
62 <bar
63 >baz
64
65 "#)]
66 fn fails_as_ref_types() {
67 let s0 = MyString("foo\nbar".to_string());
68 let s1 = "foo\nbaz".to_string();
69 ::pretty_assertions::assert_str_eq!(s0, s1);
70 }
71
72 #[test]
73 #[should_panic(expected = r#"assertion failed: `(left == right)`
74
75 Diff < left / right > :
76 foo
77 <bar
78 >baz
79
80 "#)]
81 fn fails_foo() {
82 ::pretty_assertions::assert_str_eq!("foo\nbar", "foo\nbaz");
83 }
84 }
85
86 #[allow(clippy::eq_op)]
87 mod assert_eq {
88 #[cfg(feature = "alloc")]
89 use ::alloc::string::{String, ToString};
90 #[cfg(feature = "std")]
91 use ::std::string::{String, ToString};
92
93 #[test]
94 fn passes() {
95 let a = "some value";
96 ::pretty_assertions::assert_eq!(a, a);
97 }
98
99 #[test]
100 fn passes_unsized() {
101 let a: &[u8] = b"e";
102 ::pretty_assertions::assert_eq!(*a, *a);
103 }
104
105 #[test]
106 fn passes_comparable_types() {
107 let s0: &'static str = "foo";
108 let s1: String = "foo".to_string();
109 ::pretty_assertions::assert_eq!(s0, s1);
110 }
111
112 #[test]
113 #[should_panic(expected = r#"assertion failed: `(left == right)`
114
115 Diff < left / right > :
116 <666
117 >999
118
119 "#)]
120 fn fails() {
121 ::pretty_assertions::assert_eq!(666, 999);
122 }
123
124 #[test]
125 #[should_panic(expected = r#"assertion failed: `(left == right)`
126
127 Diff < left / right > :
128 <666
129 >999
130
131 "#)]
132 fn fails_trailing_comma() {
133 ::pretty_assertions::assert_eq!(666, 999,);
134 }
135
136 #[test]
137 #[should_panic(expected = r#"assertion failed: `(left == right)`
138
139 Diff < left / right > :
140 [
141 101,
142 > 101,
143 ]
144
145 "#)]
146 fn fails_unsized() {
147 let a: &[u8] = b"e";
148 let b: &[u8] = b"ee";
149 ::pretty_assertions::assert_eq!(*a, *b);
150 }
151
152 #[test]
153 #[should_panic(
154 expected = r#"assertion failed: `(left == right)`: custom panic message
155
156 Diff < left / right > :
157 <666
158 >999
159
160 "#
161 )]
162 fn fails_custom() {
163 ::pretty_assertions::assert_eq!(666, 999, "custom panic message");
164 }
165
166 #[test]
167 #[should_panic(
168 expected = r#"assertion failed: `(left == right)`: custom panic message
169
170 Diff < left / right > :
171 <666
172 >999
173
174 "#
175 )]
176 fn fails_custom_trailing_comma() {
177 ::pretty_assertions::assert_eq!(666, 999, "custom panic message",);
178 }
179
180 #[test]
181 #[should_panic(expected = r#"assertion failed: `(left == right)`
182
183 Diff < left / right > :
184 foo
185 <bar
186 >baz
187
188 "#)]
189 fn fails_str() {
190 ::pretty_assertions::assert_eq!("foo\nbar", "foo\nbaz");
191 }
192
193 #[test]
194 #[should_panic(expected = r#"assertion failed: `(left == right)`
195
196 Diff < left / right > :
197 foo
198 <bar
199 >baz
200
201 "#)]
202 fn fails_string() {
203 ::pretty_assertions::assert_eq!("foo\nbar".to_string(), "foo\nbaz".to_string());
204 }
205 }
206
207 mod assert_ne {
208 #[cfg(feature = "alloc")]
209 use ::alloc::string::{String, ToString};
210 #[cfg(feature = "std")]
211 use ::std::string::{String, ToString};
212
213 #[test]
214 fn passes() {
215 let a = "a";
216 let b = "b";
217 ::pretty_assertions::assert_ne!(a, b);
218 }
219
220 #[test]
221 fn passes_unsized() {
222 let a: &[u8] = b"e";
223 let b: &[u8] = b"ee";
224 ::pretty_assertions::assert_ne!(*a, *b);
225 }
226
227 #[test]
228 fn passes_comparable_types() {
229 let s0: &'static str = "foo";
230 let s1: String = "bar".to_string();
231 ::pretty_assertions::assert_ne!(s0, s1);
232 }
233
234 #[test]
235 #[should_panic(expected = r#"assertion failed: `(left != right)`
236
237 Both sides:
238 666
239 "#)]
240 fn fails() {
241 ::pretty_assertions::assert_ne!(666, 666);
242 }
243
244 #[test]
245 #[should_panic(expected = r#"assertion failed: `(left != right)`
246
247 Both sides:
248 666
249 "#)]
250 fn fails_trailing_comma() {
251 ::pretty_assertions::assert_ne!(666, 666,);
252 }
253
254 #[test]
255 #[should_panic(expected = r#"assertion failed: `(left != right)`
256
257 Both sides:
258 [
259 101,
260 ]
261
262 "#)]
263 fn fails_unsized() {
264 let a: &[u8] = b"e";
265 ::pretty_assertions::assert_ne!(*a, *a);
266 }
267
268 #[test]
269 #[should_panic(
270 expected = r#"assertion failed: `(left != right)`: custom panic message
271
272 Both sides:
273 666
274 "#
275 )]
276 fn fails_custom() {
277 ::pretty_assertions::assert_ne!(666, 666, "custom panic message");
278 }
279
280 #[test]
281 #[should_panic(
282 expected = r#"assertion failed: `(left != right)`: custom panic message
283
284 Both sides:
285 666
286 "#
287 )]
288 fn fails_custom_trailing_comma() {
289 ::pretty_assertions::assert_ne!(666, 666, "custom panic message",);
290 }
291
292 // If the values are equal but their debug outputs are not
293 // show a specific warning
294
295 // Regression tests
296
297 #[test]
298 #[should_panic]
299 fn assert_ne_non_empty_return() {
300 fn not_zero(x: u32) -> u32 {
301 ::pretty_assertions::assert_ne!(x, 0);
302 x
303 }
304 not_zero(0);
305 }
306 }
307
308 #[cfg(feature = "unstable")]
309 mod assert_matches {
310 use ::core::option::Option::{None, Some};
311
312 #[test]
313 fn passes() {
314 let a = Some("some value");
315 ::pretty_assertions::assert_matches!(a, Some(_));
316 }
317
318 #[test]
319 fn passes_unsized() {
320 let a: &[u8] = b"e";
321 ::pretty_assertions::assert_matches!(*a, _);
322 }
323
324 #[test]
325 #[should_panic(expected = r#"assertion failed: `(left matches right)`
326
327 Diff < left / right > :
328 <None
329 >Some(_)
330
331 "#)]
332 fn fails() {
333 ::pretty_assertions::assert_matches!(None::<usize>, Some(_));
334 }
335
336 #[test]
337 #[should_panic(expected = r#"assertion failed: `(left matches right)`
338
339 Diff < left / right > :
340 <Some(
341 < 3,
342 <)
343 >Some(3) if 0 > 0
344
345 "#)]
346 fn fails_guard() {
347 ::pretty_assertions::assert_matches!(Some(3), Some(3) if 0 > 0,);
348 }
349
350 #[test]
351 #[should_panic(expected = r#"assertion failed: `(left matches right)`
352
353 Diff < left / right > :
354 <[
355 < 101,
356 <]
357 >ref b if b == b"ee"
358
359 "#)]
360 fn fails_unsized() {
361 let a: &[u8] = b"e";
362 ::pretty_assertions::assert_matches!(*a, ref b if b == b"ee");
363 }
364
365 #[test]
366 #[should_panic(
367 expected = r#"assertion failed: `(left matches right)`: custom panic message
368
369 Diff < left / right > :
370 <666
371 >999
372
373 "#
374 )]
375 fn fails_custom() {
376 ::pretty_assertions::assert_matches!(666, 999, "custom panic message");
377 }
378
379 #[test]
380 #[should_panic(
381 expected = r#"assertion failed: `(left matches right)`: custom panic message
382
383 Diff < left / right > :
384 <666
385 >999
386
387 "#
388 )]
389 fn fails_custom_trailing_comma() {
390 ::pretty_assertions::assert_matches!(666, 999, "custom panic message",);
391 }
392 }