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