diff --git a/Cargo.toml b/Cargo.toml index 560d91d2..512e4367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,8 @@ serde = "1.0.60" yaml-rust = "0.4" [dev-dependencies] +indoc = "1.0" serde_derive = "1.0" -unindent = "0.1" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/tests/test_de.rs b/tests/test_de.rs index 34f48aaa..d4356ebe 100644 --- a/tests/test_de.rs +++ b/tests/test_de.rs @@ -1,9 +1,9 @@ +use indoc::indoc; use serde::serde_if_integer128; use serde_derive::Deserialize; use serde_yaml::Value; use std::collections::BTreeMap; use std::fmt::Debug; -use unindent::unindent; fn test_de(yaml: &str, expected: &T) where @@ -30,16 +30,15 @@ where #[test] fn test_alias() { - let yaml = unindent( - " + let yaml = indoc! {" --- first: &alias 1 second: *alias - third: 3", - ); + third: 3 + "}; let mut expected = BTreeMap::new(); { expected.insert(String::from("first"), 1); @@ -57,12 +56,11 @@ fn test_option() { b: Option, c: Option, } - let yaml = unindent( - " + let yaml = indoc! {" --- b: - c: true", - ); + c: true + "}; let expected = Data { a: None, b: None, @@ -82,8 +80,7 @@ fn test_option_alias() { e: Option, f: Option, } - let yaml = unindent( - " + let yaml = indoc! {" --- none_f: &none_f @@ -110,8 +107,8 @@ fn test_option_alias() { c: *none_b d: *some_f e: *some_s - f: *some_b", - ); + f: *some_b + "}; let expected = Data { a: None, b: None, @@ -135,8 +132,7 @@ fn test_enum_alias() { a: E, b: E, } - let yaml = unindent( - " + let yaml = indoc! {" --- aref: &aref @@ -148,8 +144,8 @@ fn test_enum_alias() { - 2 a: *aref - b: *bref", - ); + b: *bref + "}; let expected = Data { a: E::A, b: E::B(1, 2), @@ -169,12 +165,11 @@ fn test_enum_tag() { a: E, b: E, } - let yaml = unindent( - " + let yaml = indoc! {" --- a: !A foo - b: !B bar", - ); + b: !B bar + "}; let expected = Data { a: E::A("foo".into()), b: E::B("bar".into()), @@ -188,12 +183,11 @@ fn test_number_as_string() { struct Num { value: String, } - let yaml = unindent( - " + let yaml = indoc! {" --- # Cannot be represented as u128 - value: 340282366920938463463374607431768211457", - ); + value: 340282366920938463463374607431768211457 + "}; let expected = Num { value: "340282366920938463463374607431768211457".to_owned(), }; @@ -204,22 +198,20 @@ serde_if_integer128! { #[test] fn test_i128_big() { let expected: i128 = ::std::i64::MIN as i128 - 1; - let yaml = unindent( - " + let yaml = indoc! {" --- - -9223372036854775809", - ); + -9223372036854775809 + "}; assert_eq!(expected, serde_yaml::from_str::(&yaml).unwrap()); } #[test] fn test_u128_big() { let expected: u128 = ::std::u64::MAX as u128 + 1; - let yaml = unindent( - " + let yaml = indoc! {" --- - 18446744073709551616", - ); + 18446744073709551616 + "}; assert_eq!(expected, serde_yaml::from_str::(&yaml).unwrap()); } } @@ -231,12 +223,11 @@ fn test_number_alias_as_string() { version: String, value: String, } - let yaml = unindent( - " + let yaml = indoc! {" --- version: &a 1.10 - value: *a", - ); + value: *a + "}; let expected = Num { version: "1.10".to_owned(), value: "1.10".to_owned(), @@ -250,13 +241,12 @@ fn test_de_mapping() { struct Data { pub substructure: serde_yaml::Mapping, } - let yaml = unindent( - " + let yaml = indoc! {" --- substructure: a: 'foo' - b: 'bar'", - ); + b: 'bar' + "}; let mut expected = Data { substructure: serde_yaml::Mapping::new(), @@ -282,8 +272,7 @@ fn test_bomb() { // This would deserialize an astronomical number of elements if we were // vulnerable. - let yaml = unindent( - " + let yaml = indoc! {" --- a: &a ~ b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] @@ -311,8 +300,8 @@ fn test_bomb() { x: &x [*w,*w,*w,*w,*w,*w,*w,*w,*w] y: &y [*x,*x,*x,*x,*x,*x,*x,*x,*x] z: &z [*y,*y,*y,*y,*y,*y,*y,*y,*y] - expected: string", - ); + expected: string + "}; let expected = Data { expected: "string".to_owned(), diff --git a/tests/test_error.rs b/tests/test_error.rs index 2e2ee255..6c9b222c 100644 --- a/tests/test_error.rs +++ b/tests/test_error.rs @@ -1,6 +1,6 @@ +use indoc::indoc; use serde_derive::Deserialize; use std::fmt::Debug; -use unindent::unindent; fn test_error(yaml: &str, expected: &str) where @@ -12,11 +12,10 @@ where #[test] fn test_incorrect_type() { - let yaml = unindent( - " + let yaml = indoc! {" --- - str", - ); + str + "}; let expected = "invalid type: string \"str\", expected i16 at line 2 column 1"; test_error::(&yaml, expected); } @@ -35,13 +34,12 @@ fn test_incorrect_nested_type() { struct C { d: bool, } - let yaml = unindent( - " + let yaml = indoc! {" --- b: - C: - d: fase", - ); + d: fase + "}; let expected = "b[0].C.d: invalid type: string \"fase\", expected a boolean at line 4 column 10"; test_error::(&yaml, expected); @@ -60,22 +58,20 @@ fn test_missing_field() { v: bool, w: bool, } - let yaml = unindent( - " + let yaml = indoc! {" --- - v: true", - ); + v: true + "}; let expected = "missing field `w` at line 2 column 2"; test_error::(&yaml, expected); } #[test] fn test_unknown_anchor() { - let yaml = unindent( - " + let yaml = indoc! {" --- - *some", - ); + *some + "}; let expected = "while parsing node, found unknown anchor at line 2 column 1"; test_error::(&yaml, expected); } @@ -86,25 +82,23 @@ fn test_ignored_unknown_anchor() { struct Wrapper { c: (), } - let yaml = unindent( - " + let yaml = indoc! {" --- b: [*a] - c: ~", - ); + c: ~ + "}; let expected = "while parsing node, found unknown anchor at line 2 column 5"; test_error::(&yaml, expected); } #[test] fn test_two_documents() { - let yaml = unindent( - " + let yaml = indoc! {" --- 0 --- - 1", - ); + 1 + "}; let expected = "deserializing from YAML containing more than one document is not supported"; test_error::(&yaml, expected); } @@ -115,12 +109,11 @@ fn test_variant_map_wrong_size() { enum E { V(usize), } - let yaml = unindent( - r#" + let yaml = indoc! {r#" --- "V": 16 - "other": 32"#, - ); + "other": 32 + "#}; let expected = "invalid length 2, expected map containing 1 entry"; test_error::(&yaml, expected); } @@ -131,11 +124,10 @@ fn test_variant_not_a_map() { enum E { V(usize), } - let yaml = unindent( - r#" + let yaml = indoc! {r#" --- - - "V""#, - ); + - "V" + "#}; let expected = "invalid type: sequence, expected string or singleton map at line 2 column 1"; test_error::(&yaml, expected); } @@ -146,77 +138,70 @@ fn test_variant_not_string() { enum E { V(bool), } - let yaml = unindent( - r#" + let yaml = indoc! {r#" --- - {}: true"#, - ); + {}: true + "#}; let expected = "invalid type: map, expected variant of enum `E` at line 2 column 1"; test_error::(&yaml, expected); } #[test] fn test_bad_bool() { - let yaml = unindent( - " + let yaml = indoc! {" --- - !!bool str", - ); + !!bool str + "}; let expected = "invalid value: string \"str\", expected a boolean at line 2 column 8"; test_error::(&yaml, expected); } #[test] fn test_bad_int() { - let yaml = unindent( - " + let yaml = indoc! {" --- - !!int str", - ); + !!int str + "}; let expected = "invalid value: string \"str\", expected an integer at line 2 column 7"; test_error::(&yaml, expected); } #[test] fn test_bad_float() { - let yaml = unindent( - " + let yaml = indoc! {" --- - !!float str", - ); + !!float str + "}; let expected = "invalid value: string \"str\", expected a float at line 2 column 9"; test_error::(&yaml, expected); } #[test] fn test_bad_null() { - let yaml = unindent( - " + let yaml = indoc! {" --- - !!null str", - ); + !!null str + "}; let expected = "invalid value: string \"str\", expected null at line 2 column 8"; test_error::<()>(&yaml, expected); } #[test] fn test_short_tuple() { - let yaml = unindent( - " + let yaml = indoc! {" --- - [0, 0]", - ); + [0, 0] + "}; let expected = "invalid length 2, expected a tuple of size 3 at line 2 column 1"; test_error::<(u8, u8, u8)>(&yaml, expected); } #[test] fn test_long_tuple() { - let yaml = unindent( - " + let yaml = indoc! {" --- - [0, 0, 0]", - ); + [0, 0, 0] + "}; let expected = "invalid length 3, expected sequence of 2 elements at line 2 column 1"; test_error::<(u8, u8)>(&yaml, expected); } diff --git a/tests/test_serde.rs b/tests/test_serde.rs index f6ffb3bf..8bdffd62 100644 --- a/tests/test_serde.rs +++ b/tests/test_serde.rs @@ -1,17 +1,18 @@ #![allow(clippy::decimal_literal_representation, clippy::unreadable_literal)] +use indoc::indoc; use serde::serde_if_integer128; use serde_derive::{Deserialize, Serialize}; use serde_yaml::Value; use std::collections::BTreeMap; use std::f64; use std::fmt::Debug; -use unindent::unindent; fn test_serde(thing: &T, yaml: &str) where T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug, { + let yaml = yaml.trim_end_matches('\n'); let serialized = serde_yaml::to_string(&thing).unwrap(); assert_eq!(yaml, serialized); @@ -37,44 +38,40 @@ fn test_default() { #[test] fn test_int() { let thing = 256; - let yaml = unindent( - " + let yaml = indoc! {" --- - 256", - ); + 256 + "}; test_serde(&thing, &yaml); } #[test] fn test_int_max_u64() { let thing = ::std::u64::MAX; - let yaml = unindent( - " + let yaml = indoc! {" --- - 18446744073709551615", - ); + 18446744073709551615 + "}; test_serde(&thing, &yaml); } #[test] fn test_int_min_i64() { let thing = ::std::i64::MIN; - let yaml = unindent( - " + let yaml = indoc! {" --- - -9223372036854775808", - ); + -9223372036854775808 + "}; test_serde(&thing, &yaml); } #[test] fn test_int_max_i64() { let thing = ::std::i64::MAX; - let yaml = unindent( - " + let yaml = indoc! {" --- - 9223372036854775807", - ); + 9223372036854775807 + "}; test_serde(&thing, &yaml); } @@ -82,22 +79,20 @@ serde_if_integer128! { #[test] fn test_i128_small() { let thing: i128 = -256; - let yaml = unindent( - " + let yaml = indoc! {" --- - -256", - ); + -256 + "}; test_serde(&thing, &yaml); } #[test] fn test_u128_small() { let thing: u128 = 256; - let yaml = unindent( - " + let yaml = indoc! {" --- - 256", - ); + 256 + "}; test_serde(&thing, &yaml); } } @@ -105,42 +100,37 @@ serde_if_integer128! { #[test] fn test_float() { let thing = 25.6; - let yaml = unindent( - " + let yaml = indoc! {" --- - 25.6", - ); + 25.6 + "}; test_serde(&thing, &yaml); let thing = 25.; - let yaml = unindent( - " + let yaml = indoc! {" --- - 25.0", - ); + 25.0 + "}; test_serde(&thing, &yaml); let thing = f64::INFINITY; - let yaml = unindent( - " + let yaml = indoc! {" --- - .inf", - ); + .inf + "}; test_serde(&thing, &yaml); let thing = f64::NEG_INFINITY; - let yaml = unindent( - " + let yaml = indoc! {" --- - -.inf", - ); + -.inf + "}; test_serde(&thing, &yaml); - let float: f64 = serde_yaml::from_str(&unindent( - " + let float: f64 = serde_yaml::from_str(&indoc! {" --- - .nan", - )) + .nan + "}) .unwrap(); assert!(float.is_nan()); } @@ -148,13 +138,12 @@ fn test_float() { #[test] fn test_vec() { let thing = vec![1, 2, 3]; - let yaml = unindent( - " + let yaml = indoc! {" --- - 1 - 2 - - 3", - ); + - 3 + "}; test_serde(&thing, &yaml); } @@ -163,12 +152,11 @@ fn test_map() { let mut thing = BTreeMap::new(); thing.insert(String::from("x"), 1); thing.insert(String::from("y"), 2); - let yaml = unindent( - r#" + let yaml = indoc! {" --- x: 1 - y: 2"#, - ); + y: 2 + "}; test_serde(&thing, &yaml); } @@ -185,29 +173,27 @@ fn test_basic_struct() { y: String::from("hi\tquoted"), z: true, }; - let yaml = unindent( - r#" + let yaml = indoc! {r#" --- x: -4 y: "hi\tquoted" - z: true"#, - ); + z: true + "#}; test_serde(&thing, &yaml); } #[test] fn test_nested_vec() { let thing = vec![vec![1, 2, 3], vec![4, 5, 6]]; - let yaml = unindent( - " + let yaml = indoc! {" --- - - 1 - 2 - 3 - - 4 - 5 - - 6", - ); + - 6 + "}; test_serde(&thing, &yaml); } @@ -224,37 +210,34 @@ fn test_nested_struct() { let thing = Outer { inner: Inner { v: 512 }, }; - let yaml = unindent( - r#" + let yaml = indoc! {" --- inner: - v: 512"#, - ); + v: 512 + "}; test_serde(&thing, &yaml); } #[test] fn test_option() { let thing = vec![Some(1), None, Some(3)]; - let yaml = unindent( - " + let yaml = indoc! {" --- - 1 - ~ - - 3", - ); + - 3 + "}; test_serde(&thing, &yaml); } #[test] fn test_unit() { let thing = vec![(), ()]; - let yaml = unindent( - " + let yaml = indoc! {" --- - ~ - - ~", - ); + - ~ + "}; test_serde(&thing, &yaml); } @@ -266,11 +249,10 @@ fn test_unit_variant() { Second, } let thing = Variant::First; - let yaml = unindent( - r#" + let yaml = indoc! {" --- - First"#, - ); + First + "}; test_serde(&thing, &yaml); } @@ -283,11 +265,10 @@ fn test_newtype_struct() { #[derive(Serialize, Deserialize, PartialEq, Debug)] struct NewType(OriginalType); let thing = NewType(OriginalType { v: 1 }); - let yaml = unindent( - r#" + let yaml = indoc! {" --- - v: 1"#, - ); + v: 1 + "}; test_serde(&thing, &yaml); } @@ -298,11 +279,10 @@ fn test_newtype_variant() { Size(usize), } let thing = Variant::Size(127); - let yaml = unindent( - r#" + let yaml = indoc! {" --- - Size: 127"#, - ); + Size: 127 + "}; test_serde(&thing, &yaml); } @@ -313,14 +293,13 @@ fn test_tuple_variant() { Rgb(u8, u8, u8), } let thing = Variant::Rgb(32, 64, 96); - let yaml = unindent( - r#" + let yaml = indoc! {" --- Rgb: - 32 - 64 - - 96"#, - ); + - 96 + "}; test_serde(&thing, &yaml); } @@ -335,14 +314,13 @@ fn test_struct_variant() { g: 64, b: 96, }; - let yaml = unindent( - r#" + let yaml = indoc! {" --- Color: r: 32 g: 64 - b: 96"#, - ); + b: 96 + "}; test_serde(&thing, &yaml); } @@ -367,8 +345,7 @@ fn test_value() { Value::Mapping(Mapping::new()), ]), }; - let yaml = unindent( - r#" + let yaml = indoc! {" --- type: primary config: @@ -377,8 +354,8 @@ fn test_value() { - 65535 - 0.54321 - s - - {}"#, - ); + - {} + "}; test_serde(&thing, &yaml); } @@ -402,13 +379,12 @@ fn test_mapping() { Value::String("bar".to_owned()), ); - let yaml = unindent( - " + let yaml = indoc! {" --- substructure: a: foo - b: bar", - ); + b: bar + "}; test_serde(&thing, &yaml); }