Eliminate macro from deserialization error tests

This commit is contained in:
David Tolnay
2022-01-20 19:54:20 -08:00
parent ab848060f2
commit 1833914346
+251 -154
View File
@@ -60,251 +60,328 @@ enum EnumSkipAll {
Skipped, Skipped,
} }
macro_rules! declare_error_tests {
($(
$(#[$cfg:meta])*
$name:ident<$target:ty> { $tokens:expr, $expected:expr, }
)+) => {
$(
$(#[$cfg])*
#[test] #[test]
fn $name() { fn test_unknown_field() {
assert_de_tokens_error::<$target>($tokens, $expected); assert_de_tokens_error::<StructDenyUnknown>(
}
)+
}
}
declare_error_tests! {
test_unknown_field<StructDenyUnknown> {
&[ &[
Token::Struct { name: "StructDenyUnknown", len: 1 }, Token::Struct {
name: "StructDenyUnknown",
len: 1,
},
Token::Str("a"), Token::Str("a"),
Token::I32(0), Token::I32(0),
Token::Str("d"), Token::Str("d"),
], ],
"unknown field `d`, expected `a`", "unknown field `d`, expected `a`",
);
} }
test_skipped_field_is_unknown<StructDenyUnknown> {
#[test]
fn test_skipped_field_is_unknown() {
assert_de_tokens_error::<StructDenyUnknown>(
&[ &[
Token::Struct { name: "StructDenyUnknown", len: 1 }, Token::Struct {
name: "StructDenyUnknown",
len: 1,
},
Token::Str("b"), Token::Str("b"),
], ],
"unknown field `b`, expected `a`", "unknown field `b`, expected `a`",
);
} }
test_skip_all_deny_unknown<StructSkipAllDenyUnknown> {
#[test]
fn test_skip_all_deny_unknown() {
assert_de_tokens_error::<StructSkipAllDenyUnknown>(
&[ &[
Token::Struct { name: "StructSkipAllDenyUnknown", len: 0 }, Token::Struct {
name: "StructSkipAllDenyUnknown",
len: 0,
},
Token::Str("a"), Token::Str("a"),
], ],
"unknown field `a`, there are no fields", "unknown field `a`, there are no fields",
);
} }
test_unknown_variant<Enum> {
#[test]
fn test_unknown_variant() {
assert_de_tokens_error::<Enum>(
&[ &[
Token::UnitVariant { name: "Enum", variant: "Foo" }, Token::UnitVariant { name: "Enum", variant: "Foo" },
], ],
"unknown variant `Foo`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`", "unknown variant `Foo`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`",
);
} }
test_enum_skipped_variant<Enum> {
#[test]
fn test_enum_skipped_variant() {
assert_de_tokens_error::<Enum>(
&[ &[
Token::UnitVariant { name: "Enum", variant: "Skipped" }, Token::UnitVariant { name: "Enum", variant: "Skipped" },
], ],
"unknown variant `Skipped`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`", "unknown variant `Skipped`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`",
);
} }
test_enum_skip_all<EnumSkipAll> {
&[ #[test]
Token::UnitVariant { name: "EnumSkipAll", variant: "Skipped" }, fn test_enum_skip_all() {
], assert_de_tokens_error::<EnumSkipAll>(
&[Token::UnitVariant {
name: "EnumSkipAll",
variant: "Skipped",
}],
"unknown variant `Skipped`, there are no variants", "unknown variant `Skipped`, there are no variants",
);
} }
test_duplicate_field_struct<Struct> {
#[test]
fn test_duplicate_field_struct() {
assert_de_tokens_error::<Struct>(
&[ &[
Token::Map { len: Some(3) }, Token::Map { len: Some(3) },
Token::Str("a"), Token::Str("a"),
Token::I32(1), Token::I32(1),
Token::Str("a"), Token::Str("a"),
], ],
"duplicate field `a`", "duplicate field `a`",
);
} }
test_duplicate_field_enum<Enum> {
#[test]
fn test_duplicate_field_enum() {
assert_de_tokens_error::<Enum>(
&[ &[
Token::StructVariant { name: "Enum", variant: "Map", len: 3 }, Token::StructVariant {
name: "Enum",
variant: "Map",
len: 3,
},
Token::Str("a"), Token::Str("a"),
Token::I32(1), Token::I32(1),
Token::Str("a"), Token::Str("a"),
], ],
"duplicate field `a`", "duplicate field `a`",
);
} }
test_enum_out_of_range<Enum> {
&[ #[test]
Token::Enum { name: "Enum" }, fn test_enum_out_of_range() {
Token::U32(5), assert_de_tokens_error::<Enum>(
Token::Unit, &[Token::Enum { name: "Enum" }, Token::U32(5), Token::Unit],
],
"invalid value: integer `5`, expected variant index 0 <= i < 5", "invalid value: integer `5`, expected variant index 0 <= i < 5",
);
} }
test_short_tuple<(u8, u8, u8)> {
&[ #[test]
Token::Tuple { len: 1 }, fn test_short_tuple() {
Token::U8(1), assert_de_tokens_error::<(u8, u8, u8)>(
Token::TupleEnd, &[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
],
"invalid length 1, expected a tuple of size 3", "invalid length 1, expected a tuple of size 3",
);
} }
test_short_array<[u8; 3]> {
&[ #[test]
Token::Seq { len: Some(1) }, fn test_short_array() {
Token::U8(1), assert_de_tokens_error::<[u8; 3]>(
Token::SeqEnd, &[Token::Seq { len: Some(1) }, Token::U8(1), Token::SeqEnd],
],
"invalid length 1, expected an array of length 3", "invalid length 1, expected an array of length 3",
);
} }
test_cstring_internal_null<CString> {
&[ #[test]
Token::Bytes(b"a\0c"), fn test_cstring_internal_null() {
], assert_de_tokens_error::<CString>(
&[Token::Bytes(b"a\0c")],
"nul byte found in provided data at position: 1", "nul byte found in provided data at position: 1",
);
} }
test_cstring_internal_null_end<CString> {
&[ #[test]
Token::Bytes(b"ac\0"), fn test_cstring_internal_null_end() {
], assert_de_tokens_error::<CString>(
&[Token::Bytes(b"ac\0")],
"nul byte found in provided data at position: 2", "nul byte found in provided data at position: 2",
);
} }
test_unit_from_empty_seq<()> {
&[ #[test]
Token::Seq { len: Some(0) }, fn test_unit_from_empty_seq() {
Token::SeqEnd, assert_de_tokens_error::<()>(
], &[Token::Seq { len: Some(0) }, Token::SeqEnd],
"invalid type: sequence, expected unit", "invalid type: sequence, expected unit",
);
} }
test_unit_from_empty_seq_without_len<()> {
&[ #[test]
Token::Seq { len: None }, fn test_unit_from_empty_seq_without_len() {
Token::SeqEnd, assert_de_tokens_error::<()>(
], &[Token::Seq { len: None }, Token::SeqEnd],
"invalid type: sequence, expected unit", "invalid type: sequence, expected unit",
);
} }
test_unit_from_tuple_struct<()> {
#[test]
fn test_unit_from_tuple_struct() {
assert_de_tokens_error::<()>(
&[ &[
Token::TupleStruct { name: "Anything", len: 0 }, Token::TupleStruct {
name: "Anything",
len: 0,
},
Token::TupleStructEnd, Token::TupleStructEnd,
], ],
"invalid type: sequence, expected unit", "invalid type: sequence, expected unit",
);
} }
test_string_from_unit<String> {
&[ #[test]
Token::Unit, fn test_string_from_unit() {
], assert_de_tokens_error::<String>(
&[Token::Unit],
"invalid type: unit value, expected a string", "invalid type: unit value, expected a string",
);
} }
test_btreeset_from_unit<BTreeSet<isize>> {
&[ #[test]
Token::Unit, fn test_btreeset_from_unit() {
], assert_de_tokens_error::<BTreeSet<isize>>(
&[Token::Unit],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_btreeset_from_unit_struct<BTreeSet<isize>> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_btreeset_from_unit_struct() {
], assert_de_tokens_error::<BTreeSet<isize>>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_hashset_from_unit<HashSet<isize>> {
&[ #[test]
Token::Unit, fn test_hashset_from_unit() {
], assert_de_tokens_error::<HashSet<isize>>(
&[Token::Unit],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_hashset_from_unit_struct<HashSet<isize>> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_hashset_from_unit_struct() {
], assert_de_tokens_error::<HashSet<isize>>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_vec_from_unit<Vec<isize>> {
&[ #[test]
Token::Unit, fn test_vec_from_unit() {
], assert_de_tokens_error::<Vec<isize>>(
&[Token::Unit],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_vec_from_unit_struct<Vec<isize>> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_vec_from_unit_struct() {
], assert_de_tokens_error::<Vec<isize>>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected a sequence", "invalid type: unit value, expected a sequence",
);
} }
test_zero_array_from_unit<[isize; 0]> {
&[ #[test]
Token::Unit, fn test_zero_array_from_unit() {
], assert_de_tokens_error::<[isize; 0]>(
&[Token::Unit],
"invalid type: unit value, expected an empty array", "invalid type: unit value, expected an empty array",
);
} }
test_zero_array_from_unit_struct<[isize; 0]> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_zero_array_from_unit_struct() {
], assert_de_tokens_error::<[isize; 0]>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected an empty array", "invalid type: unit value, expected an empty array",
);
} }
test_btreemap_from_unit<BTreeMap<isize, isize>> {
&[ #[test]
Token::Unit, fn test_btreemap_from_unit() {
], assert_de_tokens_error::<BTreeMap<isize, isize>>(
&[Token::Unit],
"invalid type: unit value, expected a map", "invalid type: unit value, expected a map",
);
} }
test_btreemap_from_unit_struct<BTreeMap<isize, isize>> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_btreemap_from_unit_struct() {
], assert_de_tokens_error::<BTreeMap<isize, isize>>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected a map", "invalid type: unit value, expected a map",
);
} }
test_hashmap_from_unit<HashMap<isize, isize>> {
&[ #[test]
Token::Unit, fn test_hashmap_from_unit() {
], assert_de_tokens_error::<HashMap<isize, isize>>(
&[Token::Unit],
"invalid type: unit value, expected a map", "invalid type: unit value, expected a map",
);
} }
test_hashmap_from_unit_struct<HashMap<isize, isize>> {
&[ #[test]
Token::UnitStruct { name: "Anything" }, fn test_hashmap_from_unit_struct() {
], assert_de_tokens_error::<HashMap<isize, isize>>(
&[Token::UnitStruct { name: "Anything" }],
"invalid type: unit value, expected a map", "invalid type: unit value, expected a map",
);
} }
test_bool_from_string<bool> {
&[ #[test]
Token::Str("false"), fn test_bool_from_string() {
], assert_de_tokens_error::<bool>(
&[Token::Str("false")],
"invalid type: string \"false\", expected a boolean", "invalid type: string \"false\", expected a boolean",
);
} }
test_number_from_string<isize> {
&[ #[test]
Token::Str("1"), fn test_number_from_string() {
], assert_de_tokens_error::<isize>(
&[Token::Str("1")],
"invalid type: string \"1\", expected isize", "invalid type: string \"1\", expected isize",
);
} }
test_integer_from_float<isize> {
&[ #[test]
Token::F32(0.0), fn test_integer_from_float() {
], assert_de_tokens_error::<isize>(
&[Token::F32(0.0)],
"invalid type: floating point `0`, expected isize", "invalid type: floating point `0`, expected isize",
);
} }
test_unit_struct_from_seq<UnitStruct> {
&[ #[test]
Token::Seq { len: Some(0) }, fn test_unit_struct_from_seq() {
Token::SeqEnd, assert_de_tokens_error::<UnitStruct>(
], &[Token::Seq { len: Some(0) }, Token::SeqEnd],
"invalid type: sequence, expected unit struct UnitStruct", "invalid type: sequence, expected unit struct UnitStruct",
);
} }
test_wrapping_overflow<Wrapping<u16>> {
&[ #[test]
Token::U32(65_536), fn test_wrapping_overflow() {
], assert_de_tokens_error::<Wrapping<u16>>(
&[Token::U32(65_536)],
"invalid value: integer `65536`, expected u16", "invalid value: integer `65536`, expected u16",
);
} }
test_duration_overflow_seq<Duration> {
#[test]
fn test_duration_overflow_seq() {
assert_de_tokens_error::<Duration>(
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::U64(u64::max_value()), Token::U64(u64::max_value()),
@@ -312,20 +389,30 @@ declare_error_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
"overflow deserializing Duration", "overflow deserializing Duration",
);
} }
test_duration_overflow_struct<Duration> {
#[test]
fn test_duration_overflow_struct() {
assert_de_tokens_error::<Duration>(
&[ &[
Token::Struct { name: "Duration", len: 2 }, Token::Struct {
name: "Duration",
len: 2,
},
Token::Str("secs"), Token::Str("secs"),
Token::U64(u64::max_value()), Token::U64(u64::max_value()),
Token::Str("nanos"), Token::Str("nanos"),
Token::U32(1_000_000_000), Token::U32(1_000_000_000),
Token::StructEnd, Token::StructEnd,
], ],
"overflow deserializing Duration", "overflow deserializing Duration",
);
} }
test_systemtime_overflow_seq<SystemTime> {
#[test]
fn test_systemtime_overflow_seq() {
assert_de_tokens_error::<SystemTime>(
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::U64(u64::max_value()), Token::U64(u64::max_value()),
@@ -333,21 +420,31 @@ declare_error_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
"overflow deserializing SystemTime epoch offset", "overflow deserializing SystemTime epoch offset",
);
} }
test_systemtime_overflow_struct<SystemTime> {
#[test]
fn test_systemtime_overflow_struct() {
assert_de_tokens_error::<SystemTime>(
&[ &[
Token::Struct { name: "SystemTime", len: 2 }, Token::Struct {
name: "SystemTime",
len: 2,
},
Token::Str("secs_since_epoch"), Token::Str("secs_since_epoch"),
Token::U64(u64::max_value()), Token::U64(u64::max_value()),
Token::Str("nanos_since_epoch"), Token::Str("nanos_since_epoch"),
Token::U32(1_000_000_000), Token::U32(1_000_000_000),
Token::StructEnd, Token::StructEnd,
], ],
"overflow deserializing SystemTime epoch offset", "overflow deserializing SystemTime epoch offset",
);
} }
#[cfg(systemtime_checked_add)] #[cfg(systemtime_checked_add)]
test_systemtime_overflow<SystemTime> { #[test]
fn test_systemtime_overflow() {
assert_de_tokens_error::<SystemTime>(
&[ &[
Token::Seq { len: Some(2) }, Token::Seq { len: Some(2) },
Token::U64(u64::max_value()), Token::U64(u64::max_value()),
@@ -355,7 +452,7 @@ declare_error_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
"overflow deserializing SystemTime", "overflow deserializing SystemTime",
} );
} }
#[test] #[test]