aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/formatting.rs1
-rw-r--r--tests/pretty.rs308
-rw-r--r--tests/serde.rs51
-rw-r--r--tests/valid.rs58
-rw-r--r--tests/valid/table-multi-empty.json5
-rw-r--r--tests/valid/table-multi-empty.toml5
6 files changed, 422 insertions, 6 deletions
diff --git a/tests/formatting.rs b/tests/formatting.rs
index 10fb165..4ba1418 100644
--- a/tests/formatting.rs
+++ b/tests/formatting.rs
@@ -1,4 +1,3 @@
-extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;
diff --git a/tests/pretty.rs b/tests/pretty.rs
new file mode 100644
index 0000000..19ed22d
--- /dev/null
+++ b/tests/pretty.rs
@@ -0,0 +1,308 @@
+extern crate toml;
+extern crate serde;
+
+use serde::ser::Serialize;
+
+const NO_PRETTY: &'static str = "\
+[example]
+array = [\"item 1\", \"item 2\"]
+empty = []
+oneline = \"this has no newlines.\"
+text = \"\\nthis is the first line\\nthis is the second line\\n\"
+";
+
+#[test]
+fn no_pretty() {
+ let toml = NO_PRETTY;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ value.serialize(&mut toml::Serializer::new(&mut result)).unwrap();
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+#[test]
+fn disable_pretty() {
+ let toml = NO_PRETTY;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_string(false);
+ serializer.pretty_array(false);
+ value.serialize(&mut serializer).unwrap();
+ }
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+const PRETTY_STD: &'static str = "\
+[example]
+array = [
+ 'item 1',
+ 'item 2',
+]
+empty = []
+one = ['one']
+oneline = 'this has no newlines.'
+text = '''
+this is the first line
+this is the second line
+'''
+";
+
+#[test]
+fn pretty_std() {
+ let toml = PRETTY_STD;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap();
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+
+const PRETTY_INDENT_2: &'static str = "\
+[example]
+array = [
+ 'item 1',
+ 'item 2',
+]
+empty = []
+one = ['one']
+oneline = 'this has no newlines.'
+text = '''
+this is the first line
+this is the second line
+'''
+three = [
+ 'one',
+ 'two',
+ 'three',
+]
+";
+
+#[test]
+fn pretty_indent_2() {
+ let toml = PRETTY_INDENT_2;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_array_indent(2);
+ value.serialize(&mut serializer).unwrap();
+ }
+ println!(">> Result:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+const PRETTY_INDENT_2_OTHER: &'static str = "\
+[example]
+array = [
+ \"item 1\",
+ \"item 2\",
+]
+empty = []
+oneline = \"this has no newlines.\"
+text = \"\\nthis is the first line\\nthis is the second line\\n\"
+";
+
+
+#[test]
+/// Test pretty indent when gotten the other way
+fn pretty_indent_2_other() {
+ let toml = PRETTY_INDENT_2_OTHER;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::new(&mut result);
+ serializer.pretty_array_indent(2);
+ value.serialize(&mut serializer).unwrap();
+ }
+ assert_eq!(toml, &result);
+}
+
+
+const PRETTY_ARRAY_NO_COMMA: &'static str = "\
+[example]
+array = [
+ \"item 1\",
+ \"item 2\"
+]
+empty = []
+oneline = \"this has no newlines.\"
+text = \"\\nthis is the first line\\nthis is the second line\\n\"
+";
+#[test]
+/// Test pretty indent when gotten the other way
+fn pretty_indent_array_no_comma() {
+ let toml = PRETTY_ARRAY_NO_COMMA;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::new(&mut result);
+ serializer.pretty_array_trailing_comma(false);
+ value.serialize(&mut serializer).unwrap();
+ }
+ assert_eq!(toml, &result);
+}
+
+
+const PRETTY_NO_STRING: &'static str = "\
+[example]
+array = [
+ \"item 1\",
+ \"item 2\",
+]
+empty = []
+oneline = \"this has no newlines.\"
+text = \"\\nthis is the first line\\nthis is the second line\\n\"
+";
+#[test]
+/// Test pretty indent when gotten the other way
+fn pretty_no_string() {
+ let toml = PRETTY_NO_STRING;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_string(false);
+ value.serialize(&mut serializer).unwrap();
+ }
+ assert_eq!(toml, &result);
+}
+
+const PRETTY_TRICKY: &'static str = r##"[example]
+f = "\f"
+glass = '''
+Nothing too unusual, except that I can eat glass in:
+- Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα.
+- Polish: Mogę jeść szkło, i mi nie szkodzi.
+- Hindi: मैं काँच खा सकता हूँ, मुझे उस से कोई पीडा नहीं होती.
+- Japanese: 私はガラスを食べられます。それは私を傷つけません。
+'''
+r = "\r"
+r_newline = """
+\r
+"""
+single = '''this is a single line but has '' cuz it's tricky'''
+single_tricky = "single line with ''' in it"
+tabs = '''
+this is pretty standard
+ except for some tabs right here
+'''
+text = """
+this is the first line.
+This has a ''' in it and \"\"\" cuz it's tricky yo
+Also ' and \" because why not
+this is the fourth line
+"""
+"##;
+
+#[test]
+fn pretty_tricky() {
+ let toml = PRETTY_TRICKY;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap();
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+const PRETTY_TABLE_ARRAY: &'static str = r##"[[array]]
+key = 'foo'
+
+[[array]]
+key = 'bar'
+
+[abc]
+doc = 'this is a table'
+
+[example]
+single = 'this is a single line string'
+"##;
+
+#[test]
+fn pretty_table_array() {
+ let toml = PRETTY_TABLE_ARRAY;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap();
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+const TABLE_ARRAY: &'static str = r##"[[array]]
+key = "foo"
+
+[[array]]
+key = "bar"
+
+[abc]
+doc = "this is a table"
+
+[example]
+single = "this is a single line string"
+"##;
+
+#[test]
+fn table_array() {
+ let toml = TABLE_ARRAY;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ value.serialize(&mut toml::Serializer::new(&mut result)).unwrap();
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
+
+const PRETTY_TRICKY_NON_LITERAL: &'static str = r##"[example]
+f = "\f"
+glass = """
+Nothing too unusual, except that I can eat glass in:
+- Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα.
+- Polish: Mogę jeść szkło, i mi nie szkodzi.
+- Hindi: मैं काँच खा सकता हूँ, मुझे उस से कोई पीडा नहीं होती.
+- Japanese: 私はガラスを食べられます。それは私を傷つけません。
+"""
+plain = """
+This has a couple of lines
+Because it likes to.
+"""
+r = "\r"
+r_newline = """
+\r
+"""
+single = "this is a single line but has '' cuz it's tricky"
+single_tricky = "single line with ''' in it"
+tabs = """
+this is pretty standard
+\texcept for some \ttabs right here
+"""
+text = """
+this is the first line.
+This has a ''' in it and \"\"\" cuz it's tricky yo
+Also ' and \" because why not
+this is the fourth line
+"""
+"##;
+
+#[test]
+fn pretty_tricky_non_literal() {
+ let toml = PRETTY_TRICKY_NON_LITERAL;
+ let value: toml::Value = toml::from_str(toml).unwrap();
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_string_literal(false);
+ value.serialize(&mut serializer).unwrap();
+ }
+ println!("EXPECTED:\n{}", toml);
+ println!("\nRESULT:\n{}", result);
+ assert_eq!(toml, &result);
+}
diff --git a/tests/serde.rs b/tests/serde.rs
index 3ae2bbd..57fa5db 100644
--- a/tests/serde.rs
+++ b/tests/serde.rs
@@ -525,3 +525,54 @@ fn newtypes() {
Table(map! { b: Integer(2) }),
}
}
+
+#[test]
+fn newtypes2() {
+ #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
+ struct A {
+ b: B
+ }
+
+ #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
+ struct B(Option<C>);
+
+ #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
+ struct C {
+ x: u32,
+ y: u32,
+ z: u32
+ }
+
+ equivalent! {
+ A { b: B(Some(C { x: 0, y: 1, z: 2 })) },
+ Table(map! {
+ b: Table(map! {
+ x: Integer(0),
+ y: Integer(1),
+ z: Integer(2)
+ })
+ }),
+ }
+}
+
+#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
+struct CanBeEmpty {
+ a: Option<String>,
+ b: Option<String>,
+}
+
+#[test]
+fn table_structs_empty() {
+ let text = "[bar]\n\n[baz]\n\n[bazv]\na = \"foo\"\n\n[foo]\n";
+ let value: BTreeMap<String, CanBeEmpty> = toml::from_str(text).unwrap();
+ let mut expected: BTreeMap<String, CanBeEmpty> = BTreeMap::new();
+ expected.insert("bar".to_string(), CanBeEmpty::default());
+ expected.insert("baz".to_string(), CanBeEmpty::default());
+ expected.insert(
+ "bazv".to_string(),
+ CanBeEmpty {a: Some("foo".to_string()), b: None},
+ );
+ expected.insert("foo".to_string(), CanBeEmpty::default());
+ assert_eq!(value, expected);
+ assert_eq!(toml::to_string(&value).unwrap(), text);
+}
diff --git a/tests/valid.rs b/tests/valid.rs
index e7577ad..b186800 100644
--- a/tests/valid.rs
+++ b/tests/valid.rs
@@ -1,7 +1,9 @@
extern crate toml;
+extern crate serde;
extern crate serde_json;
-use toml::Value as Toml;
+use toml::{Value as Toml, to_string_pretty};
+use serde::ser::Serialize;
use serde_json::Value as Json;
fn to_json(toml: toml::Value) -> Json {
@@ -40,10 +42,52 @@ fn to_json(toml: toml::Value) -> Json {
}
}
-fn run(toml: &str, json: &str) {
- println!("parsing:\n{}", toml);
- let toml: Toml = toml.parse().unwrap();
- let json: Json = json.parse().unwrap();
+fn run_pretty(toml: Toml) {
+ // Assert toml == json
+ println!("### pretty round trip parse.");
+
+ // standard pretty
+ let toml_raw = to_string_pretty(&toml).expect("to string");
+ let toml2 = toml_raw.parse().expect("from string");
+ assert_eq!(toml, toml2);
+
+ // pretty with indent 2
+ let mut result = String::with_capacity(128);
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_array_indent(2);
+ toml.serialize(&mut serializer).expect("to string");
+ }
+ assert_eq!(toml, result.parse().expect("from str"));
+ result.clear();
+ {
+ let mut serializer = toml::Serializer::new(&mut result);
+ serializer.pretty_array_trailing_comma(false);
+ toml.serialize(&mut serializer).expect("to string");
+ }
+ assert_eq!(toml, result.parse().expect("from str"));
+ result.clear();
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_string(false);
+ toml.serialize(&mut serializer).expect("to string");
+ assert_eq!(toml, toml2);
+ }
+ assert_eq!(toml, result.parse().expect("from str"));
+ result.clear();
+ {
+ let mut serializer = toml::Serializer::pretty(&mut result);
+ serializer.pretty_array(false);
+ toml.serialize(&mut serializer).expect("to string");
+ assert_eq!(toml, toml2);
+ }
+ assert_eq!(toml, result.parse().expect("from str"));
+}
+
+fn run(toml_raw: &str, json_raw: &str) {
+ println!("parsing:\n{}", toml_raw);
+ let toml: Toml = toml_raw.parse().unwrap();
+ let json: Json = json_raw.parse().unwrap();
// Assert toml == json
let toml_json = to_json(toml.clone());
@@ -56,6 +100,7 @@ fn run(toml: &str, json: &str) {
println!("round trip parse: {}", toml);
let toml2 = toml.to_string().parse().unwrap();
assert_eq!(toml, toml2);
+ run_pretty(toml);
}
macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
@@ -162,6 +207,9 @@ test!(table_empty,
test!(table_sub_empty,
include_str!("valid/table-sub-empty.toml"),
include_str!("valid/table-sub-empty.json"));
+test!(table_multi_empty,
+ include_str!("valid/table-multi-empty.toml"),
+ include_str!("valid/table-multi-empty.json"));
test!(table_whitespace,
include_str!("valid/table-whitespace.toml"),
include_str!("valid/table-whitespace.json"));
diff --git a/tests/valid/table-multi-empty.json b/tests/valid/table-multi-empty.json
new file mode 100644
index 0000000..a6e17c9
--- /dev/null
+++ b/tests/valid/table-multi-empty.json
@@ -0,0 +1,5 @@
+{
+ "a": { "b": {} },
+ "b": {},
+ "c": { "a": {} }
+}
diff --git a/tests/valid/table-multi-empty.toml b/tests/valid/table-multi-empty.toml
new file mode 100644
index 0000000..2266ed2
--- /dev/null
+++ b/tests/valid/table-multi-empty.toml
@@ -0,0 +1,5 @@
+[a]
+[a.b]
+[b]
+[c]
+[c.a]