From 9548288de5dca4d765eba0755955108009294951 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Thu, 27 Jul 2017 16:37:30 -0600 Subject: fix bugs with pretty --- tests/pretty.rs | 19 +++++++++++++++++++ tests/valid.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index ae9a839..f7d1612 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -166,3 +166,22 @@ fn pretty_no_string() { } assert_eq!(toml, &result); } + +const PRETTY_TRICKY: &'static str = r"[example] +text = ''' +this is the first line +This has a ''\' in it for no reason +this is the third 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); +} diff --git a/tests/valid.rs b/tests/valid.rs index 6a04866..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) => ( -- cgit v1.2.3 From a29c1eeef04eed525d7fe48b8e06ae31c0a60575 Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Thu, 27 Jul 2017 22:44:58 -0600 Subject: make single lines also pretty --- tests/pretty.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index f7d1612..308772d 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -41,11 +41,12 @@ fn disable_pretty() { const PRETTY_STD: &'static str = "\ [example] array = [ - \"item 1\", - \"item 2\", + 'item 1', + 'item 2', ] empty = [] -oneline = \"this has no newlines.\" +one = ['one'] +oneline = 'this has no newlines.' text = ''' this is the first line this is the second line @@ -67,15 +68,21 @@ fn pretty_std() { const PRETTY_INDENT_2: &'static str = "\ [example] array = [ - \"item 1\", - \"item 2\", + 'item 1', + 'item 2', ] empty = [] -oneline = \"this has no newlines.\" +one = ['one'] +oneline = 'this has no newlines.' text = ''' this is the first line this is the second line ''' +three = [ + 'one', + 'two', + 'three', +] "; #[test] @@ -88,6 +95,7 @@ fn pretty_indent_2() { serializer.pretty_array_indent(2); value.serialize(&mut serializer).unwrap(); } + println!(">> Result:\n{}", result); assert_eq!(toml, &result); } @@ -168,6 +176,7 @@ fn pretty_no_string() { } const PRETTY_TRICKY: &'static str = r"[example] +single = '''this is a single line but has '' for no reason''' text = ''' this is the first line This has a ''\' in it for no reason -- cgit v1.2.3 From ca81a4a291930aa25637cf814b2f760f4e9d487d Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Fri, 28 Jul 2017 09:12:21 -0600 Subject: add """ for non-literals with newlines, clean up logic and add tests --- tests/pretty.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/pretty.rs b/tests/pretty.rs index 308772d..cceb815 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -175,14 +175,32 @@ fn pretty_no_string() { assert_eq!(toml, &result); } -const PRETTY_TRICKY: &'static str = r"[example] -single = '''this is a single line but has '' for no reason''' -text = ''' -this is the first line -This has a ''\' in it for no reason -this is the third line +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 third line +""" +"##; #[test] fn pretty_tricky() { -- cgit v1.2.3