From b742013f0ab20b5b0f32a3356f2997293758e153 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 16:05:34 +0500 Subject: [PATCH] Test special cases that reaches SeqRefDeserializer::deserialize_any len==0 condition failures (2): newtype_enum::empty_struct_from_seq newtype_enum::tuple0 --- serde/src/private/de.rs | 2 + test_suite/tests/test_enum_untagged.rs | 56 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 90d605bf43..274e941c75 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -2184,6 +2184,7 @@ mod content { // test_partially_untagged_enum // test_partially_untagged_enum_desugared // Covered by tests/test_enum_untagged.rs + // newtype_enum::tuple0 // newtype_enum::tuple2 Some(Content::Seq(v)) => { de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor) @@ -2215,6 +2216,7 @@ mod content { } // Covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_seq + // newtype_enum::empty_struct_from_seq Some(Content::Seq(v)) => { de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor) } diff --git a/test_suite/tests/test_enum_untagged.rs b/test_suite/tests/test_enum_untagged.rs index 01ecae9c16..48f50c9c2c 100644 --- a/test_suite/tests/test_enum_untagged.rs +++ b/test_suite/tests/test_enum_untagged.rs @@ -147,8 +147,10 @@ mod newtype_enum { enum Inner { Unit, Newtype(u8), + Tuple0(), Tuple2(u8, u8), Struct { f: u8 }, + EmptyStruct {}, } // Reaches crate::private::de::content::VariantRefDeserializer::unit_variant @@ -178,6 +180,22 @@ mod newtype_enum { ); } + // Reaches crate::private::de::content::VariantRefDeserializer::tuple_variant + #[test] + fn tuple0() { + assert_tokens( + &Outer::Inner(Inner::Tuple0()), + &[ + Token::TupleVariant { + name: "Inner", + variant: "Tuple0", + len: 0, + }, + Token::TupleVariantEnd, + ], + ); + } + // Reaches crate::private::de::content::VariantRefDeserializer::tuple_variant #[test] fn tuple2() { @@ -233,6 +251,44 @@ mod newtype_enum { ], ); } + + // Reaches crate::private::de::content::VariantRefDeserializer::struct_variant + // Content::Map case + // Special case - empty map + #[test] + fn empty_struct_from_map() { + assert_de_tokens( + &Outer::Inner(Inner::EmptyStruct {}), + &[ + Token::Map { len: Some(1) }, + // tag + Token::Str("EmptyStruct"), + // content + Token::Map { len: Some(0) }, + Token::MapEnd, + Token::MapEnd, + ], + ); + } + + // Reaches crate::private::de::content::VariantRefDeserializer::struct_variant + // Content::Seq case + // Special case - empty seq + #[test] + fn empty_struct_from_seq() { + assert_de_tokens( + &Outer::Inner(Inner::EmptyStruct {}), + &[ + Token::Map { len: Some(1) }, + // tag + Token::Str("EmptyStruct"), + // content + Token::Seq { len: Some(0) }, + Token::SeqEnd, + Token::MapEnd, + ], + ); + } } // Reaches crate::private::de::content::ContentRefDeserializer::deserialize_option