Skip to content

Commit

Permalink
Test special cases that reaches SeqRefDeserializer::deserialize_any l…
Browse files Browse the repository at this point in the history
…en==0 condition

failures (2):
    newtype_enum::empty_struct_from_seq
    newtype_enum::tuple0
  • Loading branch information
Mingun committed Aug 17, 2024
1 parent 94a3d40 commit b742013
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
56 changes: 56 additions & 0 deletions test_suite/tests/test_enum_untagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b742013

Please sign in to comment.