1
#![allow(dead_code)]
2

            
3
use ron::error::{Error, Position, Span, SpannedError};
4

            
5
#[cfg(feature = "internal-span-substring-test")]
6
use ron::util::span_substring::check_error_span_inclusive;
7

            
8
#[derive(Debug, serde::Deserialize)]
9
struct Test {
10
    a: i32,
11
    b: i32,
12
}
13

            
14
#[test]
15
4
fn test_missing_comma_error() {
16
4
    let tuple_string = r#"(
17
4
        1 // <-- forgotten comma here
18
4
        2
19
4
    )"#;
20

            
21
4
    assert_eq!(
22
4
        ron::from_str::<(i32, i32)>(tuple_string).unwrap_err(),
23
        SpannedError {
24
            code: Error::ExpectedComma,
25
            span: Span {
26
                start: Position { line: 3, col: 9 },
27
                end: Position { line: 3, col: 9 }
28
            }
29
        }
30
    );
31

            
32
4
    let list_string = r#"[
33
4
        0,
34
4
        1 // <-- forgotten comma here
35
4
        2
36
4
    ]"#;
37

            
38
4
    assert_eq!(
39
4
        ron::from_str::<Vec<i32>>(list_string).unwrap_err(),
40
        SpannedError {
41
            code: Error::ExpectedComma,
42
            span: Span {
43
                start: Position { line: 4, col: 9 },
44
                end: Position { line: 4, col: 9 }
45
            }
46
        }
47
    );
48

            
49
4
    let struct_string = r#"Test(
50
4
        a: 1 // <-- forgotten comma here
51
4
        b: 2
52
4
    )"#;
53

            
54
4
    assert_eq!(
55
4
        ron::from_str::<Test>(struct_string).unwrap_err(),
56
        SpannedError {
57
            code: Error::ExpectedComma,
58
            span: Span {
59
                start: Position { line: 3, col: 9 },
60
                end: Position { line: 3, col: 9 }
61
            }
62
        }
63
    );
64

            
65
4
    let map_string = r#"{
66
4
        "a": 1 // <-- forgotten comma here
67
4
        "b": 2
68
4
    }"#;
69

            
70
4
    assert_eq!(
71
4
        ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(),
72
        SpannedError {
73
            code: Error::ExpectedComma,
74
            span: Span {
75
                start: Position { line: 3, col: 9 },
76
                end: Position { line: 3, col: 9 }
77
            }
78
        }
79
    );
80

            
81
4
    let extensions_string = r#"#![enable(
82
4
        implicit_some // <-- forgotten comma here
83
4
        unwrap_newtypes
84
4
    ]) 42"#;
85

            
86
4
    assert_eq!(
87
4
        ron::from_str::<u8>(extensions_string).unwrap_err(),
88
        SpannedError {
89
            code: Error::ExpectedComma,
90
            span: Span {
91
                start: Position { line: 2, col: 50 },
92
                end: Position { line: 3, col: 9 }
93
            }
94
        }
95
    );
96

            
97
    #[cfg(feature = "internal-span-substring-test")]
98
1
    check_error_span_inclusive::<u8>(
99
1
        extensions_string,
100
1
        Err(SpannedError {
101
1
            code: Error::ExpectedComma,
102
1
            span: Span {
103
1
                start: Position { line: 2, col: 50 },
104
1
                end: Position { line: 3, col: 9 },
105
1
            },
106
1
        }),
107
1
        "\n        u",
108
    );
109
4
}
110

            
111
#[test]
112
4
fn test_comma_end() {
113
4
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1));
114
4
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1));
115
4
    assert_eq!(ron::from_str::<()>("()"), Ok(()));
116
4
}