Move all internally tagged enum tests of test_macros into a dedicated module

Moved:
- test_internally_tagged_enum_with_untagged_variant             => untagged_variant
- test_internally_tagged_bytes                                  => string_and_bytes mod
- test_internally_tagged_struct_variant_containing_unit_variant => struct_variant_containing_unit_variant
- test_internally_tagged_borrow                                 => borrow
- test_enum_in_internally_tagged_enum                           => newtype_variant_containing_externally_tagged_enum
- test_internally_tagged_newtype_variant_containing_unit_struct => newtype_variant_containing_unit_struct

(review this commit with "ignore whitespace changes" option on)
This commit is contained in:
Mingun 2023-07-27 23:42:58 +05:00
parent aa0654332d
commit 99f905403b

View File

@ -831,10 +831,9 @@ mod internally_tagged_enum {
"unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`", "unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`",
); );
} }
}
#[test] #[test]
fn test_internally_tagged_enum_with_untagged_variant() { fn untagged_variant() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")] #[serde(tag = "kind")]
enum InternallyTagged { enum InternallyTagged {
@ -927,8 +926,9 @@ fn test_internally_tagged_enum_with_untagged_variant() {
); );
} }
#[test] mod string_and_bytes {
fn test_internally_tagged_bytes() { use super::*;
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
enum InternallyTagged { enum InternallyTagged {
@ -941,6 +941,8 @@ fn test_internally_tagged_bytes() {
}, },
} }
#[test]
fn string_from_string() {
assert_de_tokens( assert_de_tokens(
&InternallyTagged::String { &InternallyTagged::String {
string: "\0".to_owned(), string: "\0".to_owned(),
@ -974,7 +976,10 @@ fn test_internally_tagged_bytes() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
#[test]
fn string_from_bytes() {
assert_de_tokens( assert_de_tokens(
&InternallyTagged::String { &InternallyTagged::String {
string: "\0".to_owned(), string: "\0".to_owned(),
@ -1008,7 +1013,10 @@ fn test_internally_tagged_bytes() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
#[test]
fn bytes_from_string() {
assert_de_tokens( assert_de_tokens(
&InternallyTagged::Bytes { bytes: vec![0] }, &InternallyTagged::Bytes { bytes: vec![0] },
&[ &[
@ -1038,7 +1046,10 @@ fn test_internally_tagged_bytes() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
#[test]
fn bytes_from_bytes() {
assert_de_tokens( assert_de_tokens(
&InternallyTagged::Bytes { bytes: vec![0] }, &InternallyTagged::Bytes { bytes: vec![0] },
&[ &[
@ -1068,7 +1079,10 @@ fn test_internally_tagged_bytes() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
#[test]
fn bytes_from_seq() {
assert_de_tokens( assert_de_tokens(
&InternallyTagged::Bytes { bytes: vec![0] }, &InternallyTagged::Bytes { bytes: vec![0] },
&[ &[
@ -1086,9 +1100,10 @@ fn test_internally_tagged_bytes() {
], ],
); );
} }
}
#[test] #[test]
fn test_internally_tagged_struct_variant_containing_unit_variant() { fn struct_variant_containing_unit_variant() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum Level { pub enum Level {
Info, Info,
@ -1154,10 +1169,10 @@ fn test_internally_tagged_struct_variant_containing_unit_variant() {
} }
#[test] #[test]
fn test_internally_tagged_borrow() { fn borrow() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum Input<'a> { enum Input<'a> {
Package { name: &'a str }, Package { name: &'a str },
} }
@ -1177,6 +1192,151 @@ fn test_internally_tagged_borrow() {
); );
} }
#[test]
fn newtype_variant_containing_externally_tagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
enum Outer {
Inner(Inner),
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Inner {
Unit,
Newtype(u8),
Tuple(u8, u8),
Struct { f: u8 },
}
assert_tokens(
&Outer::Inner(Inner::Unit),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Unit"),
Token::Unit,
Token::MapEnd,
],
);
assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Newtype"),
Token::U8(1),
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::tuple_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Tuple"),
Token::TupleStruct {
name: "Tuple",
len: 2,
},
Token::U8(1),
Token::U8(1),
Token::TupleStructEnd,
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Map case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Struct"),
Token::Struct {
name: "Struct",
len: 1,
},
Token::Str("f"),
Token::U8(1),
Token::StructEnd,
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_de_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Struct"),
Token::Seq { len: Some(1) },
Token::U8(1), // f
Token::SeqEnd,
Token::MapEnd,
],
);
}
#[test]
fn newtype_variant_containing_unit_struct() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Info;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "topic")]
enum Message {
Info(Info),
}
assert_tokens(
&Message::Info(Info),
&[
Token::Map { len: Some(1) },
Token::Str("topic"),
Token::Str("Info"),
Token::MapEnd,
],
);
assert_de_tokens(
&Message::Info(Info),
&[
Token::Struct {
name: "Message",
len: 1,
},
Token::Str("topic"),
Token::Str("Info"),
Token::StructEnd,
],
);
assert_de_tokens(
&Message::Info(Info),
&[
Token::Seq { len: Some(1) },
Token::Str("Info"),
Token::SeqEnd,
],
);
}
}
#[test] #[test]
fn test_adjacently_tagged_enum() { fn test_adjacently_tagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
@ -1571,106 +1731,6 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
); );
} }
#[test]
fn test_enum_in_internally_tagged_enum() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
enum Outer {
Inner(Inner),
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Inner {
Unit,
Newtype(u8),
Tuple(u8, u8),
Struct { f: u8 },
}
assert_tokens(
&Outer::Inner(Inner::Unit),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Unit"),
Token::Unit,
Token::MapEnd,
],
);
assert_tokens(
&Outer::Inner(Inner::Newtype(1)),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Newtype"),
Token::U8(1),
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::tuple_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Tuple(1, 1)),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Tuple"),
Token::TupleStruct {
name: "Tuple",
len: 2,
},
Token::U8(1),
Token::U8(1),
Token::TupleStructEnd,
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Map case
// via ContentDeserializer::deserialize_enum
assert_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Struct"),
Token::Struct {
name: "Struct",
len: 1,
},
Token::Str("f"),
Token::U8(1),
Token::StructEnd,
Token::MapEnd,
],
);
// Reaches crate::private::de::content::VariantDeserializer::struct_variant
// Content::Seq case
// via ContentDeserializer::deserialize_enum
assert_de_tokens(
&Outer::Inner(Inner::Struct { f: 1 }),
&[
Token::Map { len: Some(2) },
Token::Str("type"),
Token::Str("Inner"),
Token::Str("Struct"),
Token::Seq { len: Some(1) },
Token::U8(1), // f
Token::SeqEnd,
Token::MapEnd,
],
);
}
#[test] #[test]
fn test_internally_tagged_struct() { fn test_internally_tagged_struct() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
@ -2205,50 +2265,6 @@ fn test_untagged_newtype_variant_containing_unit_struct_not_map() {
); );
} }
#[test]
fn test_internally_tagged_newtype_variant_containing_unit_struct() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Info;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "topic")]
enum Message {
Info(Info),
}
assert_tokens(
&Message::Info(Info),
&[
Token::Map { len: Some(1) },
Token::Str("topic"),
Token::Str("Info"),
Token::MapEnd,
],
);
assert_de_tokens(
&Message::Info(Info),
&[
Token::Struct {
name: "Message",
len: 1,
},
Token::Str("topic"),
Token::Str("Info"),
Token::StructEnd,
],
);
assert_de_tokens(
&Message::Info(Info),
&[
Token::Seq { len: Some(1) },
Token::Str("Info"),
Token::SeqEnd,
],
);
}
#[test] #[test]
fn test_packed_struct_can_derive_serialize() { fn test_packed_struct_can_derive_serialize() {
#[derive(Copy, Clone, Serialize)] #[derive(Copy, Clone, Serialize)]