]> git.lizzy.rs Git - rust.git/blob - library/core/tests/fmt/builders.rs
Rollup merge of #106549 - wcampbell0x2a:use-fmt-named-parameters-borrowck, r=estebank
[rust.git] / library / core / tests / fmt / builders.rs
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 {
9             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
10                 fmt.debug_struct("Foo").finish()
11             }
12         }
13
14         assert_eq!("Foo", format!("{Foo:?}"));
15         assert_eq!("Foo", format!("{Foo:#?}"));
16     }
17
18     #[test]
19     fn test_single() {
20         struct Foo;
21
22         impl fmt::Debug for Foo {
23             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
24                 fmt.debug_struct("Foo").field("bar", &true).finish()
25             }
26         }
27
28         assert_eq!("Foo { bar: true }", format!("{Foo:?}"));
29         assert_eq!(
30             "Foo {
31     bar: true,
32 }",
33             format!("{Foo:#?}")
34         );
35     }
36
37     #[test]
38     fn test_multiple() {
39         struct Foo;
40
41         impl fmt::Debug for Foo {
42             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
43                 fmt.debug_struct("Foo")
44                     .field("bar", &true)
45                     .field("baz", &format_args!("{}/{}", 10, 20))
46                     .finish()
47             }
48         }
49
50         assert_eq!("Foo { bar: true, baz: 10/20 }", format!("{Foo:?}"));
51         assert_eq!(
52             "Foo {
53     bar: true,
54     baz: 10/20,
55 }",
56             format!("{Foo:#?}")
57         );
58     }
59
60     #[test]
61     fn test_nested() {
62         struct Foo;
63
64         impl fmt::Debug for Foo {
65             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
66                 fmt.debug_struct("Foo")
67                     .field("bar", &true)
68                     .field("baz", &format_args!("{}/{}", 10, 20))
69                     .finish()
70             }
71         }
72
73         struct Bar;
74
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\" }",
83             format!("{Bar:?}")
84         );
85         assert_eq!(
86             "Bar {
87     foo: Foo {
88         bar: true,
89         baz: 10/20,
90     },
91     hello: \"world\",
92 }",
93             format!("{Bar:#?}")
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
107         assert_eq!("Foo { .. }", format!("{Foo:?}"));
108         assert_eq!("Foo { .. }", format!("{Foo:#?}"));
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
124         assert_eq!("Foo { bar: true, baz: 10/20, .. }", format!("{Foo:?}"));
125         assert_eq!(
126             "Foo {
127     bar: true,
128     baz: 10/20,
129     ..
130 }",
131             format!("{Foo:#?}")
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
150         impl fmt::Debug for Bar {
151             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
152                 fmt.debug_struct("Bar")
153                     .field("foo", &Foo)
154                     .field("hello", &"world")
155                     .finish_non_exhaustive()
156             }
157         }
158
159         assert_eq!(
160             "Bar { foo: Foo { bar: true, baz: 10/20, .. }, hello: \"world\", .. }",
161             format!("{Bar:?}")
162         );
163         assert_eq!(
164             "Bar {
165     foo: Foo {
166         bar: true,
167         baz: 10/20,
168         ..
169     },
170     hello: \"world\",
171     ..
172 }",
173             format!("{Bar:#?}")
174         );
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 {
186             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
187                 fmt.debug_tuple("Foo").finish()
188             }
189         }
190
191         assert_eq!("Foo", format!("{Foo:?}"));
192         assert_eq!("Foo", format!("{Foo:#?}"));
193     }
194
195     #[test]
196     fn test_single() {
197         struct Foo;
198
199         impl fmt::Debug for Foo {
200             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
201                 fmt.debug_tuple("Foo").field(&true).finish()
202             }
203         }
204
205         assert_eq!("Foo(true)", format!("{Foo:?}"));
206         assert_eq!(
207             "Foo(
208     true,
209 )",
210             format!("{Foo:#?}")
211         );
212     }
213
214     #[test]
215     fn test_multiple() {
216         struct Foo;
217
218         impl fmt::Debug for Foo {
219             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
220                 fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish()
221             }
222         }
223
224         assert_eq!("Foo(true, 10/20)", format!("{Foo:?}"));
225         assert_eq!(
226             "Foo(
227     true,
228     10/20,
229 )",
230             format!("{Foo:#?}")
231         );
232     }
233
234     #[test]
235     fn test_nested() {
236         struct Foo;
237
238         impl fmt::Debug for Foo {
239             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
240                 fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish()
241             }
242         }
243
244         struct Bar;
245
246         impl fmt::Debug for Bar {
247             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
248                 fmt.debug_tuple("Bar").field(&Foo).field(&"world").finish()
249             }
250         }
251
252         assert_eq!("Bar(Foo(true, 10/20), \"world\")", format!("{Bar:?}"));
253         assert_eq!(
254             "Bar(
255     Foo(
256         true,
257         10/20,
258     ),
259     \"world\",
260 )",
261             format!("{Bar:#?}")
262         );
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 {
274             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
275                 fmt.debug_map().finish()
276             }
277         }
278
279         assert_eq!("{}", format!("{Foo:?}"));
280         assert_eq!("{}", format!("{Foo:#?}"));
281     }
282
283     #[test]
284     fn test_single() {
285         struct Entry;
286
287         impl fmt::Debug for Entry {
288             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
289                 fmt.debug_map().entry(&"bar", &true).finish()
290             }
291         }
292
293         struct KeyValue;
294
295         impl fmt::Debug for KeyValue {
296             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
297                 fmt.debug_map().key(&"bar").value(&true).finish()
298             }
299         }
300
301         assert_eq!(format!("{Entry:?}"), format!("{KeyValue:?}"));
302         assert_eq!(format!("{Entry:#?}"), format!("{KeyValue:#?}"));
303
304         assert_eq!("{\"bar\": true}", format!("{Entry:?}"));
305         assert_eq!(
306             "{
307     \"bar\": true,
308 }",
309             format!("{Entry:#?}")
310         );
311     }
312
313     #[test]
314     fn test_multiple() {
315         struct Entry;
316
317         impl fmt::Debug for Entry {
318             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
319                 fmt.debug_map()
320                     .entry(&"bar", &true)
321                     .entry(&10, &format_args!("{}/{}", 10, 20))
322                     .finish()
323             }
324         }
325
326         struct KeyValue;
327
328         impl fmt::Debug for KeyValue {
329             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
330                 fmt.debug_map()
331                     .key(&"bar")
332                     .value(&true)
333                     .key(&10)
334                     .value(&format_args!("{}/{}", 10, 20))
335                     .finish()
336             }
337         }
338
339         assert_eq!(format!("{Entry:?}"), format!("{KeyValue:?}"));
340         assert_eq!(format!("{Entry:#?}"), format!("{KeyValue:#?}"));
341
342         assert_eq!("{\"bar\": true, 10: 10/20}", format!("{Entry:?}"));
343         assert_eq!(
344             "{
345     \"bar\": true,
346     10: 10/20,
347 }",
348             format!("{Entry:#?}")
349         );
350     }
351
352     #[test]
353     fn test_nested() {
354         struct Foo;
355
356         impl fmt::Debug for Foo {
357             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
358                 fmt.debug_map()
359                     .entry(&"bar", &true)
360                     .entry(&10, &format_args!("{}/{}", 10, 20))
361                     .finish()
362             }
363         }
364
365         struct Bar;
366
367         impl fmt::Debug for Bar {
368             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
369                 fmt.debug_map().entry(&"foo", &Foo).entry(&Foo, &"world").finish()
370             }
371         }
372
373         assert_eq!(
374             "{\"foo\": {\"bar\": true, 10: 10/20}, \
375                     {\"bar\": true, 10: 10/20}: \"world\"}",
376             format!("{Bar:?}")
377         );
378         assert_eq!(
379             "{
380     \"foo\": {
381         \"bar\": true,
382         10: 10/20,
383     },
384     {
385         \"bar\": true,
386         10: 10/20,
387     }: \"world\",
388 }",
389             format!("{Bar:#?}")
390         );
391     }
392
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
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 {
440                 fmt.debug_map().key(&"bar").key(&"invalid").finish()
441             }
442         }
443
444         format!("{Foo:?}");
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 {
454                 fmt.debug_map().key(&"bar").finish()
455             }
456         }
457
458         format!("{Foo:?}");
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 {
468                 fmt.debug_map().value(&"invalid").key(&"bar").finish()
469             }
470         }
471
472         format!("{Foo:?}");
473     }
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 {
484             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
485                 fmt.debug_set().finish()
486             }
487         }
488
489         assert_eq!("{}", format!("{Foo:?}"));
490         assert_eq!("{}", format!("{Foo:#?}"));
491     }
492
493     #[test]
494     fn test_single() {
495         struct Foo;
496
497         impl fmt::Debug for Foo {
498             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
499                 fmt.debug_set().entry(&true).finish()
500             }
501         }
502
503         assert_eq!("{true}", format!("{Foo:?}"));
504         assert_eq!(
505             "{
506     true,
507 }",
508             format!("{Foo:#?}")
509         );
510     }
511
512     #[test]
513     fn test_multiple() {
514         struct Foo;
515
516         impl fmt::Debug for Foo {
517             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
518                 fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
519             }
520         }
521
522         assert_eq!("{true, 10/20}", format!("{Foo:?}"));
523         assert_eq!(
524             "{
525     true,
526     10/20,
527 }",
528             format!("{Foo:#?}")
529         );
530     }
531
532     #[test]
533     fn test_nested() {
534         struct Foo;
535
536         impl fmt::Debug for Foo {
537             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
538                 fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
539             }
540         }
541
542         struct Bar;
543
544         impl fmt::Debug for Bar {
545             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
546                 fmt.debug_set().entry(&Foo).entry(&"world").finish()
547             }
548         }
549
550         assert_eq!("{{true, 10/20}, \"world\"}", format!("{Bar:?}"));
551         assert_eq!(
552             "{
553     {
554         true,
555         10/20,
556     },
557     \"world\",
558 }",
559             format!("{Bar:#?}")
560         );
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 {
572             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
573                 fmt.debug_list().finish()
574             }
575         }
576
577         assert_eq!("[]", format!("{Foo:?}"));
578         assert_eq!("[]", format!("{Foo:#?}"));
579     }
580
581     #[test]
582     fn test_single() {
583         struct Foo;
584
585         impl fmt::Debug for Foo {
586             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
587                 fmt.debug_list().entry(&true).finish()
588             }
589         }
590
591         assert_eq!("[true]", format!("{Foo:?}"));
592         assert_eq!(
593             "[
594     true,
595 ]",
596             format!("{Foo:#?}")
597         );
598     }
599
600     #[test]
601     fn test_multiple() {
602         struct Foo;
603
604         impl fmt::Debug for Foo {
605             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
606                 fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
607             }
608         }
609
610         assert_eq!("[true, 10/20]", format!("{Foo:?}"));
611         assert_eq!(
612             "[
613     true,
614     10/20,
615 ]",
616             format!("{Foo:#?}")
617         );
618     }
619
620     #[test]
621     fn test_nested() {
622         struct Foo;
623
624         impl fmt::Debug for Foo {
625             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
626                 fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
627             }
628         }
629
630         struct Bar;
631
632         impl fmt::Debug for Bar {
633             fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
634                 fmt.debug_list().entry(&Foo).entry(&"world").finish()
635             }
636         }
637
638         assert_eq!("[[true, 10/20], \"world\"]", format!("{Bar:?}"));
639         assert_eq!(
640             "[
641     [
642         true,
643         10/20,
644     ],
645     \"world\",
646 ]",
647             format!("{Bar:#?}")
648         );
649     }
650 }
651
652 #[test]
653 fn test_formatting_parameters_are_forwarded() {
654     use std::collections::{BTreeMap, BTreeSet};
655     #[derive(Debug)]
656     #[allow(dead_code)]
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
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}");
676     assert_eq!(
677         format!("{struct_:#03?}"),
678         "
679 Foo {
680     bar: 1024,
681     baz: 007,
682 }
683     "
684         .trim()
685     );
686     assert_eq!(
687         format!("{tuple:#03?}"),
688         "
689 (
690     1024,
691     007,
692 )
693     "
694         .trim()
695     );
696     assert_eq!(
697         format!("{list:#03?}"),
698         "
699 [
700     1024,
701     007,
702 ]
703     "
704         .trim()
705     );
706     assert_eq!(
707         format!("{map:#03?}"),
708         r#"
709 {
710     "bar": 1024,
711     "baz": 007,
712 }
713     "#
714         .trim()
715     );
716     assert_eq!(
717         format!("{set:#03?}"),
718         "
719 {
720     007,
721     1024,
722 }
723     "
724         .trim()
725     );
726 }