Sanitize macro unit tests

This commit is contained in:
Dirkjan Ochtman 2022-11-23 12:17:45 -08:00
parent 0be0b6cc45
commit 61df3a7835
1 changed files with 72 additions and 111 deletions

View File

@ -711,157 +711,118 @@ mod tests {
use syn::parse_quote; use syn::parse_quote;
#[test] #[test]
#[rustfmt::skip]
fn unit_enum_scalar_ser() {
let input = parse_quote! {
#[xml(scalar)]
pub enum TestEnum {
Foo,
Bar,
Baz = 1,
}
};
assert_eq!(super::ser::to_xml(&input).to_string(),
"impl ToXml for TestEnum { fn serialize < W : :: core :: fmt :: Write + ? :: core :: marker :: Sized > (& self , serializer : & mut instant_xml :: Serializer < W > ,) -> Result < () , instant_xml :: Error > { serializer . write_str (match self { TestEnum :: Foo => \"Foo\" , TestEnum :: Bar => \"Bar\" , TestEnum :: Baz => \"1\" , }) } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Scalar ; }"
)
}
#[test]
#[rustfmt::skip]
fn unit_enum_scalar_de() {
let input = parse_quote! {
#[xml(scalar)]
pub enum TestEnum {
Foo,
Bar,
Baz = 1,
}
};
assert_eq!(super::de::from_xml(&input).to_string(),
"impl < 'xml > FromXml < 'xml > for TestEnum { fn deserialize < 'cx > (deserializer : & 'cx mut :: instant_xml :: Deserializer < 'cx , 'xml >) -> Result < Self , :: instant_xml :: Error > { match deserializer . take_str () { Ok (\"Foo\") => Ok (TestEnum :: Foo) , Ok (\"Bar\") => Ok (TestEnum :: Bar) , Ok (\"1\") => Ok (TestEnum :: Baz) , _ => Err (:: instant_xml :: Error :: UnexpectedValue) } } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Scalar ; }"
)
}
#[test]
#[rustfmt::skip]
fn non_unit_enum_variant_unsupported() { fn non_unit_enum_variant_unsupported() {
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
#[xml(scalar)] #[xml(scalar)]
pub enum TestEnum { pub enum TestEnum {
Foo(String), Foo(String),
Bar, Bar,
Baz Baz
} }
}).to_string().find("compile_error ! { \"only unit enum variants are permitted!\" }").unwrap(); })
.to_string())
.find("compile_error ! { \"only unit enum variants are permitted!\" }")
.unwrap();
} }
#[test] #[test]
#[rustfmt::skip]
fn non_scalar_enums_unsupported() { fn non_scalar_enums_unsupported() {
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
#[xml()] #[xml()]
pub enum TestEnum { pub enum TestEnum {
Foo, Foo,
Bar, Bar,
Baz Baz
} }
}).to_string().find("compile_error ! { \"non-scalar enums are currently unsupported!\" }").unwrap(); })
.to_string())
.find("compile_error ! { \"missing enum mode\" }")
.unwrap();
} }
#[test] #[test]
#[rustfmt::skip]
fn scalar_variant_attribute_not_permitted() { fn scalar_variant_attribute_not_permitted() {
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
#[xml(scalar)] #[xml(scalar)]
pub enum TestEnum { pub enum TestEnum {
Foo, Foo,
Bar, Bar,
#[xml(scalar)] #[xml(scalar)]
Baz Baz
} }
}).to_string().find("compile_error ! { \"attribute 'scalar' is invalid for enum variants\" }").unwrap(); })
.to_string())
.find("compile_error ! { \"only 'rename' attribute is permitted on enum variants\" }")
.unwrap();
} }
#[test] #[test]
#[rustfmt::skip]
fn scalar_discrimintant_must_be_literal() { fn scalar_discrimintant_must_be_literal() {
assert_eq!(None, super::ser::to_xml(&parse_quote! { assert_eq!(
None,
dbg!(super::ser::to_xml(&parse_quote! {
#[xml(scalar)]
pub enum TestEnum {
Foo = 1,
Bar,
Baz
}
})
.to_string())
.find("compile_error ! { \"invalid field discriminant value!\" }")
);
dbg!(super::ser::to_xml(&parse_quote! {
#[xml(scalar)] #[xml(scalar)]
pub enum TestEnum { pub enum TestEnum {
Foo = 1, Foo = 1+1,
Bar, Bar,
Baz Baz
} }
}).to_string().find("compile_error ! { \"invalid field discriminant value!\" }")); })
.to_string())
super::ser::to_xml(&parse_quote! { .find("compile_error ! { \"invalid field discriminant value!\" }")
#[xml(scalar)] .unwrap();
pub enum TestEnum {
Foo = 1+1,
Bar,
Baz
}
}).to_string().find("compile_error ! { \"invalid field discriminant value!\" }").unwrap();
} }
#[test] #[test]
#[rustfmt::skip]
fn struct_rename_all_permitted() {
assert_eq!(super::ser::to_xml(&parse_quote! {
#[xml(rename_all = "UPPERCASE")]
pub struct TestStruct {
field_1: String,
field_2: u8,
}
}).to_string(), "impl ToXml for TestStruct { fn serialize < W : :: core :: fmt :: Write + ? :: core :: marker :: Sized > (& self , serializer : & mut instant_xml :: Serializer < W > ,) -> Result < () , instant_xml :: Error > { let prefix = serializer . write_start (\"TestStruct\" , \"\" , false) ? ; debug_assert_eq ! (prefix , None) ; let mut new = :: instant_xml :: ser :: Context :: < 0usize > :: default () ; new . default_ns = \"\" ; let old = serializer . push (new) ? ; serializer . end_start () ? ; match < String as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_1 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar | :: instant_xml :: Kind :: Vec => { let prefix = serializer . write_start (\"FIELD_1\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_1 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_1\") ? ; } } match < u8 as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_2 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar | :: instant_xml :: Kind :: Vec => { let prefix = serializer . write_start (\"FIELD_2\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_2 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_2\") ? ; } } serializer . write_close (prefix , \"TestStruct\") ? ; serializer . pop (old) ; Ok (()) } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Element (:: instant_xml :: Id { ns : \"\" , name : \"TestStruct\" , }) ; } ;");
}
#[test]
#[rustfmt::skip]
fn scalar_enum_rename_all_permitted() {
assert_eq!(super::ser::to_xml(&parse_quote! {
#[xml(scalar, rename_all = "UPPERCASE")]
pub enum TestEnum {
Foo = 1,
Bar,
Baz
}
}).to_string(), "impl ToXml for TestEnum { fn serialize < W : :: core :: fmt :: Write + ? :: core :: marker :: Sized > (& self , serializer : & mut instant_xml :: Serializer < W > ,) -> Result < () , instant_xml :: Error > { serializer . write_str (match self { TestEnum :: Foo => \"1\" , TestEnum :: Bar => \"BAR\" , TestEnum :: Baz => \"BAZ\" , }) } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Scalar ; }");
}
#[test]
#[rustfmt::skip]
fn rename_all_attribute_not_permitted() { fn rename_all_attribute_not_permitted() {
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
pub struct TestStruct { pub struct TestStruct {
#[xml(rename_all = "UPPERCASE")] #[xml(rename_all = "UPPERCASE")]
field_1: String, field_1: String,
field_2: u8, field_2: u8,
} }
}).to_string().find("compile_error ! { \"attribute 'rename_all' invalid in field xml attribute\" }").unwrap(); })
.to_string())
.find("compile_error ! { \"attribute 'rename_all' invalid in field xml attribute\" }")
.unwrap();
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
#[xml(scalar)] #[xml(scalar)]
pub enum TestEnum { pub enum TestEnum {
Foo = 1, Foo = 1,
Bar, Bar,
#[xml(rename_all = "UPPERCASE")] #[xml(rename_all = "UPPERCASE")]
Baz Baz
} }
}).to_string().find("compile_error ! { \"attribute 'rename_all' invalid in field xml attribute\" }").unwrap(); })
.to_string())
.find("compile_error ! { \"only 'rename' attribute is permitted on enum variants\" }")
.unwrap();
} }
#[test] #[test]
#[rustfmt::skip]
fn bogus_rename_all_not_permitted() { fn bogus_rename_all_not_permitted() {
super::ser::to_xml(&parse_quote! { dbg!(super::ser::to_xml(&parse_quote! {
#[xml(rename_all = "forgetaboutit")] #[xml(rename_all = "forgetaboutit")]
pub struct TestStruct { pub struct TestStruct {
field_1: String, field_1: String,
field_2: u8, field_2: u8,
} }
}).to_string().find("compile_error ! {").unwrap(); })
.to_string())
.find("compile_error ! {")
.unwrap();
} }
} }