Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate and add new tests of adjacently tagged enums #2804

Merged
merged 19 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5e37ade
Move all adjacently tagged enum tests (except flatten) into a dedicat…
Mingun Aug 13, 2023
5a359e1
Group Newtype variant checks in test_adjacently_tagged_enum together
Mingun Aug 13, 2023
bee7470
Split test test_adjacently_tagged_enum into four tests for each variant
Mingun Aug 13, 2023
59628d1
Create only one value for all checks
Mingun Aug 13, 2023
36b9a85
Test deserialization of the serialized unit format for adjacently tag…
Mingun Aug 11, 2024
9d0f811
Remove unnecessary generic
Mingun Aug 11, 2024
5445f17
Reuse AdjacentlyTagged enum in newtype_with_newtype test
Mingun Aug 11, 2024
df07751
Group newtype and newtype_with_newtype tests
Mingun Aug 11, 2024
42e63ff
Reuse AdjacentlyTagged enum in `bytes` test
Mingun Aug 13, 2023
3dc6829
Integrate `bytes` test into `struct_` test
Mingun Aug 13, 2023
a7f0bab
Document fields in internal structs used to deserialize adjacently ta…
Mingun Aug 11, 2024
29dc6c3
Unit: add tests for deserialization from sequence
Mingun Aug 11, 2024
a02da49
Unit: add tests for deserialization from integer tag and content fields
Mingun Aug 11, 2024
6bfe1c4
Unit: add tests for deserialization from bytes tag and content fields
Mingun Aug 16, 2024
d5a9c11
No need to test integer and byte array field names, they already test…
Mingun Aug 16, 2024
b0d651b
Newtype: add tests for deserialization from sequence
Mingun Aug 16, 2024
a94d875
Newtype: move up the test with tag only
Mingun Aug 16, 2024
c383e4f
Tuple: add tests for deserialization from sequence
Mingun Aug 16, 2024
9f72ce6
Struct: add tests for deserialization from sequence
Mingun Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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