File: 425_escape_strings.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 2,085 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use ron::{
    de::from_str,
    ser::{to_string, to_string_pretty, PrettyConfig},
};

fn test_string_roundtrip(s: &str, config: Option<PrettyConfig>) -> String {
    let ser = match config {
        Some(config) => to_string_pretty(s, config),
        None => to_string(s),
    }
    .unwrap();

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

    assert_eq!(s, de);

    ser
}

#[test]
fn test_escaped_string() {
    let config = Some(PrettyConfig::default());

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

    assert_eq!(test_string_roundtrip("", None), "\"\"");
    assert_eq!(test_string_roundtrip("", config.clone()), "\"\"");

    assert_eq!(test_string_roundtrip("\"", None), r#""\"""#);
    assert_eq!(test_string_roundtrip("\"", config.clone()), r#""\"""#);

    assert_eq!(test_string_roundtrip("#", None), "\"#\"");
    assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\"");

    assert_eq!(test_string_roundtrip("\"#", None), r##""\"#""##);
    assert_eq!(test_string_roundtrip("\"#", config.clone()), r##""\"#""##);

    assert_eq!(test_string_roundtrip("#\"#", None), r##""#\"#""##);
    assert_eq!(test_string_roundtrip("#\"#", config.clone()), r##""#\"#""##);

    assert_eq!(test_string_roundtrip("#\"##", None), r###""#\"##""###);
    assert_eq!(test_string_roundtrip("#\"##", config), r###""#\"##""###);
}

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

    assert_eq!(test_string_roundtrip("a\nb", config.clone()), "\"a\nb\"");
    assert_eq!(test_string_roundtrip("", config.clone()), "\"\"");
    assert_eq!(test_string_roundtrip("\"", config.clone()), "r#\"\"\"#");
    assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\"");
    assert_eq!(test_string_roundtrip("\"#", config.clone()), "r##\"\"#\"##");
    assert_eq!(
        test_string_roundtrip("#\"#", config.clone()),
        "r##\"#\"#\"##"
    );
    assert_eq!(test_string_roundtrip("#\"##", config), "r###\"#\"##\"###");
}