Debian Codegraph @

third_party/rust/derive_more/tests/debug.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code)]
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 mod structs {
8 mod unit {
9 #[cfg(not(feature = "std"))]
10 use alloc::format;
11
12 use derive_more::Debug;
13
14 #[derive(Debug)]
15 struct Unit;
16
17 #[derive(Debug)]
18 struct Tuple();
19
20 #[derive(Debug)]
21 struct Struct {}
22
23 #[test]
24 fn assert() {
25 assert_eq!(format!("{:?}", Unit), "Unit");
26 assert_eq!(format!("{:#?}", Unit), "Unit");
27 assert_eq!(format!("{:?}", Tuple()), "Tuple");
28 assert_eq!(format!("{:#?}", Tuple()), "Tuple");
29 assert_eq!(format!("{:?}", Struct {}), "Struct");
30 assert_eq!(format!("{:#?}", Struct {}), "Struct");
31 }
32 }
33
34 mod single_field {
35 #[cfg(not(feature = "std"))]
36 use alloc::format;
37
38 use derive_more::Debug;
39
40 #[derive(Debug)]
41 struct Tuple(i32);
42
43 #[derive(Debug)]
44 struct Struct {
45 field: i32,
46 }
47
48 #[test]
49 fn assert() {
50 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(0)");
51 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n 0,\n)");
52 assert_eq!(format!("{:?}", Struct { field: 0 }), "Struct { field: 0 }");
53 assert_eq!(
54 format!("{:#?}", Struct { field: 0 }),
55 "Struct {\n field: 0,\n}",
56 );
57 }
58
59 mod str_field {
60 #[cfg(not(feature = "std"))]
61 use alloc::format;
62
63 use derive_more::Debug;
64
65 #[derive(Debug)]
66 struct Tuple(#[debug("i32")] i32);
67
68 #[derive(Debug)]
69 struct Struct {
70 #[debug("i32")]
71 field: i32,
72 }
73
74 #[test]
75 fn assert() {
76 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(i32)");
77 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n i32,\n)");
78 assert_eq!(
79 format!("{:?}", Struct { field: 0 }),
80 "Struct { field: i32 }",
81 );
82 assert_eq!(
83 format!("{:#?}", Struct { field: 0 }),
84 "Struct {\n field: i32,\n}",
85 );
86 }
87 }
88
89 mod interpolated_field {
90 #[cfg(not(feature = "std"))]
91 use alloc::format;
92
93 use derive_more::Debug;
94
95 #[derive(Debug)]
96 struct Tuple(#[debug("{_0}.{}", _0)] i32);
97
98 #[derive(Debug)]
99 struct Struct {
100 #[debug("{field}.{}", field)]
101 field: i32,
102 }
103
104 #[test]
105 fn assert() {
106 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(0.0)");
107 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n 0.0,\n)");
108 assert_eq!(
109 format!("{:?}", Struct { field: 0 }),
110 "Struct { field: 0.0 }",
111 );
112 assert_eq!(
113 format!("{:#?}", Struct { field: 0 }),
114 "Struct {\n field: 0.0,\n}",
115 );
116 }
117 }
118
119 mod ignore {
120 #[cfg(not(feature = "std"))]
121 use alloc::format;
122
123 use derive_more::Debug;
124
125 #[derive(Debug)]
126 struct Tuple(#[debug(ignore)] i32);
127
128 #[derive(Debug)]
129 struct Struct {
130 #[debug(skip)]
131 field: i32,
132 }
133
134 #[test]
135 fn assert() {
136 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(..)");
137 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(..)");
138 assert_eq!(format!("{:?}", Struct { field: 0 }), "Struct { .. }");
139 assert_eq!(format!("{:#?}", Struct { field: 0 }), "Struct { .. }");
140 }
141 }
142 }
143
144 mod multi_field {
145 #[cfg(not(feature = "std"))]
146 use alloc::format;
147
148 use derive_more::Debug;
149
150 #[derive(Debug)]
151 struct Tuple(i32, i32);
152
153 #[derive(Debug)]
154 struct Struct {
155 field1: i32,
156 field2: i32,
157 }
158
159 #[test]
160 fn assert() {
161 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, 2)");
162 assert_eq!(format!("{:#?}", Tuple(1, 2)), "Tuple(\n 1,\n 2,\n)");
163 assert_eq!(
164 format!(
165 "{:?}",
166 Struct {
167 field1: 1,
168 field2: 2,
169 }
170 ),
171 "Struct { field1: 1, field2: 2 }",
172 );
173 assert_eq!(
174 format!(
175 "{:#?}",
176 Struct {
177 field1: 1,
178 field2: 2,
179 }
180 ),
181 "Struct {\n field1: 1,\n field2: 2,\n}",
182 );
183 }
184
185 mod str_field {
186 #[cfg(not(feature = "std"))]
187 use alloc::format;
188
189 use derive_more::Debug;
190
191 #[derive(Debug)]
192 struct Tuple(i32, #[debug("i32")] i32);
193
194 #[derive(Debug)]
195 struct Struct {
196 #[debug("i32")]
197 field1: i32,
198 field2: i32,
199 }
200
201 #[test]
202 fn assert() {
203 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, i32)");
204 assert_eq!(
205 format!("{:#?}", Tuple(1, 2)),
206 "Tuple(\n 1,\n i32,\n)",
207 );
208 assert_eq!(
209 format!(
210 "{:?}",
211 Struct {
212 field1: 1,
213 field2: 2,
214 }
215 ),
216 "Struct { field1: i32, field2: 2 }",
217 );
218 assert_eq!(
219 format!(
220 "{:#?}",
221 Struct {
222 field1: 1,
223 field2: 2,
224 }
225 ),
226 "Struct {\n field1: i32,\n field2: 2,\n}",
227 );
228 }
229 }
230
231 mod interpolated_field {
232 #[cfg(not(feature = "std"))]
233 use alloc::format;
234
235 use derive_more::Debug;
236
237 #[derive(Debug)]
238 struct Tuple(i32, #[debug("{_0}.{}", _1)] i32);
239
240 #[derive(Debug)]
241 struct Struct {
242 #[debug("{field1}.{}", field2)]
243 field1: i32,
244 field2: i32,
245 }
246
247 #[test]
248 fn assert() {
249 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, 1.2)");
250 assert_eq!(
251 format!("{:#?}", Tuple(1, 2)),
252 "Tuple(\n 1,\n 1.2,\n)",
253 );
254 assert_eq!(
255 format!(
256 "{:?}",
257 Struct {
258 field1: 1,
259 field2: 2,
260 }
261 ),
262 "Struct { field1: 1.2, field2: 2 }",
263 );
264 assert_eq!(
265 format!(
266 "{:#?}",
267 Struct {
268 field1: 1,
269 field2: 2,
270 }
271 ),
272 "Struct {\n field1: 1.2,\n field2: 2,\n}",
273 );
274 }
275 }
276
277 mod ignore {
278 #[cfg(not(feature = "std"))]
279 use alloc::format;
280
281 use derive_more::Debug;
282
283 #[derive(Debug)]
284 struct Tuple(#[debug(ignore)] i32, i32);
285
286 #[derive(Debug)]
287 struct Struct {
288 field1: i32,
289 #[debug(skip)]
290 field2: i32,
291 }
292
293 #[test]
294 fn assert() {
295 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(2, ..)");
296 assert_eq!(format!("{:#?}", Tuple(1, 2)), "Tuple(\n 2,\n ..\n)",);
297 assert_eq!(
298 format!(
299 "{:?}",
300 Struct {
301 field1: 1,
302 field2: 2
303 }
304 ),
305 "Struct { field1: 1, .. }",
306 );
307 assert_eq!(
308 format!(
309 "{:#?}",
310 Struct {
311 field1: 1,
312 field2: 2
313 }
314 ),
315 "Struct {\n field1: 1,\n ..\n}",
316 );
317 }
318 }
319 }
320 }
321
322 mod enums {
323 mod no_variants {
324 use derive_more::Debug;
325
326 #[derive(Debug)]
327 enum Void {}
328
329 const fn assert<T: core::fmt::Debug>() {}
330 const _: () = assert::<Void>();
331 }
332
333 mod unit_variant {
334 #[cfg(not(feature = "std"))]
335 use alloc::format;
336
337 use derive_more::Debug;
338
339 #[derive(Debug)]
340 enum Enum {
341 Unit,
342 Unnamed(),
343 Named {},
344 }
345
346 #[test]
347 fn assert() {
348 assert_eq!(format!("{:?}", Enum::Unit), "Unit");
349 assert_eq!(format!("{:#?}", Enum::Unit), "Unit");
350 assert_eq!(format!("{:?}", Enum::Unnamed()), "Unnamed");
351 assert_eq!(format!("{:#?}", Enum::Unnamed()), "Unnamed");
352 assert_eq!(format!("{:?}", Enum::Named {}), "Named");
353 assert_eq!(format!("{:#?}", Enum::Named {}), "Named");
354 }
355 }
356
357 mod single_field_variant {
358 #[cfg(not(feature = "std"))]
359 use alloc::format;
360
361 use derive_more::Debug;
362
363 #[derive(Debug)]
364 enum Enum {
365 Unnamed(i32),
366 Named {
367 field: i32,
368 },
369 StrUnnamed(#[debug("i32")] i32),
370 StrNamed {
371 #[debug("i32")]
372 field: i32,
373 },
374 InterpolatedUnnamed(#[debug("{_0}.{}", _0)] i32),
375 InterpolatedNamed {
376 #[debug("{field}.{}", field)]
377 field: i32,
378 },
379 SkippedUnnamed(#[debug(skip)] i32),
380 SkippedNamed {
381 #[debug(skip)]
382 field: i32,
383 },
384 }
385
386 #[test]
387 fn assert() {
388 assert_eq!(format!("{:?}", Enum::Unnamed(1)), "Unnamed(1)");
389 assert_eq!(format!("{:#?}", Enum::Unnamed(1)), "Unnamed(\n 1,\n)");
390 assert_eq!(
391 format!("{:?}", Enum::Named { field: 1 }),
392 "Named { field: 1 }",
393 );
394 assert_eq!(
395 format!("{:#?}", Enum::Named { field: 1 }),
396 "Named {\n field: 1,\n}",
397 );
398 assert_eq!(format!("{:?}", Enum::StrUnnamed(1)), "StrUnnamed(i32)");
399 assert_eq!(
400 format!("{:#?}", Enum::StrUnnamed(1)),
401 "StrUnnamed(\n i32,\n)",
402 );
403 assert_eq!(
404 format!("{:?}", Enum::StrNamed { field: 1 }),
405 "StrNamed { field: i32 }",
406 );
407 assert_eq!(
408 format!("{:#?}", Enum::StrNamed { field: 1 }),
409 "StrNamed {\n field: i32,\n}",
410 );
411 assert_eq!(
412 format!("{:?}", Enum::InterpolatedUnnamed(1)),
413 "InterpolatedUnnamed(1.1)",
414 );
415 assert_eq!(
416 format!("{:#?}", Enum::InterpolatedUnnamed(1)),
417 "InterpolatedUnnamed(\n 1.1,\n)",
418 );
419 assert_eq!(
420 format!("{:?}", Enum::InterpolatedNamed { field: 1 }),
421 "InterpolatedNamed { field: 1.1 }",
422 );
423 assert_eq!(
424 format!("{:#?}", Enum::InterpolatedNamed { field: 1 }),
425 "InterpolatedNamed {\n field: 1.1,\n}",
426 );
427 assert_eq!(
428 format!("{:?}", Enum::SkippedUnnamed(1)),
429 "SkippedUnnamed(..)",
430 );
431 assert_eq!(
432 format!("{:#?}", Enum::SkippedUnnamed(1)),
433 "SkippedUnnamed(..)",
434 );
435 assert_eq!(
436 format!("{:?}", Enum::SkippedNamed { field: 1 }),
437 "SkippedNamed { .. }",
438 );
439 assert_eq!(
440 format!("{:#?}", Enum::SkippedNamed { field: 1 }),
441 "SkippedNamed { .. }",
442 );
443 }
444 }
445
446 mod multi_field_variant {
447 #[cfg(not(feature = "std"))]
448 use alloc::format;
449
450 use derive_more::Debug;
451
452 #[derive(Debug)]
453 enum Enum {
454 Unnamed(i32, i32),
455 Named {
456 field1: i32,
457 field2: i32,
458 },
459 StrUnnamed(#[debug("i32")] i32, i32),
460 StrNamed {
461 field1: i32,
462 #[debug("i32")]
463 field2: i32,
464 },
465 InterpolatedUnnamed(i32, #[debug("{_0}.{}", _1)] i32),
466 InterpolatedNamed {
467 #[debug("{field1}.{}", field2)]
468 field1: i32,
469 field2: i32,
470 },
471 SkippedUnnamed(i32, #[debug(skip)] i32),
472 SkippedNamed {
473 #[debug(skip)]
474 field1: i32,
475 field2: i32,
476 },
477 }
478
479 #[test]
480 fn assert() {
481 assert_eq!(format!("{:?}", Enum::Unnamed(1, 2)), "Unnamed(1, 2)");
482 assert_eq!(
483 format!("{:#?}", Enum::Unnamed(1, 2)),
484 "Unnamed(\n 1,\n 2,\n)",
485 );
486 assert_eq!(
487 format!("{:?}", Enum::StrUnnamed(1, 2)),
488 "StrUnnamed(i32, 2)",
489 );
490 assert_eq!(
491 format!("{:#?}", Enum::StrUnnamed(1, 2)),
492 "StrUnnamed(\n i32,\n 2,\n)",
493 );
494 assert_eq!(
495 format!(
496 "{:?}",
497 Enum::StrNamed {
498 field1: 1,
499 field2: 2,
500 }
501 ),
502 "StrNamed { field1: 1, field2: i32 }",
503 );
504 assert_eq!(
505 format!(
506 "{:#?}",
507 Enum::StrNamed {
508 field1: 1,
509 field2: 2,
510 }
511 ),
512 "StrNamed {\n field1: 1,\n field2: i32,\n}",
513 );
514 assert_eq!(
515 format!("{:?}", Enum::InterpolatedUnnamed(1, 2)),
516 "InterpolatedUnnamed(1, 1.2)",
517 );
518 assert_eq!(
519 format!("{:#?}", Enum::InterpolatedUnnamed(1, 2)),
520 "InterpolatedUnnamed(\n 1,\n 1.2,\n)",
521 );
522 assert_eq!(
523 format!(
524 "{:?}",
525 Enum::InterpolatedNamed {
526 field1: 1,
527 field2: 2,
528 }
529 ),
530 "InterpolatedNamed { field1: 1.2, field2: 2 }",
531 );
532 assert_eq!(
533 format!(
534 "{:#?}",
535 Enum::InterpolatedNamed {
536 field1: 1,
537 field2: 2,
538 }
539 ),
540 "InterpolatedNamed {\n field1: 1.2,\n field2: 2,\n}",
541 );
542 assert_eq!(
543 format!("{:?}", Enum::SkippedUnnamed(1, 2)),
544 "SkippedUnnamed(1, ..)",
545 );
546 assert_eq!(
547 format!("{:#?}", Enum::SkippedUnnamed(1, 2)),
548 "SkippedUnnamed(\n 1,\n ..\n)",
549 );
550 assert_eq!(
551 format!(
552 "{:?}",
553 Enum::SkippedNamed {
554 field1: 1,
555 field2: 2,
556 }
557 ),
558 "SkippedNamed { field2: 2, .. }",
559 );
560 assert_eq!(
561 format!(
562 "{:#?}",
563 Enum::SkippedNamed {
564 field1: 1,
565 field2: 2,
566 }
567 ),
568 "SkippedNamed {\n field2: 2,\n ..\n}",
569 );
570 }
571 }
572 }
573
574 mod generic {
575 #[cfg(not(feature = "std"))]
576 use alloc::format;
577
578 use derive_more::Debug;
579
580 struct NotDebug;
581
582 trait Bound {}
583
584 impl Bound for () {}
585
586 fn display_bound<T: Bound>(_: &T) -> &'static str {
587 "()"
588 }
589
590 #[derive(Debug)]
591 struct NamedGenericStruct<T> {
592 field: T,
593 }
594 #[test]
595 fn named_generic_struct() {
596 assert_eq!(
597 format!("{:?}", NamedGenericStruct { field: 1 }),
598 "NamedGenericStruct { field: 1 }",
599 );
600 assert_eq!(
601 format!("{:#?}", NamedGenericStruct { field: 1 }),
602 "NamedGenericStruct {\n field: 1,\n}",
603 );
604 }
605
606 #[derive(Debug)]
607 struct InterpolatedNamedGenericStruct<T> {
608 #[debug("{field}.{}", field)]
609 field: T,
610 }
611 #[test]
612 fn interpolated_named_generic_struct() {
613 assert_eq!(
614 format!("{:?}", InterpolatedNamedGenericStruct { field: 1 }),
615 "InterpolatedNamedGenericStruct { field: 1.1 }",
616 );
617 assert_eq!(
618 format!("{:#?}", InterpolatedNamedGenericStruct { field: 1 }),
619 "InterpolatedNamedGenericStruct {\n field: 1.1,\n}",
620 );
621 }
622
623 #[derive(Debug)]
624 struct InterpolatedNamedGenericStructWidthPrecision<T> {
625 #[debug("{field:<>width$.prec$}.{field}")]
626 field: T,
627 width: usize,
628 prec: usize,
629 }
630 #[test]
631 fn interpolated_named_generic_struct_width_precision() {
632 assert_eq!(
633 format!(
634 "{:?}",
635 InterpolatedNamedGenericStructWidthPrecision {
636 field: 1.2345,
637 width: 9,
638 prec: 2,
639 }
640 ),
641 "InterpolatedNamedGenericStructWidthPrecision { \
642 field: <<<<<1.23.1.2345, \
643 width: 9, \
644 prec: 2 \
645 }",
646 );
647 assert_eq!(
648 format!(
649 "{:#?}",
650 InterpolatedNamedGenericStructWidthPrecision {
651 field: 1.2345,
652 width: 9,
653 prec: 2,
654 }
655 ),
656 "InterpolatedNamedGenericStructWidthPrecision {\n \
657 field: <<<<<1.23.1.2345,\n \
658 width: 9,\n \
659 prec: 2,\n\
660 }",
661 );
662 }
663
664 #[derive(Debug)]
665 struct AliasedNamedGenericStruct<T> {
666 #[debug("{alias}", alias = field)]
667 field: T,
668 }
669 #[test]
670 fn aliased_named_generic_struct() {
671 assert_eq!(
672 format!("{:?}", AliasedNamedGenericStruct { field: 1 }),
673 "AliasedNamedGenericStruct { field: 1 }",
674 );
675 assert_eq!(
676 format!("{:#?}", AliasedNamedGenericStruct { field: 1 }),
677 "AliasedNamedGenericStruct {\n field: 1,\n}",
678 );
679 }
680
681 #[derive(Debug)]
682 struct AliasedFieldNamedGenericStruct<T> {
683 #[debug("{field1}", field1 = field2)]
684 field1: T,
685 field2: i32,
686 }
687 #[test]
688 fn aliased_field_named_generic_struct() {
689 assert_eq!(
690 format!(
691 "{:?}",
692 AliasedFieldNamedGenericStruct {
693 field1: NotDebug,
694 field2: 1,
695 },
696 ),
697 "AliasedFieldNamedGenericStruct { field1: 1, field2: 1 }",
698 );
699 assert_eq!(
700 format!(
701 "{:#?}",
702 AliasedFieldNamedGenericStruct {
703 field1: NotDebug,
704 field2: 1,
705 },
706 ),
707 "AliasedFieldNamedGenericStruct {\n field1: 1,\n field2: 1,\n}",
708 );
709 }
710
711 #[derive(Debug)]
712 struct UnnamedGenericStruct<T>(T);
713 #[test]
714 fn unnamed_generic_struct() {
715 assert_eq!(
716 format!("{:?}", UnnamedGenericStruct(2)),
717 "UnnamedGenericStruct(2)",
718 );
719 assert_eq!(
720 format!("{:#?}", UnnamedGenericStruct(2)),
721 "UnnamedGenericStruct(\n 2,\n)",
722 );
723 }
724
725 #[derive(Debug)]
726 struct InterpolatedUnnamedGenericStruct<T>(#[debug("{}.{_0}", _0)] T);
727 #[test]
728 fn interpolated_unnamed_generic_struct() {
729 assert_eq!(
730 format!("{:?}", InterpolatedUnnamedGenericStruct(2)),
731 "InterpolatedUnnamedGenericStruct(2.2)",
732 );
733 assert_eq!(
734 format!("{:#?}", InterpolatedUnnamedGenericStruct(2)),
735 "InterpolatedUnnamedGenericStruct(\n 2.2,\n)",
736 );
737 }
738
739 #[derive(Debug)]
740 struct AliasedUnnamedGenericStruct<T>(#[debug("{alias}", alias = _0)] T);
741 #[test]
742 fn aliased_unnamed_generic_struct() {
743 assert_eq!(
744 format!("{:?}", AliasedUnnamedGenericStruct(2)),
745 "AliasedUnnamedGenericStruct(2)",
746 );
747 assert_eq!(
748 format!("{:#?}", AliasedUnnamedGenericStruct(2)),
749 "AliasedUnnamedGenericStruct(\n 2,\n)",
750 );
751 }
752
753 #[derive(Debug)]
754 struct AliasedFieldUnnamedGenericStruct<T>(#[debug("{_0}", _0 = _1)] T, i32);
755 #[test]
756 fn aliased_field_unnamed_generic_struct() {
757 assert_eq!(
758 format!("{:?}", AliasedFieldUnnamedGenericStruct(NotDebug, 2)),
759 "AliasedFieldUnnamedGenericStruct(2, 2)",
760 );
761 assert_eq!(
762 format!("{:#?}", AliasedFieldUnnamedGenericStruct(NotDebug, 2)),
763 "AliasedFieldUnnamedGenericStruct(\n 2,\n 2,\n)",
764 );
765 }
766
767 #[derive(Debug)]
768 enum GenericEnum<A, B> {
769 A { field: A },
770 B(B),
771 }
772 #[test]
773 fn generic_enum() {
774 assert_eq!(
775 format!("{:?}", GenericEnum::A::<_, u8> { field: 1 }),
776 "A { field: 1 }"
777 );
778 assert_eq!(
779 format!("{:#?}", GenericEnum::A::<_, u8> { field: 1 }),
780 "A {\n field: 1,\n}"
781 );
782 assert_eq!(format!("{:?}", GenericEnum::B::<u8, _>(2)), "B(2)");
783 assert_eq!(
784 format!("{:#?}", GenericEnum::B::<u8, _>(2)),
785 "B(\n 2,\n)",
786 );
787 }
788
789 #[derive(Debug)]
790 enum InterpolatedGenericEnum<A, B> {
791 A {
792 #[debug("{}.{field}", field)]
793 field: A,
794 },
795 B(#[debug("{}.{_0}", _0)] B),
796 }
797 #[test]
798 fn interpolated_generic_enum() {
799 assert_eq!(
800 format!("{:?}", InterpolatedGenericEnum::A::<_, u8> { field: 1 }),
801 "A { field: 1.1 }",
802 );
803 assert_eq!(
804 format!("{:#?}", InterpolatedGenericEnum::A::<_, u8> { field: 1 }),
805 "A {\n field: 1.1,\n}",
806 );
807 assert_eq!(
808 format!("{:?}", InterpolatedGenericEnum::B::<u8, _>(2)),
809 "B(2.2)",
810 );
811 assert_eq!(
812 format!("{:#?}", InterpolatedGenericEnum::B::<u8, _>(2)),
813 "B(\n 2.2,\n)",
814 );
815 }
816
817 #[derive(Debug)]
818 struct MultiTraitNamedGenericStruct<A, B> {
819 #[debug("{}.{}<->{0:o}.{1:#x}<->{0:?}.{1:X?}", a, b)]
820 a: A,
821 b: B,
822 }
823 #[test]
824 fn multi_trait_named_generic_struct() {
825 let s = MultiTraitNamedGenericStruct { a: 8u8, b: 255 };
826 assert_eq!(
827 format!("{s:?}"),
828 "MultiTraitNamedGenericStruct { a: 8.255<->10.0xff<->8.FF, b: 255 }",
829 );
830 assert_eq!(
831 format!("{s:#?}"),
832 "MultiTraitNamedGenericStruct {\n a: 8.255<->10.0xff<->8.FF,\n b: 255,\n}",
833 );
834 }
835
836 #[derive(Debug)]
837 struct MultiTraitUnnamedGenericStruct<A, B>(
838 #[debug("{}.{}.{{}}.{0:o}.{1:#x}-{0:>4?}.{1:^4X?}", _0, _1)] A,
839 B,
840 );
841 #[test]
842 fn multi_trait_unnamed_generic_struct() {
843 let s = MultiTraitUnnamedGenericStruct(8u8, 255);
844 assert_eq!(
845 format!("{s:?}"),
846 "MultiTraitUnnamedGenericStruct(8.255.{}.10.0xff- 8. FF , 255)",
847 );
848 assert_eq!(
849 format!("{s:#?}"),
850 "MultiTraitUnnamedGenericStruct(\n 8.255.{}.10.0xff- 8. FF ,\n 255,\n)",
851 );
852 }
853
854 #[derive(Debug)]
855 struct UnusedGenericStruct<T>(#[debug("{}", 3 * 4)] T);
856 #[test]
857 fn unused_generic_struct() {
858 let s = UnusedGenericStruct(NotDebug);
859 assert_eq!(format!("{s:?}"), "UnusedGenericStruct(12)");
860 assert_eq!(format!("{s:#?}"), "UnusedGenericStruct(\n 12,\n)");
861 }
862
863 mod associated_type_field_enumerator {
864 #[cfg(not(feature = "std"))]
865 use alloc::format;
866
867 use derive_more::Debug;
868
869 trait Trait {
870 type Type;
871 }
872
873 struct Struct;
874
875 impl Trait for Struct {
876 type Type = i32;
877 }
878
879 #[test]
880 fn auto_generic_named_struct_associated() {
881 #[derive(Debug)]
882 struct AutoGenericNamedStructAssociated<T: Trait> {
883 field: <T as Trait>::Type,
884 }
885
886 let s = AutoGenericNamedStructAssociated::<Struct> { field: 10 };
887 assert_eq!(
888 format!("{s:?}"),
889 "AutoGenericNamedStructAssociated { field: 10 }",
890 );
891 assert_eq!(
892 format!("{s:#?}"),
893 "AutoGenericNamedStructAssociated {\n field: 10,\n}",
894 );
895 }
896
897 #[test]
898 fn auto_generic_unnamed_struct_associated() {
899 #[derive(Debug)]
900 struct AutoGenericUnnamedStructAssociated<T: Trait>(<T as Trait>::Type);
901
902 let s = AutoGenericUnnamedStructAssociated::<Struct>(10);
903 assert_eq!(format!("{s:?}"), "AutoGenericUnnamedStructAssociated(10)",);
904 assert_eq!(
905 format!("{s:#?}"),
906 "AutoGenericUnnamedStructAssociated(\n 10,\n)",
907 );
908 }
909
910 #[test]
911 fn auto_generic_enum_associated() {
912 #[derive(Debug)]
913 enum AutoGenericEnumAssociated<T: Trait> {
914 Enumerator(<T as Trait>::Type),
915 }
916
917 let e = AutoGenericEnumAssociated::<Struct>::Enumerator(10);
918 assert_eq!(format!("{:?}", e), "Enumerator(10)");
919 assert_eq!(format!("{:#?}", e), "Enumerator(\n 10,\n)");
920 }
921 }
922
923 mod complex_type_field_enumerator {
924 #[cfg(not(feature = "std"))]
925 use alloc::format;
926
927 use derive_more::Debug;
928
929 #[derive(Debug)]
930 struct Struct<T>(T);
931
932 #[test]
933 fn auto_generic_named_struct_complex() {
934 #[derive(Debug)]
935 struct AutoGenericNamedStructComplex<T> {
936 field: Struct<T>,
937 }
938
939 let s = AutoGenericNamedStructComplex { field: Struct(10) };
940 assert_eq!(
941 format!("{s:?}"),
942 "AutoGenericNamedStructComplex { field: Struct(10) }",
943 );
944 assert_eq!(
945 format!("{s:#?}"),
946 "AutoGenericNamedStructComplex {\n field: Struct(\n 10,\n ),\n}",
947 );
948 }
949
950 #[test]
951 fn auto_generic_unnamed_struct_complex() {
952 #[derive(Debug)]
953 struct AutoGenericUnnamedStructComplex<T>(Struct<T>);
954
955 let s = AutoGenericUnnamedStructComplex(Struct(10));
956 assert_eq!(
957 format!("{s:?}"),
958 "AutoGenericUnnamedStructComplex(Struct(10))",
959 );
960 assert_eq!(
961 format!("{s:#?}"),
962 "AutoGenericUnnamedStructComplex(\n Struct(\n 10,\n ),\n)",
963 );
964 }
965
966 #[test]
967 fn auto_generic_enum_complex() {
968 #[derive(Debug)]
969 enum AutoGenericEnumComplex<T> {
970 Enumerator(Struct<T>),
971 }
972
973 let e = AutoGenericEnumComplex::Enumerator(Struct(10));
974 assert_eq!(format!("{:?}", e), "Enumerator(Struct(10))");
975 assert_eq!(
976 format!("{:#?}", e),
977 "Enumerator(\n Struct(\n 10,\n ),\n)",
978 )
979 }
980 }
981
982 mod reference {
983 #[cfg(not(feature = "std"))]
984 use alloc::format;
985
986 use derive_more::Debug;
987
988 #[test]
989 fn auto_generic_reference() {
990 #[derive(Debug)]
991 struct AutoGenericReference<'a, T>(&'a T);
992
993 let s = AutoGenericReference(&10);
994 assert_eq!(format!("{s:?}"), "AutoGenericReference(10)");
995 assert_eq!(format!("{s:#?}"), "AutoGenericReference(\n 10,\n)");
996 }
997
998 #[test]
999 fn auto_generic_static_reference() {
1000 #[derive(Debug)]
1001 struct AutoGenericStaticReference<T: 'static>(&'static T);
1002
1003 let s = AutoGenericStaticReference(&10);
1004 assert_eq!(format!("{s:?}"), "AutoGenericStaticReference(10)");
1005 assert_eq!(format!("{s:#?}"), "AutoGenericStaticReference(\n 10,\n)",);
1006 }
1007 }
1008
1009 mod indirect {
1010 #[cfg(not(feature = "std"))]
1011 use alloc::format;
1012
1013 use derive_more::Debug;
1014
1015 #[derive(Debug)]
1016 struct Struct<T>(T);
1017
1018 #[test]
1019 fn auto_generic_indirect() {
1020 #[derive(Debug)]
1021 struct AutoGenericIndirect<T: 'static>(Struct<&'static T>);
1022
1023 const V: i32 = 10;
1024 let s = AutoGenericIndirect(Struct(&V));
1025 assert_eq!(format!("{s:?}"), "AutoGenericIndirect(Struct(10))");
1026 assert_eq!(
1027 format!("{s:#?}"),
1028 "AutoGenericIndirect(\n Struct(\n 10,\n ),\n)",
1029 );
1030 }
1031 }
1032
1033 mod bound {
1034 #[cfg(not(feature = "std"))]
1035 use alloc::format;
1036
1037 use derive_more::Debug;
1038
1039 #[test]
1040 fn simple() {
1041 #[derive(Debug)]
1042 struct Struct<T1, T2>(#[debug("{}.{}", _0, _1)] T1, #[debug(skip)] T2);
1043
1044 let s = Struct(10, 20);
1045 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
1046 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
1047 }
1048
1049 #[test]
1050 fn underscored_simple() {
1051 #[derive(Debug)]
1052 struct Struct<T1, T2>(#[debug("{_0}.{_1}")] T1, #[debug(skip)] T2);
1053
1054 let s = Struct(10, 20);
1055 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
1056 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
1057 }
1058
1059 #[test]
1060 fn redundant() {
1061 #[derive(Debug)]
1062 #[debug(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
1063 struct Struct<T1, T2>(#[debug("{}.{}", _0, _1)] T1, #[debug(skip)] T2);
1064
1065 let s = Struct(10, 20);
1066 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
1067 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
1068 }
1069
1070 #[test]
1071 fn underscored_redundant() {
1072 #[derive(Debug)]
1073 #[debug(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
1074 struct Struct<T1, T2>(#[debug("{_0}.{_1}")] T1, #[debug(ignore)] T2);
1075
1076 let s = Struct(10, 20);
1077 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
1078 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
1079 }
1080
1081 #[test]
1082 fn complex() {
1083 trait Trait1 {
1084 fn function1(&self) -> &'static str;
1085 }
1086
1087 trait Trait2 {
1088 fn function2(&self) -> &'static str;
1089 }
1090
1091 impl Trait1 for i32 {
1092 fn function1(&self) -> &'static str {
1093 "WHAT"
1094 }
1095 }
1096
1097 impl Trait2 for i32 {
1098 fn function2(&self) -> &'static str {
1099 "EVER"
1100 }
1101 }
1102
1103 #[derive(Debug)]
1104 #[debug(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
1105 struct Struct<T1, T2>(
1106 #[debug("{}_{}_{}_{}", _0.function1(), _0, _1.function2(), _1)] T1,
1107 T2,
1108 );
1109
1110 let s = Struct(10, 20);
1111 assert_eq!(format!("{s:?}"), "Struct(WHAT_10_EVER_20, 20)");
1112 assert_eq!(
1113 format!("{s:#?}"),
1114 "Struct(\n WHAT_10_EVER_20,\n 20,\n)",
1115 );
1116 }
1117
1118 #[test]
1119 fn underscored_complex() {
1120 trait Trait1 {
1121 fn function1(&self) -> &'static str;
1122 }
1123
1124 trait Trait2 {
1125 fn function2(&self) -> &'static str;
1126 }
1127
1128 impl Trait1 for i32 {
1129 fn function1(&self) -> &'static str {
1130 "WHAT"
1131 }
1132 }
1133
1134 impl Trait2 for i32 {
1135 fn function2(&self) -> &'static str {
1136 "EVER"
1137 }
1138 }
1139
1140 #[derive(Debug)]
1141 #[debug(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
1142 struct Struct<T1, T2>(
1143 #[debug("{}_{_0}_{}_{_1}", _0.function1(), _1.function2())] T1,
1144 T2,
1145 );
1146
1147 let s = Struct(10, 20);
1148 assert_eq!(format!("{s:?}"), "Struct(WHAT_10_EVER_20, 20)");
1149 assert_eq!(
1150 format!("{s:#?}"),
1151 "Struct(\n WHAT_10_EVER_20,\n 20,\n)",
1152 );
1153 }
1154 }
1155 }