Debian Codegraph @

third_party/rust/smart-default/tests/tests.rs

raw
1 use smart_default::SmartDefault;
2
3 #[test]
4 fn test_unit() {
5 #[derive(PartialEq, SmartDefault)]
6 struct Foo;
7
8 assert!(Foo::default() == Foo);
9 }
10
11 #[test]
12 fn test_tuple() {
13 #[derive(PartialEq, SmartDefault)]
14 struct Foo(
15 #[default = 10] i32,
16 #[default = 20] i32,
17 // No default
18 i32,
19 );
20
21 assert!(Foo::default() == Foo(10, 20, 0));
22 }
23
24 #[test]
25 fn test_struct() {
26 #[derive(PartialEq, SmartDefault)]
27 struct Foo {
28 #[default = 10]
29 x: i32,
30 #[default = 20]
31 y: i32,
32 // No default
33 z: i32,
34 }
35
36 assert!(Foo::default() == Foo { x: 10, y: 20, z: 0 });
37 }
38
39 #[test]
40 fn test_enum_of_units() {
41 #[derive(PartialEq, SmartDefault)]
42 pub enum Foo {
43 #[allow(dead_code)]
44 Bar,
45 #[default]
46 Baz,
47 #[allow(dead_code)]
48 Qux,
49 }
50
51 assert!(Foo::default() == Foo::Baz);
52 }
53
54 #[test]
55 fn test_enum_of_tuples() {
56 #[derive(PartialEq, SmartDefault)]
57 pub enum Foo {
58 #[allow(dead_code)]
59 Bar(i32),
60 #[default]
61 Baz(#[default = 10] i32, i32),
62 #[allow(dead_code)]
63 Qux(i32),
64 }
65
66 assert!(Foo::default() == Foo::Baz(10, 0));
67 }
68
69 #[test]
70 fn test_enum_of_structs() {
71 #[derive(PartialEq, SmartDefault)]
72 pub enum Foo {
73 #[allow(dead_code)]
74 Bar { x: i32 },
75 #[default]
76 Baz {
77 #[default = 10]
78 y: i32,
79 z: i32,
80 },
81 #[allow(dead_code)]
82 Qux { w: i32 },
83 }
84
85 assert!(Foo::default() == Foo::Baz { y: 10, z: 0 });
86 }
87
88 #[test]
89 fn test_enum_mixed() {
90 #[derive(PartialEq, SmartDefault)]
91 enum Foo {
92 #[allow(dead_code)]
93 Bar,
94 #[default]
95 Baz(#[default = 10] i32),
96 #[allow(dead_code)]
97 Qux { w: i32 },
98 }
99
100 assert!(Foo::default() == Foo::Baz(10));
101 }
102
103 #[test]
104 fn test_generics_type_parameters() {
105 #[derive(PartialEq, SmartDefault)]
106 struct Foo<T>
107 where
108 T: Default,
109 {
110 #[default(Some(Default::default()))]
111 x: Option<T>,
112 }
113
114 assert!(Foo::default() == Foo { x: Some(0) });
115 }
116
117 #[test]
118 fn test_generics_lifetime_parameters() {
119 // NOTE: A default value makes no sense with lifetime parameters, since ::default() receives no
120 // paramters and therefore can receive no lifetimes. But it does make sense if you make a variant
121 // without ref fields the default.
122
123 #[derive(PartialEq, SmartDefault)]
124 enum Foo<'a> {
125 #[default]
126 Bar(i32),
127 #[allow(dead_code)]
128 Baz(&'a str),
129 }
130
131 assert!(Foo::default() == Foo::Bar(0));
132 }
133
134 #[test]
135 fn test_code_hack() {
136 #[derive(PartialEq, SmartDefault)]
137 struct Foo {
138 #[default(_code = "vec![1, 2, 3]")]
139 v: Vec<u32>,
140 }
141
142 assert!(Foo::default().v == [1, 2, 3]);
143 }
144
145 #[test]
146 fn test_string_conversion() {
147 #[derive(PartialEq, SmartDefault)]
148 struct Foo(#[default = "one"] &'static str, #[default("two")] String);
149
150 assert!(Foo::default() == Foo("one", "two".to_owned()));
151 }
152
153 #[test]
154 fn test_non_code_hack_valid_meta() {
155 #[derive(Debug, PartialEq, SmartDefault)]
156 struct Foo {
157 #[default(true)]
158 bar: bool,
159 #[default(Option::None)]
160 baz: Option<()>,
161 }
162
163 assert_eq!(
164 Foo::default(),
165 Foo {
166 bar: true,
167 baz: None
168 }
169 );
170 }