Skip to content

Commit

Permalink
Split test newtype_enum into four tests for each variant
Browse files Browse the repository at this point in the history
(review this commit with "ignore whitespace changes" option on)
  • Loading branch information
Mingun committed Aug 23, 2024
1 parent 171c6da commit 0093f74
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 46 deletions.
16 changes: 16 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,10 @@ mod content {
fn unit_variant(self) -> Result<(), E> {
match self.value {
Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)),
// Covered by tests/test_annotations.rs
// test_partially_untagged_adjacently_tagged_enum
// Covered by tests/test_enum_untagged.rs
// newtype_enum::unit
None => Ok(()),
}
}
Expand All @@ -2162,6 +2166,11 @@ mod content {
T: de::DeserializeSeed<'de>,
{
match self.value {
// Covered by tests/test_annotations.rs
// test_partially_untagged_enum_desugared
// test_partially_untagged_enum_generic
// Covered by tests/test_enum_untagged.rs
// newtype_enum::newtype
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
None => Err(de::Error::invalid_type(
de::Unexpected::UnitVariant,
Expand All @@ -2175,6 +2184,11 @@ mod content {
V: de::Visitor<'de>,
{
match self.value {
// Covered by tests/test_annotations.rs
// test_partially_untagged_enum
// test_partially_untagged_enum_desugared
// Covered by tests/test_enum_untagged.rs
// newtype_enum::tuple2
Some(Content::Seq(v)) => {
de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
}
Expand All @@ -2198,6 +2212,8 @@ mod content {
V: de::Visitor<'de>,
{
match self.value {
// Covered by tests/test_enum_untagged.rs
// newtype_enum::struct_from_map
Some(Content::Map(v)) => {
de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor)
}
Expand Down
110 changes: 64 additions & 46 deletions test_suite/tests/test_enum_untagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ fn newtype_struct() {
assert_de_tokens(&value, &[Token::U32(5)]);
}

#[test]
fn newtype_enum() {
mod newtype_enum {
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
enum Outer {
Expand All @@ -146,56 +147,73 @@ fn newtype_enum() {
enum Inner {
Unit,
Newtype(u8),
Tuple(u8, u8),
Tuple2(u8, u8),
Struct { f: u8 },
}

assert_tokens(
&Outer::Inner(Inner::Unit),
&[Token::UnitVariant {
name: "Inner",
variant: "Unit",
}],
);

assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::NewtypeVariant {
// Reaches crate::private::de::content::VariantRefDeserializer::unit_variant
#[test]
fn unit() {
assert_tokens(
&Outer::Inner(Inner::Unit),
&[Token::UnitVariant {
name: "Inner",
variant: "Newtype",
},
Token::U8(1),
],
);
variant: "Unit",
}],
);
}

assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
Token::TupleVariant {
name: "Inner",
variant: "Tuple",
len: 2,
},
Token::U8(1),
Token::U8(1),
Token::TupleVariantEnd,
],
);
// Reaches crate::private::de::content::VariantRefDeserializer::newtype_variant_seed
#[test]
fn newtype() {
assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::NewtypeVariant {
name: "Inner",
variant: "Newtype",
},
Token::U8(1),
],
);
}

assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::StructVariant {
name: "Inner",
variant: "Struct",
len: 1,
},
Token::Str("f"),
Token::U8(1),
Token::StructVariantEnd,
],
);
// Reaches crate::private::de::content::VariantRefDeserializer::tuple_variant
#[test]
fn tuple2() {
assert_tokens(
&Outer::Inner(Inner::Tuple2(1, 1)),
&[
Token::TupleVariant {
name: "Inner",
variant: "Tuple2",
len: 2,
},
Token::U8(1),
Token::U8(1),
Token::TupleVariantEnd,
],
);
}

// Reaches crate::private::de::content::VariantRefDeserializer::struct_variant
// Content::Map case
#[test]
fn struct_from_map() {
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::StructVariant {
name: "Inner",
variant: "Struct",
len: 1,
},
Token::Str("f"),
Token::U8(1),
Token::StructVariantEnd,
],
);
}
}

// Reaches crate::private::de::content::ContentRefDeserializer::deserialize_option
Expand Down

0 comments on commit 0093f74

Please sign in to comment.