1
use serde_bytes;
2
use serde_derive::Deserialize;
3

            
4
use crate::{
5
    error::{Error, Position, SpannedError, SpannedResult},
6
    parse::Parser,
7
    value::Number,
8
};
9

            
10
24
#[derive(Debug, PartialEq, Deserialize)]
11
struct EmptyStruct1;
12

            
13
12
#[derive(Debug, PartialEq, Deserialize)]
14
struct EmptyStruct2 {}
15

            
16
24
#[derive(Debug, PartialEq, Deserialize)]
17
struct NewType(i32);
18

            
19
12
#[derive(Debug, PartialEq, Deserialize)]
20
#[serde(rename = "")]
21
struct UnnamedNewType(i32);
22

            
23
24
#[derive(Debug, PartialEq, Deserialize)]
24
struct TupleStruct(f32, f32);
25

            
26
12
#[derive(Debug, PartialEq, Deserialize)]
27
#[serde(rename = "")]
28
struct UnnamedTupleStruct(f32, f32);
29

            
30
228
#[derive(Clone, Copy, Debug, PartialEq, Deserialize)]
31
struct MyStruct {
32
    x: f32,
33
    y: f32,
34
}
35

            
36
204
#[derive(Clone, Copy, Debug, PartialEq, Deserialize)]
37
enum MyEnum {
38
    A,
39
    B(bool),
40
    C(bool, f32),
41
    D { a: i32, b: i32 },
42
}
43

            
44
48
#[derive(Debug, Deserialize, PartialEq)]
45
struct BytesStruct {
46
    small: Vec<u8>,
47
    #[serde(with = "serde_bytes")]
48
    large: Vec<u8>,
49
}
50

            
51
#[test]
52
4
fn test_empty_struct() {
53
4
    check_from_str_bytes_reader("EmptyStruct1", Ok(EmptyStruct1));
54
4
    check_from_str_bytes_reader("EmptyStruct2()", Ok(EmptyStruct2 {}));
55
4
}
56

            
57
#[test]
58
4
fn test_struct() {
59
4
    let my_struct = MyStruct { x: 4.0, y: 7.0 };
60
4

            
61
4
    check_from_str_bytes_reader("MyStruct(x:4,y:7,)", Ok(my_struct));
62
4
    check_from_str_bytes_reader("(x:4,y:7)", Ok(my_struct));
63
4

            
64
4
    check_from_str_bytes_reader("NewType(42)", Ok(NewType(42)));
65
4
    check_from_str_bytes_reader("(33)", Ok(NewType(33)));
66
4

            
67
4
    check_from_str_bytes_reader::<NewType>(
68
4
        "NewType",
69
4
        Err(SpannedError {
70
4
            code: Error::ExpectedNamedStructLike("NewType"),
71
4
            position: Position { line: 1, col: 8 },
72
4
        }),
73
4
    );
74
4
    check_from_str_bytes_reader::<UnnamedNewType>(
75
4
        "",
76
4
        Err(SpannedError {
77
4
            code: Error::ExpectedStructLike,
78
4
            position: Position { line: 1, col: 1 },
79
4
        }),
80
4
    );
81
4
    check_from_str_bytes_reader("(33)", Ok(UnnamedNewType(33)));
82
4
    check_from_str_bytes_reader::<UnnamedNewType>(
83
4
        "Newtype",
84
4
        Err(SpannedError {
85
4
            code: Error::ExpectedNamedStructLike(""),
86
4
            position: Position { line: 1, col: 8 },
87
4
        }),
88
4
    );
89
4

            
90
4
    check_from_str_bytes_reader("TupleStruct(2,5,)", Ok(TupleStruct(2.0, 5.0)));
91
4
    check_from_str_bytes_reader("(3,4)", Ok(TupleStruct(3.0, 4.0)));
92
4
    check_from_str_bytes_reader::<TupleStruct>(
93
4
        "",
94
4
        Err(SpannedError {
95
4
            code: Error::ExpectedNamedStructLike("TupleStruct"),
96
4
            position: Position { line: 1, col: 1 },
97
4
        }),
98
4
    );
99
4
    check_from_str_bytes_reader::<UnnamedTupleStruct>(
100
4
        "TupleStruct(2,5,)",
101
4
        Err(SpannedError {
102
4
            code: Error::ExpectedNamedStructLike(""),
103
4
            position: Position { line: 1, col: 12 },
104
4
        }),
105
4
    );
106
4
    check_from_str_bytes_reader("(3,4)", Ok(UnnamedTupleStruct(3.0, 4.0)));
107
4
    check_from_str_bytes_reader::<UnnamedTupleStruct>(
108
4
        "",
109
4
        Err(SpannedError {
110
4
            code: Error::ExpectedStructLike,
111
4
            position: Position { line: 1, col: 1 },
112
4
        }),
113
4
    );
114
4
}
115

            
116
#[test]
117
4
fn test_unclosed_limited_seq_struct() {
118
    #[derive(Debug, PartialEq)]
119
    struct LimitedStruct;
120

            
121
    impl<'de> serde::Deserialize<'de> for LimitedStruct {
122
12
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
123
            struct Visitor;
124

            
125
            impl<'de> serde::de::Visitor<'de> for Visitor {
126
                type Value = LimitedStruct;
127

            
128
                // GRCOV_EXCL_START
129
                fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
130
                    fmt.write_str("struct LimitedStruct")
131
                }
132
                // GRCOV_EXCL_STOP
133

            
134
12
                fn visit_map<A: serde::de::MapAccess<'de>>(
135
12
                    self,
136
12
                    _map: A,
137
12
                ) -> Result<Self::Value, A::Error> {
138
12
                    Ok(LimitedStruct)
139
12
                }
140
            }
141

            
142
12
            deserializer.deserialize_struct("LimitedStruct", &[], Visitor)
143
12
        }
144
    }
145

            
146
4
    check_from_str_bytes_reader::<LimitedStruct>(
147
4
        "(",
148
4
        Err(SpannedError {
149
4
            code: Error::ExpectedStructLikeEnd,
150
4
            position: Position { line: 1, col: 2 },
151
4
        }),
152
4
    )
153
4
}
154

            
155
#[test]
156
4
fn test_unclosed_limited_seq() {
157
    #[derive(Debug, PartialEq)]
158
    struct LimitedSeq;
159

            
160
    impl<'de> serde::Deserialize<'de> for LimitedSeq {
161
16
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
162
            struct Visitor;
163

            
164
            impl<'de> serde::de::Visitor<'de> for Visitor {
165
                type Value = LimitedSeq;
166

            
167
                // GRCOV_EXCL_START
168
                fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
169
                    fmt.write_str("an empty sequence")
170
                }
171
                // GRCOV_EXCL_STOP
172

            
173
16
                fn visit_seq<A: serde::de::SeqAccess<'de>>(
174
16
                    self,
175
16
                    _seq: A,
176
16
                ) -> Result<Self::Value, A::Error> {
177
16
                    Ok(LimitedSeq)
178
16
                }
179
            }
180

            
181
16
            deserializer.deserialize_seq(Visitor)
182
16
        }
183
    }
184

            
185
4
    check_from_str_bytes_reader::<LimitedSeq>(
186
4
        "[",
187
4
        Err(SpannedError {
188
4
            code: Error::ExpectedArrayEnd,
189
4
            position: Position { line: 1, col: 2 },
190
4
        }),
191
4
    );
192
4

            
193
4
    assert_eq!(
194
4
        crate::Value::from(vec![42]).into_rust::<LimitedSeq>(),
195
4
        Err(Error::ExpectedDifferentLength {
196
4
            expected: String::from("a sequence of length 0"),
197
4
            found: 1
198
4
        })
199
4
    );
200
4
}
201

            
202
#[test]
203
4
fn test_unclosed_limited_map() {
204
    #[derive(Debug, PartialEq)]
205
    struct LimitedMap;
206

            
207
    impl<'de> serde::Deserialize<'de> for LimitedMap {
208
16
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
209
            struct Visitor;
210

            
211
            impl<'de> serde::de::Visitor<'de> for Visitor {
212
                type Value = LimitedMap;
213

            
214
                // GRCOV_EXCL_START
215
                fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
216
                    fmt.write_str("an empty map")
217
                }
218
                // GRCOV_EXCL_STOP
219

            
220
16
                fn visit_map<A: serde::de::MapAccess<'de>>(
221
16
                    self,
222
16
                    _map: A,
223
16
                ) -> Result<Self::Value, A::Error> {
224
16
                    Ok(LimitedMap)
225
16
                }
226
            }
227

            
228
16
            deserializer.deserialize_map(Visitor)
229
16
        }
230
    }
231

            
232
4
    check_from_str_bytes_reader::<LimitedMap>(
233
4
        "{",
234
4
        Err(SpannedError {
235
4
            code: Error::ExpectedMapEnd,
236
4
            position: Position { line: 1, col: 2 },
237
4
        }),
238
4
    );
239
4

            
240
4
    assert_eq!(
241
4
        crate::Value::Map([("a", 42)].into_iter().collect()).into_rust::<LimitedMap>(),
242
4
        Err(Error::ExpectedDifferentLength {
243
4
            expected: String::from("a map of length 0"),
244
4
            found: 1
245
4
        })
246
4
    );
247
4
}
248

            
249
#[test]
250
4
fn test_option() {
251
4
    check_from_str_bytes_reader("Some(1)", Ok(Some(1u8)));
252
4
    check_from_str_bytes_reader("None", Ok(None::<u8>));
253
4
}
254

            
255
#[test]
256
4
fn test_enum() {
257
4
    check_from_str_bytes_reader("A", Ok(MyEnum::A));
258
4
    check_from_str_bytes_reader("B(true,)", Ok(MyEnum::B(true)));
259
4
    check_from_str_bytes_reader::<MyEnum>(
260
4
        "B",
261
4
        Err(SpannedError {
262
4
            code: Error::ExpectedStructLike,
263
4
            position: Position { line: 1, col: 2 },
264
4
        }),
265
4
    );
266
4
    check_from_str_bytes_reader("C(true,3.5,)", Ok(MyEnum::C(true, 3.5)));
267
4
    check_from_str_bytes_reader("D(a:2,b:3,)", Ok(MyEnum::D { a: 2, b: 3 }));
268
4
}
269

            
270
#[test]
271
4
fn test_array() {
272
4
    check_from_str_bytes_reader::<[i32; 0]>("()", Ok([]));
273
4
    check_from_str_bytes_reader("[]", Ok(Vec::<i32>::new()));
274
4

            
275
4
    check_from_str_bytes_reader("(2,3,4,)", Ok([2, 3, 4i32]));
276
4
    check_from_str_bytes_reader("[2,3,4,]", Ok([2, 3, 4i32].to_vec()));
277
4
}
278

            
279
#[test]
280
4
fn test_map() {
281
    use std::collections::HashMap;
282

            
283
4
    let mut map = HashMap::new();
284
4
    map.insert((true, false), 4);
285
4
    map.insert((false, false), 123);
286
4

            
287
4
    check_from_str_bytes_reader(
288
4
        "{
289
4
        (true,false,):4,
290
4
        (false,false,):123,
291
4
    }",
292
4
        Ok(map),
293
4
    );
294
4
}
295

            
296
#[test]
297
4
fn test_string() {
298
4
    check_from_str_bytes_reader("\"String\"", Ok(String::from("String")));
299
4

            
300
4
    check_from_str_bytes_reader("r\"String\"", Ok(String::from("String")));
301
4
    check_from_str_bytes_reader("r#\"String\"#", Ok(String::from("String")));
302
4

            
303
4
    check_from_str_bytes_reader(
304
4
        "r#\"String with\nmultiple\nlines\n\"#",
305
4
        Ok(String::from("String with\nmultiple\nlines\n")),
306
4
    );
307
4

            
308
4
    check_from_str_bytes_reader(
309
4
        "r##\"String with \"#\"##",
310
4
        Ok(String::from("String with \"#")),
311
4
    );
312
4
}
313

            
314
#[test]
315
4
fn test_char() {
316
4
    check_from_str_bytes_reader("'c'", Ok('c'));
317
4
}
318

            
319
#[test]
320
4
fn test_escape_char() {
321
4
    check_from_str_bytes_reader("'\\''", Ok('\''));
322
4
}
323

            
324
#[test]
325
4
fn test_escape() {
326
4
    check_from_str_bytes_reader(r#""\"Quoted\"""#, Ok(String::from("\"Quoted\"")));
327
4
}
328

            
329
#[test]
330
4
fn test_comment() {
331
4
    check_from_str_bytes_reader(
332
4
        "(
333
4
x: 1.0, // x is just 1
334
4
// There is another comment in the very next line..
335
4
// And y is indeed
336
4
y: 2.0 // 2!
337
4
    )",
338
4
        Ok(MyStruct { x: 1.0, y: 2.0 }),
339
4
    );
340
4
}
341

            
342
60
fn err<T>(kind: Error, line: usize, col: usize) -> SpannedResult<T> {
343
60
    Err(SpannedError {
344
60
        code: kind,
345
60
        position: Position { line, col },
346
60
    })
347
60
}
348

            
349
#[test]
350
4
fn test_err_wrong_value() {
351
    use std::collections::HashMap;
352

            
353
4
    check_from_str_bytes_reader::<f32>("'c'", err(Error::ExpectedFloat, 1, 1));
354
4
    check_from_str_bytes_reader::<String>("'c'", err(Error::ExpectedString, 1, 1));
355
4
    check_from_str_bytes_reader::<HashMap<u32, u32>>("'c'", err(Error::ExpectedMap, 1, 1));
356
4
    check_from_str_bytes_reader::<[u8; 5]>("'c'", err(Error::ExpectedStructLike, 1, 1));
357
4
    check_from_str_bytes_reader::<Vec<u32>>("'c'", err(Error::ExpectedArray, 1, 1));
358
4
    check_from_str_bytes_reader::<MyEnum>("'c'", err(Error::ExpectedIdentifier, 1, 1));
359
4
    check_from_str_bytes_reader::<MyStruct>(
360
4
        "'c'",
361
4
        err(Error::ExpectedNamedStructLike("MyStruct"), 1, 1),
362
4
    );
363
4
    check_from_str_bytes_reader::<MyStruct>(
364
4
        "NotMyStruct(x: 4, y: 2)",
365
4
        err(
366
4
            Error::ExpectedDifferentStructName {
367
4
                expected: "MyStruct",
368
4
                found: String::from("NotMyStruct"),
369
4
            },
370
4
            1,
371
4
            12,
372
4
        ),
373
4
    );
374
4
    check_from_str_bytes_reader::<(u8, bool)>("'c'", err(Error::ExpectedStructLike, 1, 1));
375
4
    check_from_str_bytes_reader::<bool>("notabool", err(Error::ExpectedBoolean, 1, 1));
376
4

            
377
4
    check_from_str_bytes_reader::<MyStruct>(
378
4
        "MyStruct(\n    x: true)",
379
4
        err(Error::ExpectedFloat, 2, 8),
380
4
    );
381
4
    check_from_str_bytes_reader::<MyStruct>(
382
4
        "MyStruct(\n    x: 3.5, \n    y:)",
383
4
        err(Error::ExpectedFloat, 3, 7),
384
4
    );
385
4
}
386

            
387
#[test]
388
4
fn test_perm_ws() {
389
4
    check_from_str_bytes_reader(
390
4
        "\nMyStruct  \t ( \n x   : 3.5 , \t y\n: 4.5 \n ) \t\n",
391
4
        Ok(MyStruct { x: 3.5, y: 4.5 }),
392
4
    );
393
4
}
394

            
395
#[test]
396
4
fn untagged() {
397
    #[derive(Deserialize, Debug, PartialEq)]
398
    #[serde(untagged)]
399
    enum Untagged {
400
        U8(u8),
401
        Bool(bool),
402
        Value(crate::Value),
403
    }
404

            
405
4
    check_from_str_bytes_reader("true", Ok(Untagged::Bool(true)));
406
4
    check_from_str_bytes_reader("8", Ok(Untagged::U8(8)));
407
4

            
408
4
    // Check for a failure in Deserializer::check_struct_type
409
4
    // - untagged enum and a leading identifier trigger the serde content enum path
410
4
    // - serde content uses deserialize_any, which retriggers the struct type check
411
4
    // - struct type check inside a serde content performs a full newtype check
412
4
    // - newtype check fails on the unclosed struct
413
4
    check_from_str_bytes_reader::<Untagged>(
414
4
        "Value(()",
415
4
        Err(crate::error::SpannedError {
416
4
            code: crate::Error::Eof,
417
4
            position: crate::error::Position { line: 1, col: 9 },
418
4
        }),
419
4
    );
420
4
}
421

            
422
#[test]
423
4
fn rename() {
424
48
    #[derive(Deserialize, Debug, PartialEq)]
425
    enum Foo {
426
        #[serde(rename = "2d")]
427
        D2,
428
        #[serde(rename = "triangle-list")]
429
        TriangleList,
430
    }
431

            
432
4
    check_from_str_bytes_reader("r#2d", Ok(Foo::D2));
433
4
    check_from_str_bytes_reader("r#triangle-list", Ok(Foo::TriangleList));
434
4
}
435

            
436
#[test]
437
4
fn forgot_apostrophes() {
438
4
    check_from_str_bytes_reader::<(i32, String)>(
439
4
        "(4, \"Hello)",
440
4
        Err(SpannedError {
441
4
            code: Error::ExpectedStringEnd,
442
4
            position: Position { line: 1, col: 6 },
443
4
        }),
444
4
    );
445
4
}
446

            
447
#[test]
448
4
fn expected_attribute() {
449
4
    check_from_str_bytes_reader::<String>("#\"Hello\"", err(Error::ExpectedAttribute, 1, 2));
450
4
}
451

            
452
#[test]
453
4
fn expected_attribute_end() {
454
4
    check_from_str_bytes_reader::<String>(
455
4
        "#![enable(unwrap_newtypes) \"Hello\"",
456
4
        err(Error::ExpectedAttributeEnd, 1, 28),
457
4
    );
458
4
}
459

            
460
#[test]
461
4
fn invalid_attribute() {
462
4
    check_from_str_bytes_reader::<String>(
463
4
        "#![enable(invalid)] \"Hello\"",
464
4
        err(Error::NoSuchExtension("invalid".to_string()), 1, 18),
465
4
    );
466
4
}
467

            
468
#[test]
469
4
fn multiple_attributes() {
470
12
    #[derive(Debug, Deserialize, PartialEq)]
471
    struct New(String);
472

            
473
4
    check_from_str_bytes_reader(
474
4
        "#![enable(unwrap_newtypes)] #![enable(unwrap_newtypes)] \"Hello\"",
475
4
        Ok(New("Hello".to_owned())),
476
4
    );
477
4
}
478

            
479
#[test]
480
4
fn uglified_attribute() {
481
4
    check_from_str_bytes_reader(
482
4
        "#   !\
483
4
    // We definitely want to add a comment here
484
4
    [\t\tenable( // best style ever
485
4
            unwrap_newtypes  ) ] ()",
486
4
        Ok(()),
487
4
    );
488
4
}
489

            
490
#[test]
491
4
fn implicit_some() {
492
    use serde::de::DeserializeOwned;
493

            
494
28
    fn de<T: DeserializeOwned>(s: &str) -> Option<T> {
495
28
        let enable = "#![enable(implicit_some)]\n".to_string();
496
28

            
497
28
        super::from_str::<Option<T>>(&(enable + s)).unwrap()
498
28
    }
499

            
500
4
    assert_eq!(de("'c'"), Some('c'));
501
4
    assert_eq!(de("5"), Some(5));
502
4
    assert_eq!(de("\"Hello\""), Some("Hello".to_owned()));
503
4
    assert_eq!(de("false"), Some(false));
504
4
    assert_eq!(
505
4
        de("MyStruct(x: .4, y: .5)"),
506
4
        Some(MyStruct { x: 0.4, y: 0.5 })
507
4
    );
508

            
509
4
    assert_eq!(de::<char>("None"), None);
510

            
511
    // Not concise
512
4
    assert_eq!(de::<Option<Option<char>>>("None"), None);
513
4
}
514

            
515
#[test]
516
4
fn ws_tuple_newtype_variant() {
517
4
    check_from_str_bytes_reader("B  ( \n true \n ) ", Ok(MyEnum::B(true)));
518
4
}
519

            
520
#[test]
521
4
fn test_byte_stream() {
522
4
    check_from_str_bytes_reader(
523
4
        "BytesStruct( small:[1, 2], large:b\"\\x01\\x02\\x03\\x04\" )",
524
4
        Ok(BytesStruct {
525
4
            small: vec![1, 2],
526
4
            large: vec![1, 2, 3, 4],
527
4
        }),
528
4
    );
529
4
}
530

            
531
#[test]
532
4
fn test_numbers() {
533
4
    check_from_str_bytes_reader(
534
4
        "[1_234, 12_345, 1_2_3_4_5_6, 1_234_567, 5_55_55_5]",
535
4
        Ok(vec![1234, 12345, 123_456, 1_234_567, 555_555]),
536
4
    );
537
4
}
538

            
539
100
fn check_de_any_number<
540
100
    T: Copy + PartialEq + std::fmt::Debug + Into<Number> + serde::de::DeserializeOwned,
541
100
>(
542
100
    s: &str,
543
100
    cmp: T,
544
100
) {
545
100
    let mut parser = Parser::new(s).unwrap();
546
100
    let number = parser.any_number().unwrap();
547
100

            
548
100
    assert_eq!(number, Number::new(cmp));
549
100
    assert_eq!(
550
100
        Number::new(super::from_str::<T>(s).unwrap()),
551
100
        Number::new(cmp)
552
100
    );
553
100
}
554

            
555
#[test]
556
4
fn test_any_number_precision() {
557
4
    check_de_any_number("1", 1_u8);
558
4
    check_de_any_number("+1", 1_u8);
559
4
    check_de_any_number("-1", -1_i8);
560
4
    check_de_any_number("-1.0", -1.0_f32);
561
4
    check_de_any_number("1.", 1.0_f32);
562
4
    check_de_any_number("-1.", -1.0_f32);
563
4
    check_de_any_number(".3", 0.3_f64);
564
4
    check_de_any_number("-.3", -0.3_f64);
565
4
    check_de_any_number("+.3", 0.3_f64);
566
4
    check_de_any_number("0.3", 0.3_f64);
567
4
    check_de_any_number("NaN", f32::NAN);
568
4
    check_de_any_number("-NaN", -f32::NAN);
569
4
    check_de_any_number("inf", f32::INFINITY);
570
4
    check_de_any_number("-inf", f32::NEG_INFINITY);
571

            
572
    macro_rules! test_min {
573
        ($($ty:ty),*) => {
574
            $(check_de_any_number(&format!("{}", <$ty>::MIN), <$ty>::MIN);)*
575
        };
576
    }
577

            
578
    macro_rules! test_max {
579
        ($($ty:ty),*) => {
580
            $(check_de_any_number(&format!("{}", <$ty>::MAX), <$ty>::MAX);)*
581
        };
582
    }
583

            
584
4
    test_min! { i8, i16, i32, i64, f64 }
585
4
    test_max! { u8, u16, u32, u64, f64 }
586
4
    #[cfg(feature = "integer128")]
587
4
    test_min! { i128 }
588
4
    #[cfg(feature = "integer128")]
589
4
    test_max! { u128 }
590
4
}
591

            
592
#[test]
593
4
fn test_value_special_floats() {
594
    use crate::{from_str, value::Number, Value};
595

            
596
4
    assert_eq!(
597
4
        from_str("NaN"),
598
4
        Ok(Value::Number(Number::F32(f32::NAN.into())))
599
4
    );
600
4
    assert_eq!(
601
4
        from_str("+NaN"),
602
4
        Ok(Value::Number(Number::F32(f32::NAN.into())))
603
4
    );
604
4
    assert_eq!(
605
4
        from_str("-NaN"),
606
4
        Ok(Value::Number(Number::F32((-f32::NAN).into())))
607
4
    );
608

            
609
4
    assert_eq!(
610
4
        from_str("inf"),
611
4
        Ok(Value::Number(Number::F32(f32::INFINITY.into())))
612
4
    );
613
4
    assert_eq!(
614
4
        from_str("+inf"),
615
4
        Ok(Value::Number(Number::F32(f32::INFINITY.into())))
616
4
    );
617
4
    assert_eq!(
618
4
        from_str("-inf"),
619
4
        Ok(Value::Number(Number::F32(f32::NEG_INFINITY.into())))
620
4
    );
621
4
}
622

            
623
#[test]
624
4
fn test_leading_whitespace() {
625
4
    check_from_str_bytes_reader("  +1", Ok(1_u8));
626
4
    check_from_str_bytes_reader("  EmptyStruct1", Ok(EmptyStruct1));
627
4
}
628

            
629
284
fn check_from_str_bytes_reader<T: serde::de::DeserializeOwned + PartialEq + std::fmt::Debug>(
630
284
    ron: &str,
631
284
    check: SpannedResult<T>,
632
284
) {
633
284
    let res_str = super::from_str::<T>(ron);
634
284
    assert_eq!(res_str, check);
635

            
636
284
    let res_bytes = super::from_bytes::<T>(ron.as_bytes());
637
284
    assert_eq!(res_bytes, check);
638

            
639
284
    let res_reader = super::from_reader::<&[u8], T>(ron.as_bytes());
640
284
    assert_eq!(res_reader, check);
641
284
}
642

            
643
#[test]
644
4
fn test_remainder() {
645
4
    let mut deserializer = super::Deserializer::from_str("  42  ").unwrap();
646
4
    assert_eq!(
647
4
        <u8 as serde::Deserialize>::deserialize(&mut deserializer).unwrap(),
648
4
        42
649
4
    );
650
4
    assert_eq!(deserializer.remainder(), "  ");
651
4
    assert_eq!(deserializer.end(), Ok(()));
652

            
653
4
    let mut deserializer = super::Deserializer::from_str("  42 37 ").unwrap();
654
4
    assert_eq!(
655
4
        <u8 as serde::Deserialize>::deserialize(&mut deserializer).unwrap(),
656
4
        42
657
4
    );
658
4
    assert_eq!(deserializer.remainder(), " 37 ");
659
4
    assert_eq!(deserializer.end(), Err(Error::TrailingCharacters));
660
4
}
661

            
662
#[test]
663
4
fn boolean_struct_name() {
664
4
    check_from_str_bytes_reader::<bool>(
665
4
        "true_",
666
4
        Err(SpannedError {
667
4
            code: Error::ExpectedBoolean,
668
4
            position: Position { line: 1, col: 1 },
669
4
        }),
670
4
    );
671
4
    check_from_str_bytes_reader::<bool>(
672
4
        "false_",
673
4
        Err(SpannedError {
674
4
            code: Error::ExpectedBoolean,
675
4
            position: Position { line: 1, col: 1 },
676
4
        }),
677
4
    );
678
4
}