1
use ron::de::{from_str, Error, Position, Span, SpannedError as RonErr};
2

            
3
#[test]
4
4
fn test_simple() {
5
4
    assert_eq!(
6
4
        from_str(
7
4
            "/*
8
4
 * We got a hexadecimal number here!
9
4
 *
10
4
 */0x507"
11
        ),
12
        Ok(0x507)
13
    );
14
4
}
15

            
16
#[test]
17
4
fn test_nested() {
18
4
    assert_eq!(
19
4
        from_str(
20
4
            "/*
21
4
        /* quite * some * nesting * going * on * /* here /* (yeah, maybe a bit too much) */ */ */
22
4
    */
23
4
    // The actual value comes.. /*
24
4
    // very soon, these are just checks that */
25
4
    // multi-line comments don't trigger in line comments /*
26
4
\"THE VALUE\" /* This is the value /* :) */ */
27
4
    "
28
        ),
29
4
        Ok("THE VALUE".to_owned())
30
    );
31
4
}
32

            
33
#[test]
34
4
fn test_unclosed() {
35
4
    assert_eq!(
36
4
        from_str::<String>("\"hi\" /*"),
37
        Err(RonErr {
38
            code: Error::UnclosedBlockComment,
39
            span: Span {
40
                start: Position { line: 1, col: 6 },
41
                end: Position { line: 1, col: 8 }
42
            }
43
        })
44
    );
45
4
    assert_eq!(
46
4
        from_str::<String>(
47
4
            "/*
48
4
        /* quite * some * nesting * going * on * /* here /* (yeah, maybe a bit too much) */ */ */
49
4
    */
50
4
    // The actual value comes.. /*
51
4
    // very soon, these are just checks that */
52
4
    // multi-line comments don't trigger in line comments /*
53
4
/* Unfortunately, this comment won't get closed :(
54
4
\"THE VALUE (which is invalid)\"
55
4
"
56
        ),
57
        Err(RonErr {
58
            code: Error::UnclosedBlockComment,
59
            span: Span {
60
                start: Position { line: 7, col: 3 },
61
                end: Position { line: 9, col: 1 }
62
            }
63
        })
64
    );
65
4
}
66

            
67
#[test]
68
4
fn test_unexpected_byte() {
69
4
    assert_eq!(
70
4
        from_str::<u8>("42 /q"),
71
        Err(RonErr {
72
            code: Error::UnexpectedChar('q'),
73
            span: Span {
74
                start: Position { line: 1, col: 4 },
75
                end: Position { line: 1, col: 6 },
76
            }
77
        })
78
    );
79
4
}
80

            
81
#[test]
82
4
fn test_comment_after_literal_in_newtype_lookahead() {
83
    #[derive(Debug, PartialEq, serde::Deserialize)]
84
    enum Cmd {
85
        Echo(Msg),
86
    }
87

            
88
    #[derive(Debug, PartialEq, serde::Deserialize)]
89
    #[serde(untagged)]
90
    enum Msg {
91
        One(ron::Value),
92
        Many(Vec<ron::Value>),
93
    }
94

            
95
    #[derive(Debug, PartialEq, serde::Deserialize)]
96
    #[serde(untagged)]
97
    enum Untagged {
98
        Cmd(Cmd),
99
    }
100

            
101
    // A comment directly after a literal must not be scanned as code
102
12
    for (ron, inner) in [
103
4
        ("\"hi\"", ron::Value::String(String::from("hi"))),
104
4
        ("'h'", ron::Value::Char('h')),
105
4
        ("b\"hi\"", ron::Value::Bytes(b"hi".to_vec())),
106
4
    ] {
107
12
        let expected = Cmd::Echo(Msg::One(inner));
108

            
109
36
        for comment in ["", "/*,x*/", "// note\n"] {
110
36
            let ron = format!("Echo({}{})", ron, comment);
111

            
112
36
            assert_eq!(
113
36
                from_str::<Cmd>(&format!("#![enable(unwrap_variant_newtypes)] {}", ron)).as_ref(),
114
36
                Ok(&expected),
115
                "{}",
116
                ron,
117
            );
118
36
            assert_eq!(
119
36
                from_str::<Untagged>(&ron)
120
36
                    .map(|Untagged::Cmd(cmd)| cmd)
121
36
                    .as_ref(),
122
36
                Ok(&expected),
123
                "{}",
124
                ron,
125
            );
126
        }
127
    }
128
4
}
129

            
130
#[test]
131
4
fn test_whitespace_and_comments_inside_unit() {
132
    #[derive(Debug, PartialEq, serde::Deserialize)]
133
    struct Unit;
134

            
135
    #[derive(Debug, PartialEq, serde::Deserialize)]
136
    struct Struct {
137
        a: (),
138
        b: Unit,
139
    }
140

            
141
20
    for unit in ["()", "( )", "(\n)", "(/* empty */)", "( // empty\n)"] {
142
20
        assert_eq!(from_str::<()>(unit), Ok(()), "{}", unit);
143
20
        assert_eq!(from_str::<Unit>(unit), Ok(Unit), "{}", unit);
144
20
        assert_eq!(
145
20
            from_str::<Struct>(&format!("(a: {0}, b: {0})", unit)),
146
            Ok(Struct { a: (), b: Unit }),
147
            "{}",
148
            unit,
149
        );
150
    }
151

            
152
4
    assert_eq!(
153
4
        from_str::<()>("( 42 )"),
154
        Err(RonErr {
155
            code: Error::ExpectedUnit,
156
            span: Span {
157
                start: Position { line: 1, col: 1 },
158
                end: Position { line: 1, col: 1 },
159
            }
160
        })
161
    );
162
4
    assert_eq!(
163
4
        from_str::<()>("( /* )"),
164
        Err(RonErr {
165
            code: Error::UnclosedBlockComment,
166
            span: Span {
167
                start: Position { line: 1, col: 5 },
168
                end: Position { line: 1, col: 7 },
169
            }
170
        })
171
    );
172
4
}
173

            
174
#[test]
175
4
fn test_whitespace_and_comments_inside_untyped_unit() {
176
    use ron::value::{Map, Number, Value};
177

            
178
    #[derive(Debug, PartialEq, serde::Deserialize)]
179
    #[serde(untagged)]
180
    enum Untagged {
181
        Struct { a: () },
182
    }
183

            
184
20
    for unit in ["()", "( )", "(\n)", "(/* empty */)", "( // empty\n)"] {
185
20
        assert_eq!(from_str::<Value>(unit), Ok(Value::Unit), "{}", unit);
186
20
        assert_eq!(
187
20
            from_str::<Untagged>(&format!("(a: {})", unit)),
188
            Ok(Untagged::Struct { a: () }),
189
            "{}",
190
            unit,
191
        );
192
    }
193

            
194
4
    assert_eq!(
195
4
        from_str::<Value>("( /* 1, */ 2 )"),
196
4
        Ok(Value::Seq(vec![Value::Number(Number::U8(2))]))
197
    );
198
4
    assert_eq!(
199
4
        from_str::<Value>("( 1, 2 )"),
200
4
        Ok(Value::Seq(vec![
201
4
            Value::Number(Number::U8(1)),
202
4
            Value::Number(Number::U8(2)),
203
4
        ]))
204
    );
205

            
206
4
    let mut map = Map::new();
207
4
    map.insert(
208
4
        Value::String(String::from("a")),
209
4
        Value::Number(Number::U8(1)),
210
    );
211
4
    assert_eq!(from_str::<Value>("( /* a */ a: 1 )"), Ok(Value::Map(map)));
212

            
213
    #[derive(Debug, PartialEq, serde::Deserialize)]
214
    #[serde(untagged)]
215
    enum NotUnit {
216
        Newtype(i32),
217
    }
218

            
219
4
    assert_eq!(
220
4
        from_str::<NotUnit>("( )"),
221
4
        Err(RonErr {
222
4
            code: Error::Message(String::from(
223
4
                "data did not match any variant of untagged enum NotUnit"
224
4
            )),
225
4
            span: Span {
226
4
                start: Position { line: 1, col: 1 },
227
4
                end: Position { line: 1, col: 4 },
228
4
            }
229
4
        })
230
    );
231
4
}