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
4
fn test_escape_basic() {
8
4
    assert_eq!(to_string(&"\x07").unwrap(), "\"\\u{7}\"");
9

            
10
4
    assert_eq!(from_str::<String>("\"\\x07\"").unwrap(), "\x07");
11
4
    assert_eq!(from_str::<String>("\"\\u{7}\"").unwrap(), "\x07");
12

            
13
4
    assert_eq!(from_str::<char>("\'\\x07\'").unwrap(), '\x07');
14
4
    assert_eq!(from_str::<char>("\'\\u{7}\'").unwrap(), '\x07');
15

            
16
4
    assert_eq!(
17
4
        from_str::<char>("\'\\u{}\'").unwrap_err(),
18
4
        ron::error::SpannedError {
19
4
            code: ron::Error::InvalidEscape("Expected 1-6 digits, got 0 digits in Unicode escape"),
20
4
            position: ron::error::Position { line: 1, col: 5 },
21
4
        }
22
4
    );
23

            
24
4
    assert_eq!(
25
4
        from_str::<char>("\'\\q\'").unwrap_err(),
26
4
        ron::error::SpannedError {
27
4
            code: ron::Error::InvalidEscape("Unknown escape character"),
28
4
            position: ron::error::Position { line: 1, col: 4 },
29
4
        }
30
4
    )
31
4
}
32

            
33
528
fn check_same<T>(t: T)
34
528
where
35
528
    T: Debug + for<'a> Deserialize<'a> + PartialEq + Serialize,
36
528
{
37
528
    let s: String = to_string(&t).unwrap();
38
528

            
39
528
    println!("Serialized: \n\n{}\n\n", s);
40
528

            
41
528
    assert_eq!(from_str(&s), Ok(t));
42
528
}
43

            
44
#[test]
45
4
fn test_ascii_10() {
46
4
    check_same("\u{10}".to_owned());
47
4
}
48

            
49
#[test]
50
4
fn test_ascii_chars() {
51
4
    (1..128).flat_map(from_u32).for_each(check_same)
52
4
}
53

            
54
#[test]
55
4
fn test_ascii_string() {
56
4
    let s: String = (1..128).flat_map(from_u32).collect();
57
4

            
58
4
    check_same(s);
59
4
}
60

            
61
#[test]
62
4
fn test_non_ascii() {
63
4
    assert_eq!(to_string(&"♠").unwrap(), "\"♠\"");
64
4
    assert_eq!(to_string(&"ß").unwrap(), "\"ß\"");
65
4
    assert_eq!(to_string(&"ä").unwrap(), "\"ä\"");
66
4
    assert_eq!(to_string(&"ö").unwrap(), "\"ö\"");
67
4
    assert_eq!(to_string(&"ü").unwrap(), "\"ü\"");
68
4
}
69

            
70
#[test]
71
4
fn test_chars() {
72
4
    assert_eq!(to_string(&'♠').unwrap(), "'♠'");
73
4
    assert_eq!(to_string(&'ß').unwrap(), "'ß'");
74
4
    assert_eq!(to_string(&'ä').unwrap(), "'ä'");
75
4
    assert_eq!(to_string(&'ö').unwrap(), "'ö'");
76
4
    assert_eq!(to_string(&'ü').unwrap(), "'ü'");
77
4
    assert_eq!(to_string(&'\u{715}').unwrap(), "'\u{715}'");
78
4
    assert_eq!(
79
4
        from_str::<char>("'\u{715}'").unwrap(),
80
4
        from_str("'\\u{715}'").unwrap()
81
4
    );
82
4
}
83

            
84
#[test]
85
4
fn test_nul_in_string() {
86
4
    assert_eq!(
87
4
        from_str("\"Hello\0World!\""),
88
4
        Ok(String::from("Hello\0World!"))
89
4
    );
90

            
91
4
    check_same("Hello\0World!".to_owned());
92
4
    check_same("Hello\x00World!".to_owned());
93
4
    check_same("Hello\u{0}World!".to_owned());
94
4
}