From 4b67205a28bee53762ecd9ad307b6dd1ee80b8a9 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 15 May 2022 19:24:32 +0500 Subject: [PATCH 1/2] Remove duplicated tests Those tests were removed because... serde-migrated: - test_doctype: not testing anything useful - doctype not parsed because it is deprecated technology - test_parse_i64: covered by `serde-de::trivial` tests - test_parse_u64: covered by `serde-de::trivial` tests - test_parse_bool: covered by `serde-de::trivial` tests - test_parse_unit: covered by `serde-de::unit` tests - test_parse_f64: covered by `serde-de::trivial` tests - test_parse_struct: covered by `serde-de::struct_` tests - test_amoskvin: covered by `serde-de::struct_` and `serde-de::seq` tests - test_nicolai86: covered by `serde-de::struct_` and `serde-de::seq` tests. Sequence fields should me marked as `#[serde(default)]` or be wrapped in Option to be able to deserialize from empty list - test_hugo_duncan2: covered by `serde-de::struct_` and `serde-de::seq` tests - test_hugo_duncan: covered by `serde-de::struct_` and `serde-de::unit` tests - test_parse_xml_value: covered by `serde-de::explicit_value` test - test_parse_complexstruct: test 1 just incorrect; tests 2 and 3 covered by `serde-de::unit`, `serde-de::tuple`, and `serde-de::seq` tests - test_parse_attributes: covered by `serde-de::explicit_value`, `serde-de::struct_` and `serde-de::nested_struct` tests - test_parse_hierarchies: covered by `serde-de::struct_`, `serde-de::nested_struct` and `serde-de::seq` tests. Sequence fields should me marked as `#[serde(default)]` or be wrapped in Option to be able to deserialize from empty list - unknown_field: covered by `serde-de::struct_` and `serde-de::seq` tests - futile: covered by `serde-de::trivial` tests - futile2: covered by `serde-de::trivial` tests serde_roundtrip: - basic_struct: covered by `serde-de::struct_` tests - no_contiguous_fields: covered by `serde-de::seq` tests - escapes_in_cdata: covered by `serde-de::trivial::struct::cdata` tests --- tests/serde-migrated.rs | 794 --------------------------------------- tests/serde_roundtrip.rs | 109 ------ 2 files changed, 903 deletions(-) diff --git a/tests/serde-migrated.rs b/tests/serde-migrated.rs index cd07fcb2..82d65963 100644 --- a/tests/serde-migrated.rs +++ b/tests/serde-migrated.rs @@ -22,18 +22,6 @@ struct Simple { d: Option, } -#[derive(PartialEq, Debug, Serialize, Deserialize)] -struct Inner { - a: (), - b: (usize, String, i8), - c: Vec, -} - -#[derive(PartialEq, Debug, Serialize, Deserialize)] -struct Outer { - inner: Option, -} - fn test_parse_ok<'a, T: std::fmt::Debug>(errors: &[(&'a str, T)]) where T: PartialEq + Debug + ser::Serialize + for<'de> de::Deserialize<'de>, @@ -66,15 +54,6 @@ where } } -fn test_parse_invalid<'a, T>(errors: &[&'a str]) -where - T: PartialEq + Debug + ser::Serialize + for<'de> de::Deserialize<'de>, -{ - for &s in errors { - assert!(from_str::(s).is_err()); - } -} - #[test] fn test_namespaces() { #[derive(PartialEq, Serialize, Deserialize, Debug)] @@ -94,82 +73,6 @@ fn test_namespaces() { )]); } -#[test] -fn test_doctype() { - #[derive(PartialEq, Serialize, Deserialize, Debug)] - struct Envelope { - subject: String, - } - - test_parse_ok(&[ - ( - r#" - - - - Reference rates - "#, - Envelope { - subject: "Reference rates".to_string(), - }, - ), - ( - r#" - - - - Reference rates - "#, - Envelope { - subject: "Reference rates".to_string(), - }, - ), - ( - r#" - - - ] > - - Reference rates - "#, - Envelope { - subject: "Reference rates".to_string(), - }, - ), - ]); -} - -// pass in quick-xml because there is no proper doctype support -// -// -// #[test] -// fn test_doctype_fail() { -// #[derive(PartialEq, Serialize, Deserialize, Debug)] -// struct Envelope { -// subject: String, -// } -// -// test_parse_err::(&[ -// r#" -// -// -// > -// -// Reference rates -// "#, -// r#" -// -// -// Reference rates -// -// ]> -// "#, -// ]) -// } - #[test] #[ignore] // FIXME fn test_forwarded_namespace() { @@ -286,103 +189,6 @@ fn test_parse_enum() { ]); } -#[test] -fn test_parse_i64() { - test_parse_ok(&[ - ("0", 0), - ("-2", -2), - ("-1234", -1234), - (" -1234 ", -1234), - ]); -} - -#[test] -fn test_parse_u64() { - test_parse_ok(&[ - ("0", 0), - ("1234", 1234), - (" 1234 ", 1234), - ]); -} - -#[test] -fn test_parse_bool() { - test_parse_ok(&[ - ("true", true), - ("false", false), - (" true ", true), - (" false ", false), - ("1", true), - ("0", false), - ]); - - test_parse_invalid::(&["verum"]); -} - -#[test] -fn test_parse_unit() { - test_parse_ok(&[("", ())]); -} - -#[test] -fn test_parse_f64() { - test_parse_ok(&[ - ("3.0", 3.0f64), - ("3.1", 3.1), - ("-1.2", -1.2), - ("0.4", 0.4), - ("0.4e5", 0.4e5), - ("0.4e15", 0.4e15), - ("0.4e-01", 0.4e-01), // precision troubles - (" 0.4e-01 ", 0.4e-01), - ]); -} - -#[test] -fn test_parse_struct() { - test_parse_ok(&[ - ( - " - abc - - 2 - ", - Simple { - a: (), - b: 2, - c: "abc".to_string(), - d: None, - }, - ), - ( - " - abc - - 2 - ", - Simple { - a: (), - b: 2, - c: "abc".to_string(), - d: None, - }, - ), - ( - " - abc - - 2 - ", - Simple { - a: (), - b: 2, - c: "abc".to_string(), - d: Some("Foo".to_string()), - }, - ), - ]); -} - #[test] fn test_option() { test_parse_ok(&[ @@ -399,486 +205,6 @@ fn test_option_not_trim() { test_parse_ok(&[(" ", Some(" ".to_string()))]); } -#[test] -fn test_amoskvin() { - #[derive(Debug, Deserialize, PartialEq, Serialize)] - struct Root { - foo: Vec, - } - - #[derive(Debug, Deserialize, PartialEq, Serialize)] - struct Foo { - a: String, - b: Option, - } - test_parse_ok(&[( - " - - - Hello - World - - - Hi - -", - Root { - foo: vec![ - Foo { - a: "Hello".to_string(), - b: Some("World".to_string()), - }, - Foo { - a: "Hi".to_string(), - b: None, - }, - ], - }, - )]); -} - -#[test] -#[ignore] // FIXME -fn test_nicolai86() { - #[derive(Serialize, Deserialize, PartialEq, Debug)] - struct TheSender { - name: String, - } - - #[derive(Serialize, Deserialize, PartialEq, Debug)] - struct CurrencyCube { - currency: String, - rate: String, - } - - #[derive(Serialize, Deserialize, PartialEq, Debug)] - #[allow(non_snake_case)] - struct InnerCube { - Cube: Vec, - } - - #[derive(Serialize, Deserialize, PartialEq, Debug)] - #[allow(non_snake_case)] - struct OuterCube { - Cube: Vec, - } - - #[derive(Serialize, Deserialize, PartialEq, Debug)] - #[allow(non_snake_case)] - struct Envelope { - subject: String, - Sender: TheSender, - Cube: OuterCube, - } - test_parse_ok(&[ - ( - r#" - - - Reference rates - - European Central Bank - - - "#, - Envelope { - subject: "Reference rates".to_string(), - Sender: TheSender { - name: "European Central Bank".to_string(), - }, - Cube: OuterCube { Cube: vec![] }, - }, - ), - ( - r#" - - - Reference rates - - European Central Bank - - - - - - "#, - Envelope { - subject: "Reference rates".to_string(), - Sender: TheSender { - name: "European Central Bank".to_string(), - }, - Cube: OuterCube { - Cube: vec![InnerCube { - Cube: vec![ - CurrencyCube { - currency: "GBP".to_string(), - rate: "0.81725".to_string(), - }, - CurrencyCube { - currency: "Latinum".to_string(), - rate: "999999".to_string(), - }, - ], - }], - }, - }, - ), - ]); -} - -#[test] -fn test_hugo_duncan2() { - let s = r#" - - - 8d521e9a-509e-4ef6-bbb7-9f1ac0d49cd1 - - - vpc-ba0d18d8 - available - - - "#; - #[derive(PartialEq, Debug, Serialize, Deserialize)] - #[allow(non_snake_case)] - struct VpcSet { - vpcId: String, - state: String, - } - - #[derive(PartialEq, Debug, Serialize)] - struct ItemVec(Vec); - - impl<'de, T: de::Deserialize<'de>> de::Deserialize<'de> for ItemVec { - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: de::Deserializer<'de>, - { - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct Helper { - item: Vec, - } - let h: Helper<_> = de::Deserialize::deserialize(deserializer)?; - Ok(ItemVec(h.item)) - } - } - #[derive(PartialEq, Debug, Serialize, Deserialize)] - #[allow(non_snake_case)] - struct DescribeVpcsResponse { - requestId: String, - vpcSet: ItemVec, - } - test_parse_ok(&[( - s, - DescribeVpcsResponse { - requestId: "8d521e9a-509e-4ef6-bbb7-9f1ac0d49cd1".to_string(), - vpcSet: ItemVec(vec![VpcSet { - vpcId: "vpc-ba0d18d8".to_string(), - state: "available".to_string(), - }]), - }, - )]); -} - -#[test] -fn test_hugo_duncan() { - let s = " - - - 9474f558-10a5-42e8-84d1-f9ee181fe943 - - - "; - #[derive(PartialEq, Debug, Serialize, Deserialize)] - #[allow(non_snake_case)] - struct DescribeInstancesResponse { - requestId: String, - reservationSet: (), - } - test_parse_ok(&[( - s, - DescribeInstancesResponse { - requestId: "9474f558-10a5-42e8-84d1-f9ee181fe943".to_string(), - reservationSet: (), - }, - )]); -} - -#[test] -fn test_parse_xml_value() { - #[derive(Eq, Debug, PartialEq, Deserialize, Serialize)] - struct Test { - #[serde(rename = "$value")] - myval: String, - } - test_parse_ok(&[( - "abc", - Test { - myval: "abc".to_string(), - }, - )]); -} - -#[test] -#[ignore] // FIXME -fn test_parse_complexstruct() { - test_parse_ok(&[ - ( - " - - 2 - boom - 88 - - ", - Outer { - inner: Some(Inner { - a: (), - b: (2, "boom".to_string(), 88), - c: vec![], - }), - }, - ), - ( - " - - abc - xyz - - 2 - boom - 88 - - ", - Outer { - inner: Some(Inner { - a: (), - b: (2, "boom".to_string(), 88), - c: vec!["abc".to_string(), "xyz".to_string()], - }), - }, - ), - ("", Outer { inner: None }), - ]); -} - -#[test] -fn test_parse_attributes() { - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct A { - a1: String, - #[serde(rename = "$value")] - a2: i32, - } - - test_parse_ok(&[( - r#"42"#, - A { - a1: "What is the answer to the ultimate question?".to_string(), - a2: 42, - }, - )]); - - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct B { - b1: String, - b2: i32, - } - - test_parse_ok(&[( - r#""#, - B { - b1: "What is the answer to the ultimate question?".to_string(), - b2: 42, - }, - )]); - - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct C { - c1: B, - } - - test_parse_ok(&[ - ( - r#""#, - C { - c1: B { - b1: "What is the answer to the ultimate question?".to_string(), - b2: 42, - }, - }, - ), - ( - r#" "#, - C { - c1: B { - b1: "What is the answer to the ultimate question?".to_string(), - b2: 42, - }, - }, - ), - ( - r#" - "#, - C { - c1: B { - b1: "What is the answer to the ultimate question?".to_string(), - b2: 42, - }, - }, - ), - ]); - - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct D { - d1: Option, - } - test_parse_ok(&[( - r#"42"#, - D { - d1: Some(A { - a1: "What is the answer to the ultimate question?".to_string(), - a2: 42, - }), - }, - )]); -} - -#[test] -#[ignore] // FIXME -fn test_parse_hierarchies() { - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct A { - a1: String, - a2: (String, String), - } - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct B { - b1: A, - b2: (A, A), - } - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct C { - c1: B, - c2: Vec, - } - - test_parse_ok(&[ - ( - " - - No - Maybe - Yes - - - Red - Green - Blue - - - London - Berlin - Paris - - ", - C { - c1: B { - b1: A { - a1: "No".to_string(), - a2: ("Maybe".to_string(), "Yes".to_string()), - }, - b2: ( - A { - a1: "Red".to_string(), - a2: ("Green".to_string(), "Blue".to_string()), - }, - A { - a1: "London".to_string(), - a2: ("Berlin".to_string(), "Paris".to_string()), - }, - ), - }, - c2: vec![], - }, - ), - ( - " - - Green - Blue - Red - - - Berlin - Paris - London - - - Maybe - Yes - No - - ", - C { - c1: B { - b1: A { - a1: "No".to_string(), - a2: ("Maybe".to_string(), "Yes".to_string()), - }, - b2: ( - A { - a1: "Red".to_string(), - a2: ("Green".to_string(), "Blue".to_string()), - }, - A { - a1: "London".to_string(), - a2: ("Berlin".to_string(), "Paris".to_string()), - }, - ), - }, - c2: vec![], - }, - ), - ]); -} - -#[test] -fn unknown_field() { - #[derive(Deserialize, Debug, PartialEq, Eq, Serialize)] - struct A { - other: Vec, - } - - #[derive(Deserialize, Debug, PartialEq, Eq, Serialize)] - struct Other { - d: i32, - } - test_parse_ok(&[( - " - - 5 - - - 6 - - ", - A { - other: vec![Other { d: 6 }], - }, - )]); -} - -// #[test] -// fn eoz() { -// use std::io::Read; -// let mut file = std::fs::File::open("Report_test.2.xml").unwrap(); -// let mut s = String::new(); -// file.read_to_string(&mut s).unwrap(); - -// let _xml_value: Element = from_str(&s).unwrap(); -// } - #[test] fn test_parse_unfinished() { test_parse_err::(&[" @@ -892,123 +218,3 @@ fn test_parse_unfinished() { fn test_things_qc_found() { test_parse_err::(&["<\u{0}:/"]); } - -#[test] -fn futile() { - #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] - struct Object { - id: u8, - name: String, - x: u8, - y: u8, - width: u8, - height: u8, - ellipse: Option<()>, - } - - test_parse_ok(&[ - ( - r###" - - - - "###, - Object { - id: 11, - name: "testEllipse".to_owned(), - x: 102, - y: 38, - width: 21, - height: 14, - ellipse: Some(()), - }, - ), - ( - r###" - - - "###, - Object { - id: 11, - name: "testEllipse".to_owned(), - x: 102, - y: 38, - width: 21, - height: 14, - ellipse: None, - }, - ), - ]); -} - -#[test] -fn futile2() { - #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] - struct Null; - - #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] - struct Object { - field: Option, - } - - #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] - struct Stuff { - stuff_field: Option, - } - - test_parse_ok(&[ - ( - r###" - - - - "###, - Object { field: Some(Null) }, - ), - ( - r###" - - - "###, - Object { field: None }, - ), - ]); - - test_parse_ok(&[ - ( - r###" - - - - "###, - Stuff { - stuff_field: Some(Object { field: None }), - }, - ), - ( - r###" - - - - - - "###, - Stuff { - stuff_field: Some(Object { field: Some(Null) }), - }, - ), - ( - r###" - - - "###, - Stuff { stuff_field: None }, - ), - ( - r###" - - "###, - Stuff { stuff_field: None }, - ), - ]); -} diff --git a/tests/serde_roundtrip.rs b/tests/serde_roundtrip.rs index 6cf20948..dc183654 100644 --- a/tests/serde_roundtrip.rs +++ b/tests/serde_roundtrip.rs @@ -3,12 +3,6 @@ use serde::{Deserialize, Serialize}; use pretty_assertions::assert_eq; -#[derive(Debug, Serialize, Deserialize, PartialEq)] -struct Item { - name: String, - source: String, -} - #[derive(Debug, Serialize, Deserialize, PartialEq)] enum Node { Boolean(bool), @@ -22,22 +16,6 @@ struct Nodes { items: Vec, } -#[test] -fn basic_struct() { - let src = r#"BananaStore"#; - let should_be = Item { - name: "Banana".to_string(), - source: "Store".to_string(), - }; - - let item: Item = from_str(src).unwrap(); - assert_eq!(item, should_be); - - let reserialized_item = to_string(&item).unwrap(); - let src = ""; - assert_eq!(src, reserialized_item); -} - #[test] #[ignore] fn round_trip_list_of_enums() { @@ -74,53 +52,6 @@ fn round_trip_list_of_enums() { assert_eq!(deserialized_nodes, nodes); } -#[test] -fn no_contiguous_fields() { - #[derive(Serialize, Deserialize, PartialEq, Debug)] - struct Xml { - #[serde(rename = "$value")] - fields: Vec, - } - - #[derive(Serialize, Deserialize, PartialEq, Debug)] - enum Field { - #[serde(rename = "field1")] - Field1 { name: String }, - #[serde(rename = "field2")] - Field2 { name: String }, - } - - let source = r#" - - - - - -"#; - - let xml: Xml = ::quick_xml::de::from_str(source).unwrap(); - assert_eq!( - xml, - Xml { - fields: vec![ - Field::Field1 { - name: "a".to_string() - }, - Field::Field2 { - name: "b".to_string() - }, - Field::Field1 { - name: "a".to_string() - }, - ], - } - ); - - // TODO: impl Serialize for struct variants - // let serialized = to_string(&xml).unwrap(); - // assert_eq!(serialized, source); -} - #[test] fn test_parse_unflatten_field() { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -140,43 +71,3 @@ fn test_parse_unflatten_field() { let stringified = to_string(&parsed).unwrap(); assert_eq!(&stringified, source); } - -#[test] -fn escapes_in_cdata() { - #[derive(Debug, Deserialize, PartialEq)] - pub struct Protocols { - protocol: Vec, - } - - #[derive(Debug, Deserialize, PartialEq)] - pub struct Protocol { - pub name: String, - pub irp: String, - } - - // this is from https://github.com/bengtmartensson/IrpTransmogrifier/blob/master/src/main/resources/IrpProtocols.xml - // no copyright claimed - let source = r###" - - - - - - (T=1,(7,-6,3,D:4,1:1,T:1,1:2,0:8,F:8,15:4,C:4,-79m,T=0)+){C =(D:4+4*T+9+F:4+F:4:4+15)&15} [D:0..15,F:0..255]]]> - - - "###; - - let protocols: Protocols = from_str(&source).expect("unexpected xml"); - - assert_eq!( - protocols.protocol[0].irp, - r#"{37.3k,268,msb}<-1,1|1,-1>(T=1,(7,-6,3,D:4,1:1,T:1,1:2,0:8,F:8,15:4,C:4,-79m,T=0)+){C =(D:4+4*T+9+F:4+F:4:4+15)&15} [D:0..15,F:0..255]"# - ); -} From f3228919908d322479aa55106b876f3ef7e437f9 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 22 May 2022 23:01:07 +0500 Subject: [PATCH 2/2] Move struct inside test function where it only used --- tests/serde-migrated.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/serde-migrated.rs b/tests/serde-migrated.rs index 82d65963..5dfc7b87 100644 --- a/tests/serde-migrated.rs +++ b/tests/serde-migrated.rs @@ -6,14 +6,6 @@ use serde::{Deserialize, Serialize}; use pretty_assertions::assert_eq; -#[derive(PartialEq, Debug, Serialize, Deserialize)] -enum Animal { - Dog, - Frog(String), - Ant(Simple), - Cat { age: usize, name: String }, -} - #[derive(PartialEq, Debug, Serialize, Deserialize)] struct Simple { a: (), @@ -126,7 +118,15 @@ fn test_parse_string_not_trim() { #[test] #[ignore] // FIXME fn test_parse_enum() { - use self::Animal::*; + use Animal::*; + + #[derive(PartialEq, Debug, Serialize, Deserialize)] + enum Animal { + Dog, + Frog(String), + Ant(Simple), + Cat { age: usize, name: String }, + } test_parse_ok(&[ ("", Dog),