aboutsummaryrefslogtreecommitdiff
path: root/tests/valid.rs
blob: b3cc09687c2d100e6bc0ebf781c7441feb7d4a6f (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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
extern crate toml;
extern crate serde_json;

use toml::Value as Toml;
use serde_json::Value as Json;

fn to_json(toml: toml::Value) -> Json {
    fn doit(s: &str, json: Json) -> Json {
        let mut map = serde_json::Map::new();
        map.insert("type".to_string(), Json::String(s.to_string()));
        map.insert("value".to_string(), json);
        Json::Object(map)
    }

    match toml {
        Toml::String(s) => doit("string", Json::String(s)),
        Toml::Integer(i) => doit("integer", Json::String(i.to_string())),
        Toml::Float(f) => doit("float", Json::String({
            let s = format!("{:.15}", f);
            let s = format!("{}", s.trim_right_matches('0'));
            if s.ends_with('.') {format!("{}0", s)} else {s}
        })),
        Toml::Boolean(b) => doit("bool", Json::String(format!("{}", b))),
        Toml::Datetime(s) => doit("datetime", Json::String(s.to_string())),
        Toml::Array(arr) => {
            let is_table = match arr.first() {
                Some(&Toml::Table(..)) => true,
                _ => false,
            };
            let json = Json::Array(arr.into_iter().map(to_json).collect());
            if is_table {json} else {doit("array", json)}
        }
        Toml::Table(table) => {
            let mut map = serde_json::Map::new();
            for (k, v) in table {
                map.insert(k, to_json(v));
            }
            Json::Object(map)
        }
    }
}

fn run(toml: &str, json: &str) {
    println!("parsing:\n{}", toml);
    let toml: Toml = toml.parse().unwrap();
    let json: Json = json.parse().unwrap();

    // Assert toml == json
    let toml_json = to_json(toml.clone());
    assert!(json == toml_json,
            "expected\n{}\ngot\n{}\n",
            serde_json::to_string_pretty(&json).unwrap(),
            serde_json::to_string_pretty(&toml_json).unwrap());

    // Assert round trip
    println!("round trip parse: {}", toml);
    let toml2 = toml.to_string().parse().unwrap();
    assert_eq!(toml, toml2);
}

macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
    #[test]
    fn $name() { run($toml, $json); }
) );

test!(array_empty,
       include_str!("valid/array-empty.toml"),
       include_str!("valid/array-empty.json"));
test!(array_nospaces,
       include_str!("valid/array-nospaces.toml"),
       include_str!("valid/array-nospaces.json"));
test!(arrays_hetergeneous,
       include_str!("valid/arrays-hetergeneous.toml"),
       include_str!("valid/arrays-hetergeneous.json"));
test!(arrays,
       include_str!("valid/arrays.toml"),
       include_str!("valid/arrays.json"));
test!(arrays_nested,
       include_str!("valid/arrays-nested.toml"),
       include_str!("valid/arrays-nested.json"));
test!(empty,
       include_str!("valid/empty.toml"),
       include_str!("valid/empty.json"));
test!(bool,
       include_str!("valid/bool.toml"),
       include_str!("valid/bool.json"));
test!(datetime,
       include_str!("valid/datetime.toml"),
       include_str!("valid/datetime.json"));
test!(example,
       include_str!("valid/example.toml"),
       include_str!("valid/example.json"));
test!(float,
       include_str!("valid/float.toml"),
       include_str!("valid/float.json"));
test!(implicit_and_explicit_after,
       include_str!("valid/implicit-and-explicit-after.toml"),
       include_str!("valid/implicit-and-explicit-after.json"));
test!(implicit_and_explicit_before,
       include_str!("valid/implicit-and-explicit-before.toml"),
       include_str!("valid/implicit-and-explicit-before.json"));
test!(implicit_groups,
       include_str!("valid/implicit-groups.toml"),
       include_str!("valid/implicit-groups.json"));
test!(integer,
       include_str!("valid/integer.toml"),
       include_str!("valid/integer.json"));
test!(key_equals_nospace,
       include_str!("valid/key-equals-nospace.toml"),
       include_str!("valid/key-equals-nospace.json"));
test!(key_space,
       include_str!("valid/key-space.toml"),
       include_str!("valid/key-space.json"));
test!(key_special_chars,
       include_str!("valid/key-special-chars.toml"),
       include_str!("valid/key-special-chars.json"));
test!(key_with_pound,
       include_str!("valid/key-with-pound.toml"),
       include_str!("valid/key-with-pound.json"));
test!(long_float,
       include_str!("valid/long-float.toml"),
       include_str!("valid/long-float.json"));
test!(long_integer,
       include_str!("valid/long-integer.toml"),
       include_str!("valid/long-integer.json"));
test!(multiline_string,
       include_str!("valid/multiline-string.toml"),
       include_str!("valid/multiline-string.json"));
test!(raw_multiline_string,
       include_str!("valid/raw-multiline-string.toml"),
       include_str!("valid/raw-multiline-string.json"));
test!(raw_string,
       include_str!("valid/raw-string.toml"),
       include_str!("valid/raw-string.json"));
test!(string_empty,
       include_str!("valid/string-empty.toml"),
       include_str!("valid/string-empty.json"));
test!(string_escapes,
       include_str!("valid/string-escapes.toml"),
       include_str!("valid/string-escapes.json"));
test!(string_simple,
       include_str!("valid/string-simple.toml"),
       include_str!("valid/string-simple.json"));
test!(string_with_pound,
       include_str!("valid/string-with-pound.toml"),
       include_str!("valid/string-with-pound.json"));
test!(table_array_implicit,
       include_str!("valid/table-array-implicit.toml"),
       include_str!("valid/table-array-implicit.json"));
test!(table_array_many,
       include_str!("valid/table-array-many.toml"),
       include_str!("valid/table-array-many.json"));
test!(table_array_nest,
       include_str!("valid/table-array-nest.toml"),
       include_str!("valid/table-array-nest.json"));
test!(table_array_one,
       include_str!("valid/table-array-one.toml"),
       include_str!("valid/table-array-one.json"));
test!(table_empty,
       include_str!("valid/table-empty.toml"),
       include_str!("valid/table-empty.json"));
test!(table_sub_empty,
       include_str!("valid/table-sub-empty.toml"),
       include_str!("valid/table-sub-empty.json"));
test!(table_whitespace,
       include_str!("valid/table-whitespace.toml"),
       include_str!("valid/table-whitespace.json"));
test!(table_with_pound,
       include_str!("valid/table-with-pound.toml"),
       include_str!("valid/table-with-pound.json"));
test!(unicode_escape,
       include_str!("valid/unicode-escape.toml"),
       include_str!("valid/unicode-escape.json"));
test!(unicode_literal,
       include_str!("valid/unicode-literal.toml"),
       include_str!("valid/unicode-literal.json"));
test!(hard_example,
       include_str!("valid/hard_example.toml"),
       include_str!("valid/hard_example.json"));
test!(example2,
       include_str!("valid/example2.toml"),
       include_str!("valid/example2.json"));
test!(example3,
       include_str!("valid/example-v0.3.0.toml"),
       include_str!("valid/example-v0.3.0.json"));
test!(example4,
       include_str!("valid/example-v0.4.0.toml"),
       include_str!("valid/example-v0.4.0.json"));
test!(example_bom,
       include_str!("valid/example-bom.toml"),
       include_str!("valid/example.json"));

test!(datetime_truncate,
      include_str!("valid/datetime-truncate.toml"),
      include_str!("valid/datetime-truncate.json"));
test!(table_array_nest_no_keys,
      include_str!("valid/table-array-nest-no-keys.toml"),
      include_str!("valid/table-array-nest-no-keys.json"));