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, PartialEq, 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
    #[cfg(feature = "internal-span-substring-test")]
33
1
    check_error_span_inclusive::<(i32, i32)>(
34
1
        tuple_string,
35
1
        Err(SpannedError {
36
1
            code: Error::ExpectedComma,
37
1
            span: Span {
38
1
                start: Position { line: 3, col: 9 },
39
1
                end: Position { line: 3, col: 9 },
40
1
            },
41
1
        }),
42
1
        "2",
43
    );
44

            
45
4
    let list_string = r#"[
46
4
        0,
47
4
        1 // <-- forgotten comma here
48
4
        2
49
4
    ]"#;
50

            
51
4
    assert_eq!(
52
4
        ron::from_str::<Vec<i32>>(list_string).unwrap_err(),
53
        SpannedError {
54
            code: Error::ExpectedComma,
55
            span: Span {
56
                start: Position { line: 4, col: 9 },
57
                end: Position { line: 4, col: 9 }
58
            }
59
        }
60
    );
61

            
62
    #[cfg(feature = "internal-span-substring-test")]
63
1
    check_error_span_inclusive::<Vec<i32>>(
64
1
        list_string,
65
1
        Err(SpannedError {
66
1
            code: Error::ExpectedComma,
67
1
            span: Span {
68
1
                start: Position { line: 4, col: 9 },
69
1
                end: Position { line: 4, col: 9 },
70
1
            },
71
1
        }),
72
1
        "2",
73
    );
74

            
75
4
    let struct_string = r#"Test(
76
4
        a: 1 // <-- forgotten comma here
77
4
        b: 2
78
4
    )"#;
79

            
80
4
    assert_eq!(
81
4
        ron::from_str::<Test>(struct_string).unwrap_err(),
82
        SpannedError {
83
            code: Error::ExpectedComma,
84
            span: Span {
85
                start: Position { line: 3, col: 9 },
86
                end: Position { line: 3, col: 9 }
87
            }
88
        }
89
    );
90

            
91
    #[cfg(feature = "internal-span-substring-test")]
92
1
    check_error_span_inclusive::<Test>(
93
1
        struct_string,
94
1
        Err(SpannedError {
95
1
            code: Error::ExpectedComma,
96
1
            span: Span {
97
1
                start: Position { line: 3, col: 9 },
98
1
                end: Position { line: 3, col: 9 },
99
1
            },
100
1
        }),
101
1
        "b",
102
    );
103

            
104
4
    let map_string = r#"{
105
4
        "a": 1 // <-- forgotten comma here
106
4
        "b": 2
107
4
    }"#;
108

            
109
4
    assert_eq!(
110
4
        ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(),
111
        SpannedError {
112
            code: Error::ExpectedComma,
113
            span: Span {
114
                start: Position { line: 3, col: 9 },
115
                end: Position { line: 3, col: 9 }
116
            }
117
        }
118
    );
119

            
120
    #[cfg(feature = "internal-span-substring-test")]
121
1
    check_error_span_inclusive::<std::collections::HashMap<String, i32>>(
122
1
        map_string,
123
1
        Err(SpannedError {
124
1
            code: Error::ExpectedComma,
125
1
            span: Span {
126
1
                start: Position { line: 3, col: 9 },
127
1
                end: Position { line: 3, col: 9 },
128
1
            },
129
1
        }),
130
1
        "\"",
131
    );
132

            
133
4
    let extensions_string = r#"#![enable(
134
4
        implicit_some // <-- forgotten comma here
135
4
        unwrap_newtypes
136
4
    ]) 42"#;
137

            
138
4
    assert_eq!(
139
4
        ron::from_str::<u8>(extensions_string).unwrap_err(),
140
        SpannedError {
141
            code: Error::ExpectedComma,
142
            span: Span {
143
                start: Position { line: 2, col: 50 },
144
                end: Position { line: 3, col: 9 }
145
            }
146
        }
147
    );
148

            
149
    #[cfg(feature = "internal-span-substring-test")]
150
1
    check_error_span_inclusive::<u8>(
151
1
        extensions_string,
152
1
        Err(SpannedError {
153
1
            code: Error::ExpectedComma,
154
1
            span: Span {
155
1
                start: Position { line: 2, col: 50 },
156
1
                end: Position { line: 3, col: 9 },
157
1
            },
158
1
        }),
159
1
        "\n        u",
160
    );
161
4
}
162

            
163
#[test]
164
4
fn test_comma_end() {
165
4
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1));
166
4
    assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1));
167
4
    assert_eq!(ron::from_str::<()>("()"), Ok(()));
168
4
}