2015-03-03 22:33:17 -06:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
use token::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")],
|
|
|
|
"abc".to_string() => &[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) => &[
|
2015-07-29 16:45:29 -05: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) => &[
|
2015-07-29 16:45:29 -05: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] => &[
|
2015-05-16 04:51:35 -05:00
|
|
|
Token::SeqStart(Some(0)),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
[1, 2, 3] => &[
|
2015-05-16 04:51:35 -05:00
|
|
|
Token::SeqStart(Some(3)),
|
|
|
|
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,) => &[
|
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-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-03-17 00:30:02 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
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)),
|
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-03-17 00:30:02 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
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)),
|
2015-03-17 00:30:02 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:42:33 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
test_enum {
|
2015-08-27 22:01:09 -05:00
|
|
|
Enum::Unit => &[Token::EnumUnit("Enum", "Unit")],
|
|
|
|
Enum::One(42) => &[Token::EnumNewtype("Enum", "One"), Token::I32(42)],
|
|
|
|
Enum::Seq(1, 2) => &[
|
2015-03-12 21:40:46 -05:00
|
|
|
Token::EnumSeqStart("Enum", "Seq", Some(2)),
|
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),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
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)),
|
2015-03-17 00:30:02 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|