Give meaningful names to enum variants

This commit is contained in:
Mingun 2023-04-22 20:51:00 +05:00 committed by Mingun
parent 99f905403b
commit da0ed4021d

View File

@ -693,24 +693,24 @@ mod internally_tagged_enum {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
enum InternallyTagged { enum InternallyTagged {
A { a: u8 }, Struct { a: u8 },
B, Unit,
C(BTreeMap<String, String>), NewtypeMap(BTreeMap<String, String>),
D(Newtype), NewtypeNewtype(Newtype),
E(Struct), NewtypeStruct(Struct),
} }
#[test] #[test]
fn struct_() { fn struct_() {
assert_tokens( assert_tokens(
&InternallyTagged::A { a: 1 }, &InternallyTagged::Struct { a: 1 },
&[ &[
Token::Struct { Token::Struct {
name: "InternallyTagged", name: "InternallyTagged",
len: 2, len: 2,
}, },
Token::Str("type"), Token::Str("type"),
Token::Str("A"), Token::Str("Struct"),
Token::Str("a"), Token::Str("a"),
Token::U8(1), Token::U8(1),
Token::StructEnd, Token::StructEnd,
@ -718,10 +718,10 @@ mod internally_tagged_enum {
); );
assert_de_tokens( assert_de_tokens(
&InternallyTagged::A { a: 1 }, &InternallyTagged::Struct { a: 1 },
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::Str("A"), Token::Str("Struct"),
Token::U8(1), Token::U8(1),
Token::SeqEnd, Token::SeqEnd,
], ],
@ -731,32 +731,36 @@ mod internally_tagged_enum {
#[test] #[test]
fn unit() { fn unit() {
assert_tokens( assert_tokens(
&InternallyTagged::B, &InternallyTagged::Unit,
&[ &[
Token::Struct { Token::Struct {
name: "InternallyTagged", name: "InternallyTagged",
len: 1, len: 1,
}, },
Token::Str("type"), Token::Str("type"),
Token::Str("B"), Token::Str("Unit"),
Token::StructEnd, Token::StructEnd,
], ],
); );
assert_de_tokens( assert_de_tokens(
&InternallyTagged::B, &InternallyTagged::Unit,
&[Token::Seq { len: Some(1) }, Token::Str("B"), Token::SeqEnd], &[
Token::Seq { len: Some(1) },
Token::Str("Unit"),
Token::SeqEnd,
],
); );
} }
#[test] #[test]
fn newtype_map() { fn newtype_map() {
assert_tokens( assert_tokens(
&InternallyTagged::C(BTreeMap::new()), &InternallyTagged::NewtypeMap(BTreeMap::new()),
&[ &[
Token::Map { len: Some(1) }, Token::Map { len: Some(1) },
Token::Str("type"), Token::Str("type"),
Token::Str("C"), Token::Str("NewtypeMap"),
Token::MapEnd, Token::MapEnd,
], ],
); );
@ -764,7 +768,7 @@ mod internally_tagged_enum {
assert_de_tokens_error::<InternallyTagged>( assert_de_tokens_error::<InternallyTagged>(
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::Str("C"), Token::Str("NewtypeMap"),
Token::Map { len: Some(0) }, Token::Map { len: Some(0) },
Token::MapEnd, Token::MapEnd,
Token::SeqEnd, Token::SeqEnd,
@ -776,11 +780,11 @@ mod internally_tagged_enum {
#[test] #[test]
fn newtype_newtype() { fn newtype_newtype() {
assert_tokens( assert_tokens(
&InternallyTagged::D(Newtype(BTreeMap::new())), &InternallyTagged::NewtypeNewtype(Newtype(BTreeMap::new())),
&[ &[
Token::Map { len: Some(1) }, Token::Map { len: Some(1) },
Token::Str("type"), Token::Str("type"),
Token::Str("D"), Token::Str("NewtypeNewtype"),
Token::MapEnd, Token::MapEnd,
], ],
); );
@ -789,14 +793,14 @@ mod internally_tagged_enum {
#[test] #[test]
fn newtype_struct() { fn newtype_struct() {
assert_tokens( assert_tokens(
&InternallyTagged::E(Struct { f: 6 }), &InternallyTagged::NewtypeStruct(Struct { f: 6 }),
&[ &[
Token::Struct { Token::Struct {
name: "Struct", name: "Struct",
len: 2, len: 2,
}, },
Token::Str("type"), Token::Str("type"),
Token::Str("E"), Token::Str("NewtypeStruct"),
Token::Str("f"), Token::Str("f"),
Token::U8(6), Token::U8(6),
Token::StructEnd, Token::StructEnd,
@ -804,10 +808,10 @@ mod internally_tagged_enum {
); );
assert_de_tokens( assert_de_tokens(
&InternallyTagged::E(Struct { f: 6 }), &InternallyTagged::NewtypeStruct(Struct { f: 6 }),
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::Str("E"), Token::Str("NewtypeStruct"),
Token::U8(6), Token::U8(6),
Token::SeqEnd, Token::SeqEnd,
], ],
@ -828,7 +832,7 @@ mod internally_tagged_enum {
Token::Str("Z"), Token::Str("Z"),
Token::MapEnd, Token::MapEnd,
], ],
"unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`", "unknown variant `Z`, expected one of `Struct`, `Unit`, `NewtypeMap`, `NewtypeNewtype`, `NewtypeStruct`",
); );
} }