refactor(tests): Rename and move some structs and enums

This commit is contained in:
Erick Tryzelaar 2016-02-15 16:02:09 -08:00
parent 17bf789708
commit 8ea6c66cf7

View File

@ -1,5 +1,3 @@
use std::default;
use token::{
Error,
Token,
@ -16,76 +14,6 @@ struct Default {
a2: i32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct DisallowUnknown {
a1: i32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename="Superhero")]
struct RenameStruct {
a1: i32,
#[serde(rename="a3")]
a2: i32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
struct RenameStructSerializeDeserialize {
a1: i32,
#[serde(rename(serialize="a4", deserialize="a5"))]
a2: i32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename="Superhero")]
enum RenameEnum {
#[serde(rename="bruce_wayne")]
Batman,
#[serde(rename="clark_kent")]
Superman(i8),
#[serde(rename="diana_prince")]
WonderWoman(i8, i8),
#[serde(rename="barry_allan")]
Flash {
#[serde(rename="b")]
a: i32,
},
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
enum RenameEnumSerializeDeserialize<A> {
#[serde(rename(serialize="dick_grayson", deserialize="jason_todd"))]
Robin {
a: i8,
#[serde(rename(serialize="c", deserialize="d"))]
b: A,
},
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct SkipSerializingFields<A: default::Default> {
a: i8,
#[serde(skip_serializing, default)]
b: A,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct SkipSerializingIfEmptyFields<A: default::Default> {
a: i8,
#[serde(skip_serializing_if_empty, default)]
b: Vec<A>,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct SkipSerializingIfNoneFields<A: default::Default> {
a: i8,
#[serde(skip_serializing_if_none, default)]
b: Option<A>,
}
#[test]
fn test_default() {
assert_de_tokens(
@ -119,6 +47,12 @@ fn test_default() {
);
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct DenyUnknown {
a1: i32,
}
#[test]
fn test_ignore_unknown() {
// 'Default' allows unknown. Basic smoke test of ignore...
@ -154,9 +88,9 @@ fn test_ignore_unknown() {
]
);
assert_de_tokens_error::<DisallowUnknown>(
assert_de_tokens_error::<DenyUnknown>(
vec![
Token::StructStart("DisallowUnknown", Some(2)),
Token::StructStart("DenyUnknown", Some(2)),
Token::MapSep,
Token::Str("a1"),
@ -172,6 +106,22 @@ fn test_ignore_unknown() {
);
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename="Superhero")]
struct RenameStruct {
a1: i32,
#[serde(rename="a3")]
a2: i32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
struct RenameStructSerializeDeserialize {
a1: i32,
#[serde(rename(serialize="a4", deserialize="a5"))]
a2: i32,
}
#[test]
fn test_rename_struct() {
assert_tokens(
@ -190,10 +140,7 @@ fn test_rename_struct() {
Token::MapEnd,
]
);
}
#[test]
fn test_rename_struct_serialize_deserialize() {
assert_ser_tokens(
&RenameStructSerializeDeserialize { a1: 1, a2: 2 },
&[
@ -229,6 +176,33 @@ fn test_rename_struct_serialize_deserialize() {
);
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename="Superhero")]
enum RenameEnum {
#[serde(rename="bruce_wayne")]
Batman,
#[serde(rename="clark_kent")]
Superman(i8),
#[serde(rename="diana_prince")]
WonderWoman(i8, i8),
#[serde(rename="barry_allan")]
Flash {
#[serde(rename="b")]
a: i32,
},
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
enum RenameEnumSerializeDeserialize<A> {
#[serde(rename(serialize="dick_grayson", deserialize="jason_todd"))]
Robin {
a: i8,
#[serde(rename(serialize="c", deserialize="d"))]
b: A,
},
}
#[test]
fn test_rename_enum() {
assert_tokens(
@ -273,10 +247,7 @@ fn test_rename_enum() {
Token::MapEnd,
]
);
}
#[test]
fn test_enum_serialize_deserialize() {
assert_ser_tokens(
&RenameEnumSerializeDeserialize::Robin {
a: 0,
@ -318,194 +289,129 @@ fn test_enum_serialize_deserialize() {
);
}
#[derive(Debug, PartialEq, Serialize)]
struct SkipSerializingStruct<'a, B, D, E> {
a: &'a i8,
#[serde(skip_serializing)]
b: B,
#[serde(skip_serializing_if_none)]
d: Option<D>,
#[serde(skip_serializing_if_empty)]
e: Vec<E>,
}
#[test]
fn test_skip_serializing_fields() {
fn test_skip_serializing_struct() {
let a = 1;
assert_ser_tokens(
&SkipSerializingFields {
a: 1,
&SkipSerializingStruct {
a: &a,
b: 2,
d: Some(4),
e: vec![5],
},
&[
Token::StructStart("SkipSerializingFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
assert_de_tokens(
&SkipSerializingFields {
a: 1,
b: 0,
},
vec![
Token::StructStart("SkipSerializingFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
}
#[test]
fn test_skip_serializing_fields_if_empty() {
assert_ser_tokens(
&SkipSerializingIfEmptyFields::<i32> {
a: 1,
b: vec![],
},
&[
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
assert_de_tokens(
&SkipSerializingIfEmptyFields::<i32> {
a: 1,
b: vec![],
},
vec![
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
assert_ser_tokens(
&SkipSerializingIfEmptyFields {
a: 1,
b: vec![2],
},
&[
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
Token::StructStart("SkipSerializingStruct", Some(3)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapSep,
Token::Str("b"),
Token::Str("d"),
Token::Option(true),
Token::I32(4),
Token::MapSep,
Token::Str("e"),
Token::SeqStart(Some(1)),
Token::SeqSep,
Token::I32(2),
Token::I32(5),
Token::SeqEnd,
Token::MapEnd,
]
);
assert_de_tokens(
&SkipSerializingIfEmptyFields {
a: 1,
b: vec![2],
assert_ser_tokens(
&SkipSerializingStruct {
a: &a,
b: 2,
d: None::<u8>,
e: Vec::<u8>::new(),
},
vec![
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
&[
Token::StructStart("SkipSerializingStruct", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
}
#[derive(Debug, PartialEq, Serialize)]
enum SkipSerializingEnum<'a, B, D, E> {
Struct {
a: &'a i8,
#[serde(skip_serializing)]
_b: B,
#[serde(skip_serializing_if_none)]
d: Option<D>,
#[serde(skip_serializing_if_empty)]
e: Vec<E>,
}
}
#[test]
fn test_skip_serializing_enum() {
let a = 1;
assert_ser_tokens(
&SkipSerializingEnum::Struct {
a: &a,
_b: 2,
d: Some(4),
e: vec![5],
},
&[
Token::EnumMapStart("SkipSerializingEnum", "Struct", Some(3)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapSep,
Token::Str("b"),
Token::Str("d"),
Token::Option(true),
Token::I32(4),
Token::MapSep,
Token::Str("e"),
Token::SeqStart(Some(1)),
Token::SeqSep,
Token::I32(2),
Token::I32(5),
Token::SeqEnd,
Token::MapEnd,
]
);
}
#[test]
fn test_skip_serializing_fields_if_none() {
assert_ser_tokens(
&SkipSerializingIfNoneFields::<i32> {
a: 1,
b: None,
},
&[
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
assert_de_tokens(
&SkipSerializingIfNoneFields::<i32> {
a: 1,
b: None,
},
vec![
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapEnd,
]
);
assert_ser_tokens(
&SkipSerializingIfNoneFields {
a: 1,
b: Some(2),
&SkipSerializingEnum::Struct {
a: &a,
_b: 2,
d: None::<u8>,
e: Vec::<u8>::new(),
},
&[
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
Token::EnumMapStart("SkipSerializingEnum", "Struct", Some(1)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapSep,
Token::Str("b"),
Token::Option(true),
Token::I32(2),
Token::MapEnd,
]
);
assert_de_tokens(
&SkipSerializingIfNoneFields {
a: 1,
b: Some(2),
},
vec![
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
Token::MapSep,
Token::Str("a"),
Token::I8(1),
Token::MapSep,
Token::Str("b"),
Token::Option(true),
Token::I32(2),
Token::MapEnd,
]
);