Debian Codegraph @

debian/missing-sources/derive_more/tests/add.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code)] // some code is tested for type checking only
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 mod structs {
8 use derive_more::Add;
9
10 #[test]
11 fn multi_field_tuple() {
12 #[derive(Add, Debug, PartialEq)]
13 struct MyInts(i32, i32);
14
15 let a = MyInts(12, 21);
16 let b = MyInts(1, 2);
17
18 assert_eq!(a + b, MyInts(13, 23));
19 }
20
21 #[test]
22 fn multi_field_struct() {
23 #[derive(Add, Debug, PartialEq)]
24 struct Point2D {
25 x: i32,
26 y: i32,
27 }
28
29 let a = Point2D { x: 12, y: 21 };
30 let b = Point2D { x: 1, y: 2 };
31
32 assert_eq!(a + b, Point2D { x: 13, y: 23 });
33 }
34
35 mod generic {
36 use derive_more::Add;
37
38 trait Some {
39 type Assoc;
40 }
41
42 impl<T> Some for T {
43 type Assoc = u32;
44 }
45
46 #[test]
47 fn multi_field_tuple() {
48 #[derive(Add)]
49 struct Foo<A: Some, B>(A::Assoc, B);
50
51 let a = Foo::<(), _>(12, 21);
52 let b = Foo::<(), _>(1, 2);
53 let res = a + b;
54
55 assert_eq!((res.0, res.1), (13, 23));
56 }
57
58 #[test]
59 fn multi_field_struct() {
60 #[derive(Add)]
61 struct Bar<A, B: Some> {
62 b: B::Assoc,
63 i: A,
64 }
65
66 let a = Bar::<_, ()> { b: 12, i: 21 };
67 let b = Bar::<_, ()> { b: 1, i: 2 };
68 let res = a + b;
69
70 assert_eq!((res.b, res.i), (13, 23));
71 }
72 }
73
74 mod ignore {
75 use core::marker::PhantomData;
76
77 use derive_more::Add;
78
79 #[test]
80 fn tuple() {
81 #[derive(Add)]
82 struct TupleWithZst<T = ()>(i32, #[add(ignore)] PhantomData<T>);
83
84 let a: TupleWithZst = TupleWithZst(12, PhantomData);
85 let b: TupleWithZst = TupleWithZst(2, PhantomData);
86
87 assert_eq!((a + b).0, 14);
88 }
89
90 #[test]
91 fn struct_() {
92 #[derive(Add)]
93 struct StructWithZst<T = ()> {
94 x: i32,
95 #[add(skip)]
96 _marker: PhantomData<T>,
97 }
98
99 let a: StructWithZst = StructWithZst {
100 x: 12,
101 _marker: PhantomData,
102 };
103 let b: StructWithZst = StructWithZst {
104 x: 2,
105 _marker: PhantomData,
106 };
107
108 assert_eq!((a + b).x, 14);
109 }
110 }
111 }
112
113 mod enums {
114 #[cfg(not(feature = "std"))]
115 use alloc::string::ToString as _;
116 use derive_more::Add;
117
118 #[test]
119 fn empty() {
120 #[derive(Add)]
121 enum Empty {}
122 }
123
124 #[test]
125 fn multi() {
126 #[derive(Add, Debug, PartialEq)]
127 enum MixedInts {
128 SmallInt(i32),
129 BigInt(i64),
130 TwoSmallInts(i32, i32),
131 NamedSmallInts { x: i32, y: i32 },
132 UnsignedOne(u32),
133 UnsignedTwo(u32),
134 Unit,
135 }
136
137 assert_eq!(
138 (MixedInts::SmallInt(-1) + MixedInts::SmallInt(2)).unwrap(),
139 MixedInts::SmallInt(1),
140 );
141 assert_eq!(
142 (MixedInts::BigInt(-1) + MixedInts::BigInt(2)).unwrap(),
143 MixedInts::BigInt(1),
144 );
145 assert_eq!(
146 (MixedInts::TwoSmallInts(-1, 3) + MixedInts::TwoSmallInts(2, -5)).unwrap(),
147 MixedInts::TwoSmallInts(1, -2),
148 );
149 assert_eq!(
150 (MixedInts::NamedSmallInts { x: -1, y: 3 }
151 + MixedInts::NamedSmallInts { x: 2, y: -5 })
152 .unwrap(),
153 MixedInts::NamedSmallInts { x: 1, y: -2 },
154 );
155 assert_eq!(
156 (MixedInts::UnsignedOne(1) + MixedInts::UnsignedOne(2)).unwrap(),
157 MixedInts::UnsignedOne(3),
158 );
159 assert_eq!(
160 (MixedInts::UnsignedTwo(1) + MixedInts::UnsignedTwo(2)).unwrap(),
161 MixedInts::UnsignedTwo(3),
162 );
163
164 assert_eq!(
165 (MixedInts::Unit + MixedInts::Unit).unwrap_err().to_string(),
166 "Cannot add() unit variants",
167 );
168 assert_eq!(
169 (MixedInts::SmallInt(-1) + MixedInts::BigInt(2))
170 .unwrap_err()
171 .to_string(),
172 "Trying to add() mismatched enum variants",
173 );
174 }
175
176 mod ignore {
177 #[cfg(not(feature = "std"))]
178 use alloc::string::ToString as _;
179 use derive_more::Add;
180
181 #[test]
182 fn multi() {
183 #[derive(Add, Debug, PartialEq)]
184 enum MixedInts {
185 TwoSmallInts(i32, #[add(skip)] i32),
186 NamedSmallInts {
187 #[add(ignore)]
188 x: i32,
189 y: i32,
190 },
191 Unit,
192 }
193
194 assert_eq!(
195 (MixedInts::TwoSmallInts(-1, 3) + MixedInts::TwoSmallInts(2, -5))
196 .unwrap(),
197 MixedInts::TwoSmallInts(1, 3),
198 );
199 assert_eq!(
200 (MixedInts::NamedSmallInts { x: -1, y: 3 }
201 + MixedInts::NamedSmallInts { x: 2, y: -5 })
202 .unwrap(),
203 MixedInts::NamedSmallInts { x: -1, y: -2 },
204 );
205
206 assert_eq!(
207 (MixedInts::Unit + MixedInts::Unit).unwrap_err().to_string(),
208 "Cannot add() unit variants",
209 );
210 assert_eq!(
211 (MixedInts::TwoSmallInts(-1, 3)
212 + MixedInts::NamedSmallInts { x: -1, y: 3 })
213 .unwrap_err()
214 .to_string(),
215 "Trying to add() mismatched enum variants",
216 );
217 }
218 }
219 }