1
use ron::{
2
    de::from_str,
3
    ser::{to_string, to_string_pretty, PrettyConfig},
4
};
5

            
6
132
fn test_string_roundtrip(s: &str, config: Option<PrettyConfig>) -> String {
7
132
    let ser = match config {
8
80
        Some(config) => to_string_pretty(s, config),
9
52
        None => to_string(s),
10
    }
11
132
    .unwrap();
12

            
13
132
    let de: String = from_str(&ser).unwrap();
14

            
15
132
    assert_eq!(s, de);
16

            
17
132
    ser
18
132
}
19

            
20
#[test]
21
4
fn test_escaped_string() {
22
4
    let config = Some(PrettyConfig::default());
23

            
24
4
    assert_eq!(test_string_roundtrip("a\nb", None), r#""a\nb""#);
25
4
    assert_eq!(test_string_roundtrip("a\nb", config.clone()), r#""a\nb""#);
26

            
27
4
    assert_eq!(test_string_roundtrip("", None), "\"\"");
28
4
    assert_eq!(test_string_roundtrip("", config.clone()), "\"\"");
29

            
30
4
    assert_eq!(test_string_roundtrip("\"", None), r#""\"""#);
31
4
    assert_eq!(test_string_roundtrip("\"", config.clone()), r#""\"""#);
32

            
33
4
    assert_eq!(test_string_roundtrip("#", None), "\"#\"");
34
4
    assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\"");
35

            
36
4
    assert_eq!(test_string_roundtrip("\"#", None), r##""\"#""##);
37
4
    assert_eq!(test_string_roundtrip("\"#", config.clone()), r##""\"#""##);
38

            
39
4
    assert_eq!(test_string_roundtrip("#\"#", None), r##""#\"#""##);
40
4
    assert_eq!(test_string_roundtrip("#\"#", config.clone()), r##""#\"#""##);
41

            
42
4
    assert_eq!(test_string_roundtrip("#\"##", None), r###""#\"##""###);
43
4
    assert_eq!(test_string_roundtrip("#\"##", config), r###""#\"##""###);
44
4
}
45

            
46
#[test]
47
4
fn test_unescaped_string() {
48
4
    let config = Some(PrettyConfig::default().escape_strings(false));
49

            
50
4
    assert_eq!(test_string_roundtrip("a\nb", config.clone()), "\"a\nb\"");
51
4
    assert_eq!(test_string_roundtrip("", config.clone()), "\"\"");
52
4
    assert_eq!(test_string_roundtrip("\"", config.clone()), "r#\"\"\"#");
53
4
    assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\"");
54
4
    assert_eq!(test_string_roundtrip("\"#", config.clone()), "r##\"\"#\"##");
55
4
    assert_eq!(
56
4
        test_string_roundtrip("#\"#", config.clone()),
57
        "r##\"#\"#\"##"
58
    );
59
4
    assert_eq!(test_string_roundtrip("#\"##", config), "r###\"#\"##\"###");
60
4
}
61

            
62
#[test]
63
4
fn test_escaped_string_matches_escape_debug() {
64
    // Mix runs of plain ASCII with every kind of escaped character.
65
4
    let inputs = [
66
4
        "plain ascii text, no escapes",
67
4
        "tab\there, quote\" apostrophe' backslash\\ end",
68
4
        "\0\u{7}\u{7f}\u{80}\u{a0} control and latin-1",
69
4
        "é中😀 multi-byte between ascii",
70
4
        "e\u{301} combining, \u{200b}\u{feff}\u{2028}\u{e000} invisible",
71
4
        "~ !} edges of the printable range",
72
4
    ];
73

            
74
24
    for s in inputs {
75
24
        let expected = format!(
76
            "\"{}\"",
77
24
            s.chars().flat_map(char::escape_debug).collect::<String>()
78
        );
79
24
        assert_eq!(test_string_roundtrip(s, None), expected);
80
24
        assert_eq!(
81
24
            test_string_roundtrip(s, Some(PrettyConfig::default())),
82
            expected
83
        );
84
    }
85
4
}