toml-rs/tests/valid.rs

178 lines
6.6 KiB
Rust
Raw Normal View History

2015-01-15 14:44:05 -06:00
extern crate "rustc-serialize" as rustc_serialize;
extern crate toml;
2014-06-20 19:01:38 -05:00
2014-12-21 00:35:14 -06:00
use std::collections::BTreeMap;
2015-01-15 14:44:05 -06:00
use rustc_serialize::json::Json;
2014-06-20 19:01:38 -05:00
2015-01-15 14:44:05 -06:00
use toml::{Parser, Value};
use toml::Value::{Table, Integer, Float, Boolean, Datetime, Array};
2014-06-20 19:01:38 -05:00
2014-12-03 01:57:17 -06:00
fn to_json(toml: Value) -> Json {
fn doit(s: &str, json: Json) -> Json {
2014-12-21 00:35:14 -06:00
let mut map = BTreeMap::new();
map.insert(format!("{}", "type"), Json::String(format!("{}", s)));
map.insert(format!("{}", "value"), json);
2014-12-03 01:57:17 -06:00
Json::Object(map)
2014-06-20 19:01:38 -05:00
}
match toml {
2014-12-03 01:57:17 -06:00
Value::String(s) => doit("string", Json::String(s)),
Integer(i) => doit("integer", Json::String(format!("{}", i))),
2014-12-03 01:57:17 -06:00
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}
2014-06-20 19:01:38 -05:00
})),
Boolean(b) => doit("bool", Json::String(format!("{}", b))),
2014-12-03 01:57:17 -06:00
Datetime(s) => doit("datetime", Json::String(s)),
2014-06-20 19:01:38 -05:00
Array(arr) => {
let is_table = match arr.first() {
2014-06-20 19:01:38 -05:00
Some(&Table(..)) => true,
_ => false,
};
2014-12-03 01:57:17 -06:00
let json = Json::Array(arr.into_iter().map(to_json).collect());
2014-06-20 19:01:38 -05:00
if is_table {json} else {doit("array", json)}
}
2014-12-03 01:57:17 -06:00
Table(table) => Json::Object(table.into_iter().map(|(k, v)| {
2014-06-20 19:01:38 -05:00
(k, to_json(v))
}).collect()),
}
}
fn run(toml: &str, json: &str) {
let mut p = Parser::new(toml);
let table = p.parse();
assert!(p.errors.len() == 0, "had_errors: {:?}",
2014-06-20 19:01:38 -05:00
p.errors.iter().map(|e| {
(e.desc.clone(), toml.slice(e.lo - 5, e.hi + 5))
}).collect::<Vec<(String, &str)>>());
assert!(table.is_some());
let toml = Table(table.unwrap());
let toml_string = format!("{}", toml);
2014-06-20 19:01:38 -05:00
2015-01-15 14:44:05 -06:00
let json = Json::from_str(json).unwrap();
let toml_json = to_json(toml.clone());
2014-06-20 19:01:38 -05:00
assert!(json == toml_json,
"expected\n{}\ngot\n{}\n",
json.pretty(),
toml_json.pretty());
let table2 = Parser::new(&toml_string).parse().unwrap();
// floats are a little lossy
if table2.values().any(|v| v.as_float().is_some()) { return }
assert_eq!(toml, Table(table2));
2014-06-20 19:01:38 -05:00
}
macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
#[test]
fn $name() { run($toml, $json); }
) );
2014-06-20 19:01:38 -05:00
test!(array_empty,
include_str!("valid/array-empty.toml"),
include_str!("valid/array-empty.json"));
2014-06-20 19:01:38 -05:00
test!(array_nospaces,
include_str!("valid/array-nospaces.toml"),
include_str!("valid/array-nospaces.json"));
2014-06-20 19:01:38 -05:00
test!(arrays_hetergeneous,
include_str!("valid/arrays-hetergeneous.toml"),
include_str!("valid/arrays-hetergeneous.json"));
2014-06-20 19:01:38 -05:00
test!(arrays,
include_str!("valid/arrays.toml"),
include_str!("valid/arrays.json"));
2014-06-20 19:01:38 -05:00
test!(arrays_nested,
include_str!("valid/arrays-nested.toml"),
include_str!("valid/arrays-nested.json"));
2014-06-20 19:01:38 -05:00
test!(empty,
include_str!("valid/empty.toml"),
include_str!("valid/empty.json"));
2014-06-20 19:01:38 -05:00
test!(bool,
include_str!("valid/bool.toml"),
include_str!("valid/bool.json"));
2014-06-20 19:01:38 -05:00
test!(datetime,
include_str!("valid/datetime.toml"),
include_str!("valid/datetime.json"));
2014-06-20 19:01:38 -05:00
test!(example,
include_str!("valid/example.toml"),
include_str!("valid/example.json"));
2014-06-20 19:01:38 -05:00
test!(float,
include_str!("valid/float.toml"),
include_str!("valid/float.json"));
2014-06-20 19:01:38 -05:00
test!(implicit_and_explicit_after,
include_str!("valid/implicit-and-explicit-after.toml"),
include_str!("valid/implicit-and-explicit-after.json"));
2014-06-20 19:01:38 -05:00
test!(implicit_and_explicit_before,
include_str!("valid/implicit-and-explicit-before.toml"),
include_str!("valid/implicit-and-explicit-before.json"));
2014-06-20 19:01:38 -05:00
test!(implicit_groups,
include_str!("valid/implicit-groups.toml"),
include_str!("valid/implicit-groups.json"));
2014-06-20 19:01:38 -05:00
test!(integer,
include_str!("valid/integer.toml"),
include_str!("valid/integer.json"));
2014-06-20 19:01:38 -05:00
test!(key_equals_nospace,
include_str!("valid/key-equals-nospace.toml"),
include_str!("valid/key-equals-nospace.json"));
2014-06-20 19:01:38 -05:00
test!(key_special_chars,
include_str!("valid/key-special-chars.toml"),
include_str!("valid/key-special-chars.json"));
2014-06-20 19:01:38 -05:00
test!(key_with_pound,
include_str!("valid/key-with-pound.toml"),
include_str!("valid/key-with-pound.json"));
2014-06-20 19:01:38 -05:00
test!(long_float,
include_str!("valid/long-float.toml"),
include_str!("valid/long-float.json"));
2014-06-20 19:01:38 -05:00
test!(long_integer,
include_str!("valid/long-integer.toml"),
include_str!("valid/long-integer.json"));
test!(string_empty,
include_str!("valid/string-empty.toml"),
include_str!("valid/string-empty.json"));
2014-06-20 19:01:38 -05:00
test!(string_escapes,
include_str!("valid/string-escapes.toml"),
include_str!("valid/string-escapes.json"));
2014-06-20 19:01:38 -05:00
test!(string_simple,
include_str!("valid/string-simple.toml"),
include_str!("valid/string-simple.json"));
2014-06-20 19:01:38 -05:00
test!(string_with_pound,
include_str!("valid/string-with-pound.toml"),
include_str!("valid/string-with-pound.json"));
2014-06-20 19:01:38 -05:00
test!(table_array_implicit,
include_str!("valid/table-array-implicit.toml"),
include_str!("valid/table-array-implicit.json"));
2014-06-20 19:01:38 -05:00
test!(table_array_many,
include_str!("valid/table-array-many.toml"),
include_str!("valid/table-array-many.json"));
2014-06-20 19:01:38 -05:00
test!(table_array_nest,
include_str!("valid/table-array-nest.toml"),
include_str!("valid/table-array-nest.json"));
2014-06-20 19:01:38 -05:00
test!(table_array_one,
include_str!("valid/table-array-one.toml"),
include_str!("valid/table-array-one.json"));
2014-06-20 19:01:38 -05:00
test!(table_empty,
include_str!("valid/table-empty.toml"),
include_str!("valid/table-empty.json"));
2014-06-20 19:01:38 -05:00
test!(table_sub_empty,
include_str!("valid/table-sub-empty.toml"),
include_str!("valid/table-sub-empty.json"));
2014-06-20 19:01:38 -05:00
test!(table_whitespace,
include_str!("valid/table-whitespace.toml"),
include_str!("valid/table-whitespace.json"));
2014-06-20 19:01:38 -05:00
test!(table_with_pound,
include_str!("valid/table-with-pound.toml"),
include_str!("valid/table-with-pound.json"));
2014-06-20 19:01:38 -05:00
test!(unicode_escape,
include_str!("valid/unicode-escape.toml"),
include_str!("valid/unicode-escape.json"));
2014-06-20 19:01:38 -05:00
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"));