]>
Commit | Line | Data |
---|---|---|
c34b1796 AL |
1 | mod debug_struct { |
2 | use std::fmt; | |
3 | ||
4 | #[test] | |
5 | fn test_empty() { | |
6 | struct Foo; | |
7 | ||
8 | impl fmt::Debug for Foo { | |
48663c56 | 9 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
10 | fmt.debug_struct("Foo").finish() |
11 | } | |
12 | } | |
13 | ||
ee023bcb FG |
14 | assert_eq!("Foo", format!("{Foo:?}")); |
15 | assert_eq!("Foo", format!("{Foo:#?}")); | |
c34b1796 AL |
16 | } |
17 | ||
18 | #[test] | |
19 | fn test_single() { | |
20 | struct Foo; | |
21 | ||
22 | impl fmt::Debug for Foo { | |
48663c56 | 23 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 24 | fmt.debug_struct("Foo").field("bar", &true).finish() |
c34b1796 AL |
25 | } |
26 | } | |
27 | ||
ee023bcb | 28 | assert_eq!("Foo { bar: true }", format!("{Foo:?}")); |
c34b1796 | 29 | assert_eq!( |
dfeec247 | 30 | "Foo { |
532ac7d7 | 31 | bar: true, |
c34b1796 | 32 | }", |
ee023bcb | 33 | format!("{Foo:#?}") |
dfeec247 | 34 | ); |
c34b1796 AL |
35 | } |
36 | ||
37 | #[test] | |
38 | fn test_multiple() { | |
39 | struct Foo; | |
40 | ||
41 | impl fmt::Debug for Foo { | |
48663c56 | 42 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
43 | fmt.debug_struct("Foo") |
44 | .field("bar", &true) | |
54a0048b | 45 | .field("baz", &format_args!("{}/{}", 10, 20)) |
c34b1796 AL |
46 | .finish() |
47 | } | |
48 | } | |
49 | ||
ee023bcb | 50 | assert_eq!("Foo { bar: true, baz: 10/20 }", format!("{Foo:?}")); |
c34b1796 | 51 | assert_eq!( |
dfeec247 | 52 | "Foo { |
c34b1796 | 53 | bar: true, |
532ac7d7 | 54 | baz: 10/20, |
c34b1796 | 55 | }", |
ee023bcb | 56 | format!("{Foo:#?}") |
dfeec247 | 57 | ); |
c34b1796 AL |
58 | } |
59 | ||
60 | #[test] | |
61 | fn test_nested() { | |
62 | struct Foo; | |
63 | ||
64 | impl fmt::Debug for Foo { | |
48663c56 | 65 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
66 | fmt.debug_struct("Foo") |
67 | .field("bar", &true) | |
54a0048b | 68 | .field("baz", &format_args!("{}/{}", 10, 20)) |
c34b1796 AL |
69 | .finish() |
70 | } | |
71 | } | |
72 | ||
73 | struct Bar; | |
74 | ||
dfeec247 XL |
75 | impl fmt::Debug for Bar { |
76 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
77 | fmt.debug_struct("Bar").field("foo", &Foo).field("hello", &"world").finish() | |
78 | } | |
79 | } | |
80 | ||
81 | assert_eq!( | |
82 | "Bar { foo: Foo { bar: true, baz: 10/20 }, hello: \"world\" }", | |
ee023bcb | 83 | format!("{Bar:?}") |
dfeec247 XL |
84 | ); |
85 | assert_eq!( | |
86 | "Bar { | |
87 | foo: Foo { | |
88 | bar: true, | |
89 | baz: 10/20, | |
90 | }, | |
91 | hello: \"world\", | |
92 | }", | |
ee023bcb | 93 | format!("{Bar:#?}") |
dfeec247 XL |
94 | ); |
95 | } | |
96 | ||
97 | #[test] | |
98 | fn test_only_non_exhaustive() { | |
99 | struct Foo; | |
100 | ||
101 | impl fmt::Debug for Foo { | |
102 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
103 | fmt.debug_struct("Foo").finish_non_exhaustive() | |
104 | } | |
105 | } | |
106 | ||
ee023bcb FG |
107 | assert_eq!("Foo { .. }", format!("{Foo:?}")); |
108 | assert_eq!("Foo { .. }", format!("{Foo:#?}")); | |
dfeec247 XL |
109 | } |
110 | ||
111 | #[test] | |
112 | fn test_multiple_and_non_exhaustive() { | |
113 | struct Foo; | |
114 | ||
115 | impl fmt::Debug for Foo { | |
116 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
117 | fmt.debug_struct("Foo") | |
118 | .field("bar", &true) | |
119 | .field("baz", &format_args!("{}/{}", 10, 20)) | |
120 | .finish_non_exhaustive() | |
121 | } | |
122 | } | |
123 | ||
ee023bcb | 124 | assert_eq!("Foo { bar: true, baz: 10/20, .. }", format!("{Foo:?}")); |
dfeec247 XL |
125 | assert_eq!( |
126 | "Foo { | |
127 | bar: true, | |
128 | baz: 10/20, | |
129 | .. | |
130 | }", | |
ee023bcb | 131 | format!("{Foo:#?}") |
dfeec247 XL |
132 | ); |
133 | } | |
134 | ||
135 | #[test] | |
136 | fn test_nested_non_exhaustive() { | |
137 | struct Foo; | |
138 | ||
139 | impl fmt::Debug for Foo { | |
140 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
141 | fmt.debug_struct("Foo") | |
142 | .field("bar", &true) | |
143 | .field("baz", &format_args!("{}/{}", 10, 20)) | |
144 | .finish_non_exhaustive() | |
145 | } | |
146 | } | |
147 | ||
148 | struct Bar; | |
149 | ||
c34b1796 | 150 | impl fmt::Debug for Bar { |
48663c56 | 151 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
152 | fmt.debug_struct("Bar") |
153 | .field("foo", &Foo) | |
154 | .field("hello", &"world") | |
dfeec247 | 155 | .finish_non_exhaustive() |
c34b1796 AL |
156 | } |
157 | } | |
158 | ||
c34b1796 | 159 | assert_eq!( |
dfeec247 | 160 | "Bar { foo: Foo { bar: true, baz: 10/20, .. }, hello: \"world\", .. }", |
ee023bcb | 161 | format!("{Bar:?}") |
dfeec247 XL |
162 | ); |
163 | assert_eq!( | |
164 | "Bar { | |
c34b1796 AL |
165 | foo: Foo { |
166 | bar: true, | |
532ac7d7 | 167 | baz: 10/20, |
dfeec247 | 168 | .. |
c34b1796 | 169 | }, |
532ac7d7 | 170 | hello: \"world\", |
dfeec247 | 171 | .. |
c34b1796 | 172 | }", |
ee023bcb | 173 | format!("{Bar:#?}") |
dfeec247 | 174 | ); |
c34b1796 AL |
175 | } |
176 | } | |
177 | ||
178 | mod debug_tuple { | |
179 | use std::fmt; | |
180 | ||
181 | #[test] | |
182 | fn test_empty() { | |
183 | struct Foo; | |
184 | ||
185 | impl fmt::Debug for Foo { | |
48663c56 | 186 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
187 | fmt.debug_tuple("Foo").finish() |
188 | } | |
189 | } | |
190 | ||
ee023bcb FG |
191 | assert_eq!("Foo", format!("{Foo:?}")); |
192 | assert_eq!("Foo", format!("{Foo:#?}")); | |
c34b1796 AL |
193 | } |
194 | ||
195 | #[test] | |
196 | fn test_single() { | |
197 | struct Foo; | |
198 | ||
199 | impl fmt::Debug for Foo { | |
48663c56 | 200 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 201 | fmt.debug_tuple("Foo").field(&true).finish() |
c34b1796 AL |
202 | } |
203 | } | |
204 | ||
ee023bcb | 205 | assert_eq!("Foo(true)", format!("{Foo:?}")); |
c34b1796 | 206 | assert_eq!( |
dfeec247 | 207 | "Foo( |
532ac7d7 | 208 | true, |
c34b1796 | 209 | )", |
ee023bcb | 210 | format!("{Foo:#?}") |
dfeec247 | 211 | ); |
c34b1796 AL |
212 | } |
213 | ||
214 | #[test] | |
215 | fn test_multiple() { | |
216 | struct Foo; | |
217 | ||
218 | impl fmt::Debug for Foo { | |
48663c56 | 219 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 220 | fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
221 | } |
222 | } | |
223 | ||
ee023bcb | 224 | assert_eq!("Foo(true, 10/20)", format!("{Foo:?}")); |
c34b1796 | 225 | assert_eq!( |
dfeec247 | 226 | "Foo( |
c34b1796 | 227 | true, |
532ac7d7 | 228 | 10/20, |
c34b1796 | 229 | )", |
ee023bcb | 230 | format!("{Foo:#?}") |
dfeec247 | 231 | ); |
c34b1796 AL |
232 | } |
233 | ||
234 | #[test] | |
235 | fn test_nested() { | |
236 | struct Foo; | |
237 | ||
238 | impl fmt::Debug for Foo { | |
48663c56 | 239 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 240 | fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
241 | } |
242 | } | |
243 | ||
244 | struct Bar; | |
245 | ||
246 | impl fmt::Debug for Bar { | |
48663c56 | 247 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 248 | fmt.debug_tuple("Bar").field(&Foo).field(&"world").finish() |
c34b1796 AL |
249 | } |
250 | } | |
251 | ||
ee023bcb | 252 | assert_eq!("Bar(Foo(true, 10/20), \"world\")", format!("{Bar:?}")); |
c34b1796 | 253 | assert_eq!( |
dfeec247 | 254 | "Bar( |
c34b1796 AL |
255 | Foo( |
256 | true, | |
532ac7d7 | 257 | 10/20, |
c34b1796 | 258 | ), |
532ac7d7 | 259 | \"world\", |
c34b1796 | 260 | )", |
ee023bcb | 261 | format!("{Bar:#?}") |
dfeec247 | 262 | ); |
c34b1796 AL |
263 | } |
264 | } | |
265 | ||
266 | mod debug_map { | |
267 | use std::fmt; | |
268 | ||
269 | #[test] | |
270 | fn test_empty() { | |
271 | struct Foo; | |
272 | ||
273 | impl fmt::Debug for Foo { | |
48663c56 | 274 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
275 | fmt.debug_map().finish() |
276 | } | |
277 | } | |
278 | ||
ee023bcb FG |
279 | assert_eq!("{}", format!("{Foo:?}")); |
280 | assert_eq!("{}", format!("{Foo:#?}")); | |
c34b1796 AL |
281 | } |
282 | ||
283 | #[test] | |
284 | fn test_single() { | |
416331ca | 285 | struct Entry; |
c34b1796 | 286 | |
416331ca | 287 | impl fmt::Debug for Entry { |
48663c56 | 288 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 289 | fmt.debug_map().entry(&"bar", &true).finish() |
c34b1796 AL |
290 | } |
291 | } | |
292 | ||
416331ca XL |
293 | struct KeyValue; |
294 | ||
295 | impl fmt::Debug for KeyValue { | |
296 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
dfeec247 | 297 | fmt.debug_map().key(&"bar").value(&true).finish() |
416331ca XL |
298 | } |
299 | } | |
300 | ||
ee023bcb FG |
301 | assert_eq!(format!("{Entry:?}"), format!("{KeyValue:?}")); |
302 | assert_eq!(format!("{Entry:#?}"), format!("{KeyValue:#?}")); | |
416331ca | 303 | |
ee023bcb | 304 | assert_eq!("{\"bar\": true}", format!("{Entry:?}")); |
c34b1796 | 305 | assert_eq!( |
dfeec247 | 306 | "{ |
532ac7d7 | 307 | \"bar\": true, |
c34b1796 | 308 | }", |
ee023bcb | 309 | format!("{Entry:#?}") |
dfeec247 | 310 | ); |
c34b1796 AL |
311 | } |
312 | ||
313 | #[test] | |
314 | fn test_multiple() { | |
416331ca | 315 | struct Entry; |
c34b1796 | 316 | |
416331ca | 317 | impl fmt::Debug for Entry { |
48663c56 | 318 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
319 | fmt.debug_map() |
320 | .entry(&"bar", &true) | |
54a0048b | 321 | .entry(&10, &format_args!("{}/{}", 10, 20)) |
c34b1796 AL |
322 | .finish() |
323 | } | |
324 | } | |
325 | ||
416331ca XL |
326 | struct KeyValue; |
327 | ||
328 | impl fmt::Debug for KeyValue { | |
329 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
330 | fmt.debug_map() | |
dfeec247 XL |
331 | .key(&"bar") |
332 | .value(&true) | |
333 | .key(&10) | |
334 | .value(&format_args!("{}/{}", 10, 20)) | |
416331ca XL |
335 | .finish() |
336 | } | |
337 | } | |
338 | ||
ee023bcb FG |
339 | assert_eq!(format!("{Entry:?}"), format!("{KeyValue:?}")); |
340 | assert_eq!(format!("{Entry:#?}"), format!("{KeyValue:#?}")); | |
416331ca | 341 | |
ee023bcb | 342 | assert_eq!("{\"bar\": true, 10: 10/20}", format!("{Entry:?}")); |
c34b1796 | 343 | assert_eq!( |
dfeec247 | 344 | "{ |
c34b1796 | 345 | \"bar\": true, |
532ac7d7 | 346 | 10: 10/20, |
c34b1796 | 347 | }", |
ee023bcb | 348 | format!("{Entry:#?}") |
dfeec247 | 349 | ); |
c34b1796 AL |
350 | } |
351 | ||
352 | #[test] | |
353 | fn test_nested() { | |
354 | struct Foo; | |
355 | ||
356 | impl fmt::Debug for Foo { | |
48663c56 | 357 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
358 | fmt.debug_map() |
359 | .entry(&"bar", &true) | |
54a0048b | 360 | .entry(&10, &format_args!("{}/{}", 10, 20)) |
c34b1796 AL |
361 | .finish() |
362 | } | |
363 | } | |
364 | ||
365 | struct Bar; | |
366 | ||
367 | impl fmt::Debug for Bar { | |
48663c56 | 368 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 369 | fmt.debug_map().entry(&"foo", &Foo).entry(&Foo, &"world").finish() |
c34b1796 AL |
370 | } |
371 | } | |
372 | ||
dfeec247 XL |
373 | assert_eq!( |
374 | "{\"foo\": {\"bar\": true, 10: 10/20}, \ | |
c34b1796 | 375 | {\"bar\": true, 10: 10/20}: \"world\"}", |
ee023bcb | 376 | format!("{Bar:?}") |
dfeec247 | 377 | ); |
c34b1796 | 378 | assert_eq!( |
dfeec247 | 379 | "{ |
c34b1796 AL |
380 | \"foo\": { |
381 | \"bar\": true, | |
532ac7d7 | 382 | 10: 10/20, |
c34b1796 AL |
383 | }, |
384 | { | |
385 | \"bar\": true, | |
532ac7d7 XL |
386 | 10: 10/20, |
387 | }: \"world\", | |
c34b1796 | 388 | }", |
ee023bcb | 389 | format!("{Bar:#?}") |
dfeec247 | 390 | ); |
c34b1796 | 391 | } |
416331ca | 392 | |
e74abb32 XL |
393 | #[test] |
394 | fn test_entry_err() { | |
395 | // Ensure errors in a map entry don't trigger panics (#65231) | |
396 | use std::fmt::Write; | |
397 | ||
398 | struct ErrorFmt; | |
399 | ||
400 | impl fmt::Debug for ErrorFmt { | |
401 | fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { | |
402 | Err(fmt::Error) | |
403 | } | |
404 | } | |
405 | ||
406 | struct KeyValue<K, V>(usize, K, V); | |
407 | ||
408 | impl<K, V> fmt::Debug for KeyValue<K, V> | |
409 | where | |
410 | K: fmt::Debug, | |
411 | V: fmt::Debug, | |
412 | { | |
413 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
414 | let mut map = fmt.debug_map(); | |
415 | ||
416 | for _ in 0..self.0 { | |
417 | map.entry(&self.1, &self.2); | |
418 | } | |
419 | ||
420 | map.finish() | |
421 | } | |
422 | } | |
423 | ||
424 | let mut buf = String::new(); | |
425 | ||
426 | assert!(write!(&mut buf, "{:?}", KeyValue(1, ErrorFmt, "bar")).is_err()); | |
427 | assert!(write!(&mut buf, "{:?}", KeyValue(1, "foo", ErrorFmt)).is_err()); | |
428 | ||
429 | assert!(write!(&mut buf, "{:?}", KeyValue(2, ErrorFmt, "bar")).is_err()); | |
430 | assert!(write!(&mut buf, "{:?}", KeyValue(2, "foo", ErrorFmt)).is_err()); | |
431 | } | |
432 | ||
416331ca XL |
433 | #[test] |
434 | #[should_panic] | |
435 | fn test_invalid_key_when_entry_is_incomplete() { | |
436 | struct Foo; | |
437 | ||
438 | impl fmt::Debug for Foo { | |
439 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
dfeec247 | 440 | fmt.debug_map().key(&"bar").key(&"invalid").finish() |
416331ca XL |
441 | } |
442 | } | |
443 | ||
ee023bcb | 444 | format!("{Foo:?}"); |
416331ca XL |
445 | } |
446 | ||
447 | #[test] | |
448 | #[should_panic] | |
449 | fn test_invalid_finish_incomplete_entry() { | |
450 | struct Foo; | |
451 | ||
452 | impl fmt::Debug for Foo { | |
453 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
dfeec247 | 454 | fmt.debug_map().key(&"bar").finish() |
416331ca XL |
455 | } |
456 | } | |
457 | ||
ee023bcb | 458 | format!("{Foo:?}"); |
416331ca XL |
459 | } |
460 | ||
461 | #[test] | |
462 | #[should_panic] | |
463 | fn test_invalid_value_before_key() { | |
464 | struct Foo; | |
465 | ||
466 | impl fmt::Debug for Foo { | |
467 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | |
dfeec247 | 468 | fmt.debug_map().value(&"invalid").key(&"bar").finish() |
416331ca XL |
469 | } |
470 | } | |
471 | ||
ee023bcb | 472 | format!("{Foo:?}"); |
416331ca | 473 | } |
c34b1796 AL |
474 | } |
475 | ||
476 | mod debug_set { | |
477 | use std::fmt; | |
478 | ||
479 | #[test] | |
480 | fn test_empty() { | |
481 | struct Foo; | |
482 | ||
483 | impl fmt::Debug for Foo { | |
48663c56 | 484 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
485 | fmt.debug_set().finish() |
486 | } | |
487 | } | |
488 | ||
ee023bcb FG |
489 | assert_eq!("{}", format!("{Foo:?}")); |
490 | assert_eq!("{}", format!("{Foo:#?}")); | |
c34b1796 AL |
491 | } |
492 | ||
493 | #[test] | |
494 | fn test_single() { | |
495 | struct Foo; | |
496 | ||
497 | impl fmt::Debug for Foo { | |
48663c56 | 498 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 499 | fmt.debug_set().entry(&true).finish() |
c34b1796 AL |
500 | } |
501 | } | |
502 | ||
ee023bcb | 503 | assert_eq!("{true}", format!("{Foo:?}")); |
c34b1796 | 504 | assert_eq!( |
dfeec247 | 505 | "{ |
532ac7d7 | 506 | true, |
c34b1796 | 507 | }", |
ee023bcb | 508 | format!("{Foo:#?}") |
dfeec247 | 509 | ); |
c34b1796 AL |
510 | } |
511 | ||
512 | #[test] | |
513 | fn test_multiple() { | |
514 | struct Foo; | |
515 | ||
516 | impl fmt::Debug for Foo { | |
48663c56 | 517 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 518 | fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
519 | } |
520 | } | |
521 | ||
ee023bcb | 522 | assert_eq!("{true, 10/20}", format!("{Foo:?}")); |
c34b1796 | 523 | assert_eq!( |
dfeec247 | 524 | "{ |
c34b1796 | 525 | true, |
532ac7d7 | 526 | 10/20, |
c34b1796 | 527 | }", |
ee023bcb | 528 | format!("{Foo:#?}") |
dfeec247 | 529 | ); |
c34b1796 AL |
530 | } |
531 | ||
532 | #[test] | |
533 | fn test_nested() { | |
534 | struct Foo; | |
535 | ||
536 | impl fmt::Debug for Foo { | |
48663c56 | 537 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 538 | fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
539 | } |
540 | } | |
541 | ||
542 | struct Bar; | |
543 | ||
544 | impl fmt::Debug for Bar { | |
48663c56 | 545 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 546 | fmt.debug_set().entry(&Foo).entry(&"world").finish() |
c34b1796 AL |
547 | } |
548 | } | |
549 | ||
ee023bcb | 550 | assert_eq!("{{true, 10/20}, \"world\"}", format!("{Bar:?}")); |
c34b1796 | 551 | assert_eq!( |
dfeec247 | 552 | "{ |
c34b1796 AL |
553 | { |
554 | true, | |
532ac7d7 | 555 | 10/20, |
c34b1796 | 556 | }, |
532ac7d7 | 557 | \"world\", |
c34b1796 | 558 | }", |
ee023bcb | 559 | format!("{Bar:#?}") |
dfeec247 | 560 | ); |
c34b1796 AL |
561 | } |
562 | } | |
563 | ||
564 | mod debug_list { | |
565 | use std::fmt; | |
566 | ||
567 | #[test] | |
568 | fn test_empty() { | |
569 | struct Foo; | |
570 | ||
571 | impl fmt::Debug for Foo { | |
48663c56 | 572 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
573 | fmt.debug_list().finish() |
574 | } | |
575 | } | |
576 | ||
ee023bcb FG |
577 | assert_eq!("[]", format!("{Foo:?}")); |
578 | assert_eq!("[]", format!("{Foo:#?}")); | |
c34b1796 AL |
579 | } |
580 | ||
581 | #[test] | |
582 | fn test_single() { | |
583 | struct Foo; | |
584 | ||
585 | impl fmt::Debug for Foo { | |
48663c56 | 586 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 587 | fmt.debug_list().entry(&true).finish() |
c34b1796 AL |
588 | } |
589 | } | |
590 | ||
ee023bcb | 591 | assert_eq!("[true]", format!("{Foo:?}")); |
c34b1796 | 592 | assert_eq!( |
dfeec247 | 593 | "[ |
532ac7d7 | 594 | true, |
c34b1796 | 595 | ]", |
ee023bcb | 596 | format!("{Foo:#?}") |
dfeec247 | 597 | ); |
c34b1796 AL |
598 | } |
599 | ||
600 | #[test] | |
601 | fn test_multiple() { | |
602 | struct Foo; | |
603 | ||
604 | impl fmt::Debug for Foo { | |
48663c56 | 605 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 606 | fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
607 | } |
608 | } | |
609 | ||
ee023bcb | 610 | assert_eq!("[true, 10/20]", format!("{Foo:?}")); |
c34b1796 | 611 | assert_eq!( |
dfeec247 | 612 | "[ |
c34b1796 | 613 | true, |
532ac7d7 | 614 | 10/20, |
c34b1796 | 615 | ]", |
ee023bcb | 616 | format!("{Foo:#?}") |
dfeec247 | 617 | ); |
c34b1796 AL |
618 | } |
619 | ||
620 | #[test] | |
621 | fn test_nested() { | |
622 | struct Foo; | |
623 | ||
624 | impl fmt::Debug for Foo { | |
48663c56 | 625 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 626 | fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish() |
c34b1796 AL |
627 | } |
628 | } | |
629 | ||
630 | struct Bar; | |
631 | ||
632 | impl fmt::Debug for Bar { | |
48663c56 | 633 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
dfeec247 | 634 | fmt.debug_list().entry(&Foo).entry(&"world").finish() |
c34b1796 AL |
635 | } |
636 | } | |
637 | ||
ee023bcb | 638 | assert_eq!("[[true, 10/20], \"world\"]", format!("{Bar:?}")); |
c34b1796 | 639 | assert_eq!( |
dfeec247 | 640 | "[ |
c34b1796 AL |
641 | [ |
642 | true, | |
532ac7d7 | 643 | 10/20, |
c34b1796 | 644 | ], |
532ac7d7 | 645 | \"world\", |
c34b1796 | 646 | ]", |
ee023bcb | 647 | format!("{Bar:#?}") |
dfeec247 | 648 | ); |
c34b1796 AL |
649 | } |
650 | } | |
ff7c6d11 XL |
651 | |
652 | #[test] | |
653 | fn test_formatting_parameters_are_forwarded() { | |
654 | use std::collections::{BTreeMap, BTreeSet}; | |
655 | #[derive(Debug)] | |
c295e0f8 | 656 | #[allow(dead_code)] |
ff7c6d11 XL |
657 | struct Foo { |
658 | bar: u32, | |
659 | baz: u32, | |
660 | } | |
661 | let struct_ = Foo { bar: 1024, baz: 7 }; | |
662 | let tuple = (1024, 7); | |
663 | let list = [1024, 7]; | |
664 | let mut map = BTreeMap::new(); | |
665 | map.insert("bar", 1024); | |
666 | map.insert("baz", 7); | |
667 | let mut set = BTreeSet::new(); | |
668 | set.insert(1024); | |
669 | set.insert(7); | |
670 | ||
ee023bcb FG |
671 | assert_eq!(format!("{struct_:03?}"), "Foo { bar: 1024, baz: 007 }"); |
672 | assert_eq!(format!("{tuple:03?}"), "(1024, 007)"); | |
673 | assert_eq!(format!("{list:03?}"), "[1024, 007]"); | |
674 | assert_eq!(format!("{map:03?}"), r#"{"bar": 1024, "baz": 007}"#); | |
675 | assert_eq!(format!("{set:03?}"), "{007, 1024}"); | |
dfeec247 | 676 | assert_eq!( |
ee023bcb | 677 | format!("{struct_:#03?}"), |
dfeec247 | 678 | " |
ff7c6d11 XL |
679 | Foo { |
680 | bar: 1024, | |
532ac7d7 | 681 | baz: 007, |
ff7c6d11 | 682 | } |
dfeec247 XL |
683 | " |
684 | .trim() | |
685 | ); | |
686 | assert_eq!( | |
ee023bcb | 687 | format!("{tuple:#03?}"), |
dfeec247 | 688 | " |
ff7c6d11 XL |
689 | ( |
690 | 1024, | |
532ac7d7 | 691 | 007, |
ff7c6d11 | 692 | ) |
dfeec247 XL |
693 | " |
694 | .trim() | |
695 | ); | |
696 | assert_eq!( | |
ee023bcb | 697 | format!("{list:#03?}"), |
dfeec247 | 698 | " |
ff7c6d11 XL |
699 | [ |
700 | 1024, | |
532ac7d7 | 701 | 007, |
ff7c6d11 | 702 | ] |
dfeec247 XL |
703 | " |
704 | .trim() | |
705 | ); | |
706 | assert_eq!( | |
ee023bcb | 707 | format!("{map:#03?}"), |
dfeec247 | 708 | r#" |
ff7c6d11 XL |
709 | { |
710 | "bar": 1024, | |
532ac7d7 | 711 | "baz": 007, |
ff7c6d11 | 712 | } |
dfeec247 XL |
713 | "# |
714 | .trim() | |
715 | ); | |
716 | assert_eq!( | |
ee023bcb | 717 | format!("{set:#03?}"), |
dfeec247 | 718 | " |
ff7c6d11 XL |
719 | { |
720 | 007, | |
532ac7d7 | 721 | 1024, |
ff7c6d11 | 722 | } |
dfeec247 XL |
723 | " |
724 | .trim() | |
725 | ); | |
ff7c6d11 | 726 | } |