Debian Codegraph @

third_party/rust/ron/src/ser/tests.rs

raw
1 use serde_derive::Serialize;
2
3 use crate::Number;
4
5 #[derive(Serialize)]
6 struct EmptyStruct1;
7
8 #[derive(Serialize)]
9 struct EmptyStruct2 {}
10
11 #[derive(Serialize)]
12 struct NewType(i32);
13
14 #[derive(Serialize)]
15 struct TupleStruct(f32, f32);
16
17 #[derive(Serialize)]
18 struct MyStruct {
19 x: f32,
20 y: f32,
21 }
22
23 #[derive(Serialize)]
24 enum MyEnum {
25 A,
26 B(bool),
27 C(bool, f32),
28 D { a: i32, b: i32 },
29 }
30
31 #[test]
32 fn test_empty_struct() {
33 check_to_string_writer(&EmptyStruct1, "()", "EmptyStruct1");
34 check_to_string_writer(&EmptyStruct2 {}, "()", "EmptyStruct2()");
35 }
36
37 #[test]
38 fn test_struct() {
39 let my_struct = MyStruct { x: 4.0, y: 7.0 };
40
41 check_to_string_writer(&my_struct, "(x:4.0,y:7.0)", "MyStruct(x: 4.0, y: 7.0)");
42
43 check_to_string_writer(&NewType(42), "(42)", "NewType(42)");
44
45 check_to_string_writer(&TupleStruct(2.0, 5.0), "(2.0,5.0)", "TupleStruct(2.0, 5.0)");
46 }
47
48 #[test]
49 fn test_option() {
50 check_to_string_writer(&Some(1u8), "Some(1)", "Some(1)");
51 check_to_string_writer(&None::<u8>, "None", "None");
52 }
53
54 #[test]
55 fn test_enum() {
56 check_to_string_writer(&MyEnum::A, "A", "A");
57 check_to_string_writer(&MyEnum::B(true), "B(true)", "B(true)");
58 check_to_string_writer(&MyEnum::C(true, 3.5), "C(true,3.5)", "C(true, 3.5)");
59 check_to_string_writer(&MyEnum::D { a: 2, b: 3 }, "D(a:2,b:3)", "D(a: 2, b: 3)");
60 }
61
62 #[test]
63 fn test_array() {
64 let empty: [i32; 0] = [];
65 check_to_string_writer(&empty, "()", "()");
66 let empty_ref: &[i32] = &empty;
67 check_to_string_writer(&empty_ref, "[]", "[]");
68
69 check_to_string_writer(&[2, 3, 4i32], "(2,3,4)", "(2, 3, 4)");
70 check_to_string_writer(
71 &(&[2, 3, 4i32] as &[i32]),
72 "[2,3,4]",
73 "[\n 2,\n 3,\n 4,\n]",
74 );
75 }
76
77 #[test]
78 fn test_slice() {
79 check_to_string_writer(
80 &[0, 1, 2, 3, 4, 5][..],
81 "[0,1,2,3,4,5]",
82 "[\n 0,\n 1,\n 2,\n 3,\n 4,\n 5,\n]",
83 );
84 check_to_string_writer(
85 &[0, 1, 2, 3, 4, 5][1..4],
86 "[1,2,3]",
87 "[\n 1,\n 2,\n 3,\n]",
88 );
89 }
90
91 #[test]
92 fn test_vec() {
93 check_to_string_writer(
94 &vec![0, 1, 2, 3, 4, 5],
95 "[0,1,2,3,4,5]",
96 "[\n 0,\n 1,\n 2,\n 3,\n 4,\n 5,\n]",
97 );
98 }
99
100 #[test]
101 fn test_map() {
102 use std::collections::BTreeMap;
103
104 let mut map = BTreeMap::new();
105 map.insert((true, false), 4);
106 map.insert((false, false), 123);
107
108 check_to_string_writer(
109 &map,
110 "{(false,false):123,(true,false):4}",
111 "{\n (false, false): 123,\n (true, false): 4,\n}",
112 );
113 }
114
115 #[test]
116 fn test_string() {
117 check_to_string_writer(&"Some string", "\"Some string\"", "\"Some string\"");
118 }
119
120 #[test]
121 fn test_char() {
122 check_to_string_writer(&'c', "'c'", "'c'");
123 }
124
125 #[test]
126 fn test_escape() {
127 check_to_string_writer(&r#""Quoted""#, r#""\"Quoted\"""#, r#""\"Quoted\"""#);
128 }
129
130 #[test]
131 fn test_byte_stream() {
132 use serde_bytes;
133
134 let small: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
135 check_to_string_writer(
136 &small,
137 "(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)",
138 "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)",
139 );
140
141 let large = vec![0x01, 0x02, 0x03, 0x04];
142 let large = serde_bytes::Bytes::new(&large);
143 check_to_string_writer(
144 &large,
145 "b\"\\x01\\x02\\x03\\x04\"",
146 "b\"\\x01\\x02\\x03\\x04\"",
147 );
148
149 let large = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
150 let large = serde_bytes::Bytes::new(&large);
151 check_to_string_writer(
152 &large,
153 "b\"\\x01\\x02\\x03\\x04\\x05\\x06\"",
154 "b\"\\x01\\x02\\x03\\x04\\x05\\x06\"",
155 );
156
157 let large = vec![255u8; 64];
158 let large = serde_bytes::Bytes::new(&large);
159 check_to_string_writer(
160 &large,
161 concat!(
162 "b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
163 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
164 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
165 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
166 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\""
167 ),
168 concat!(
169 "b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
170 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
171 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
172 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff",
173 "\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\""
174 ),
175 );
176 }
177
178 #[test]
179 fn rename() {
180 #[derive(Serialize, Debug, PartialEq)]
181 enum Foo {
182 #[serde(rename = "2d")]
183 D2,
184 #[serde(rename = "triangle-list")]
185 TriangleList,
186 }
187
188 check_to_string_writer(&Foo::D2, "r#2d", "r#2d");
189 check_to_string_writer(&Foo::TriangleList, "r#triangle-list", "r#triangle-list");
190 }
191
192 #[test]
193 fn test_any_number_precision() {
194 check_ser_any_number(1_u8);
195 check_ser_any_number(-1_i8);
196 check_ser_any_number(1_f32);
197 check_ser_any_number(-1_f32);
198 check_ser_any_number(0.3_f64);
199
200 check_to_string_writer(&Number::new(f32::NAN), "NaN", "NaN");
201 check_to_string_writer(&f32::NAN, "NaN", "NaN");
202 check_to_string_writer(&Number::new(-f32::NAN), "-NaN", "-NaN");
203 check_to_string_writer(&(-f32::NAN), "-NaN", "-NaN");
204 check_to_string_writer(&Number::new(f32::INFINITY), "inf", "inf");
205 check_to_string_writer(&f32::INFINITY, "inf", "inf");
206 check_to_string_writer(&Number::new(f32::NEG_INFINITY), "-inf", "-inf");
207 check_to_string_writer(&f32::NEG_INFINITY, "-inf", "-inf");
208
209 macro_rules! test_min_max {
210 ($ty:ty) => {
211 check_ser_any_number(<$ty>::MIN);
212 check_ser_any_number(<$ty>::MAX);
213 };
214 ($($ty:ty),*) => {
215 $(test_min_max! { $ty })*
216 };
217 }
218
219 test_min_max! { i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 }
220 #[cfg(feature = "integer128")]
221 test_min_max! { i128, u128 }
222 }
223
224 fn check_ser_any_number<T: Copy + Into<Number> + std::fmt::Display + serde::Serialize>(n: T) {
225 let mut fmt = format!("{}", n);
226 if !fmt.contains('.') && std::any::type_name::<T>().contains('f') {
227 fmt.push_str(".0");
228 }
229
230 check_to_string_writer(&n.into(), &fmt, &fmt);
231 check_to_string_writer(&n, &fmt, &fmt);
232 }
233
234 #[test]
235 fn recursion_limit() {
236 assert_eq!(
237 crate::Options::default()
238 .with_recursion_limit(0)
239 .to_string(&[42]),
240 Err(crate::Error::ExceededRecursionLimit),
241 );
242 assert_eq!(
243 crate::Options::default()
244 .with_recursion_limit(1)
245 .to_string(&[42])
246 .as_deref(),
247 Ok("(42)"),
248 );
249 assert_eq!(
250 crate::Options::default()
251 .without_recursion_limit()
252 .to_string(&[42])
253 .as_deref(),
254 Ok("(42)"),
255 );
256
257 assert_eq!(
258 crate::Options::default()
259 .with_recursion_limit(1)
260 .to_string(&[[42]]),
261 Err(crate::Error::ExceededRecursionLimit),
262 );
263 assert_eq!(
264 crate::Options::default()
265 .with_recursion_limit(2)
266 .to_string(&[[42]])
267 .as_deref(),
268 Ok("((42))"),
269 );
270 assert_eq!(
271 crate::Options::default()
272 .without_recursion_limit()
273 .to_string(&[[42]])
274 .as_deref(),
275 Ok("((42))"),
276 );
277 }
278
279 fn check_to_string_writer<T: ?Sized + serde::Serialize>(val: &T, check: &str, check_pretty: &str) {
280 let ron_str = super::to_string(val).unwrap();
281 assert_eq!(ron_str, check);
282
283 let ron_str_pretty = super::to_string_pretty(
284 val,
285 super::PrettyConfig::default()
286 .struct_names(true)
287 .compact_structs(true),
288 )
289 .unwrap();
290 assert_eq!(ron_str_pretty, check_pretty);
291
292 let mut ron_writer = std::ffi::OsString::new();
293 super::to_writer(&mut ron_writer, val).unwrap();
294 assert_eq!(ron_writer, check);
295
296 let mut ron_writer_pretty = std::ffi::OsString::new();
297 super::to_writer_pretty(
298 &mut ron_writer_pretty,
299 val,
300 super::PrettyConfig::default()
301 .struct_names(true)
302 .compact_structs(true),
303 )
304 .unwrap();
305 assert_eq!(ron_writer_pretty, check_pretty);
306 }