Debian Codegraph @

tests/escape.rs

raw symbols SCIP
1 use std::{char::from_u32, fmt::Debug};
2
3 use ron::{de::from_str, ser::to_string};
4 use serde::{Deserialize, Serialize};
5
6 #[test]
7 fn test_escape_basic() {
8 assert_eq!(to_string(&"\x07").unwrap(), "\"\\u{7}\"");
9
10 assert_eq!(from_str::<String>("\"\\x07\"").unwrap(), "\x07");
11 assert_eq!(from_str::<String>("\"\\u{7}\"").unwrap(), "\x07");
12
13 assert_eq!(from_str::<char>("\'\\x07\'").unwrap(), '\x07');
14 assert_eq!(from_str::<char>("\'\\u{7}\'").unwrap(), '\x07');
15
16 assert_eq!(
17 from_str::<char>("\'\\u{}\'").unwrap_err(),
18 ron::error::SpannedError {
19 code: ron::Error::InvalidEscape("Expected 1-6 digits, got 0 digits in Unicode escape"),
20 span: ron::error::Span {
21 start: ron::error::Position { line: 1, col: 4 },
22 end: ron::error::Position { line: 1, col: 5 },
23 }
24 }
25 );
26
27 assert_eq!(
28 from_str::<char>("\'\\q\'").unwrap_err(),
29 ron::error::SpannedError {
30 code: ron::Error::InvalidEscape("Unknown escape character"),
31 span: ron::error::Span {
32 start: ron::error::Position { line: 1, col: 1 },
33 end: ron::error::Position { line: 1, col: 4 },
34 }
35 }
36 )
37 }
38
39 fn check_same<T>(t: T)
40 where
41 T: Debug + for<'a> Deserialize<'a> + PartialEq + Serialize,
42 {
43 let s: String = to_string(&t).unwrap();
44
45 println!("Serialized: \n\n{}\n\n", s);
46
47 assert_eq!(from_str(&s), Ok(t));
48 }
49
50 #[test]
51 fn test_ascii_10() {
52 check_same("\u{10}".to_owned());
53 }
54
55 #[test]
56 fn test_ascii_chars() {
57 (1..128).flat_map(from_u32).for_each(check_same)
58 }
59
60 #[test]
61 fn test_ascii_string() {
62 let s: String = (1..128).flat_map(from_u32).collect();
63
64 check_same(s);
65 }
66
67 #[test]
68 fn test_non_ascii() {
69 assert_eq!(to_string(&"♠").unwrap(), "\"♠\"");
70 assert_eq!(to_string(&"ß").unwrap(), "\"ß\"");
71 assert_eq!(to_string(&"ä").unwrap(), "\"ä\"");
72 assert_eq!(to_string(&"ö").unwrap(), "\"ö\"");
73 assert_eq!(to_string(&"ü").unwrap(), "\"ü\"");
74 }
75
76 #[test]
77 fn test_chars() {
78 assert_eq!(to_string(&'♠').unwrap(), "'♠'");
79 assert_eq!(to_string(&'ß').unwrap(), "'ß'");
80 assert_eq!(to_string(&'ä').unwrap(), "'ä'");
81 assert_eq!(to_string(&'ö').unwrap(), "'ö'");
82 assert_eq!(to_string(&'ü').unwrap(), "'ü'");
83 assert_eq!(to_string(&'\u{715}').unwrap(), "'\u{715}'");
84 assert_eq!(
85 from_str::<char>("'\u{715}'").unwrap(),
86 from_str("'\\u{715}'").unwrap()
87 );
88 }
89
90 #[test]
91 fn test_nul_in_string() {
92 assert_eq!(
93 from_str("\"Hello\0World!\""),
94 Ok(String::from("Hello\0World!"))
95 );
96
97 check_same("Hello\0World!".to_owned());
98 check_same("Hello\x00World!".to_owned());
99 check_same("Hello\u{0}World!".to_owned());
100 }