2015-03-03 22:33:17 -06:00
|
|
|
|
use std::collections::BTreeMap;
|
2016-02-21 19:02:25 -06:00
|
|
|
|
use std::net;
|
2016-01-28 11:45:38 -06:00
|
|
|
|
use std::path::{Path, PathBuf};
|
2016-01-28 12:41:21 -06:00
|
|
|
|
use std::str;
|
2015-03-03 22:33:17 -06:00
|
|
|
|
|
2016-01-28 12:41:21 -06:00
|
|
|
|
use token::{self, Token};
|
2015-03-03 22:33:17 -06:00
|
|
|
|
|
2015-03-03 23:42:33 -06:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
|
#[derive(Serialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
|
struct UnitStruct;
|
2015-03-03 22:33:17 -06:00
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
|
#[derive(Serialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
|
struct TupleStruct(i32, i32, i32);
|
2015-03-03 22:33:17 -06:00
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
|
#[derive(Serialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
|
struct Struct {
|
2015-03-03 23:42:33 -06:00
|
|
|
|
a: i32,
|
|
|
|
|
b: i32,
|
|
|
|
|
c: i32,
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
|
#[derive(Serialize)]
|
2015-03-03 22:33:17 -06:00
|
|
|
|
enum Enum {
|
|
|
|
|
Unit,
|
2015-07-17 08:19:11 -05:00
|
|
|
|
One(i32),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Seq(i32, i32),
|
|
|
|
|
Map { a: i32, b: i32 },
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 23:42:33 -06:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
|
declare_ser_tests! {
|
2015-03-03 22:33:17 -06:00
|
|
|
|
test_unit {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
() => &[Token::Unit],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_bool {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
true => &[Token::Bool(true)],
|
|
|
|
|
false => &[Token::Bool(false)],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_isizes {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
0isize => &[Token::Isize(0)],
|
|
|
|
|
0i8 => &[Token::I8(0)],
|
|
|
|
|
0i16 => &[Token::I16(0)],
|
|
|
|
|
0i32 => &[Token::I32(0)],
|
|
|
|
|
0i64 => &[Token::I64(0)],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_usizes {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
0usize => &[Token::Usize(0)],
|
|
|
|
|
0u8 => &[Token::U8(0)],
|
|
|
|
|
0u16 => &[Token::U16(0)],
|
|
|
|
|
0u32 => &[Token::U32(0)],
|
|
|
|
|
0u64 => &[Token::U64(0)],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_floats {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
0f32 => &[Token::F32(0.)],
|
|
|
|
|
0f64 => &[Token::F64(0.)],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_char {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
'a' => &[Token::Char('a')],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_str {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
"abc" => &[Token::Str("abc")],
|
2016-01-18 14:24:03 -06:00
|
|
|
|
"abc".to_owned() => &[Token::Str("abc")],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
|
|
|
|
test_option {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
None::<i32> => &[Token::Option(false)],
|
|
|
|
|
Some(1) => &[
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::Option(true),
|
|
|
|
|
Token::I32(1),
|
|
|
|
|
],
|
|
|
|
|
}
|
2015-07-19 13:16:46 -05:00
|
|
|
|
test_result {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Ok::<i32, i32>(0) => &[
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumNewType("Result", "Ok"),
|
2015-07-19 13:16:46 -05:00
|
|
|
|
Token::I32(0),
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Err::<i32, i32>(1) => &[
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumNewType("Result", "Err"),
|
2015-07-19 13:16:46 -05:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
],
|
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
|
test_slice {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
&[0][..0] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(0)),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
&[1, 2, 3][..] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(3)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
2015-05-16 04:51:35 -05:00
|
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
|
Token::I32(3),
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
test_array {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
[0; 0] => &[
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::SeqArrayStart(0),
|
2015-05-16 04:51:35 -05:00
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
[1, 2, 3] => &[
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::SeqArrayStart(3),
|
2015-05-16 04:51:35 -05:00
|
|
|
|
Token::SeqSep,
|
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
|
Token::I32(2),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
test_vec {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Vec::<isize>::new() => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(0)),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
vec![vec![], vec![1], vec![2, 3]] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(3)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(0)),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(1)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::SeqStart(Some(2)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
test_tuple {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
(1,) => &[
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStart(1),
|
|
|
|
|
Token::TupleSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
(1, 2, 3) => &[
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStart(3),
|
|
|
|
|
Token::TupleSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
test_btreemap {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
btreemap![1 => 2] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::MapStart(Some(1)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
Token::I32(2),
|
|
|
|
|
Token::MapEnd,
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
btreemap![1 => 2, 3 => 4] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::MapStart(Some(2)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
|
|
|
|
Token::I32(4),
|
|
|
|
|
Token::MapEnd,
|
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::MapStart(Some(2)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::MapStart(Some(0)),
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::MapEnd,
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::MapStart(Some(2)),
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
|
|
|
|
Token::I32(4),
|
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(5),
|
|
|
|
|
Token::I32(6),
|
|
|
|
|
Token::MapEnd,
|
|
|
|
|
Token::MapEnd,
|
|
|
|
|
],
|
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
|
test_unit_struct {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
UnitStruct => &[Token::UnitStruct("UnitStruct")],
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
|
test_tuple_struct {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
TupleStruct(1, 2, 3) => &[
|
2015-07-04 14:11:20 -05:00
|
|
|
|
Token::TupleStructStart("TupleStruct", Some(3)),
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStructSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStructSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStructSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(3),
|
2016-02-23 07:15:44 -06:00
|
|
|
|
Token::TupleStructEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
],
|
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
|
test_struct {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Struct { a: 1, b: 2, c: 3 } => &[
|
2015-07-04 14:11:20 -05:00
|
|
|
|
Token::StructStart("Struct", Some(3)),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::StructSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
|
Token::Str("a"),
|
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::StructSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
|
Token::Str("b"),
|
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::StructSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
|
Token::Str("c"),
|
|
|
|
|
Token::I32(3),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::StructEnd,
|
2015-03-03 23:42:33 -06:00
|
|
|
|
],
|
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
|
test_enum {
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Enum::Unit => &[Token::EnumUnit("Enum", "Unit")],
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Enum::One(42) => &[Token::EnumNewType("Enum", "One"), Token::I32(42)],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Enum::Seq(1, 2) => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::EnumSeqStart("Enum", "Seq", Some(2)),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumSeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumSeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::I32(2),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumSeqEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
|
Enum::Map { a: 1, b: 2 } => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
|
Token::EnumMapStart("Enum", "Map", Some(2)),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumMapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::Str("a"),
|
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumMapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
Token::Str("b"),
|
|
|
|
|
Token::I32(2),
|
2016-02-13 00:05:02 -06:00
|
|
|
|
Token::EnumMapEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
|
],
|
|
|
|
|
}
|
2016-02-21 19:02:25 -06:00
|
|
|
|
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")],
|
|
|
|
|
}
|
2016-02-21 21:07:13 -06:00
|
|
|
|
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")],
|
|
|
|
|
}
|
2016-01-28 11:45:38 -06:00
|
|
|
|
test_path {
|
|
|
|
|
Path::new("/usr/local/lib") => &[
|
|
|
|
|
Token::Str("/usr/local/lib"),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
test_path_buf {
|
|
|
|
|
PathBuf::from("/usr/local/lib") => &[
|
|
|
|
|
Token::Str("/usr/local/lib"),
|
|
|
|
|
],
|
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
|
}
|
2016-01-28 12:41:21 -06:00
|
|
|
|
|
2016-02-21 19:02:25 -06:00
|
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_net_ipaddr() {
|
|
|
|
|
assert_ser_tokens(
|
|
|
|
|
"1.2.3.4".parse::<net::IpAddr>().unwrap(),
|
|
|
|
|
&[Token::Str("1.2.3.4")],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 12:41:21 -06:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_cannot_serialize_paths() {
|
|
|
|
|
let path = unsafe {
|
|
|
|
|
str::from_utf8_unchecked(b"Hello \xF0\x90\x80World")
|
|
|
|
|
};
|
|
|
|
|
token::assert_ser_tokens_error(
|
|
|
|
|
&Path::new(path),
|
|
|
|
|
&[Token::Str("Hello <20>World")],
|
|
|
|
|
token::Error::InvalidValue("Path contains invalid UTF-8 characters".to_owned()));
|
|
|
|
|
|
|
|
|
|
let mut path_buf = PathBuf::new();
|
|
|
|
|
path_buf.push(path);
|
|
|
|
|
|
|
|
|
|
token::assert_ser_tokens_error(
|
|
|
|
|
&path_buf,
|
|
|
|
|
&[Token::Str("Hello <20>World")],
|
|
|
|
|
token::Error::InvalidValue("Path contains invalid UTF-8 characters".to_owned()));
|
|
|
|
|
}
|