Add tests of num conversion errors

This commit is contained in:
David Tolnay 2022-01-20 21:24:27 -08:00
parent ff04e8be9b
commit 5498dc0550
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -60,6 +60,317 @@ enum EnumSkipAll {
Skipped,
}
#[test]
fn test_i8() {
let test = assert_de_tokens_error::<i8>;
// from signed
test(
&[Token::I16(-129)],
"invalid value: integer `-129`, expected i8",
);
test(
&[Token::I32(-129)],
"invalid value: integer `-129`, expected i8",
);
test(
&[Token::I64(-129)],
"invalid value: integer `-129`, expected i8",
);
test(
&[Token::I16(128)],
"invalid value: integer `128`, expected i8",
);
test(
&[Token::I32(128)],
"invalid value: integer `128`, expected i8",
);
test(
&[Token::I64(128)],
"invalid value: integer `128`, expected i8",
);
// from unsigned
test(
&[Token::U8(128)],
"invalid value: integer `128`, expected i8",
);
test(
&[Token::U16(128)],
"invalid value: integer `128`, expected i8",
);
test(
&[Token::U32(128)],
"invalid value: integer `128`, expected i8",
);
test(
&[Token::U64(128)],
"invalid value: integer `128`, expected i8",
);
}
#[test]
fn test_i16() {
let test = assert_de_tokens_error::<i16>;
// from signed
test(
&[Token::I32(-32769)],
"invalid value: integer `-32769`, expected i16",
);
test(
&[Token::I64(-32769)],
"invalid value: integer `-32769`, expected i16",
);
test(
&[Token::I32(32768)],
"invalid value: integer `32768`, expected i16",
);
test(
&[Token::I64(32768)],
"invalid value: integer `32768`, expected i16",
);
// from unsigned
test(
&[Token::U16(32768)],
"invalid value: integer `32768`, expected i16",
);
test(
&[Token::U32(32768)],
"invalid value: integer `32768`, expected i16",
);
test(
&[Token::U64(32768)],
"invalid value: integer `32768`, expected i16",
);
}
#[test]
fn test_i32() {
let test = assert_de_tokens_error::<i32>;
// from signed
test(
&[Token::I64(-2147483649)],
"invalid value: integer `-2147483649`, expected i32",
);
test(
&[Token::I64(2147483648)],
"invalid value: integer `2147483648`, expected i32",
);
// from unsigned
test(
&[Token::U32(2147483648)],
"invalid value: integer `2147483648`, expected i32",
);
test(
&[Token::U64(2147483648)],
"invalid value: integer `2147483648`, expected i32",
);
}
#[test]
fn test_i64() {
let test = assert_de_tokens_error::<i64>;
// from unsigned
test(
&[Token::U64(9223372036854775808)],
"invalid value: integer `9223372036854775808`, expected i64",
);
}
#[test]
fn test_u8() {
let test = assert_de_tokens_error::<u8>;
// from signed
test(&[Token::I8(-1)], "invalid value: integer `-1`, expected u8");
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected u8",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected u8",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected u8",
);
test(
&[Token::I16(256)],
"invalid value: integer `256`, expected u8",
);
test(
&[Token::I32(256)],
"invalid value: integer `256`, expected u8",
);
test(
&[Token::I64(256)],
"invalid value: integer `256`, expected u8",
);
// from unsigned
test(
&[Token::U16(256)],
"invalid value: integer `256`, expected u8",
);
test(
&[Token::U32(256)],
"invalid value: integer `256`, expected u8",
);
test(
&[Token::U64(256)],
"invalid value: integer `256`, expected u8",
);
}
#[test]
fn test_u16() {
let test = assert_de_tokens_error::<u16>;
// from signed
test(
&[Token::I8(-1)],
"invalid value: integer `-1`, expected u16",
);
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected u16",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected u16",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected u16",
);
test(
&[Token::I32(65536)],
"invalid value: integer `65536`, expected u16",
);
test(
&[Token::I64(65536)],
"invalid value: integer `65536`, expected u16",
);
// from unsigned
test(
&[Token::U32(65536)],
"invalid value: integer `65536`, expected u16",
);
test(
&[Token::U64(65536)],
"invalid value: integer `65536`, expected u16",
);
}
#[test]
fn test_u32() {
let test = assert_de_tokens_error::<u32>;
// from signed
test(
&[Token::I8(-1)],
"invalid value: integer `-1`, expected u32",
);
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected u32",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected u32",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected u32",
);
test(
&[Token::I64(4294967296)],
"invalid value: integer `4294967296`, expected u32",
);
// from unsigned
test(
&[Token::U64(4294967296)],
"invalid value: integer `4294967296`, expected u32",
);
}
#[test]
fn test_u64() {
let test = assert_de_tokens_error::<u64>;
// from signed
test(
&[Token::I8(-1)],
"invalid value: integer `-1`, expected u64",
);
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected u64",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected u64",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected u64",
);
}
#[test]
fn test_u128() {
let test = assert_de_tokens_error::<u128>;
// from signed
test(
&[Token::I8(-1)],
"invalid value: integer `-1`, expected u128",
);
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected u128",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected u128",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected u128",
);
}
#[test]
fn test_usize() {
let test = assert_de_tokens_error::<usize>;
// from signed
test(
&[Token::I8(-1)],
"invalid value: integer `-1`, expected usize",
);
test(
&[Token::I16(-1)],
"invalid value: integer `-1`, expected usize",
);
test(
&[Token::I32(-1)],
"invalid value: integer `-1`, expected usize",
);
test(
&[Token::I64(-1)],
"invalid value: integer `-1`, expected usize",
);
}
#[test]
fn test_unknown_field() {
assert_de_tokens_error::<StructDenyUnknown>(