summaryrefslogtreecommitdiffstats
path: root/vendor/write-json/tests/tests.rs
blob: 116c06a26fba27bebfb7939ed23145324a015282 (plain)
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
#[test]
fn smoke() {
    let mut buf = String::new();

    {
        let mut obj = write_json::object(&mut buf);
        obj.string("name", "Peter").number("favorite number", 92.0);
        obj.array("films")
            .string("Drowning By Numbers")
            .string("A Zed & Two Noughts");
        obj.null("suitcase");
    }

    assert_eq!(
        buf,
        r#"{"name":"Peter","favorite number":92,"films":["Drowning By Numbers","A Zed & Two Noughts"],"suitcase":null}"#
    )
}

#[test]
fn string_escaping() {
    let mut buf = String::new();
    {
        write_json::array(&mut buf)
            .string("")
            .string("'")
            .string("\"")
            .string("\\")
            .string("hello world")
            .string(" \r\n\t\\ \\r\\n\\t")
            .string("❤😂")
            .string("\x00\x07\x1F\x20\x7E\x7F\u{80}\u{9f}!");
    }
    let strings = buf.replace(|c: char| "[],".contains(c), "\n");
    let expected = r#"
""
"'"
"\""
"\\"
"hello world"
" \r\n\t\\ \\r\\n\\t"
"❤\u0020"
"\u0000\u0070\u00F1 ~\u00F7\u0008\u00F9!"
"#;

    assert_eq!(strings, expected);
}