toml-rs/tests/display.rs
Alex Crichton e256931e9b Rewrite crate with serde support from ground up
This commit completely rewrites this crate from the ground up,
supporting serde at the lowest levels as I believe serde support was
intended to do. This is a major change from the previous versions of
this crate, with a summary of changes being:

* Serialization directly to TOML is now supported without going through
  a `Value` first.

* Deserialization directly from TOML is now supported without going
  through a `Value`. Note that due to the TOML format some values still
  are buffered in intermediate memory, but overall this should be at a
  minimum now.

* The API of `Value` was overhauled to match the API of
  `serde_json::Value`. The changes here were to:

  * Add `is_*` accessors
  * Add `get` and `get_mut` for one-field lookups.
  * Implement panicking lookups through `Index`

  The old `index` methods are now gone in favor of `get` and `Index`
  implementations.

* A `Datetime` type has been added to represent a TOML datetime in a
  first-class fashion. Currently this type provides no accessors other
  than a `Display` implementation, but the idea is that this will grow
  support over time for decomposing the date.

* Support for the `rustc-serialize` crate has been dropped, that'll stay
  on the 0.2 and 0.1 release trains.

* This crate no longer supports the detection of unused fields, for that though
  you can use the `serde_ignored` crate on crates.io
2017-02-08 21:21:18 -08:00

98 lines
3 KiB
Rust

extern crate toml;
use std::collections::BTreeMap;
use toml::Value::{String, Integer, Float, Boolean, Array, Table};
macro_rules! map( ($($k:expr => $v:expr),*) => ({
let mut _m = BTreeMap::new();
$(_m.insert($k.to_string(), $v);)*
_m
}) );
#[test]
fn simple_show() {
assert_eq!(String("foo".to_string()).to_string(),
"\"foo\"");
assert_eq!(Integer(10).to_string(),
"10");
assert_eq!(Float(10.0).to_string(),
"10.0");
assert_eq!(Float(2.4).to_string(),
"2.4");
assert_eq!(Boolean(true).to_string(),
"true");
assert_eq!(Array(vec![]).to_string(),
"[]");
assert_eq!(Array(vec![Integer(1), Integer(2)]).to_string(),
"[1, 2]");
}
#[test]
fn table() {
assert_eq!(Table(map! { }).to_string(),
"");
assert_eq!(Table(map! {
"test" => Integer(2),
"test2" => Integer(3) }).to_string(),
"test = 2\ntest2 = 3\n");
assert_eq!(Table(map! {
"test" => Integer(2),
"test2" => Table(map! {
"test" => String("wut".to_string())
})
}).to_string(),
"test = 2\n\
\n\
[test2]\n\
test = \"wut\"\n");
assert_eq!(Table(map! {
"test" => Integer(2),
"test2" => Table(map! {
"test" => String("wut".to_string())
})
}).to_string(),
"test = 2\n\
\n\
[test2]\n\
test = \"wut\"\n");
assert_eq!(Table(map! {
"test" => Integer(2),
"test2" => Array(vec![Table(map! {
"test" => String("wut".to_string())
})])
}).to_string(),
"test = 2\n\
\n\
[[test2]]\n\
test = \"wut\"\n");
assert_eq!(Table(map! {
"foo.bar" => Integer(2),
"foo\"bar" => Integer(2)
}).to_string(),
"\"foo\\\"bar\" = 2\n\
\"foo.bar\" = 2\n");
assert_eq!(Table(map! {
"test" => Integer(2),
"test2" => Array(vec![Table(map! {
"test" => Array(vec![Integer(2)])
})])
}).to_string(),
"test = 2\n\
\n\
[[test2]]\n\
test = [2]\n");
let table = Table(map! {
"test" => Integer(2),
"test2" => Array(vec![Table(map! {
"test" => Array(vec![Array(vec![Integer(2), Integer(3)]),
Array(vec![String("foo".to_string()), String("bar".to_string())])])
})])
});
assert_eq!(table.to_string(),
"test = 2\n\
\n\
[[test2]]\n\
test = [[2, 3], [\"foo\", \"bar\"]]\n");
}