Catch wrong field names length in serde_test

This commit is contained in:
David Tolnay 2017-11-12 09:52:39 -08:00
parent 98bb02e9b4
commit 436cafb0a3
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
6 changed files with 44 additions and 23 deletions

View File

@ -215,7 +215,7 @@ where
///
/// assert_de_tokens_error::<S>(
/// &[
/// Token::Struct { name: "S", len: 1 },
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("x"),
/// ],
/// "unknown field `x`, expected `a` or `b`",

View File

@ -352,8 +352,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
V: Visitor<'de>,
{
match self.peek_token() {
Token::Struct { len: n, .. } => {
assert_next_token!(self, Token::Struct { name: name, len: n });
Token::Struct { .. } => {
assert_next_token!(self, Token::Struct { name: name, len: fields.len() });
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
}
Token::Map { .. } => {

View File

@ -425,6 +425,10 @@ pub enum Token {
/// The header of a struct.
///
/// When testing deserialization, the `len` field must match the number of
/// fields that the struct expects to deserialize. This may be different
/// from the number of fields contained in the input tokens.
///
/// After this header are the fields of the struct, followed by `StructEnd`.
///
/// ```rust
@ -461,6 +465,10 @@ pub enum Token {
/// The header of a struct variant of an enum.
///
/// When testing deserialization, the `len` field must match the number of
/// fields that the struct variant expects to deserialize. This may be
/// different from the number of fields contained in the input tokens.
///
/// After this header are the fields of the struct variant, followed by
/// `StructVariantEnd`.
///

View File

@ -135,7 +135,7 @@ fn test_default_struct() {
a5: 123,
},
&[
Token::Struct { name: "DefaultStruct", len: 1 },
Token::Struct { name: "DefaultStruct", len: 3 },
Token::Str("a1"),
Token::I32(1),
@ -309,7 +309,7 @@ fn test_elt_not_deserialize() {
c: NotDeserializeStruct(123),
e: NotDeserializeEnum::Trouble,
},
&[Token::Struct { name: "ContainsNotDeserialize", len: 3 }, Token::StructEnd],
&[Token::Struct { name: "ContainsNotDeserialize", len: 1 }, Token::StructEnd],
);
}
@ -331,7 +331,7 @@ fn test_ignore_unknown() {
a5: 123,
},
&[
Token::Struct { name: "DefaultStruct", len: 5 },
Token::Struct { name: "DefaultStruct", len: 3 },
Token::Str("whoops1"),
Token::I32(2),
@ -359,7 +359,7 @@ fn test_ignore_unknown() {
assert_de_tokens_error::<DenyUnknown>(
&[
Token::Struct { name: "DenyUnknown", len: 2 },
Token::Struct { name: "DenyUnknown", len: 1 },
Token::Str("a1"),
Token::I32(1),

View File

@ -538,7 +538,7 @@ declare_tests! {
Token::MapEnd,
],
Struct { a: 1, b: 2, c: 0 } => &[
Token::Struct { name: "Struct", len: 3 },
Token::Struct { name: "Struct", len: 2 },
Token::Str("a"),
Token::I32(1),
@ -570,7 +570,7 @@ declare_tests! {
Token::MapEnd,
],
Struct { a: 1, b: 2, c: 0 } => &[
Token::Struct { name: "Struct", len: 3 },
Token::Struct { name: "Struct", len: 2 },
Token::Str("a"),
Token::I32(1),
@ -591,7 +591,7 @@ declare_tests! {
Token::StructEnd,
],
StructSkipAll { a: 0 } => &[
Token::Struct { name: "StructSkipAll", len: 1 },
Token::Struct { name: "StructSkipAll", len: 0 },
Token::Str("a"),
Token::I32(1),
@ -608,7 +608,7 @@ declare_tests! {
}
test_struct_default {
StructDefault { a: 50, b: "overwritten".to_string() } => &[
Token::Struct { name: "StructDefault", len: 1 },
Token::Struct { name: "StructDefault", len: 2 },
Token::Str("a"),
Token::I32(50),
@ -617,7 +617,7 @@ declare_tests! {
Token::StructEnd,
],
StructDefault { a: 100, b: "default".to_string() } => &[
Token::Struct { name: "StructDefault", len: 0 },
Token::Struct { name: "StructDefault", len: 2 },
Token::StructEnd,
],
}
@ -954,7 +954,7 @@ fn test_cstr_internal_null_end() {
declare_error_tests! {
test_unknown_field<StructDenyUnknown> {
&[
Token::Struct { name: "StructDenyUnknown", len: 2 },
Token::Struct { name: "StructDenyUnknown", len: 1 },
Token::Str("a"),
Token::I32(0),
@ -964,14 +964,14 @@ declare_error_tests! {
}
test_skipped_field_is_unknown<StructDenyUnknown> {
&[
Token::Struct { name: "StructDenyUnknown", len: 2 },
Token::Struct { name: "StructDenyUnknown", len: 1 },
Token::Str("b"),
],
"unknown field `b`, expected `a`",
}
test_skip_all_deny_unknown<StructSkipAllDenyUnknown> {
&[
Token::Struct { name: "StructSkipAllDenyUnknown", len: 1 },
Token::Struct { name: "StructSkipAllDenyUnknown", len: 0 },
Token::Str("a"),
],
"unknown field `a`, there are no fields",

View File

@ -786,7 +786,7 @@ fn test_adjacently_tagged_enum() {
}
// unit with no content
assert_tokens(
assert_ser_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Struct { name: "AdjacentlyTagged", len: 1 },
@ -798,11 +798,24 @@ fn test_adjacently_tagged_enum() {
],
);
// unit with no content
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("t"),
Token::Str("Unit"),
Token::StructEnd,
],
);
// unit with tag first
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Struct { name: "AdjacentlyTagged", len: 1 },
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("t"),
Token::Str("Unit"),
@ -818,7 +831,7 @@ fn test_adjacently_tagged_enum() {
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Struct { name: "AdjacentlyTagged", len: 1 },
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("c"),
Token::Unit,
@ -834,7 +847,7 @@ fn test_adjacently_tagged_enum() {
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Struct { name: "AdjacentlyTagged", len: 3 },
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("f"),
Token::Unit,
@ -975,7 +988,7 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
assert_de_tokens(
&AdjacentlyTagged::Unit,
&[
Token::Struct { name: "AdjacentlyTagged", len: 2},
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("t"),
Token::Str("Unit"),
@ -989,7 +1002,7 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
assert_de_tokens_error::<AdjacentlyTagged>(
&[
Token::Struct { name: "AdjacentlyTagged", len: 3},
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("t"),
Token::Str("Unit"),
@ -1004,7 +1017,7 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
assert_de_tokens_error::<AdjacentlyTagged>(
&[
Token::Struct { name: "AdjacentlyTagged", len: 3},
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("h"),
],
@ -1013,7 +1026,7 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
assert_de_tokens_error::<AdjacentlyTagged>(
&[
Token::Struct { name: "AdjacentlyTagged", len: 3},
Token::Struct { name: "AdjacentlyTagged", len: 2 },
Token::Str("c"),
Token::Unit,