1
use ron::{
2
    de::from_bytes,
3
    error::{Error, Position, Span, SpannedError},
4
    from_str, to_string,
5
    value::RawValue,
6
};
7
use serde::{Deserialize, Serialize};
8

            
9
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
10
struct WithRawValue {
11
    a: bool,
12
    b: Box<RawValue>,
13
}
14

            
15
#[test]
16
4
fn test_raw_value_simple() {
17
4
    let raw: &RawValue = from_str("true").unwrap();
18
4
    assert_eq!(raw.get_ron(), "true");
19
4
    let ser = to_string(raw).unwrap();
20
4
    assert_eq!(ser, "true");
21
4
}
22

            
23
#[test]
24
4
fn test_raw_value_inner() {
25
4
    let raw: WithRawValue = from_str("(a: false, b: [1, /* lol */ 2, 3])").unwrap();
26
4
    assert_eq!(raw.b.get_ron(), " [1, /* lol */ 2, 3]");
27
4
    let ser = to_string(&raw).unwrap();
28
4
    assert_eq!(ser, "(a:false,b: [1, /* lol */ 2, 3])");
29
4
}
30

            
31
#[test]
32
4
fn test_raw_value_comment() {
33
4
    let raw: WithRawValue = from_str("(a: false, b: /* yeah */ 4)").unwrap();
34
4
    assert_eq!(raw.b.get_ron(), " /* yeah */ 4");
35

            
36
4
    let raw: WithRawValue = from_str("(a: false, b: 4 /* yes */)").unwrap();
37
4
    assert_eq!(raw.b.get_ron(), " 4 /* yes */");
38

            
39
4
    let raw: WithRawValue = from_str("(a: false, b: (/* this */ 4 /* too */))").unwrap();
40
4
    assert_eq!(raw.b.get_ron(), " (/* this */ 4 /* too */)");
41
4
}
42

            
43
#[test]
44
4
fn test_raw_value_invalid() {
45
4
    let err = from_str::<&RawValue>("4.d").unwrap_err();
46
4
    assert_eq!(
47
        err,
48
        SpannedError {
49
            code: Error::TrailingCharacters,
50
            span: Span {
51
                start: Position { line: 1, col: 3 },
52
                end: Position { line: 1, col: 3 }
53
            }
54
        }
55
    );
56

            
57
4
    let err = from_bytes::<&RawValue>(b"\0").unwrap_err();
58
4
    assert_eq!(
59
        err,
60
        SpannedError {
61
            code: Error::UnexpectedChar('\0'),
62
            span: Span {
63
                start: Position { line: 1, col: 1 },
64
                end: Position { line: 1, col: 1 }
65
            }
66
        }
67
    )
68
4
}
69

            
70
#[test]
71
4
fn test_raw_value_from_ron() {
72
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
73
4
    assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
74

            
75
4
    let err = RawValue::from_ron("4.d").unwrap_err();
76
4
    assert_eq!(
77
        err,
78
        SpannedError {
79
            code: Error::TrailingCharacters,
80
            span: Span {
81
                start: Position { line: 1, col: 3 },
82
                end: Position { line: 1, col: 3 }
83
            }
84
        }
85
    );
86

            
87
4
    let raw =
88
4
        RawValue::from_boxed_ron(String::from("/* hi */ (None, 4.2) /* bye */").into_boxed_str())
89
4
            .unwrap();
90
4
    assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
91

            
92
4
    let err = RawValue::from_boxed_ron(String::from("(").into_boxed_str()).unwrap_err();
93
4
    assert_eq!(
94
        err,
95
        SpannedError {
96
            code: Error::Eof,
97
            span: Span {
98
                start: Position { line: 1, col: 1 },
99
                end: Position { line: 1, col: 2 },
100
            }
101
        }
102
    );
103
4
}
104

            
105
#[test]
106
4
fn test_raw_value_into_rust() {
107
4
    let raw = RawValue::from_ron("/* hi */ (a: false, b: None) /* bye */").unwrap();
108

            
109
4
    let with: WithRawValue = raw.into_rust().unwrap();
110
4
    assert_eq!(
111
        with,
112
4
        WithRawValue {
113
4
            a: false,
114
4
            b: from_str(" None").unwrap(),
115
4
        }
116
    );
117

            
118
4
    let err = raw.into_rust::<i32>().unwrap_err();
119
4
    assert_eq!(
120
        err,
121
        SpannedError {
122
            code: Error::ExpectedInteger,
123
            span: Span {
124
                start: Position { line: 1, col: 9 },
125
                end: Position { line: 1, col: 10 },
126
            }
127
        }
128
    );
129
4
}
130

            
131
#[test]
132
4
fn test_raw_value_from_rust() {
133
4
    let raw = RawValue::from_rust(&42).unwrap();
134
4
    assert_eq!(raw.get_ron(), "42");
135

            
136
4
    let raw = RawValue::from_rust(&WithRawValue {
137
4
        a: true,
138
4
        b: from_str("4.2").unwrap(),
139
4
    })
140
4
    .unwrap();
141
4
    assert_eq!(raw.get_ron(), "(a:true,b:4.2)");
142
4
}
143

            
144
#[test]
145
4
fn test_raw_value_serde_json() {
146
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
147

            
148
4
    let ser = serde_json::to_string(&WithRawValue {
149
4
        a: true,
150
4
        b: raw.to_owned(),
151
4
    })
152
4
    .unwrap();
153
4
    assert_eq!(ser, "{\"a\":true,\"b\":\"/* hi */ (None, 4.2) /* bye */\"}");
154

            
155
4
    let with: WithRawValue = serde_json::from_str(&ser).unwrap();
156
4
    assert_eq!(raw, &*with.b);
157

            
158
4
    let err =
159
4
        serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":\"/* hi */ (a:) /* bye */\"}")
160
4
            .unwrap_err();
161
4
    assert_eq!(
162
4
        err.to_string(),
163
        "invalid RON value at 1:13: Unexpected char ')' at line 1 column 39"
164
    );
165

            
166
4
    let err = serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":42}").unwrap_err();
167
4
    assert_eq!(
168
4
        err.to_string(),
169
        "invalid type: integer `42`, expected any valid RON-value-string at line 1 column 16"
170
    );
171

            
172
4
    let err = serde_json::from_str::<&RawValue>("\"/* hi */ (a:) /* bye */\"").unwrap_err();
173
4
    assert_eq!(
174
4
        err.to_string(),
175
        "invalid RON value at 1:13: Unexpected char ')' at line 1 column 25"
176
    );
177

            
178
4
    let err = serde_json::from_str::<&RawValue>("42").unwrap_err();
179
4
    assert_eq!(
180
4
        err.to_string(),
181
        "invalid type: integer `42`, expected any valid borrowed RON-value-string at line 1 column 2"
182
    );
183
4
}
184

            
185
#[test]
186
4
fn test_raw_value_clone_into() {
187
4
    let raw = RawValue::from_boxed_ron(String::from("(None, 4.2)").into_boxed_str()).unwrap();
188
4
    let raw2 = raw.clone();
189
4
    assert_eq!(raw, raw2);
190

            
191
4
    let boxed_str: Box<str> = raw2.into();
192
4
    assert_eq!(&*boxed_str, "(None, 4.2)");
193
4
}
194

            
195
#[test]
196
4
fn test_raw_value_debug_display() {
197
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
198

            
199
4
    assert_eq!(format!("{}", raw), "/* hi */ (None, 4.2) /* bye */");
200
4
    assert_eq!(
201
4
        format!("{:#?}", raw),
202
        "\
203
RawValue(
204
    /* hi */ (None, 4.2) /* bye */,
205
)\
206
    "
207
    );
208
4
}
209

            
210
#[test]
211
4
fn test_boxed_raw_value_deserialise_from_string() {
212
4
    let string = serde::de::value::StringDeserializer::<Error>::new(String::from("4.2"));
213

            
214
4
    let err = <&RawValue>::deserialize(string.clone()).unwrap_err();
215
4
    assert_eq!(
216
        err,
217
4
        Error::InvalidValueForType {
218
4
            expected: String::from("any valid borrowed RON-value-string"),
219
4
            found: String::from("the string \"4.2\""),
220
4
        }
221
    );
222

            
223
4
    let boxed_raw = Box::<RawValue>::deserialize(string).unwrap();
224
4
    assert_eq!(boxed_raw.get_ron(), "4.2");
225

            
226
4
    let string = serde::de::value::StringDeserializer::<Error>::new(String::from("["));
227

            
228
4
    let err = Box::<RawValue>::deserialize(string).unwrap_err();
229
4
    assert_eq!(
230
        err,
231
4
        Error::Message(String::from(
232
4
            "invalid RON value at 1:1-1:2: Unexpected end of RON"
233
4
        ))
234
    );
235
4
}
236

            
237
#[test]
238
4
fn test_whitespace_rountrip_issues_and_trim() {
239
4
    let mut v = WithRawValue {
240
4
        a: true,
241
4
        b: RawValue::from_ron("42 ").unwrap().to_owned(),
242
4
    };
243
4
    v = ron::from_str(&ron::to_string(&v).unwrap()).unwrap();
244
4
    assert_eq!(v.b.get_ron(), "42 ");
245
4
    assert_eq!(v.b.trim().get_ron(), "42");
246
4
    assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
247

            
248
44
    for i in 1..=10 {
249
40
        v = ron::from_str(
250
40
            &ron::ser::to_string_pretty(&v, ron::ser::PrettyConfig::default()).unwrap(),
251
        )
252
40
        .unwrap();
253
40
        assert_eq!(v.b.get_ron(), &format!("{: <1$}42 ", "", i));
254
40
        assert_eq!(v.b.trim().get_ron(), "42");
255
40
        assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
256
    }
257

            
258
4
    assert_eq!(RawValue::from_ron("42").unwrap().trim().get_ron(), "42");
259
4
    assert_eq!(
260
4
        RawValue::from_ron(" /* start */ 42    // end\n ")
261
4
            .unwrap()
262
4
            .trim()
263
4
            .get_ron(),
264
        "42"
265
    );
266

            
267
4
    assert_eq!(
268
4
        RawValue::from_ron("42")
269
4
            .unwrap()
270
4
            .to_owned()
271
4
            .trim_boxed()
272
4
            .get_ron(),
273
        "42"
274
    );
275
4
    assert_eq!(
276
4
        RawValue::from_ron(" /* start */ 42    // end\n ")
277
4
            .unwrap()
278
4
            .to_owned()
279
4
            .trim_boxed()
280
4
            .get_ron(),
281
        "42"
282
    );
283
4
}
284

            
285
#[test]
286
4
fn test_fuzzer_found_issue() {
287
4
    assert_eq!(
288
4
        RawValue::from_ron(""),
289
        Err(SpannedError {
290
            code: Error::Eof,
291
            span: Span {
292
                start: Position { line: 1, col: 1 },
293
                end: Position { line: 1, col: 1 },
294
            }
295
        })
296
    );
297

            
298
4
    assert_eq!(ron::from_str("C"), RawValue::from_ron("C"));
299

            
300
4
    assert_eq!(ron::from_str(" t "), RawValue::from_ron(" t "));
301

            
302
4
    assert_eq!(
303
4
        RawValue::from_ron("#![enable(implicit_some)] 42"),
304
4
        Err(SpannedError {
305
4
            code: Error::Message(String::from(
306
4
                "ron::value::RawValue cannot enable extensions"
307
4
            )),
308
4
            span: Span {
309
4
                start: Position { line: 1, col: 27 },
310
4
                end: Position { line: 1, col: 27 },
311
4
            }
312
4
        })
313
    );
314

            
315
4
    assert_eq!(
316
4
        RawValue::from_boxed_ron(String::from("#![enable(implicit_some)] 42").into_boxed_str()),
317
4
        Err(SpannedError {
318
4
            code: Error::Message(String::from(
319
4
                "ron::value::RawValue cannot enable extensions"
320
4
            )),
321
4
            span: Span {
322
4
                start: Position { line: 1, col: 27 },
323
4
                end: Position { line: 1, col: 27 },
324
4
            }
325
4
        })
326
    );
327

            
328
4
    assert_eq!(
329
4
        RawValue::from_ron("42 //"),
330
        Err(SpannedError {
331
            code: Error::UnclosedLineComment,
332
            span: Span {
333
                start: Position { line: 1, col: 6 },
334
                end: Position { line: 1, col: 6 },
335
            }
336
        })
337
    );
338
4
    assert_eq!(
339
4
        ron::from_str::<&RawValue>("42 //"),
340
        Err(SpannedError {
341
            code: Error::UnclosedLineComment,
342
            span: Span {
343
                start: Position { line: 1, col: 6 },
344
                end: Position { line: 1, col: 6 },
345
            }
346
        })
347
    );
348
4
    assert_eq!(
349
4
        ron::from_str::<&RawValue>("42 //\n").unwrap(),
350
4
        RawValue::from_ron("42 //\n").unwrap()
351
    );
352
4
    assert_eq!(
353
4
        ron::from_str::<&RawValue>("42 /**/").unwrap(),
354
4
        RawValue::from_ron("42 /**/").unwrap()
355
    );
356
4
    assert_eq!(
357
4
        ron::from_str::<&RawValue>("42 ").unwrap(),
358
4
        RawValue::from_ron("42 ").unwrap()
359
    );
360

            
361
4
    assert_eq!(
362
4
        RawValue::from_ron("a//"),
363
        Err(SpannedError {
364
            code: Error::UnclosedLineComment,
365
            span: Span {
366
                start: Position { line: 1, col: 4 },
367
                end: Position { line: 1, col: 4 },
368
            }
369
        })
370
    );
371
4
    assert_eq!(
372
4
        ron::from_str::<&RawValue>("a//"),
373
        Err(SpannedError {
374
            code: Error::UnclosedLineComment,
375
            span: Span {
376
                start: Position { line: 1, col: 4 },
377
                end: Position { line: 1, col: 4 },
378
            }
379
        })
380
    );
381
4
    assert_eq!(
382
4
        ron::from_str::<&RawValue>("a//\n").unwrap(),
383
4
        RawValue::from_ron("a//\n").unwrap()
384
    );
385
4
    assert_eq!(
386
4
        ron::from_str::<&RawValue>("a/**/").unwrap(),
387
4
        RawValue::from_ron("a/**/").unwrap()
388
    );
389
4
    assert_eq!(
390
4
        ron::from_str::<&RawValue>("a").unwrap(),
391
4
        RawValue::from_ron("a").unwrap()
392
    );
393

            
394
4
    assert_eq!(
395
4
        ron::to_string(&Some(Some(RawValue::from_ron("None").unwrap()))).unwrap(),
396
        "Some(Some(None))"
397
    );
398
    // Since a RawValue can contain anything, no implicit Some are allowed around it
399
4
    assert_eq!(
400
4
        ron::Options::default()
401
4
            .with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME)
402
4
            .to_string(&Some(Some(RawValue::from_ron("None").unwrap())))
403
4
            .unwrap(),
404
        "Some(Some(None))"
405
    );
406
4
    assert_eq!(
407
4
        ron::from_str::<Option<Option<&RawValue>>>("Some(Some(None))").unwrap(),
408
4
        Some(Some(RawValue::from_ron("None").unwrap()))
409
    );
410
4
}