From 391cb61dffd51322bd310be7cba8641fe3398c19 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sun, 9 Jul 2017 14:58:48 -0600 Subject: add pretty sting serialization --- tests/pretty.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/pretty.rs (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs new file mode 100644 index 0000000..71c9e63 --- /dev/null +++ b/tests/pretty.rs @@ -0,0 +1,20 @@ +extern crate toml; +extern crate serde; + +use serde::ser::Serialize; + +const example: &str = "\ +[example] +text = ''' +this is the first line +this is the second line +''' +"; + +#[test] +fn test_pretty() { + let value: toml::Value = toml::from_str(example).unwrap(); + let mut result = String::with_capacity(128); + value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap(); + assert_eq!(example, &result); +} -- cgit v1.2.3 From ffdafc4d361df0d6f6063af50863b7f87f4aa46a Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sun, 9 Jul 2017 15:10:36 -0600 Subject: array doesn't break anything... --- tests/pretty.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index 71c9e63..6758cfb 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -3,7 +3,7 @@ extern crate serde; use serde::ser::Serialize; -const example: &str = "\ +const EXAMPLE: &str = "\ [example] text = ''' this is the first line @@ -13,8 +13,8 @@ this is the second line #[test] fn test_pretty() { - let value: toml::Value = toml::from_str(example).unwrap(); + let value: toml::Value = toml::from_str(EXAMPLE).unwrap(); let mut result = String::with_capacity(128); value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap(); - assert_eq!(example, &result); + assert_eq!(EXAMPLE, &result); } -- cgit v1.2.3 From ae6096ab2611750fe44d3092b407d496ab94b0b9 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sun, 9 Jul 2017 15:20:29 -0600 Subject: pretty arrays --- tests/pretty.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index 6758cfb..718ef07 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -5,6 +5,10 @@ use serde::ser::Serialize; const EXAMPLE: &str = "\ [example] +array = [ + \"item 1\", + \"item 2\", +] text = ''' this is the first line this is the second line @@ -16,5 +20,7 @@ fn test_pretty() { let value: toml::Value = toml::from_str(EXAMPLE).unwrap(); let mut result = String::with_capacity(128); value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap(); + println!("example:\n{}", EXAMPLE); + println!("result:\n{}", result); assert_eq!(EXAMPLE, &result); } -- cgit v1.2.3 From ef8af0e52a01f1a7c92bff6f5bebde805e19d022 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Mon, 10 Jul 2017 09:45:59 -0600 Subject: fix for rust 1.15 --- tests/pretty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index 718ef07..f9b6d20 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -3,7 +3,7 @@ extern crate serde; use serde::ser::Serialize; -const EXAMPLE: &str = "\ +const EXAMPLE: &'static str = "\ [example] array = [ \"item 1\", -- cgit v1.2.3 From 11e2baa1d4816a96a38c1ab2546332c83b1beb4f Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Mon, 24 Jul 2017 09:40:14 -0600 Subject: add tests --- tests/pretty.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index f9b6d20..759d1c9 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -3,12 +3,14 @@ extern crate serde; use serde::ser::Serialize; -const EXAMPLE: &'static str = "\ +const PRETTY_STD: &'static str = "\ [example] array = [ \"item 1\", \"item 2\", ] +empty = [] +oneline = \"this has no newlines.\" text = ''' this is the first line this is the second line @@ -16,11 +18,116 @@ this is the second line "; #[test] -fn test_pretty() { - let value: toml::Value = toml::from_str(EXAMPLE).unwrap(); +fn test_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!("example:\n{}", EXAMPLE); - println!("result:\n{}", result); - assert_eq!(EXAMPLE, &result); + 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 = [] +oneline = \"this has no newlines.\" +text = ''' +this is the first line +this is the second line +''' +"; + +#[test] +fn test_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(); + } + 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 test_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 test_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 test_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); } -- cgit v1.2.3 From 766b27e902bf4fc49f131443cd5a247c73cd4e72 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Mon, 24 Jul 2017 09:46:24 -0600 Subject: fix Serializer::pretty_array(false) --- tests/pretty.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index 759d1c9..ae9a839 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -3,6 +3,41 @@ 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 = [ @@ -18,7 +53,7 @@ this is the second line "; #[test] -fn test_pretty_std() { +fn pretty_std() { let toml = PRETTY_STD; let value: toml::Value = toml::from_str(toml).unwrap(); let mut result = String::with_capacity(128); @@ -44,7 +79,7 @@ this is the second line "; #[test] -fn test_pretty_indent_2() { +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); @@ -70,7 +105,7 @@ text = \"\\nthis is the first line\\nthis is the second line\\n\" #[test] /// Test pretty indent when gotten the other way -fn test_pretty_indent_2_other() { +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); @@ -95,7 +130,7 @@ text = \"\\nthis is the first line\\nthis is the second line\\n\" "; #[test] /// Test pretty indent when gotten the other way -fn test_pretty_indent_array_no_comma() { +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); @@ -120,7 +155,7 @@ text = \"\\nthis is the first line\\nthis is the second line\\n\" "; #[test] /// Test pretty indent when gotten the other way -fn test_pretty_no_string() { +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); -- cgit v1.2.3