| 1 | use ron::{ |
| 2 | de::from_str, |
| 3 | ser::{to_string, to_string_pretty, PrettyConfig}, |
| 4 | }; |
| 5 | |
| 6 | fn test_string_roundtrip(s: &str, config: Option<PrettyConfig>) -> String { |
| 7 | let ser = match config { |
| 8 | Some(config) => to_string_pretty(s, config), |
| 9 | None => to_string(s), |
| 10 | } |
| 11 | .unwrap(); |
| 12 | |
| 13 | let de: String = from_str(&ser).unwrap(); |
| 14 | |
| 15 | assert_eq!(s, de); |
| 16 | |
| 17 | ser |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn test_escaped_string() { |
| 22 | let config = Some(PrettyConfig::default()); |
| 23 | |
| 24 | assert_eq!(test_string_roundtrip("a\nb", None), r#""a\nb""#); |
| 25 | assert_eq!(test_string_roundtrip("a\nb", config.clone()), r#""a\nb""#); |
| 26 | |
| 27 | assert_eq!(test_string_roundtrip("", None), "\"\""); |
| 28 | assert_eq!(test_string_roundtrip("", config.clone()), "\"\""); |
| 29 | |
| 30 | assert_eq!(test_string_roundtrip("\"", None), r#""\"""#); |
| 31 | assert_eq!(test_string_roundtrip("\"", config.clone()), r#""\"""#); |
| 32 | |
| 33 | assert_eq!(test_string_roundtrip("#", None), "\"#\""); |
| 34 | assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\""); |
| 35 | |
| 36 | assert_eq!(test_string_roundtrip("\"#", None), r##""\"#""##); |
| 37 | assert_eq!(test_string_roundtrip("\"#", config.clone()), r##""\"#""##); |
| 38 | |
| 39 | assert_eq!(test_string_roundtrip("#\"#", None), r##""#\"#""##); |
| 40 | assert_eq!(test_string_roundtrip("#\"#", config.clone()), r##""#\"#""##); |
| 41 | |
| 42 | assert_eq!(test_string_roundtrip("#\"##", None), r###""#\"##""###); |
| 43 | assert_eq!(test_string_roundtrip("#\"##", config), r###""#\"##""###); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn test_unescaped_string() { |
| 48 | let config = Some(PrettyConfig::default().escape_strings(false)); |
| 49 | |
| 50 | assert_eq!(test_string_roundtrip("a\nb", config.clone()), "\"a\nb\""); |
| 51 | assert_eq!(test_string_roundtrip("", config.clone()), "\"\""); |
| 52 | assert_eq!(test_string_roundtrip("\"", config.clone()), "r#\"\"\"#"); |
| 53 | assert_eq!(test_string_roundtrip("#", config.clone()), "\"#\""); |
| 54 | assert_eq!(test_string_roundtrip("\"#", config.clone()), "r##\"\"#\"##"); |
| 55 | assert_eq!( |
| 56 | test_string_roundtrip("#\"#", config.clone()), |
| 57 | "r##\"#\"#\"##" |
| 58 | ); |
| 59 | assert_eq!(test_string_roundtrip("#\"##", config), "r###\"#\"##\"###"); |
| 60 | } |