File: test_round_trip.rs

package info (click to toggle)
rust-yaml-rust2 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 552 kB
  • sloc: makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download | duplicates (7)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
extern crate yaml_rust2;

use yaml_rust2::{Yaml, YamlEmitter, YamlLoader};

fn roundtrip(original: &Yaml) {
    let mut emitted = String::new();
    YamlEmitter::new(&mut emitted).dump(original).unwrap();

    let documents = YamlLoader::load_from_str(&emitted).unwrap();
    println!("emitted {emitted}");

    assert_eq!(documents.len(), 1);
    assert_eq!(documents[0], *original);
}

fn roundtrip_multiline(original: &Yaml) {
    let mut emitted = String::new();
    let mut emitter = YamlEmitter::new(&mut emitted);
    emitter.multiline_strings(true);
    emitter.dump(original).unwrap();

    let documents = YamlLoader::load_from_str(&emitted).unwrap();
    println!("emitted {emitted}");

    assert_eq!(documents.len(), 1);
    assert_eq!(documents[0], *original);
}

fn double_roundtrip(original: &str) {
    let parsed = YamlLoader::load_from_str(original).unwrap();

    let mut serialized = String::new();
    YamlEmitter::new(&mut serialized).dump(&parsed[0]).unwrap();

    let reparsed = YamlLoader::load_from_str(&serialized).unwrap();

    assert_eq!(parsed, reparsed);
}

#[test]
fn test_escape_character() {
    let y = Yaml::String("\x1b".to_owned());
    roundtrip(&y);
}

#[test]
fn test_colon_in_string() {
    let y = Yaml::String("x: %".to_owned());
    roundtrip(&y);
}

#[test]
fn test_numberlike_strings() {
    let docs = [
        r#"x: "1234""#,
        r#"x: "01234""#,
        r#""1234""#,
        r#""01234""#,
        r#"" 01234""#,
        r#""0x1234""#,
        r#"" 0x1234""#,
    ];

    for doc in &docs {
        roundtrip(&Yaml::String((*doc).to_string()));
        double_roundtrip(doc);
    }
}

/// Example from <https://github.com/chyh1990/yaml-rust/issues/133>
#[test]
fn test_issue133() {
    let doc = YamlLoader::load_from_str("\"0x123\"")
        .unwrap()
        .pop()
        .unwrap();
    assert_eq!(doc, Yaml::String("0x123".to_string()));

    let mut out_str = String::new();
    YamlEmitter::new(&mut out_str).dump(&doc).unwrap();
    let doc2 = YamlLoader::load_from_str(&out_str).unwrap().pop().unwrap();
    assert_eq!(doc, doc2); // This failed because the type has changed to a number now
}

#[test]
fn test_newline() {
    let y = Yaml::Array(vec![Yaml::String("\n".to_owned())]);
    roundtrip(&y);
}

#[test]
fn test_crlf() {
    let y = Yaml::Array(vec![Yaml::String("\r\n".to_owned())]);
    roundtrip(&y);
}

#[test]
fn test_multiline_noline() {
    let y = Yaml::Array(vec![Yaml::String("a".to_owned())]);
    roundtrip_multiline(&y);
}

#[test]
fn test_multiline_inner_newline() {
    let y = Yaml::Array(vec![Yaml::String("a\nb".to_owned())]);
    roundtrip_multiline(&y);
}

#[test]
fn test_multiline_trailing_newline() {
    let y = Yaml::Array(vec![Yaml::String("a\n".to_owned())]);
    roundtrip_multiline(&y);
}

#[test]
fn test_multiline_leading_newline() {
    let y = Yaml::Array(vec![Yaml::String("\na".to_owned())]);
    roundtrip_multiline(&y);
}