Debian Codegraph @

third_party/rust/derive_more/tests/display.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code, unused_imports)]
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 #[cfg(not(feature = "std"))]
8 use alloc::{
9 boxed::Box,
10 format,
11 string::{String, ToString},
12 vec::Vec,
13 };
14
15 use derive_more::{Binary, Display, Octal, UpperHex};
16
17 mod structs {
18 use super::*;
19
20 mod unit {
21 use super::*;
22
23 #[derive(Display)]
24 struct Unit;
25
26 #[derive(Display)]
27 struct Tuple();
28
29 #[derive(Display)]
30 struct Struct {}
31
32 #[test]
33 fn assert() {
34 assert_eq!(Unit.to_string(), "Unit");
35 assert_eq!(Tuple().to_string(), "Tuple");
36 assert_eq!(Struct {}.to_string(), "Struct");
37 }
38
39 mod str {
40 use super::*;
41
42 #[derive(Display)]
43 #[display("unit")]
44 pub struct Unit;
45
46 #[derive(Display)]
47 #[display("tuple")]
48 pub struct Tuple();
49
50 #[derive(Display)]
51 #[display("struct")]
52 pub struct Struct {}
53
54 #[test]
55 fn assert() {
56 assert_eq!(Unit.to_string(), "unit");
57 assert_eq!(Tuple().to_string(), "tuple");
58 assert_eq!(Struct {}.to_string(), "struct");
59 }
60 }
61
62 mod interpolated {
63 use super::*;
64
65 #[derive(Display)]
66 #[display("unit: {}", 0)]
67 pub struct Unit;
68
69 #[derive(Display)]
70 #[display("tuple: {}", 0)]
71 pub struct Tuple();
72
73 #[derive(Display)]
74 #[display("struct: {}", 0)]
75 pub struct Struct {}
76
77 #[test]
78 fn assert() {
79 assert_eq!(Unit.to_string(), "unit: 0");
80 assert_eq!(Tuple().to_string(), "tuple: 0");
81 assert_eq!(Struct {}.to_string(), "struct: 0");
82 }
83 }
84 }
85
86 mod single_field {
87 use super::*;
88
89 #[derive(Display)]
90 struct Tuple(i32);
91
92 #[derive(Binary)]
93 struct Binary(i32);
94
95 #[derive(Display)]
96 struct Struct {
97 field: i32,
98 }
99
100 #[derive(Octal)]
101 struct Octal {
102 field: i32,
103 }
104
105 #[test]
106 fn assert() {
107 assert_eq!(Tuple(0).to_string(), "0");
108 assert_eq!(format!("{:b}", Binary(10)), "1010");
109 assert_eq!(Struct { field: 0 }.to_string(), "0");
110 assert_eq!(format!("{:o}", Octal { field: 10 }).to_string(), "12");
111 }
112
113 mod str {
114 use super::*;
115
116 #[derive(Display)]
117 #[display("tuple")]
118 struct Tuple(i32);
119
120 #[derive(Display)]
121 #[display("struct")]
122 struct Struct {
123 field: i32,
124 }
125
126 #[test]
127 fn assert() {
128 assert_eq!(Tuple(0).to_string(), "tuple");
129 assert_eq!(Struct { field: 0 }.to_string(), "struct");
130 }
131 }
132
133 mod interpolated {
134 use super::*;
135
136 #[derive(Display)]
137 #[display("tuple: {_0} {}", _0)]
138 struct Tuple(i32);
139
140 #[derive(Display)]
141 #[display("struct: {field} {}", field)]
142 struct Struct {
143 field: i32,
144 }
145
146 #[test]
147 fn assert() {
148 assert_eq!(Tuple(0).to_string(), "tuple: 0 0");
149 assert_eq!(Struct { field: 0 }.to_string(), "struct: 0 0");
150 }
151 }
152 }
153
154 mod multi_field {
155 use super::*;
156
157 mod str {
158 use super::*;
159
160 #[derive(Display)]
161 #[display("tuple")]
162 struct Tuple(i32, i32);
163
164 #[derive(Display)]
165 #[display("struct")]
166 struct Struct {
167 field1: i32,
168 field2: i32,
169 }
170
171 #[test]
172 fn assert() {
173 assert_eq!(Tuple(1, 2).to_string(), "tuple");
174 assert_eq!(
175 Struct {
176 field1: 1,
177 field2: 2,
178 }
179 .to_string(),
180 "struct",
181 );
182 }
183 }
184
185 mod interpolated {
186 use super::*;
187
188 #[derive(Display)]
189 #[display(
190 "{_0} {ident} {_1} {} {}",
191 _1, _0 + _1, ident = 123, _1 = _0,
192 )]
193 struct Tuple(i32, i32);
194
195 #[derive(Display)]
196 #[display(
197 "{field1} {ident} {field2} {} {}",
198 field2, field1 + field2, ident = 123, field2 = field1,
199 )]
200 struct Struct {
201 field1: i32,
202 field2: i32,
203 }
204
205 #[test]
206 fn assert() {
207 assert_eq!(Tuple(1, 2).to_string(), "1 123 1 2 3");
208 assert_eq!(
209 Struct {
210 field1: 1,
211 field2: 2,
212 }
213 .to_string(),
214 "1 123 1 2 3",
215 );
216 }
217 }
218 }
219 }
220
221 mod enums {
222 use super::*;
223
224 mod no_variants {
225 use super::*;
226
227 #[derive(Display)]
228 enum Void {}
229
230 const fn assert<T: Display>() {}
231 const _: () = assert::<Void>();
232 }
233
234 mod unit_variant {
235 use super::*;
236
237 #[derive(Display)]
238 enum Enum {
239 Unit,
240 Unnamed(),
241 Named {},
242 #[display("STR_UNIT")]
243 StrUnit,
244 #[display("STR_UNNAMED")]
245 StrUnnamed(),
246 #[display("STR_NAMED")]
247 StrNamed {},
248 }
249
250 #[test]
251 fn assert() {
252 assert_eq!(Enum::Unit.to_string(), "Unit");
253 assert_eq!(Enum::Unnamed().to_string(), "Unnamed");
254 assert_eq!(Enum::Named {}.to_string(), "Named");
255 assert_eq!(Enum::StrUnit.to_string(), "STR_UNIT");
256 assert_eq!(Enum::StrUnnamed().to_string(), "STR_UNNAMED");
257 assert_eq!(Enum::StrNamed {}.to_string(), "STR_NAMED");
258 }
259 }
260
261 mod single_field_variant {
262 use super::*;
263
264 #[derive(Display)]
265 enum Enum {
266 Unnamed(i32),
267 Named {
268 field: i32,
269 },
270 #[display("unnamed")]
271 StrUnnamed(i32),
272 #[display("named")]
273 StrNamed {
274 field: i32,
275 },
276 #[display("{_0} {}", _0)]
277 InterpolatedUnnamed(i32),
278 #[display("{field} {}", field)]
279 InterpolatedNamed {
280 field: i32,
281 },
282 }
283
284 #[test]
285 fn assert() {
286 assert_eq!(Enum::Unnamed(1).to_string(), "1");
287 assert_eq!(Enum::Named { field: 1 }.to_string(), "1");
288 assert_eq!(Enum::StrUnnamed(1).to_string(), "unnamed");
289 assert_eq!(Enum::StrNamed { field: 1 }.to_string(), "named");
290 assert_eq!(Enum::InterpolatedUnnamed(1).to_string(), "1 1");
291 assert_eq!(Enum::InterpolatedNamed { field: 1 }.to_string(), "1 1");
292 }
293 }
294
295 mod multi_field_variant {
296 use super::*;
297
298 #[derive(Display)]
299 enum Enum {
300 #[display("unnamed")]
301 StrUnnamed(i32, i32),
302 #[display("named")]
303 StrNamed { field1: i32, field2: i32 },
304 #[display(
305 "{_0} {ident} {_1} {} {}",
306 _1, _0 + _1, ident = 123, _1 = _0,
307 )]
308 InterpolatedUnnamed(i32, i32),
309 #[display(
310 "{field1} {ident} {field2} {} {}",
311 field2, field1 + field2, ident = 123, field2 = field1,
312 )]
313 InterpolatedNamed { field1: i32, field2: i32 },
314 }
315
316 #[test]
317 fn assert() {
318 assert_eq!(Enum::StrUnnamed(1, 2).to_string(), "unnamed");
319 assert_eq!(
320 Enum::StrNamed {
321 field1: 1,
322 field2: 2,
323 }
324 .to_string(),
325 "named",
326 );
327 assert_eq!(Enum::InterpolatedUnnamed(1, 2).to_string(), "1 123 1 2 3");
328 assert_eq!(
329 Enum::InterpolatedNamed {
330 field1: 1,
331 field2: 2,
332 }
333 .to_string(),
334 "1 123 1 2 3",
335 );
336 }
337 }
338 }
339
340 mod generic {
341 use super::*;
342
343 trait Bound {}
344
345 impl Bound for () {}
346
347 fn display_bound<T: Bound>(_: &T) -> &'static str {
348 "()"
349 }
350
351 #[derive(Display)]
352 #[display("Generic {}", field)]
353 struct NamedGenericStruct<T> {
354 field: T,
355 }
356 #[test]
357 fn named_generic_struct() {
358 assert_eq!(NamedGenericStruct { field: 1 }.to_string(), "Generic 1");
359 }
360
361 #[derive(Display)]
362 #[display("Generic {field}")]
363 struct InterpolatedNamedGenericStruct<T> {
364 field: T,
365 }
366 #[test]
367 fn interpolated_named_generic_struct() {
368 assert_eq!(
369 InterpolatedNamedGenericStruct { field: 1 }.to_string(),
370 "Generic 1",
371 );
372 }
373
374 #[derive(Display)]
375 #[display("Generic {field:<>width$.prec$} {field}")]
376 struct InterpolatedNamedGenericStructWidthPrecision<T> {
377 field: T,
378 width: usize,
379 prec: usize,
380 }
381 #[test]
382 fn interpolated_named_generic_struct_width_precision() {
383 assert_eq!(
384 InterpolatedNamedGenericStructWidthPrecision {
385 field: 1.2345,
386 width: 9,
387 prec: 2,
388 }
389 .to_string(),
390 "Generic <<<<<1.23 1.2345",
391 );
392 }
393
394 #[derive(Display)]
395 struct AutoNamedGenericStruct<T> {
396 field: T,
397 }
398 #[test]
399 fn auto_named_generic_struct() {
400 assert_eq!(AutoNamedGenericStruct { field: 1 }.to_string(), "1");
401 }
402
403 #[derive(Display)]
404 #[display("{alias}", alias = field)]
405 struct AliasedNamedGenericStruct<T> {
406 field: T,
407 }
408 #[test]
409 fn aliased_named_generic_struct() {
410 assert_eq!(AliasedNamedGenericStruct { field: 1 }.to_string(), "1");
411 }
412
413 #[derive(Display)]
414 #[display("{field1}", field1 = field2)]
415 struct AliasedFieldNamedGenericStruct<T> {
416 field1: T,
417 field2: i32,
418 }
419 #[test]
420 fn aliased_field_named_generic_struct() {
421 assert_eq!(
422 AliasedFieldNamedGenericStruct {
423 field1: (),
424 field2: 1,
425 }
426 .to_string(),
427 "1",
428 );
429 }
430
431 #[derive(Display)]
432 #[display("Generic {}", _0)]
433 struct UnnamedGenericStruct<T>(T);
434 #[test]
435 fn unnamed_generic_struct() {
436 assert_eq!(UnnamedGenericStruct(2).to_string(), "Generic 2");
437 }
438
439 #[derive(Display)]
440 #[display("Generic {_0}")]
441 struct InterpolatedUnnamedGenericStruct<T>(T);
442 #[test]
443 fn interpolated_unnamed_generic_struct() {
444 assert_eq!(InterpolatedUnnamedGenericStruct(2).to_string(), "Generic 2");
445 }
446
447 #[derive(Display)]
448 struct AutoUnnamedGenericStruct<T>(T);
449 #[test]
450 fn auto_unnamed_generic_struct() {
451 assert_eq!(AutoUnnamedGenericStruct(2).to_string(), "2");
452 }
453
454 #[derive(Display)]
455 #[display("{alias}", alias = _0)]
456 struct AliasedUnnamedGenericStruct<T>(T);
457 #[test]
458 fn aliased_unnamed_generic_struct() {
459 assert_eq!(AliasedUnnamedGenericStruct(2).to_string(), "2");
460 }
461
462 #[derive(Display)]
463 #[display("{_0}", _0 = _1)]
464 struct AliasedFieldUnnamedGenericStruct<T>(T, i32);
465 #[test]
466 fn aliased_field_unnamed_generic_struct() {
467 assert_eq!(AliasedFieldUnnamedGenericStruct((), 2).to_string(), "2");
468 }
469
470 #[derive(Display)]
471 enum GenericEnum<A, B> {
472 #[display("Gen::A {}", field)]
473 A { field: A },
474 #[display("Gen::B {}", _0)]
475 B(B),
476 }
477 #[test]
478 fn generic_enum() {
479 assert_eq!(GenericEnum::A::<_, u8> { field: 1 }.to_string(), "Gen::A 1");
480 assert_eq!(GenericEnum::B::<u8, _>(2).to_string(), "Gen::B 2");
481 }
482
483 #[derive(Display)]
484 enum InterpolatedGenericEnum<A, B> {
485 #[display("Gen::A {field}")]
486 A { field: A },
487 #[display("Gen::B {_0}")]
488 B(B),
489 }
490 #[test]
491 fn interpolated_generic_enum() {
492 assert_eq!(
493 InterpolatedGenericEnum::A::<_, u8> { field: 1 }.to_string(),
494 "Gen::A 1",
495 );
496 assert_eq!(
497 InterpolatedGenericEnum::B::<u8, _>(2).to_string(),
498 "Gen::B 2",
499 );
500 }
501
502 #[derive(Display)]
503 enum AutoGenericEnum<A, B> {
504 A { field: A },
505 B(B),
506 }
507 #[test]
508 fn auto_generic_enum() {
509 assert_eq!(AutoGenericEnum::A::<_, u8> { field: 1 }.to_string(), "1");
510 assert_eq!(AutoGenericEnum::B::<u8, _>(2).to_string(), "2");
511 }
512
513 #[derive(Display)]
514 #[display("{} {} <-> {0:o} {1:#x} <-> {0:?} {1:X?}", a, b)]
515 struct MultiTraitNamedGenericStruct<A, B> {
516 a: A,
517 b: B,
518 }
519 #[test]
520 fn multi_trait_named_generic_struct() {
521 let s = MultiTraitNamedGenericStruct { a: 8u8, b: 255 };
522 assert_eq!(s.to_string(), "8 255 <-> 10 0xff <-> 8 FF");
523 }
524
525 #[derive(Display)]
526 #[display("{} {b} <-> {0:o} {1:#x} <-> {0:?} {1:X?}", a, b)]
527 struct InterpolatedMultiTraitNamedGenericStruct<A, B> {
528 a: A,
529 b: B,
530 }
531 #[test]
532 fn interpolated_multi_trait_named_generic_struct() {
533 let s = InterpolatedMultiTraitNamedGenericStruct { a: 8u8, b: 255 };
534 assert_eq!(s.to_string(), "8 255 <-> 10 0xff <-> 8 FF");
535 }
536
537 #[derive(Display)]
538 #[display("{} {} {{}} {0:o} {1:#x} - {0:>4?} {1:^4X?}", _0, _1)]
539 struct MultiTraitUnnamedGenericStruct<A, B>(A, B);
540 #[test]
541 fn multi_trait_unnamed_generic_struct() {
542 let s = MultiTraitUnnamedGenericStruct(8u8, 255);
543 assert_eq!(s.to_string(), "8 255 {} 10 0xff - 8 FF ");
544 }
545
546 #[derive(Display)]
547 #[display("{} {_1} {{}} {0:o} {1:#x} - {0:>4?} {1:^4X?}", _0, _1)]
548 struct InterpolatedMultiTraitUnnamedGenericStruct<A, B>(A, B);
549 #[test]
550 fn interpolated_multi_trait_unnamed_generic_struct() {
551 let s = InterpolatedMultiTraitUnnamedGenericStruct(8u8, 255);
552 assert_eq!(s.to_string(), "8 255 {} 10 0xff - 8 FF ");
553 }
554
555 #[derive(Display)]
556 #[display("{}", 3 * 4)]
557 struct UnusedGenericStruct<T>(T);
558 #[test]
559 fn unused_generic_struct() {
560 let s = UnusedGenericStruct(());
561 assert_eq!(s.to_string(), "12");
562 }
563
564 mod associated_type_field_enumerator {
565 use super::*;
566
567 trait Trait {
568 type Type;
569 }
570
571 struct Struct;
572
573 impl Trait for Struct {
574 type Type = i32;
575 }
576
577 #[test]
578 fn auto_generic_named_struct_associated() {
579 #[derive(Display)]
580 struct AutoGenericNamedStructAssociated<T: Trait> {
581 field: <T as Trait>::Type,
582 }
583
584 let s = AutoGenericNamedStructAssociated::<Struct> { field: 10 };
585 assert_eq!(s.to_string(), "10");
586 }
587
588 #[test]
589 fn auto_generic_unnamed_struct_associated() {
590 #[derive(Display)]
591 struct AutoGenericUnnamedStructAssociated<T: Trait>(<T as Trait>::Type);
592
593 let s = AutoGenericUnnamedStructAssociated::<Struct>(10);
594 assert_eq!(s.to_string(), "10");
595 }
596
597 #[test]
598 fn auto_generic_enum_associated() {
599 #[derive(Display)]
600 enum AutoGenericEnumAssociated<T: Trait> {
601 Enumerator(<T as Trait>::Type),
602 }
603
604 let e = AutoGenericEnumAssociated::<Struct>::Enumerator(10);
605 assert_eq!(e.to_string(), "10");
606 }
607 }
608
609 mod complex_type_field_enumerator {
610 use super::*;
611
612 #[derive(Display)]
613 struct Struct<T>(T);
614
615 #[test]
616 fn auto_generic_named_struct_complex() {
617 #[derive(Display)]
618 struct AutoGenericNamedStructComplex<T> {
619 field: Struct<T>,
620 }
621
622 let s = AutoGenericNamedStructComplex { field: Struct(10) };
623 assert_eq!(s.to_string(), "10");
624 }
625
626 #[test]
627 fn auto_generic_unnamed_struct_complex() {
628 #[derive(Display)]
629 struct AutoGenericUnnamedStructComplex<T>(Struct<T>);
630
631 let s = AutoGenericUnnamedStructComplex(Struct(10));
632 assert_eq!(s.to_string(), "10");
633 }
634
635 #[test]
636 fn auto_generic_enum_complex() {
637 #[derive(Display)]
638 enum AutoGenericEnumComplex<T> {
639 Enumerator(Struct<T>),
640 }
641
642 let e = AutoGenericEnumComplex::Enumerator(Struct(10));
643 assert_eq!(e.to_string(), "10")
644 }
645 }
646
647 mod reference {
648 use super::*;
649
650 #[test]
651 fn auto_generic_reference() {
652 #[derive(Display)]
653 struct AutoGenericReference<'a, T>(&'a T);
654
655 let s = AutoGenericReference(&10);
656 assert_eq!(s.to_string(), "10");
657 }
658
659 #[test]
660 fn auto_generic_static_reference() {
661 #[derive(Display)]
662 struct AutoGenericStaticReference<T: 'static>(&'static T);
663
664 let s = AutoGenericStaticReference(&10);
665 assert_eq!(s.to_string(), "10");
666 }
667 }
668
669 mod indirect {
670 use super::*;
671
672 #[derive(Display)]
673 struct Struct<T>(T);
674
675 #[test]
676 fn auto_generic_indirect() {
677 #[derive(Display)]
678 struct AutoGenericIndirect<T: 'static>(Struct<&'static T>);
679
680 const V: i32 = 10;
681 let s = AutoGenericIndirect(Struct(&V));
682 assert_eq!(s.to_string(), "10");
683 }
684 }
685
686 mod bound {
687 use super::*;
688
689 #[test]
690 fn simple() {
691 #[derive(Display)]
692 #[display("{} {}", _0, _1)]
693 struct Struct<T1, T2>(T1, T2);
694
695 let s = Struct(10, 20);
696 assert_eq!(s.to_string(), "10 20");
697 }
698
699 #[test]
700 fn underscored_simple() {
701 #[derive(Display)]
702 #[display("{_0} {_1}")]
703 struct Struct<T1, T2>(T1, T2);
704
705 let s = Struct(10, 20);
706 assert_eq!(s.to_string(), "10 20");
707 }
708
709 #[test]
710 fn redundant() {
711 #[derive(Display)]
712 #[display(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
713 #[display("{} {}", _0, _1)]
714 struct Struct<T1, T2>(T1, T2);
715
716 let s = Struct(10, 20);
717 assert_eq!(s.to_string(), "10 20");
718 }
719
720 #[test]
721 fn underscored_redundant() {
722 #[derive(Display)]
723 #[display(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
724 #[display("{_0} {_1}")]
725 struct Struct<T1, T2>(T1, T2);
726
727 let s = Struct(10, 20);
728 assert_eq!(s.to_string(), "10 20");
729 }
730
731 #[test]
732 fn complex() {
733 trait Trait1 {
734 fn function1(&self) -> &'static str;
735 }
736
737 trait Trait2 {
738 fn function2(&self) -> &'static str;
739 }
740
741 impl Trait1 for i32 {
742 fn function1(&self) -> &'static str {
743 "WHAT"
744 }
745 }
746
747 impl Trait2 for i32 {
748 fn function2(&self) -> &'static str {
749 "EVER"
750 }
751 }
752
753 #[derive(Display)]
754 #[display(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
755 #[display("{} {} {} {}", _0.function1(), _0, _1.function2(), _1)]
756 struct Struct<T1, T2>(T1, T2);
757
758 let s = Struct(10, 20);
759 assert_eq!(s.to_string(), "WHAT 10 EVER 20");
760 }
761
762 #[test]
763 fn underscored_complex() {
764 trait Trait1 {
765 fn function1(&self) -> &'static str;
766 }
767
768 trait Trait2 {
769 fn function2(&self) -> &'static str;
770 }
771
772 impl Trait1 for i32 {
773 fn function1(&self) -> &'static str {
774 "WHAT"
775 }
776 }
777
778 impl Trait2 for i32 {
779 fn function2(&self) -> &'static str {
780 "EVER"
781 }
782 }
783
784 #[derive(Display)]
785 #[display(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
786 #[display("{} {_0} {} {_1}", _0.function1(), _1.function2())]
787 struct Struct<T1, T2>(T1, T2);
788
789 let s = Struct(10, 20);
790 assert_eq!(s.to_string(), "WHAT 10 EVER 20");
791 }
792 }
793 }