serde/test_suite/tests/test_de.rs

1030 lines
27 KiB
Rust
Raw Normal View History

2017-03-08 23:02:15 -06:00
#![cfg_attr(feature = "unstable", feature(into_boxed_c_str))]
#[macro_use]
extern crate serde_derive;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::net;
use std::path::PathBuf;
use std::time::Duration;
use std::default::Default;
use std::ffi::{CString, OsString};
#[cfg(feature = "unstable")]
use std::ffi::CStr;
extern crate serde;
2017-01-11 04:39:16 -06:00
use serde::Deserialize;
2016-06-11 02:43:02 -05:00
extern crate fnv;
use self::fnv::FnvHasher;
extern crate serde_test;
2017-04-13 14:28:23 -05:00
use self::serde_test::{Error, Token, assert_de_tokens, assert_de_tokens_error};
2015-03-03 23:19:55 -06:00
#[macro_use]
mod macros;
2015-03-03 23:19:55 -06:00
//////////////////////////////////////////////////////////////////////////
2015-04-05 12:20:33 -05:00
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
struct UnitStruct;
2015-03-03 23:19:55 -06:00
#[derive(PartialEq, Debug, Deserialize)]
struct NewtypeStruct(i32);
#[derive(PartialEq, Debug, Deserialize)]
struct TupleStruct(i32, i32, i32);
2015-03-03 23:19:55 -06:00
#[derive(PartialEq, Debug, Deserialize)]
struct Struct {
2015-03-03 23:19:55 -06:00
a: i32,
b: i32,
2016-03-07 01:27:12 -06:00
#[serde(skip_deserializing)]
2015-03-03 23:19:55 -06:00
c: i32,
}
2017-01-13 01:17:45 -06:00
#[derive(PartialEq, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct StructDenyUnknown {
a: i32,
#[serde(skip_deserializing)]
b: i32,
}
#[derive(PartialEq, Debug, Deserialize)]
#[serde(default)]
struct StructDefault<T> {
a: i32,
b: T,
}
impl Default for StructDefault<String> {
fn default() -> Self {
StructDefault {
a: 100,
b: "default".to_string(),
}
}
}
2017-01-13 03:12:31 -06:00
#[derive(PartialEq, Debug, Deserialize)]
struct StructSkipAll {
#[serde(skip_deserializing)]
a: i32,
}
#[derive(PartialEq, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct StructSkipAllDenyUnknown {
#[serde(skip_deserializing)]
a: i32,
}
#[derive(PartialEq, Debug, Deserialize)]
2015-03-03 23:19:55 -06:00
enum Enum {
#[allow(dead_code)]
#[serde(skip_deserializing)]
Skipped,
2015-03-03 23:19:55 -06:00
Unit,
Simple(i32),
2015-03-03 23:19:55 -06:00
Seq(i32, i32, i32),
Map { a: i32, b: i32, c: i32 },
2015-03-03 23:19:55 -06:00
}
2017-01-13 03:14:03 -06:00
#[derive(PartialEq, Debug, Deserialize)]
enum EnumSkipAll {
#[allow(dead_code)]
#[serde(skip_deserializing)]
Skipped,
}
2015-03-03 23:19:55 -06:00
//////////////////////////////////////////////////////////////////////////
macro_rules! declare_test {
($name:ident { $($value:expr => $tokens:expr,)+ }) => {
#[test]
fn $name() {
$(
// Test ser/de roundtripping
2015-08-27 22:01:09 -05:00
assert_de_tokens(&$value, $tokens);
// Test that the tokens are ignorable
assert_de_tokens_ignore($tokens);
2015-03-03 23:19:55 -06:00
)+
}
}
}
macro_rules! declare_tests {
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
$(
declare_test!($name { $($value => $tokens,)+ });
)+
}
}
2016-03-07 01:27:12 -06:00
macro_rules! declare_error_tests {
($($name:ident<$target:ty> { $tokens:expr, $expected:expr, })+) => {
2016-03-07 01:27:12 -06:00
$(
#[test]
fn $name() {
assert_de_tokens_error::<$target>($tokens, $expected);
}
)+
}
}
2017-04-05 02:17:50 -05:00
fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
#[derive(PartialEq, Debug, Deserialize)]
struct IgnoreBase {
a: i32,
}
2017-04-13 14:28:23 -05:00
let expected = IgnoreBase { a: 1 };
// Embed the tokens to be ignored in the normal token
// stream for an IgnoreBase type
2017-04-13 14:28:23 -05:00
let concated_tokens: Vec<Token> = vec![
Token::Map(Some(2)),
Token::Str("a"),
Token::I32(1),
Token::Str("ignored"),
]
.into_iter()
.chain(ignorable_tokens.to_vec().into_iter())
.chain(vec![Token::MapEnd].into_iter())
.collect();
let mut de = serde_test::Deserializer::new(&concated_tokens);
let v: Result<IgnoreBase, Error> = Deserialize::deserialize(&mut de);
assert_eq!(v.as_ref(), Ok(&expected));
assert_eq!(de.next_token(), None);
}
2015-03-03 23:19:55 -06:00
//////////////////////////////////////////////////////////////////////////
declare_tests! {
test_bool {
true => &[Token::Bool(true)],
false => &[Token::Bool(false)],
2015-03-03 23:19:55 -06:00
}
test_isize {
0isize => &[Token::I8(0)],
0isize => &[Token::I16(0)],
0isize => &[Token::I32(0)],
0isize => &[Token::I64(0)],
0isize => &[Token::U8(0)],
0isize => &[Token::U16(0)],
0isize => &[Token::U32(0)],
0isize => &[Token::U64(0)],
2015-03-03 23:19:55 -06:00
}
test_ints {
0i8 => &[Token::I8(0)],
0i16 => &[Token::I16(0)],
0i32 => &[Token::I32(0)],
0i64 => &[Token::I64(0)],
2015-03-03 23:19:55 -06:00
}
test_uints {
0u8 => &[Token::U8(0)],
0u16 => &[Token::U16(0)],
0u32 => &[Token::U32(0)],
0u64 => &[Token::U64(0)],
2015-03-03 23:19:55 -06:00
}
test_floats {
0f32 => &[Token::F32(0.)],
0f64 => &[Token::F64(0.)],
2015-03-03 23:19:55 -06:00
}
test_char {
'a' => &[Token::Char('a')],
'a' => &[Token::Str("a")],
2017-04-05 02:17:50 -05:00
'a' => &[Token::String("a")],
2015-03-03 23:19:55 -06:00
}
test_string {
"abc".to_owned() => &[Token::Str("abc")],
2017-04-05 02:17:50 -05:00
"abc".to_owned() => &[Token::String("abc")],
"a".to_owned() => &[Token::Char('a')],
2015-03-03 23:19:55 -06:00
}
test_option {
None::<i32> => &[Token::Unit],
2017-04-05 12:40:14 -05:00
None::<i32> => &[Token::None],
Some(1) => &[
2017-04-05 12:40:14 -05:00
Token::Some,
2015-03-03 23:19:55 -06:00
Token::I32(1),
],
}
test_result {
Ok::<i32, i32>(0) => &[
2017-04-05 12:40:14 -05:00
Token::Enum("Result"),
2015-08-27 22:01:09 -05:00
Token::Str("Ok"),
Token::I32(0),
],
Err::<i32, i32>(1) => &[
2017-04-05 12:40:14 -05:00
Token::Enum("Result"),
2015-08-27 22:01:09 -05:00
Token::Str("Err"),
Token::I32(1),
],
}
2015-03-03 23:19:55 -06:00
test_unit {
() => &[Token::Unit],
2015-03-03 23:19:55 -06:00
}
test_unit_struct {
UnitStruct => &[Token::Unit],
UnitStruct => &[
2015-08-27 22:01:09 -05:00
Token::UnitStruct("UnitStruct"),
],
2015-03-03 23:19:55 -06:00
}
test_newtype_struct {
NewtypeStruct(1) => &[
2017-04-05 12:40:14 -05:00
Token::NewtypeStruct("NewtypeStruct"),
Token::I32(1),
],
}
test_tuple_struct {
TupleStruct(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
],
TupleStruct(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(None),
2015-08-27 22:01:09 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
],
TupleStruct(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("TupleStruct", 3),
2015-08-27 22:01:09 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::TupleStructEnd,
2015-08-27 22:01:09 -05:00
],
TupleStruct(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("TupleStruct", 3),
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::TupleStructEnd,
],
}
test_btreeset {
BTreeSet::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(0)),
Token::SeqEnd,
],
btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
Token::Seq(Some(0)),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(1)),
Token::I32(1),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
Token::SeqEnd,
],
BTreeSet::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::TupleStructEnd,
],
}
test_hashset {
HashSet::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(0)),
Token::SeqEnd,
],
hashset![1, 2, 3] => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
],
HashSet::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::TupleStructEnd,
2016-06-11 11:59:29 -05:00
],
hashset![FnvHasher @ 1, 2, 3] => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2016-06-11 11:59:29 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
],
2015-03-03 23:19:55 -06:00
}
test_vec {
Vec::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(0)),
2015-03-03 23:19:55 -06:00
Token::SeqEnd,
],
vec![vec![], vec![1], vec![2, 3]] => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
Token::Seq(Some(0)),
2015-03-03 23:19:55 -06:00
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(1)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
2015-03-03 23:19:55 -06:00
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
Token::SeqEnd,
],
Vec::<isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::TupleStructEnd,
],
2015-03-03 23:19:55 -06:00
}
test_array {
[0; 0] => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(0)),
Token::SeqEnd,
],
[0; 0] => &[
2017-04-05 12:40:14 -05:00
Token::SeqFixedSize(0),
Token::SeqEnd,
],
([0; 0], [1], [2, 3]) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
Token::Seq(Some(0)),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(1)),
Token::I32(1),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
Token::SeqEnd,
],
([0; 0], [1], [2, 3]) => &[
2017-04-05 12:40:14 -05:00
Token::SeqFixedSize(3),
Token::SeqFixedSize(0),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::SeqFixedSize(1),
Token::I32(1),
Token::SeqEnd,
2017-04-05 12:40:14 -05:00
Token::SeqFixedSize(2),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
Token::SeqEnd,
],
[0; 0] => &[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::TupleStructEnd,
],
}
2015-03-03 23:19:55 -06:00
test_tuple {
(1,) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(1)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::SeqEnd,
],
(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::SeqEnd,
],
(1,) => &[
2017-04-05 12:40:14 -05:00
Token::Tuple(1),
Token::I32(1),
Token::TupleEnd,
],
(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::Tuple(3),
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::TupleEnd,
],
2015-03-03 23:19:55 -06:00
}
test_btreemap {
BTreeMap::<isize, isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(0)),
Token::MapEnd,
],
btreemap![1 => 2] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(1)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::I32(2),
Token::MapEnd,
],
btreemap![1 => 2, 3 => 4] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::I32(4),
Token::MapEnd,
],
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
2015-03-03 23:19:55 -06:00
Token::I32(1),
2017-04-05 12:40:14 -05:00
Token::Map(Some(0)),
2015-03-03 23:19:55 -06:00
Token::MapEnd,
Token::I32(2),
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
2015-03-03 23:19:55 -06:00
Token::I32(3),
Token::I32(4),
Token::I32(5),
Token::I32(6),
Token::MapEnd,
Token::MapEnd,
],
BTreeMap::<isize, isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::StructEnd,
],
2015-03-03 23:19:55 -06:00
}
test_hashmap {
HashMap::<isize, isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(0)),
Token::MapEnd,
],
hashmap![1 => 2] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(1)),
Token::I32(1),
Token::I32(2),
Token::MapEnd,
],
hashmap![1 => 2, 3 => 4] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::I32(4),
Token::MapEnd,
],
hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
Token::I32(1),
2017-04-05 12:40:14 -05:00
Token::Map(Some(0)),
Token::MapEnd,
Token::I32(2),
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
Token::I32(3),
Token::I32(4),
Token::I32(5),
Token::I32(6),
Token::MapEnd,
Token::MapEnd,
],
HashMap::<isize, isize>::new() => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Anything", 0),
2017-02-02 14:10:07 -06:00
Token::StructEnd,
],
hashmap![FnvHasher @ 1 => 2, 3 => 4] => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(2)),
2016-06-11 02:43:02 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
Token::I32(4),
Token::MapEnd,
],
}
test_struct {
Struct { a: 1, b: 2, c: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(3)),
2016-03-07 01:27:12 -06:00
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::MapEnd,
],
Struct { a: 1, b: 2, c: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Struct", 3),
2016-03-07 01:27:12 -06:00
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::StructEnd,
],
Struct { a: 1, b: 2, c: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2016-03-07 01:27:12 -06:00
Token::I32(1),
Token::I32(2),
Token::SeqEnd,
],
}
test_struct_with_skip {
Struct { a: 1, b: 2, c: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Map(Some(3)),
2015-03-03 23:19:55 -06:00
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::Str("c"),
Token::I32(3),
2016-03-07 01:27:12 -06:00
Token::Str("d"),
Token::I32(4),
2015-03-03 23:19:55 -06:00
Token::MapEnd,
],
Struct { a: 1, b: 2, c: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Struct", 3),
2015-03-03 23:19:55 -06:00
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::Str("c"),
Token::I32(3),
2016-03-07 01:27:12 -06:00
Token::Str("d"),
Token::I32(4),
Token::StructEnd,
2015-03-03 23:19:55 -06:00
],
}
2017-01-13 03:12:31 -06:00
test_struct_skip_all {
StructSkipAll { a: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("StructSkipAll", 0),
2017-01-13 03:12:31 -06:00
Token::StructEnd,
],
StructSkipAll { a: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("StructSkipAll", 1),
2017-01-13 03:12:31 -06:00
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::StructEnd,
],
}
test_struct_skip_all_deny_unknown {
StructSkipAllDenyUnknown { a: 0 } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("StructSkipAllDenyUnknown", 0),
2017-01-13 03:12:31 -06:00
Token::StructEnd,
],
}
test_struct_default {
StructDefault { a: 50, b: "overwritten".to_string() } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("StructDefault", 1),
Token::Str("a"),
Token::I32(50),
Token::Str("b"),
2017-04-05 02:17:50 -05:00
Token::String("overwritten"),
Token::StructEnd,
],
StructDefault { a: 100, b: "default".to_string() } => &[
2017-04-05 12:40:14 -05:00
Token::Struct("StructDefault", 0),
Token::StructEnd,
],
}
test_enum_unit {
Enum::Unit => &[
2017-04-05 12:40:14 -05:00
Token::UnitVariant("Enum", "Unit"),
2015-03-03 23:19:55 -06:00
],
}
test_enum_simple {
Enum::Simple(1) => &[
2017-04-05 12:40:14 -05:00
Token::NewtypeVariant("Enum", "Simple"),
2015-08-27 22:01:09 -05:00
Token::I32(1),
],
}
2015-03-03 23:19:55 -06:00
test_enum_seq {
Enum::Seq(1, 2, 3) => &[
2017-04-05 12:40:14 -05:00
Token::TupleVariant("Enum", "Seq", 3),
2015-08-27 22:01:09 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
2017-04-05 12:40:14 -05:00
Token::TupleVariantEnd,
2015-03-03 23:19:55 -06:00
],
}
test_enum_map {
Enum::Map { a: 1, b: 2, c: 3 } => &[
2017-04-05 12:40:14 -05:00
Token::StructVariant("Enum", "Map", 3),
2015-08-27 22:01:09 -05:00
Token::Str("a"),
Token::I32(1),
2015-03-03 23:19:55 -06:00
2015-08-27 22:01:09 -05:00
Token::Str("b"),
Token::I32(2),
2015-03-03 23:19:55 -06:00
2015-08-27 22:01:09 -05:00
Token::Str("c"),
Token::I32(3),
2017-04-05 12:40:14 -05:00
Token::StructVariantEnd,
2015-03-03 23:19:55 -06:00
],
}
test_enum_unit_usize {
Enum::Unit => &[
2017-04-05 12:40:14 -05:00
Token::Enum("Enum"),
2017-01-25 10:59:34 -06:00
Token::U32(0),
Token::Unit,
],
}
test_enum_unit_bytes {
Enum::Unit => &[
2017-04-05 12:40:14 -05:00
Token::Enum("Enum"),
Token::Bytes(b"Unit"),
Token::Unit,
],
}
2016-04-17 22:59:26 -05:00
test_box {
Box::new(0i32) => &[Token::I32(0)],
2016-04-17 22:59:26 -05:00
}
test_boxed_slice {
Box::new([0, 1, 2]) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(3)),
2016-04-17 22:59:26 -05:00
Token::I32(0),
Token::I32(1),
Token::I32(2),
Token::SeqEnd,
],
}
test_duration {
Duration::new(1, 2) => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Duration", 2),
Token::Str("secs"),
Token::U64(1),
Token::Str("nanos"),
Token::U32(2),
Token::StructEnd,
],
Duration::new(1, 2) => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
Token::I64(1),
Token::I64(2),
Token::SeqEnd,
],
}
test_range {
1u32..2u32 => &[
2017-04-05 12:40:14 -05:00
Token::Struct("Range", 2),
Token::Str("start"),
Token::U32(1),
Token::Str("end"),
Token::U32(2),
Token::StructEnd,
],
1u32..2u32 => &[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
Token::U64(1),
Token::U64(2),
Token::SeqEnd,
],
}
test_net_ipv4addr {
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
}
test_net_ipv6addr {
"::1".parse::<net::Ipv6Addr>().unwrap() => &[Token::Str("::1")],
}
test_net_socketaddr {
"1.2.3.4:1234".parse::<net::SocketAddr>().unwrap() => &[Token::Str("1.2.3.4:1234")],
"1.2.3.4:1234".parse::<net::SocketAddrV4>().unwrap() => &[Token::Str("1.2.3.4:1234")],
"[::1]:1234".parse::<net::SocketAddrV6>().unwrap() => &[Token::Str("[::1]:1234")],
}
test_path_buf {
PathBuf::from("/usr/local/lib") => &[
2017-04-05 02:17:50 -05:00
Token::String("/usr/local/lib"),
],
}
2017-03-03 17:06:04 -06:00
test_cstring {
CString::new("abc").unwrap() => &[
Token::Bytes(b"abc"),
],
}
2015-03-03 23:19:55 -06:00
}
#[cfg(unix)]
#[test]
fn test_osstring() {
use std::os::unix::ffi::OsStringExt;
let value = OsString::from_vec(vec![1, 2, 3]);
let tokens = [
2017-04-05 12:40:14 -05:00
Token::Enum("OsString"),
Token::Str("Unix"),
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
2017-04-13 14:28:23 -05:00
Token::U8(1),
Token::U8(2),
Token::U8(3),
Token::SeqEnd,
];
assert_de_tokens(&value, &tokens);
assert_de_tokens_ignore(&tokens);
}
#[cfg(windows)]
#[test]
fn test_osstring() {
use std::os::windows::ffi::OsStringExt;
let value = OsString::from_wide(&[1, 2, 3]);
let tokens = [
2017-04-05 12:40:14 -05:00
Token::Enum("OsString"),
Token::Str("Windows"),
2017-04-05 12:40:14 -05:00
Token::Seq(Some(2)),
2017-04-13 14:28:23 -05:00
Token::U16(1),
Token::U16(2),
Token::U16(3),
Token::SeqEnd,
];
assert_de_tokens(&value, &tokens);
assert_de_tokens_ignore(&tokens);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr() {
2017-04-13 14:28:23 -05:00
assert_de_tokens::<Box<CStr>>(
&CString::new("abc").unwrap().into_boxed_c_str(),
&[Token::Bytes(b"abc")],
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_net_ipaddr() {
assert_de_tokens(
2017-03-08 23:02:15 -06:00
&"1.2.3.4".parse::<net::IpAddr>().unwrap(),
&[Token::Str("1.2.3.4")],
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null() {
assert_de_tokens_error::<Box<CStr>>(
2017-04-13 14:28:23 -05:00
&[Token::Bytes(b"a\0c")],
Error::Message("nul byte found in provided data at position: 1".into()),
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null_end() {
assert_de_tokens_error::<Box<CStr>>(
2017-04-13 14:28:23 -05:00
&[Token::Bytes(b"ac\0")],
Error::Message("nul byte found in provided data at position: 2".into()),
);
}
2016-03-07 01:27:12 -06:00
declare_error_tests! {
2017-01-13 01:17:45 -06:00
test_unknown_field<StructDenyUnknown> {
&[
2017-04-05 12:40:14 -05:00
Token::Struct("StructDenyUnknown", 2),
2017-01-13 01:17:45 -06:00
Token::Str("a"),
Token::I32(0),
Token::Str("d"),
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown field `d`, expected `a`".to_owned()),
2017-01-13 01:17:45 -06:00
}
test_skipped_field_is_unknown<StructDenyUnknown> {
&[
2017-04-05 12:40:14 -05:00
Token::Struct("StructDenyUnknown", 2),
2017-01-13 01:17:45 -06:00
Token::Str("b"),
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown field `b`, expected `a`".to_owned()),
2017-01-13 01:17:45 -06:00
}
2017-01-13 03:12:31 -06:00
test_skip_all_deny_unknown<StructSkipAllDenyUnknown> {
&[
2017-04-05 12:40:14 -05:00
Token::Struct("StructSkipAllDenyUnknown", 1),
2017-01-13 03:12:31 -06:00
Token::Str("a"),
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown field `a`, there are no fields".to_owned()),
2017-01-13 03:12:31 -06:00
}
2016-03-07 01:27:12 -06:00
test_unknown_variant<Enum> {
&[
2017-04-05 12:40:14 -05:00
Token::UnitVariant("Enum", "Foo"),
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown variant `Foo`, expected one of `Unit`, `Simple`, `Seq`, `Map`".to_owned()),
2016-03-07 01:27:12 -06:00
}
test_enum_skipped_variant<Enum> {
&[
2017-04-05 12:40:14 -05:00
Token::UnitVariant("Enum", "Skipped"),
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown variant `Skipped`, expected one of `Unit`, `Simple`, `Seq`, `Map`".to_owned()),
}
2017-01-13 03:14:03 -06:00
test_enum_skip_all<EnumSkipAll> {
&[
2017-04-05 12:40:14 -05:00
Token::UnitVariant("EnumSkipAll", "Skipped"),
2017-01-13 03:14:03 -06:00
],
2017-01-21 14:34:04 -06:00
Error::Message("unknown variant `Skipped`, there are no variants".to_owned()),
2017-01-13 03:14:03 -06:00
}
2016-03-07 01:27:12 -06:00
test_struct_seq_too_long<Struct> {
&[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(4)),
2017-04-05 11:42:24 -05:00
Token::I32(1),
Token::I32(2),
Token::I32(3),
2016-03-07 01:27:12 -06:00
],
2017-04-05 11:42:24 -05:00
Error::UnexpectedToken(Token::I32(3)),
2016-03-07 01:27:12 -06:00
}
test_duplicate_field_struct<Struct> {
&[
2017-04-05 12:40:14 -05:00
Token::Map(Some(3)),
Token::Str("a"),
Token::I32(1),
Token::Str("a"),
],
2017-01-11 04:39:16 -06:00
Error::Message("duplicate field `a`".to_owned()),
}
test_duplicate_field_enum<Enum> {
&[
2017-04-05 12:40:14 -05:00
Token::StructVariant("Enum", "Map", 3),
Token::Str("a"),
Token::I32(1),
Token::Str("a"),
],
2017-01-11 04:39:16 -06:00
Error::Message("duplicate field `a`".to_owned()),
}
test_enum_out_of_range<Enum> {
&[
2017-04-05 12:40:14 -05:00
Token::Enum("Enum"),
2017-01-25 10:59:34 -06:00
Token::U32(4),
Token::Unit,
],
Error::Message("invalid value: integer `4`, expected variant index 0 <= i < 4".into()),
}
test_short_tuple<(u8, u8, u8)> {
&[
2017-04-05 12:40:14 -05:00
Token::Tuple(1),
Token::U8(1),
Token::TupleEnd,
],
Error::Message("invalid length 1, expected a tuple of size 3".into()),
}
test_short_array<[u8; 3]> {
&[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(1)),
Token::U8(1),
Token::SeqEnd,
],
Error::Message("invalid length 1, expected an array of length 3".into()),
}
2017-03-03 17:06:04 -06:00
test_cstring_internal_null<CString> {
&[
Token::Bytes(b"a\0c"),
],
2017-03-05 15:59:18 -06:00
Error::Message("nul byte found in provided data at position: 1".into()),
2017-03-03 17:06:04 -06:00
}
test_cstring_internal_null_end<CString> {
&[
Token::Bytes(b"ac\0"),
],
2017-03-05 15:59:18 -06:00
Error::Message("nul byte found in provided data at position: 2".into()),
2017-03-03 17:06:04 -06:00
}
2017-04-04 20:50:40 -05:00
test_unit_from_empty_seq<()> {
&[
2017-04-05 12:40:14 -05:00
Token::Seq(Some(0)),
2017-04-04 20:50:40 -05:00
Token::SeqEnd,
],
Error::Message("invalid type: sequence, expected unit".into()),
}
test_unit_from_empty_seq_without_len<()> {
&[
2017-04-05 12:40:14 -05:00
Token::Seq(None),
2017-04-04 20:50:40 -05:00
Token::SeqEnd,
],
Error::Message("invalid type: sequence, expected unit".into()),
}
test_unit_from_tuple_struct<()> {
&[
2017-04-05 12:40:14 -05:00
Token::TupleStruct("Anything", 0),
2017-04-04 20:50:40 -05:00
Token::TupleStructEnd,
],
Error::Message("invalid type: sequence, expected unit".into()),
}
test_string_from_unit<String> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a string".into()),
}
test_btreeset_from_unit<BTreeSet<isize>> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_btreeset_from_unit_struct<BTreeSet<isize>> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_hashset_from_unit<HashSet<isize>> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_hashset_from_unit_struct<HashSet<isize>> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_vec_from_unit<Vec<isize>> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_vec_from_unit_struct<Vec<isize>> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected a sequence".into()),
}
test_zero_array_from_unit<[isize; 0]> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected an empty array".into()),
}
test_zero_array_from_unit_struct<[isize; 0]> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected an empty array".into()),
}
test_btreemap_from_unit<BTreeMap<isize, isize>> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a map".into()),
}
test_btreemap_from_unit_struct<BTreeMap<isize, isize>> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected a map".into()),
}
test_hashmap_from_unit<HashMap<isize, isize>> {
&[
Token::Unit,
],
Error::Message("invalid type: unit value, expected a map".into()),
}
test_hashmap_from_unit_struct<HashMap<isize, isize>> {
&[
Token::UnitStruct("Anything"),
],
Error::Message("invalid type: unit value, expected a map".into()),
}
test_bool_from_string<bool> {
&[
Token::Str("false"),
],
Error::Message("invalid type: string \"false\", expected a boolean".into()),
}
test_number_from_string<isize> {
&[
Token::Str("1"),
],
Error::Message("invalid type: string \"1\", expected isize".into()),
}
test_integer_from_float<isize> {
&[
Token::F32(0.0),
],
Error::Message("invalid type: floating point `0`, expected isize".into()),
}
test_unit_struct_from_seq<UnitStruct> {
&[
Token::Seq(Some(0)),
Token::SeqEnd,
],
Error::Message("invalid type: sequence, expected unit struct UnitStruct".into()),
}
}