]>
git.proxmox.com Git - rustc.git/blob - src/vendor/toml/tests/valid.rs
2 extern crate serde_json
;
4 use toml
::Value
as Toml
;
5 use serde_json
::Value
as Json
;
7 fn to_json(toml
: toml
::Value
) -> Json
{
8 fn doit(s
: &str, json
: Json
) -> Json
{
9 let mut map
= serde_json
::Map
::new();
10 map
.insert("type".to_string(), Json
::String(s
.to_string()));
11 map
.insert("value".to_string(), json
);
16 Toml
::String(s
) => doit("string", Json
::String(s
)),
17 Toml
::Integer(i
) => doit("integer", Json
::String(i
.to_string())),
18 Toml
::Float(f
) => doit("float", Json
::String({
19 let s
= format
!("{:.15}", f
);
20 let s
= format
!("{}", s
.trim_right_matches('
0'
));
21 if s
.ends_with(".") {format!("{}
0", s)} else {s}
23 Toml::Boolean(b) => doit("bool
", Json::String(format!("{}
", b))),
24 Toml::Datetime(s) => doit("datetime
", Json::String(s.to_string())),
26 let is_table = match arr.first() {
27 Some(&Toml::Table(..)) => true,
30 let json = Json::Array(arr.into_iter().map(to_json).collect());
31 if is_table {json} else {doit("array", json)}
33 Toml::Table(table) => {
34 let mut map = serde_json::Map::new();
36 map.insert(k, to_json(v));
43 fn run(toml: &str, json: &str) {
44 println!("parsing
:\n{}
", toml);
45 let toml: Toml = toml.parse().unwrap();
46 let json: Json = json.parse().unwrap();
48 // Assert toml == json
49 let toml_json = to_json(toml.clone());
50 assert!(json == toml_json,
51 "expected
\n{}
\ngot
\n{}
\n",
52 serde_json::to_string_pretty(&json).unwrap(),
53 serde_json::to_string_pretty(&toml_json).unwrap());
56 println!("round trip parse
: {}
", toml);
57 let toml2 = toml.to_string().parse().unwrap();
58 assert_eq!(toml, toml2);
61 macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
63 fn $name() { run($toml, $json); }
67 include_str!("valid
/array
-empty
.toml
"),
68 include_str!("valid
/array
-empty
.json
"));
70 include_str!("valid
/array
-nospaces
.toml
"),
71 include_str!("valid
/array
-nospaces
.json
"));
72 test!(arrays_hetergeneous,
73 include_str!("valid
/arrays
-hetergeneous
.toml
"),
74 include_str!("valid
/arrays
-hetergeneous
.json
"));
76 include_str!("valid
/arrays
.toml
"),
77 include_str!("valid
/arrays
.json
"));
79 include_str!("valid
/arrays
-nested
.toml
"),
80 include_str!("valid
/arrays
-nested
.json
"));
82 include_str!("valid
/empty
.toml
"),
83 include_str!("valid
/empty
.json
"));
85 include_str!("valid
/bool
.toml
"),
86 include_str!("valid
/bool
.json
"));
88 include_str!("valid
/datetime
.toml
"),
89 include_str!("valid
/datetime
.json
"));
91 include_str!("valid
/example
.toml
"),
92 include_str!("valid
/example
.json
"));
94 include_str!("valid
/float
.toml
"),
95 include_str!("valid
/float
.json
"));
96 test!(implicit_and_explicit_after,
97 include_str!("valid
/implicit
-and
-explicit
-after
.toml
"),
98 include_str!("valid
/implicit
-and
-explicit
-after
.json
"));
99 test!(implicit_and_explicit_before,
100 include_str!("valid
/implicit
-and
-explicit
-before
.toml
"),
101 include_str!("valid
/implicit
-and
-explicit
-before
.json
"));
102 test!(implicit_groups,
103 include_str!("valid
/implicit
-groups
.toml
"),
104 include_str!("valid
/implicit
-groups
.json
"));
106 include_str!("valid
/integer
.toml
"),
107 include_str!("valid
/integer
.json
"));
108 test!(key_equals_nospace,
109 include_str!("valid
/key
-equals
-nospace
.toml
"),
110 include_str!("valid
/key
-equals
-nospace
.json
"));
112 include_str!("valid
/key
-space
.toml
"),
113 include_str!("valid
/key
-space
.json
"));
114 test!(key_special_chars,
115 include_str!("valid
/key
-special
-chars
.toml
"),
116 include_str!("valid
/key
-special
-chars
.json
"));
117 test!(key_with_pound,
118 include_str!("valid
/key
-with
-pound
.toml
"),
119 include_str!("valid
/key
-with
-pound
.json
"));
121 include_str!("valid
/long
-float
.toml
"),
122 include_str!("valid
/long
-float
.json
"));
124 include_str!("valid
/long
-integer
.toml
"),
125 include_str!("valid
/long
-integer
.json
"));
126 test!(multiline_string,
127 include_str!("valid
/multiline
-string
.toml
"),
128 include_str!("valid
/multiline
-string
.json
"));
129 test!(raw_multiline_string,
130 include_str!("valid
/raw
-multiline
-string
.toml
"),
131 include_str!("valid
/raw
-multiline
-string
.json
"));
133 include_str!("valid
/raw
-string
.toml
"),
134 include_str!("valid
/raw
-string
.json
"));
136 include_str!("valid
/string
-empty
.toml
"),
137 include_str!("valid
/string
-empty
.json
"));
138 test!(string_escapes,
139 include_str!("valid
/string
-escapes
.toml
"),
140 include_str!("valid
/string
-escapes
.json
"));
142 include_str!("valid
/string
-simple
.toml
"),
143 include_str!("valid
/string
-simple
.json
"));
144 test!(string_with_pound,
145 include_str!("valid
/string
-with
-pound
.toml
"),
146 include_str!("valid
/string
-with
-pound
.json
"));
147 test!(table_array_implicit,
148 include_str!("valid
/table
-array
-implicit
.toml
"),
149 include_str!("valid
/table
-array
-implicit
.json
"));
150 test!(table_array_many,
151 include_str!("valid
/table
-array
-many
.toml
"),
152 include_str!("valid
/table
-array
-many
.json
"));
153 test!(table_array_nest,
154 include_str!("valid
/table
-array
-nest
.toml
"),
155 include_str!("valid
/table
-array
-nest
.json
"));
156 test!(table_array_one,
157 include_str!("valid
/table
-array
-one
.toml
"),
158 include_str!("valid
/table
-array
-one
.json
"));
160 include_str!("valid
/table
-empty
.toml
"),
161 include_str!("valid
/table
-empty
.json
"));
162 test!(table_sub_empty,
163 include_str!("valid
/table
-sub
-empty
.toml
"),
164 include_str!("valid
/table
-sub
-empty
.json
"));
165 test!(table_whitespace,
166 include_str!("valid
/table
-whitespace
.toml
"),
167 include_str!("valid
/table
-whitespace
.json
"));
168 test!(table_with_pound,
169 include_str!("valid
/table
-with
-pound
.toml
"),
170 include_str!("valid
/table
-with
-pound
.json
"));
171 test!(unicode_escape,
172 include_str!("valid
/unicode
-escape
.toml
"),
173 include_str!("valid
/unicode
-escape
.json
"));
174 test!(unicode_literal,
175 include_str!("valid
/unicode
-literal
.toml
"),
176 include_str!("valid
/unicode
-literal
.json
"));
178 include_str!("valid
/hard_example
.toml
"),
179 include_str!("valid
/hard_example
.json
"));
181 include_str!("valid
/example2
.toml
"),
182 include_str!("valid
/example2
.json
"));
184 include_str!("valid
/example
-v0
.3
.0.toml
"),
185 include_str!("valid
/example
-v0
.3
.0.json
"));
187 include_str!("valid
/example
-v0
.4
.0.toml
"),
188 include_str!("valid
/example
-v0
.4
.0.json
"));
190 include_str!("valid
/example
-bom
.toml
"),
191 include_str!("valid
/example
.json
"));