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

            
9
228
#[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
4
        err,
48
4
        SpannedError {
49
4
            code: Error::TrailingCharacters,
50
4
            position: Position { line: 1, col: 3 }
51
4
        }
52
4
    );
53

            
54
4
    let err = from_bytes::<&RawValue>(b"\0").unwrap_err();
55
4
    assert_eq!(
56
4
        err,
57
4
        SpannedError {
58
4
            code: Error::UnexpectedChar('\0'),
59
4
            position: Position { line: 1, col: 1 }
60
4
        }
61
4
    )
62
4
}
63

            
64
#[test]
65
4
fn test_raw_value_from_ron() {
66
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
67
4
    assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
68

            
69
4
    let err = RawValue::from_ron("4.d").unwrap_err();
70
4
    assert_eq!(
71
4
        err,
72
4
        SpannedError {
73
4
            code: Error::TrailingCharacters,
74
4
            position: Position { line: 1, col: 3 }
75
4
        }
76
4
    );
77

            
78
4
    let raw =
79
4
        RawValue::from_boxed_ron(String::from("/* hi */ (None, 4.2) /* bye */").into_boxed_str())
80
4
            .unwrap();
81
4
    assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
82

            
83
4
    let err = RawValue::from_boxed_ron(String::from("(").into_boxed_str()).unwrap_err();
84
4
    assert_eq!(
85
4
        err,
86
4
        SpannedError {
87
4
            code: Error::Eof,
88
4
            position: Position { line: 1, col: 2 },
89
4
        }
90
4
    );
91
4
}
92

            
93
#[test]
94
4
fn test_raw_value_into_rust() {
95
4
    let raw = RawValue::from_ron("/* hi */ (a: false, b: None) /* bye */").unwrap();
96
4

            
97
4
    let with: WithRawValue = raw.into_rust().unwrap();
98
4
    assert_eq!(
99
4
        with,
100
4
        WithRawValue {
101
4
            a: false,
102
4
            b: from_str(" None").unwrap(),
103
4
        }
104
4
    );
105

            
106
4
    let err = raw.into_rust::<i32>().unwrap_err();
107
4
    assert_eq!(
108
4
        err,
109
4
        SpannedError {
110
4
            code: Error::ExpectedInteger,
111
4
            position: Position { line: 1, col: 10 },
112
4
        }
113
4
    );
114
4
}
115

            
116
#[test]
117
4
fn test_raw_value_from_rust() {
118
4
    let raw = RawValue::from_rust(&42).unwrap();
119
4
    assert_eq!(raw.get_ron(), "42");
120

            
121
4
    let raw = RawValue::from_rust(&WithRawValue {
122
4
        a: true,
123
4
        b: from_str("4.2").unwrap(),
124
4
    })
125
4
    .unwrap();
126
4
    assert_eq!(raw.get_ron(), "(a:true,b:4.2)");
127
4
}
128

            
129
#[test]
130
4
fn test_raw_value_serde_json() {
131
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
132
4

            
133
4
    let ser = serde_json::to_string(&WithRawValue {
134
4
        a: true,
135
4
        b: raw.to_owned(),
136
4
    })
137
4
    .unwrap();
138
4
    assert_eq!(ser, "{\"a\":true,\"b\":\"/* hi */ (None, 4.2) /* bye */\"}");
139

            
140
4
    let with: WithRawValue = serde_json::from_str(&ser).unwrap();
141
4
    assert_eq!(raw, &*with.b);
142

            
143
4
    let err =
144
4
        serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":\"/* hi */ (a:) /* bye */\"}")
145
4
            .unwrap_err();
146
4
    assert_eq!(
147
4
        err.to_string(),
148
4
        "invalid RON value at 1:13: Unexpected char ')' at line 1 column 39"
149
4
    );
150

            
151
4
    let err = serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":42}").unwrap_err();
152
4
    assert_eq!(
153
4
        err.to_string(),
154
4
        "invalid type: integer `42`, expected any valid RON-value-string at line 1 column 16"
155
4
    );
156

            
157
4
    let err = serde_json::from_str::<&RawValue>("\"/* hi */ (a:) /* bye */\"").unwrap_err();
158
4
    assert_eq!(
159
4
        err.to_string(),
160
4
        "invalid RON value at 1:13: Unexpected char ')' at line 1 column 25"
161
4
    );
162

            
163
4
    let err = serde_json::from_str::<&RawValue>("42").unwrap_err();
164
4
    assert_eq!(
165
4
        err.to_string(),
166
4
        "invalid type: integer `42`, expected any valid borrowed RON-value-string at line 1 column 2"
167
4
    );
168
4
}
169

            
170
#[test]
171
4
fn test_raw_value_clone_into() {
172
4
    let raw = RawValue::from_boxed_ron(String::from("(None, 4.2)").into_boxed_str()).unwrap();
173
4
    let raw2 = raw.clone();
174
4
    assert_eq!(raw, raw2);
175

            
176
4
    let boxed_str: Box<str> = raw2.into();
177
4
    assert_eq!(&*boxed_str, "(None, 4.2)");
178
4
}
179

            
180
#[test]
181
4
fn test_raw_value_debug_display() {
182
4
    let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
183
4

            
184
4
    assert_eq!(format!("{}", raw), "/* hi */ (None, 4.2) /* bye */");
185
4
    assert_eq!(
186
4
        format!("{:#?}", raw),
187
4
        "\
188
4
RawValue(
189
4
    /* hi */ (None, 4.2) /* bye */,
190
4
)\
191
4
    "
192
4
    );
193
4
}
194

            
195
#[test]
196
4
fn test_boxed_raw_value_deserialise_from_string() {
197
4
    let string = serde::de::value::StringDeserializer::<Error>::new(String::from("4.2"));
198
4

            
199
4
    let err = <&RawValue>::deserialize(string.clone()).unwrap_err();
200
4
    assert_eq!(
201
4
        err,
202
4
        Error::InvalidValueForType {
203
4
            expected: String::from("any valid borrowed RON-value-string"),
204
4
            found: String::from("the string \"4.2\""),
205
4
        }
206
4
    );
207

            
208
4
    let boxed_raw = Box::<RawValue>::deserialize(string).unwrap();
209
4
    assert_eq!(boxed_raw.get_ron(), "4.2");
210

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

            
213
4
    let err = Box::<RawValue>::deserialize(string).unwrap_err();
214
4
    assert_eq!(
215
4
        err,
216
4
        Error::Message(String::from(
217
4
            "invalid RON value at 1:2: Unexpected end of RON"
218
4
        ))
219
4
    );
220
4
}
221

            
222
#[test]
223
4
fn test_whitespace_rountrip_issues_and_trim() {
224
4
    let mut v = WithRawValue {
225
4
        a: true,
226
4
        b: RawValue::from_ron("42 ").unwrap().to_owned(),
227
4
    };
228
4
    v = ron::from_str(&ron::to_string(&v).unwrap()).unwrap();
229
4
    assert_eq!(v.b.get_ron(), "42 ");
230
4
    assert_eq!(v.b.trim().get_ron(), "42");
231
4
    assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
232

            
233
44
    for i in 1..=10 {
234
40
        v = ron::from_str(
235
40
            &ron::ser::to_string_pretty(&v, ron::ser::PrettyConfig::default()).unwrap(),
236
40
        )
237
40
        .unwrap();
238
40
        assert_eq!(v.b.get_ron(), &format!("{: <1$}42 ", "", i));
239
40
        assert_eq!(v.b.trim().get_ron(), "42");
240
40
        assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
241
    }
242

            
243
4
    assert_eq!(RawValue::from_ron("42").unwrap().trim().get_ron(), "42");
244
4
    assert_eq!(
245
4
        RawValue::from_ron(" /* start */ 42    // end\n ")
246
4
            .unwrap()
247
4
            .trim()
248
4
            .get_ron(),
249
4
        "42"
250
4
    );
251

            
252
4
    assert_eq!(
253
4
        RawValue::from_ron("42")
254
4
            .unwrap()
255
4
            .to_owned()
256
4
            .trim_boxed()
257
4
            .get_ron(),
258
4
        "42"
259
4
    );
260
4
    assert_eq!(
261
4
        RawValue::from_ron(" /* start */ 42    // end\n ")
262
4
            .unwrap()
263
4
            .to_owned()
264
4
            .trim_boxed()
265
4
            .get_ron(),
266
4
        "42"
267
4
    );
268
4
}
269

            
270
#[test]
271
4
fn test_fuzzer_found_issue() {
272
4
    assert_eq!(
273
4
        RawValue::from_ron(""),
274
4
        Err(SpannedError {
275
4
            code: Error::Eof,
276
4
            position: Position { line: 1, col: 1 },
277
4
        })
278
4
    );
279

            
280
4
    assert_eq!(ron::from_str("C"), RawValue::from_ron("C"));
281

            
282
4
    assert_eq!(ron::from_str(" t "), RawValue::from_ron(" t "));
283

            
284
4
    assert_eq!(
285
4
        RawValue::from_ron("#![enable(implicit_some)] 42"),
286
4
        Err(SpannedError {
287
4
            code: Error::Message(String::from(
288
4
                "ron::value::RawValue cannot enable extensions"
289
4
            )),
290
4
            position: Position { line: 1, col: 27 },
291
4
        })
292
4
    );
293

            
294
4
    assert_eq!(
295
4
        RawValue::from_boxed_ron(String::from("#![enable(implicit_some)] 42").into_boxed_str()),
296
4
        Err(SpannedError {
297
4
            code: Error::Message(String::from(
298
4
                "ron::value::RawValue cannot enable extensions"
299
4
            )),
300
4
            position: Position { line: 1, col: 27 },
301
4
        })
302
4
    );
303

            
304
4
    assert_eq!(
305
4
        RawValue::from_ron("42 //"),
306
4
        Err(SpannedError {
307
4
            code: Error::UnclosedLineComment,
308
4
            position: Position { line: 1, col: 6 },
309
4
        })
310
4
    );
311
4
    assert_eq!(
312
4
        ron::from_str::<&RawValue>("42 //"),
313
4
        Err(SpannedError {
314
4
            code: Error::UnclosedLineComment,
315
4
            position: Position { line: 1, col: 6 },
316
4
        })
317
4
    );
318
4
    assert_eq!(
319
4
        ron::from_str::<&RawValue>("42 //\n").unwrap(),
320
4
        RawValue::from_ron("42 //\n").unwrap()
321
4
    );
322
4
    assert_eq!(
323
4
        ron::from_str::<&RawValue>("42 /**/").unwrap(),
324
4
        RawValue::from_ron("42 /**/").unwrap()
325
4
    );
326
4
    assert_eq!(
327
4
        ron::from_str::<&RawValue>("42 ").unwrap(),
328
4
        RawValue::from_ron("42 ").unwrap()
329
4
    );
330

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

            
358
4
    assert_eq!(
359
4
        ron::to_string(&Some(Some(RawValue::from_ron("None").unwrap()))).unwrap(),
360
4
        "Some(Some(None))"
361
4
    );
362
    // Since a RawValue can contain anything, no implicit Some are allowed around it
363
4
    assert_eq!(
364
4
        ron::Options::default()
365
4
            .with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME)
366
4
            .to_string(&Some(Some(RawValue::from_ron("None").unwrap())))
367
4
            .unwrap(),
368
4
        "Some(Some(None))"
369
4
    );
370
4
    assert_eq!(
371
4
        ron::from_str::<Option<Option<&RawValue>>>("Some(Some(None))").unwrap(),
372
4
        Some(Some(RawValue::from_ron("None").unwrap()))
373
4
    );
374
4
}