Skip to content

Commit

Permalink
Merge pull request #2804 from Mingun/adjacently-tagged-tests
Browse files Browse the repository at this point in the history
Consolidate and add new tests of adjacently tagged enums
  • Loading branch information
dtolnay authored Aug 23, 2024
2 parents 1a9ffdb + 9f72ce6 commit 31ca16d
Show file tree
Hide file tree
Showing 4 changed files with 803 additions and 579 deletions.
4 changes: 4 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,9 @@ mod content {

/// Not public API.
pub struct TagOrContentFieldVisitor {
/// Name of the tag field of the adjacently tagged enum
pub tag: &'static str,
/// Name of the content field of the adjacently tagged enum
pub content: &'static str,
}

Expand Down Expand Up @@ -979,7 +981,9 @@ mod content {

/// Not public API.
pub struct TagContentOtherFieldVisitor {
/// Name of the tag field of the adjacently tagged enum
pub tag: &'static str,
/// Name of the content field of the adjacently tagged enum
pub content: &'static str,
}

Expand Down
157 changes: 0 additions & 157 deletions test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,59 +1607,6 @@ fn test_collect_other() {
);
}

#[test]
fn test_adjacently_tagged_enum_bytes() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "t", content = "c")]
enum Data {
A { a: i32 },
}

let data = Data::A { a: 0 };

assert_tokens(
&data,
&[
Token::Struct {
name: "Data",
len: 2,
},
Token::Str("t"),
Token::UnitVariant {
name: "Data",
variant: "A",
},
Token::Str("c"),
Token::Struct { name: "A", len: 1 },
Token::Str("a"),
Token::I32(0),
Token::StructEnd,
Token::StructEnd,
],
);

assert_de_tokens(
&data,
&[
Token::Struct {
name: "Data",
len: 2,
},
Token::Bytes(b"t"),
Token::UnitVariant {
name: "Data",
variant: "A",
},
Token::Bytes(b"c"),
Token::Struct { name: "A", len: 1 },
Token::Str("a"),
Token::I32(0),
Token::StructEnd,
Token::StructEnd,
],
);
}

#[test]
fn test_partially_untagged_enum() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand Down Expand Up @@ -1846,38 +1793,6 @@ fn test_partially_untagged_internally_tagged_enum() {
// TODO test error output
}

#[test]
fn test_partially_untagged_adjacently_tagged_enum() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "t", content = "c")]
enum Data {
A(u32),
B,
#[serde(untagged)]
Var(u32),
}

let data = Data::A(7);

assert_de_tokens(
&data,
&[
Token::Map { len: None },
Token::Str("t"),
Token::Str("A"),
Token::Str("c"),
Token::U32(7),
Token::MapEnd,
],
);

let data = Data::Var(42);

assert_de_tokens(&data, &[Token::U32(42)]);

// TODO test error output
}

#[test]
fn test_transparent_struct() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand Down Expand Up @@ -1980,32 +1895,6 @@ fn test_expecting_message_externally_tagged_enum() {
);
}

#[test]
fn test_expecting_message_adjacently_tagged_enum() {
#[derive(Deserialize)]
#[serde(tag = "tag", content = "content")]
#[serde(expecting = "something strange...")]
enum Enum {
AdjacentlyTagged,
}

assert_de_tokens_error::<Enum>(
&[Token::Str("AdjacentlyTagged")],
r#"invalid type: string "AdjacentlyTagged", expected something strange..."#,
);

assert_de_tokens_error::<Enum>(
&[Token::Map { len: None }, Token::Unit],
r#"invalid type: unit value, expected "tag", "content", or other ignored fields"#,
);

// Check that #[serde(expecting = "...")] doesn't affect variant identifier error message
assert_de_tokens_error::<Enum>(
&[Token::Map { len: None }, Token::Str("tag"), Token::Unit],
"invalid type: unit value, expected variant of enum Enum",
);
}

#[test]
fn test_expecting_message_untagged_tagged_enum() {
#[derive(Deserialize)]
Expand Down Expand Up @@ -2891,52 +2780,6 @@ mod flatten {
mod adjacently_tagged {
use super::*;

#[test]
fn straightforward() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "t", content = "c")]
enum Data {
A {
a: i32,
#[serde(flatten)]
flat: Flat,
},
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Flat {
b: i32,
}

let data = Data::A {
a: 0,
flat: Flat { b: 0 },
};

assert_tokens(
&data,
&[
Token::Struct {
name: "Data",
len: 2,
},
Token::Str("t"),
Token::UnitVariant {
name: "Data",
variant: "A",
},
Token::Str("c"),
Token::Map { len: None },
Token::Str("a"),
Token::I32(0),
Token::Str("b"),
Token::I32(0),
Token::MapEnd,
Token::StructEnd,
],
);
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Flatten {
outer: u32,
Expand Down
Loading

0 comments on commit 31ca16d

Please sign in to comment.