Debian Codegraph @

tests/407_raw_value.rs

raw symbols SCIP
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 fn test_raw_value_simple() {
17 let raw: &RawValue = from_str("true").unwrap();
18 assert_eq!(raw.get_ron(), "true");
19 let ser = to_string(raw).unwrap();
20 assert_eq!(ser, "true");
21 }
22
23 #[test]
24 fn test_raw_value_inner() {
25 let raw: WithRawValue = from_str("(a: false, b: [1, /* lol */ 2, 3])").unwrap();
26 assert_eq!(raw.b.get_ron(), " [1, /* lol */ 2, 3]");
27 let ser = to_string(&raw).unwrap();
28 assert_eq!(ser, "(a:false,b: [1, /* lol */ 2, 3])");
29 }
30
31 #[test]
32 fn test_raw_value_comment() {
33 let raw: WithRawValue = from_str("(a: false, b: /* yeah */ 4)").unwrap();
34 assert_eq!(raw.b.get_ron(), " /* yeah */ 4");
35
36 let raw: WithRawValue = from_str("(a: false, b: 4 /* yes */)").unwrap();
37 assert_eq!(raw.b.get_ron(), " 4 /* yes */");
38
39 let raw: WithRawValue = from_str("(a: false, b: (/* this */ 4 /* too */))").unwrap();
40 assert_eq!(raw.b.get_ron(), " (/* this */ 4 /* too */)");
41 }
42
43 #[test]
44 fn test_raw_value_invalid() {
45 let err = from_str::<&RawValue>("4.d").unwrap_err();
46 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 let err = from_bytes::<&RawValue>(b"\0").unwrap_err();
58 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 }
69
70 #[test]
71 fn test_raw_value_from_ron() {
72 let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
73 assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
74
75 let err = RawValue::from_ron("4.d").unwrap_err();
76 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 let raw =
88 RawValue::from_boxed_ron(String::from("/* hi */ (None, 4.2) /* bye */").into_boxed_str())
89 .unwrap();
90 assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */");
91
92 let err = RawValue::from_boxed_ron(String::from("(").into_boxed_str()).unwrap_err();
93 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 }
104
105 #[test]
106 fn test_raw_value_into_rust() {
107 let raw = RawValue::from_ron("/* hi */ (a: false, b: None) /* bye */").unwrap();
108
109 let with: WithRawValue = raw.into_rust().unwrap();
110 assert_eq!(
111 with,
112 WithRawValue {
113 a: false,
114 b: from_str(" None").unwrap(),
115 }
116 );
117
118 let err = raw.into_rust::<i32>().unwrap_err();
119 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 }
130
131 #[test]
132 fn test_raw_value_from_rust() {
133 let raw = RawValue::from_rust(&42).unwrap();
134 assert_eq!(raw.get_ron(), "42");
135
136 let raw = RawValue::from_rust(&WithRawValue {
137 a: true,
138 b: from_str("4.2").unwrap(),
139 })
140 .unwrap();
141 assert_eq!(raw.get_ron(), "(a:true,b:4.2)");
142 }
143
144 #[test]
145 fn test_raw_value_serde_json() {
146 let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
147
148 let ser = serde_json::to_string(&WithRawValue {
149 a: true,
150 b: raw.to_owned(),
151 })
152 .unwrap();
153 assert_eq!(ser, "{\"a\":true,\"b\":\"/* hi */ (None, 4.2) /* bye */\"}");
154
155 let with: WithRawValue = serde_json::from_str(&ser).unwrap();
156 assert_eq!(raw, &*with.b);
157
158 let err =
159 serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":\"/* hi */ (a:) /* bye */\"}")
160 .unwrap_err();
161 assert_eq!(
162 err.to_string(),
163 "invalid RON value at 1:13: Unexpected char ')' at line 1 column 39"
164 );
165
166 let err = serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":42}").unwrap_err();
167 assert_eq!(
168 err.to_string(),
169 "invalid type: integer `42`, expected any valid RON-value-string at line 1 column 16"
170 );
171
172 let err = serde_json::from_str::<&RawValue>("\"/* hi */ (a:) /* bye */\"").unwrap_err();
173 assert_eq!(
174 err.to_string(),
175 "invalid RON value at 1:13: Unexpected char ')' at line 1 column 25"
176 );
177
178 let err = serde_json::from_str::<&RawValue>("42").unwrap_err();
179 assert_eq!(
180 err.to_string(),
181 "invalid type: integer `42`, expected any valid borrowed RON-value-string at line 1 column 2"
182 );
183 }
184
185 #[test]
186 fn test_raw_value_clone_into() {
187 let raw = RawValue::from_boxed_ron(String::from("(None, 4.2)").into_boxed_str()).unwrap();
188 let raw2 = raw.clone();
189 assert_eq!(raw, raw2);
190
191 let boxed_str: Box<str> = raw2.into();
192 assert_eq!(&*boxed_str, "(None, 4.2)");
193 }
194
195 #[test]
196 fn test_raw_value_debug_display() {
197 let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap();
198
199 assert_eq!(format!("{}", raw), "/* hi */ (None, 4.2) /* bye */");
200 assert_eq!(
201 format!("{:#?}", raw),
202 "\
203 RawValue(
204 /* hi */ (None, 4.2) /* bye */,
205 )\
206 "
207 );
208 }
209
210 #[test]
211 fn test_boxed_raw_value_deserialise_from_string() {
212 let string = serde::de::value::StringDeserializer::<Error>::new(String::from("4.2"));
213
214 let err = <&RawValue>::deserialize(string.clone()).unwrap_err();
215 assert_eq!(
216 err,
217 Error::InvalidValueForType {
218 expected: String::from("any valid borrowed RON-value-string"),
219 found: String::from("the string \"4.2\""),
220 }
221 );
222
223 let boxed_raw = Box::<RawValue>::deserialize(string).unwrap();
224 assert_eq!(boxed_raw.get_ron(), "4.2");
225
226 let string = serde::de::value::StringDeserializer::<Error>::new(String::from("["));
227
228 let err = Box::<RawValue>::deserialize(string).unwrap_err();
229 assert_eq!(
230 err,
231 Error::Message(String::from(
232 "invalid RON value at 1:1-1:2: Unexpected end of RON"
233 ))
234 );
235 }
236
237 #[test]
238 fn test_whitespace_rountrip_issues_and_trim() {
239 let mut v = WithRawValue {
240 a: true,
241 b: RawValue::from_ron("42 ").unwrap().to_owned(),
242 };
243 v = ron::from_str(&ron::to_string(&v).unwrap()).unwrap();
244 assert_eq!(v.b.get_ron(), "42 ");
245 assert_eq!(v.b.trim().get_ron(), "42");
246 assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
247
248 for i in 1..=10 {
249 v = ron::from_str(
250 &ron::ser::to_string_pretty(&v, ron::ser::PrettyConfig::default()).unwrap(),
251 )
252 .unwrap();
253 assert_eq!(v.b.get_ron(), &format!("{: <1$}42 ", "", i));
254 assert_eq!(v.b.trim().get_ron(), "42");
255 assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42");
256 }
257
258 assert_eq!(RawValue::from_ron("42").unwrap().trim().get_ron(), "42");
259 assert_eq!(
260 RawValue::from_ron(" /* start */ 42 // end\n ")
261 .unwrap()
262 .trim()
263 .get_ron(),
264 "42"
265 );
266
267 assert_eq!(
268 RawValue::from_ron("42")
269 .unwrap()
270 .to_owned()
271 .trim_boxed()
272 .get_ron(),
273 "42"
274 );
275 assert_eq!(
276 RawValue::from_ron(" /* start */ 42 // end\n ")
277 .unwrap()
278 .to_owned()
279 .trim_boxed()
280 .get_ron(),
281 "42"
282 );
283 }
284
285 #[test]
286 fn test_fuzzer_found_issue() {
287 assert_eq!(
288 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 assert_eq!(ron::from_str("C"), RawValue::from_ron("C"));
299
300 assert_eq!(ron::from_str(" t "), RawValue::from_ron(" t "));
301
302 assert_eq!(
303 RawValue::from_ron("#![enable(implicit_some)] 42"),
304 Err(SpannedError {
305 code: Error::Message(String::from(
306 "ron::value::RawValue cannot enable extensions"
307 )),
308 span: Span {
309 start: Position { line: 1, col: 27 },
310 end: Position { line: 1, col: 27 },
311 }
312 })
313 );
314
315 assert_eq!(
316 RawValue::from_boxed_ron(String::from("#![enable(implicit_some)] 42").into_boxed_str()),
317 Err(SpannedError {
318 code: Error::Message(String::from(
319 "ron::value::RawValue cannot enable extensions"
320 )),
321 span: Span {
322 start: Position { line: 1, col: 27 },
323 end: Position { line: 1, col: 27 },
324 }
325 })
326 );
327
328 assert_eq!(
329 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 assert_eq!(
339 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 assert_eq!(
349 ron::from_str::<&RawValue>("42 //\n").unwrap(),
350 RawValue::from_ron("42 //\n").unwrap()
351 );
352 assert_eq!(
353 ron::from_str::<&RawValue>("42 /**/").unwrap(),
354 RawValue::from_ron("42 /**/").unwrap()
355 );
356 assert_eq!(
357 ron::from_str::<&RawValue>("42 ").unwrap(),
358 RawValue::from_ron("42 ").unwrap()
359 );
360
361 assert_eq!(
362 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 assert_eq!(
372 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 assert_eq!(
382 ron::from_str::<&RawValue>("a//\n").unwrap(),
383 RawValue::from_ron("a//\n").unwrap()
384 );
385 assert_eq!(
386 ron::from_str::<&RawValue>("a/**/").unwrap(),
387 RawValue::from_ron("a/**/").unwrap()
388 );
389 assert_eq!(
390 ron::from_str::<&RawValue>("a").unwrap(),
391 RawValue::from_ron("a").unwrap()
392 );
393
394 assert_eq!(
395 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 assert_eq!(
400 ron::Options::default()
401 .with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME)
402 .to_string(&Some(Some(RawValue::from_ron("None").unwrap())))
403 .unwrap(),
404 "Some(Some(None))"
405 );
406 assert_eq!(
407 ron::from_str::<Option<Option<&RawValue>>>("Some(Some(None))").unwrap(),
408 Some(Some(RawValue::from_ron("None").unwrap()))
409 );
410 }