diff --git a/.travis.yml b/.travis.yml index ff56c482..de282ca2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,15 +22,15 @@ script: - (cd serde && travis-cargo build -- --no-default-features) - (cd serde && travis-cargo --only nightly build -- --no-default-features --features alloc) - (cd serde && travis-cargo --only nightly build -- --no-default-features --features collections) -- (cd serde_tests && travis-cargo --skip nightly test) -- (cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing) +- (cd testing && travis-cargo --skip nightly test) +- (cd testing && travis-cargo --only nightly test -- --features nightly-testing) - (cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing) - (cd examples/serde-syntex-example && travis-cargo --skip nightly run) - (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --no-default-features --features nightly) - (cd serde && travis-cargo --only stable doc) after_success: - (cd serde && travis-cargo --only stable doc-upload) -- (cd serde_tests && travis-cargo --only stable coveralls --no-sudo) +- (cd testing && travis-cargo --only stable coveralls --no-sudo) env: global: - TRAVIS_CARGO_NIGHTLY_FEATURE="" diff --git a/serde_macros/Cargo.toml b/serde_macros/Cargo.toml index cec301bd..53b7d443 100644 --- a/serde_macros/Cargo.toml +++ b/serde_macros/Cargo.toml @@ -25,6 +25,7 @@ compiletest_rs = "^0.2.0" fnv = "1.0" rustc-serialize = "^0.3.16" serde = { version = "^0.7.11", path = "../serde" } +serde_test = { version = "^0.7.11", path = "../serde_test" } [[test]] name = "test" diff --git a/serde_macros/benches/bench.rs b/serde_macros/benches/bench.rs index 089bc852..ec15c18c 100644 --- a/serde_macros/benches/bench.rs +++ b/serde_macros/benches/bench.rs @@ -6,4 +6,4 @@ extern crate rustc_serialize; extern crate serde; extern crate test; -include!("../../serde_tests/benches/bench.rs.in"); +include!("../../testing/benches/bench.rs.in"); diff --git a/serde_macros/tests/test.rs b/serde_macros/tests/test.rs index f2e785a5..e9211218 100644 --- a/serde_macros/tests/test.rs +++ b/serde_macros/tests/test.rs @@ -4,6 +4,6 @@ extern crate serde; extern crate test; -include!("../../serde_tests/tests/test.rs.in"); +include!("../../testing/tests/test.rs.in"); mod compile_tests; diff --git a/serde_test/Cargo.toml b/serde_test/Cargo.toml new file mode 100644 index 00000000..055c3d9f --- /dev/null +++ b/serde_test/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "serde_test" +version = "0.7.11" +authors = ["Erick Tryzelaar "] +license = "MIT/Apache-2.0" +description = "Token De/Serializer for testing De/Serialize implementations" +repository = "https://github.com/serde-rs/serde" +documentation = "https://serde-rs.github.io/serde/serde/" +readme = "../README.md" +keywords = ["serde", "serialization"] +include = ["Cargo.toml", "src/**/*.rs"] + +[dependencies] +serde = { version = "0.7.11", path = "../serde" } diff --git a/serde_test/src/assert.rs b/serde_test/src/assert.rs new file mode 100644 index 00000000..b5e03f52 --- /dev/null +++ b/serde_test/src/assert.rs @@ -0,0 +1,54 @@ +use serde::{Serialize, Deserialize}; + +use de::Deserializer; +use error::Error; +use ser::Serializer; +use token::Token; + +use std::fmt::Debug; + +pub fn assert_tokens(value: &T, tokens: &[Token<'static>]) + where T: Serialize + Deserialize + PartialEq + Debug, +{ + assert_ser_tokens(value, tokens); + assert_de_tokens(value, tokens); +} + +pub fn assert_ser_tokens(value: &T, tokens: &[Token]) + where T: Serialize, +{ + let mut ser = Serializer::new(tokens.iter()); + assert_eq!(Serialize::serialize(value, &mut ser), Ok(())); + assert_eq!(ser.next_token(), None); +} + +/// Expect an error serializing `T`. +pub fn assert_ser_tokens_error(value: &T, tokens: &[Token], error: Error) + where T: Serialize + PartialEq + Debug, +{ + let mut ser = Serializer::new(tokens.iter()); + let v: Result<(), Error> = Serialize::serialize(value, &mut ser); + assert_eq!(v.as_ref(), Err(&error)); + assert_eq!(ser.next_token(), None); +} + +pub fn assert_de_tokens(value: &T, tokens: &[Token<'static>]) + where T: Deserialize + PartialEq + Debug, +{ + let mut de = Deserializer::new(tokens.to_vec().into_iter()); + let v: Result = Deserialize::deserialize(&mut de); + assert_eq!(v.as_ref(), Ok(value)); + assert_eq!(de.next_token(), None); +} + +/// Expect an error deserializing tokens into a `T`. +pub fn assert_de_tokens_error(tokens: &[Token<'static>], error: Error) + where T: Deserialize + PartialEq + Debug, +{ + let mut de = Deserializer::new(tokens.to_vec().into_iter()); + let v: Result = Deserialize::deserialize(&mut de); + assert_eq!(v, Err(error)); + // There may be one token left if a peek caused the error + de.next_token(); + assert_eq!(de.next_token(), None); +} diff --git a/serde_tests/tests/token.rs b/serde_test/src/de.rs similarity index 52% rename from serde_tests/tests/token.rs rename to serde_test/src/de.rs index bd20a982..a2d4c3d7 100644 --- a/serde_tests/tests/token.rs +++ b/serde_test/src/de.rs @@ -1,493 +1,39 @@ -use std::fmt; use std::iter; -use std::error; -extern crate serde; -use self::serde::ser::{self, Serialize}; -use self::serde::de; -use self::serde::de::value::{self, ValueDeserializer}; +use serde::de::{ + self, + Deserialize, + EnumVisitor, + MapVisitor, + SeqVisitor, + VariantVisitor, + Visitor, +}; -#[derive(Clone, PartialEq, Debug)] -pub enum Token<'a> { - Bool(bool), - Isize(isize), - I8(i8), - I16(i16), - I32(i32), - I64(i64), - Usize(usize), - U8(u8), - U16(u16), - U32(u32), - U64(u64), - F32(f32), - F64(f64), - Char(char), - Str(&'a str), - String(String), - Bytes(&'a [u8]), +use error::Error; +use token::Token; - Option(bool), - - Unit, - UnitStruct(&'a str), - - StructNewType(&'a str), - - EnumStart(&'a str), - EnumUnit(&'a str, &'a str), - EnumNewType(&'a str, &'a str), - - SeqStart(Option), - SeqArrayStart(usize), - SeqSep, - SeqEnd, - - TupleStart(usize), - TupleSep, - TupleEnd, - - TupleStructStart(&'a str, Option), - TupleStructSep, - TupleStructEnd, - - MapStart(Option), - MapSep, - MapEnd, - - StructStart(&'a str, Option), - StructSep, - StructEnd, - - EnumSeqStart(&'a str, &'a str, Option), - EnumSeqSep, - EnumSeqEnd, - - EnumMapStart(&'a str, &'a str, Option), - EnumMapSep, - EnumMapEnd, -} - -////////////////////////////////////////////////////////////////////////////// - -pub struct Serializer { - tokens: I, -} - -impl<'a, I> Serializer - where I: Iterator> +pub struct Deserializer + where I: Iterator>, { - pub fn new(tokens: I) -> Serializer { - Serializer { - tokens: tokens, - } - } - - fn visit_sequence(&mut self, mut visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::SeqEnd)); - - Ok(()) - } - - fn visit_mapping(&mut self, mut visitor: V) -> Result<(), Error> - where V: ser::MapVisitor - { - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::MapEnd)); - - Ok(()) - } -} - -impl<'a, I> ser::Serializer for Serializer - where I: Iterator>, -{ - type Error = Error; - - fn serialize_unit(&mut self) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Unit)); - Ok(()) - } - - fn serialize_newtype_variant(&mut self, - name: &str, - _variant_index: usize, - variant: &str, - value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant))); - value.serialize(self) - } - - fn serialize_unit_struct(&mut self, name: &str) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name))); - Ok(()) - } - - fn serialize_unit_variant(&mut self, - name: &str, - _variant_index: usize, - variant: &str) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant))); - - Ok(()) - } - - fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Bool(v))); - Ok(()) - } - - fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Isize(v))); - Ok(()) - } - - fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::I8(v))); - Ok(()) - } - - fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::I16(v))); - Ok(()) - } - - fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::I32(v))); - Ok(()) - } - - fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::I64(v))); - Ok(()) - } - - fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Usize(v))); - Ok(()) - } - - fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::U8(v))); - Ok(()) - } - - fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::U16(v))); - Ok(()) - } - - fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::U32(v))); - Ok(()) - } - - fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::U64(v))); - Ok(()) - } - - fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::F32(v))); - Ok(()) - } - - fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::F64(v))); - Ok(()) - } - - fn serialize_char(&mut self, v: char) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Char(v))); - Ok(()) - } - - fn serialize_str(&mut self, v: &str) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Str(v))); - Ok(()) - } - - fn serialize_none(&mut self) -> Result<(), Error> { - assert_eq!(self.tokens.next(), Some(&Token::Option(false))); - Ok(()) - } - - fn serialize_some(&mut self, value: V) -> Result<(), Error> - where V: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::Option(true))); - value.serialize(self) - } - - - fn serialize_seq(&mut self, visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len))); - - self.visit_sequence(visitor) - } - - fn serialize_fixed_size_array(&mut self, visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - let len = visitor.len().expect("arrays must have a length"); - - assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len))); - - self.visit_sequence(visitor) - } - - fn serialize_seq_elt(&mut self, value: T) -> Result<(), Error> - where T: ser::Serialize - { - assert_eq!(self.tokens.next(), Some(&Token::SeqSep)); - value.serialize(self) - } - - fn serialize_tuple(&mut self, mut visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - let len = visitor.len().expect("arrays must have a length"); - - assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len))); - - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::TupleEnd)); - - Ok(()) - } - - fn serialize_tuple_elt(&mut self, value: T) -> Result<(), Error> - where T: ser::Serialize - { - assert_eq!(self.tokens.next(), Some(&Token::TupleSep)); - value.serialize(self) - } - - fn serialize_newtype_struct(&mut self, - name: &'static str, - value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name))); - value.serialize(self) - } - - fn serialize_tuple_struct(&mut self, name: &str, mut visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len))); - - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd)); - - Ok(()) - } - - fn serialize_tuple_struct_elt(&mut self, value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep)); - value.serialize(self) - } - - fn serialize_tuple_variant(&mut self, - name: &str, - _variant_index: usize, - variant: &str, - mut visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len))); - - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd)); - - Ok(()) - } - - fn serialize_tuple_variant_elt(&mut self, value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep)); - value.serialize(self) - } - - fn serialize_map(&mut self, visitor: V) -> Result<(), Error> - where V: ser::MapVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::MapStart(len))); - - self.visit_mapping(visitor) - } - - fn serialize_map_elt(&mut self, key: K, value: V) -> Result<(), Error> - where K: ser::Serialize, - V: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::MapSep)); - - try!(key.serialize(self)); - value.serialize(self) - } - - fn serialize_struct(&mut self, name: &str, mut visitor: V) -> Result<(), Error> - where V: ser::MapVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len))); - - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::StructEnd)); - - Ok(()) - } - - fn serialize_struct_elt(&mut self, key: &'static str, value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::StructSep)); - - try!(key.serialize(self)); - value.serialize(self) - } - - fn serialize_struct_variant(&mut self, - name: &str, - _variant_index: usize, - variant: &str, - mut visitor: V) -> Result<(), Error> - where V: ser::MapVisitor - { - let len = visitor.len(); - - assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len))); - - while let Some(()) = try!(visitor.visit(self)) { } - - assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd)); - - Ok(()) - } - - fn serialize_struct_variant_elt(&mut self, key: &'static str, value: T) -> Result<(), Error> - where T: ser::Serialize, - { - assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep)); - - try!(key.serialize(self)); - value.serialize(self) - } -} - -////////////////////////////////////////////////////////////////////////////// - -#[derive(Clone, PartialEq, Debug)] -pub enum Error { - SyntaxError, - EndOfStreamError, - UnknownFieldError(String), - UnknownVariantError(String), - MissingFieldError(&'static str), - DuplicateFieldError(&'static str), - InvalidName(&'static str), - InvalidValue(String), - UnexpectedToken(Token<'static>), - ValueError(value::Error), -} - -impl ser::Error for Error { - fn custom>(_: T) -> Error { Error::SyntaxError } - - fn invalid_value(msg: &str) -> Error { - Error::InvalidValue(msg.to_owned()) - } -} - -impl de::Error for Error { - fn custom>(_: T) -> Error { Error::SyntaxError } - - fn end_of_stream() -> Error { Error::EndOfStreamError } - - fn invalid_value(msg: &str) -> Error { - Error::InvalidValue(msg.to_owned()) - } - - fn unknown_field(field: &str) -> Error { - Error::UnknownFieldError(field.to_owned()) - } - - fn unknown_variant(variant: &str) -> Error { - Error::UnknownVariantError(variant.to_owned()) - } - - fn missing_field(field: &'static str) -> Error { - Error::MissingFieldError(field) - } - - fn duplicate_field(field: &'static str) -> Error { - Error::DuplicateFieldError(field) - } -} - -impl fmt::Display for Error { - fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { - formatter.write_str(format!("{:?}", self).as_ref()) - } -} - -impl error::Error for Error { - fn description(&self) -> &str { - "Serde Deserialization Error" - } - - fn cause(&self) -> Option<&error::Error> { - None - } -} - -impl From for Error { - fn from(error: value::Error) -> Error { - Error::ValueError(error) - } -} - -struct Deserializer where I: Iterator> { tokens: iter::Peekable, } impl Deserializer - where I: Iterator> + where I: Iterator>, { - fn new(tokens: I) -> Deserializer { + pub fn new(tokens: I) -> Deserializer { Deserializer { tokens: tokens.peekable(), } } + pub fn next_token(&mut self) -> Option> { + self.tokens.next() + } + fn visit_seq(&mut self, len: Option, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_seq(DeserializerSeqVisitor { de: self, @@ -496,7 +42,7 @@ impl Deserializer } fn visit_array(&mut self, len: usize, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_seq(DeserializerArrayVisitor { de: self, @@ -505,7 +51,7 @@ impl Deserializer } fn visit_tuple(&mut self, len: usize, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_seq(DeserializerTupleVisitor { de: self, @@ -514,7 +60,7 @@ impl Deserializer } fn visit_tuple_struct(&mut self, len: usize, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_seq(DeserializerTupleStructVisitor { de: self, @@ -523,7 +69,7 @@ impl Deserializer } fn visit_variant_seq(&mut self, len: Option, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_seq(DeserializerVariantSeqVisitor { de: self, @@ -532,7 +78,7 @@ impl Deserializer } fn visit_map(&mut self, len: Option, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_map(DeserializerMapVisitor { de: self, @@ -541,7 +87,7 @@ impl Deserializer } fn visit_struct(&mut self, len: Option, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_map(DeserializerStructVisitor { de: self, @@ -550,7 +96,7 @@ impl Deserializer } fn visit_variant_map(&mut self, len: Option, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { visitor.visit_map(DeserializerVariantMapVisitor { de: self, @@ -560,12 +106,12 @@ impl Deserializer } impl de::Deserializer for Deserializer - where I: Iterator> + where I: Iterator>, { type Error = Error; fn deserialize(&mut self, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.next() { Some(Token::Bool(v)) => visitor.visit_bool(v), @@ -599,14 +145,14 @@ impl de::Deserializer for Deserializer self.visit_map(len, visitor) } Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } /// Hook into `Option` deserializing so we can treat `Unit` as a /// `None`, or a regular value as `Some(value)`. fn deserialize_option(&mut self, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::Option(false)) => { @@ -622,7 +168,7 @@ impl de::Deserializer for Deserializer visitor.visit_none() } Some(_) => visitor.visit_some(self), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -630,7 +176,7 @@ impl de::Deserializer for Deserializer name: &str, _variants: &'static [&'static str], mut visitor: V) -> Result - where V: de::EnumVisitor, + where V: EnumVisitor, { match self.tokens.peek() { Some(&Token::EnumStart(n)) if name == n => { @@ -652,12 +198,12 @@ impl de::Deserializer for Deserializer let token = self.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => { return Err(Error::EndOfStreamError); } + None => { return Err(Error::EndOfStream); } } } fn deserialize_unit_struct(&mut self, name: &str, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::UnitStruct(n)) => { @@ -669,14 +215,14 @@ impl de::Deserializer for Deserializer } } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn deserialize_newtype_struct(&mut self, name: &str, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::StructNewType(n)) => { @@ -688,14 +234,14 @@ impl de::Deserializer for Deserializer } } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn deserialize_fixed_size_array(&mut self, len: usize, visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::SeqArrayStart(_)) => { @@ -703,14 +249,14 @@ impl de::Deserializer for Deserializer self.visit_array(len, visitor) } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn deserialize_tuple(&mut self, len: usize, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::Unit) => { @@ -738,7 +284,7 @@ impl de::Deserializer for Deserializer self.visit_tuple_struct(len, visitor) } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -746,7 +292,7 @@ impl de::Deserializer for Deserializer name: &str, len: usize, mut visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::Unit) => { @@ -782,7 +328,7 @@ impl de::Deserializer for Deserializer } } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -790,7 +336,7 @@ impl de::Deserializer for Deserializer name: &str, fields: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.tokens.peek() { Some(&Token::StructStart(n, _)) => { @@ -806,7 +352,7 @@ impl de::Deserializer for Deserializer self.visit_map(Some(fields.len()), visitor) } Some(_) => self.deserialize(visitor), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } } @@ -818,26 +364,26 @@ struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator> len: Option, } -impl<'a, I> de::SeqVisitor for DeserializerSeqVisitor<'a, I> +impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit(&mut self) -> Result, Error> - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::SeqSep) => { self.de.tokens.next(); self.len = self.len.map(|len| len - 1); - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::SeqEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -846,7 +392,7 @@ impl<'a, I> de::SeqVisitor for DeserializerSeqVisitor<'a, I> match self.de.tokens.next() { Some(Token::SeqEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -863,26 +409,26 @@ struct DeserializerArrayVisitor<'a, I: 'a> where I: Iterator len: usize, } -impl<'a, I> de::SeqVisitor for DeserializerArrayVisitor<'a, I> +impl<'a, I> SeqVisitor for DeserializerArrayVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit(&mut self) -> Result, Error> - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::SeqSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::SeqEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -891,7 +437,7 @@ impl<'a, I> de::SeqVisitor for DeserializerArrayVisitor<'a, I> match self.de.tokens.next() { Some(Token::SeqEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -907,26 +453,26 @@ struct DeserializerTupleVisitor<'a, I: 'a> where I: Iterator len: usize, } -impl<'a, I> de::SeqVisitor for DeserializerTupleVisitor<'a, I> +impl<'a, I> SeqVisitor for DeserializerTupleVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit(&mut self) -> Result, Error> - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::TupleSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::TupleEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -935,7 +481,7 @@ impl<'a, I> de::SeqVisitor for DeserializerTupleVisitor<'a, I> match self.de.tokens.next() { Some(Token::TupleEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -951,26 +497,26 @@ struct DeserializerTupleStructVisitor<'a, I: 'a> where I: Iterator de::SeqVisitor for DeserializerTupleStructVisitor<'a, I> +impl<'a, I> SeqVisitor for DeserializerTupleStructVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit(&mut self) -> Result, Error> - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::TupleStructSep) => { self.de.tokens.next(); self.len -= 1; - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::TupleStructEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -979,7 +525,7 @@ impl<'a, I> de::SeqVisitor for DeserializerTupleStructVisitor<'a, I> match self.de.tokens.next() { Some(Token::TupleStructEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -995,26 +541,26 @@ struct DeserializerVariantSeqVisitor<'a, I: 'a> where I: Iterator, } -impl<'a, I> de::SeqVisitor for DeserializerVariantSeqVisitor<'a, I> +impl<'a, I> SeqVisitor for DeserializerVariantSeqVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit(&mut self) -> Result, Error> - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::EnumSeqSep) => { self.de.tokens.next(); self.len = self.len.map(|len| len - 1); - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::EnumSeqEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1023,7 +569,7 @@ impl<'a, I> de::SeqVisitor for DeserializerVariantSeqVisitor<'a, I> match self.de.tokens.next() { Some(Token::EnumSeqEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1040,33 +586,33 @@ struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator> len: Option, } -impl<'a, I> de::MapVisitor for DeserializerMapVisitor<'a, I> +impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit_key(&mut self) -> Result, Error> - where K: de::Deserialize, + where K: Deserialize, { match self.de.tokens.peek() { Some(&Token::MapSep) => { self.de.tokens.next(); self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 }); - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::MapEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_value(&mut self) -> Result - where V: de::Deserialize, + where V: Deserialize, { - Ok(try!(de::Deserialize::deserialize(self.de))) + Ok(try!(Deserialize::deserialize(self.de))) } fn end(&mut self) -> Result<(), Error> { @@ -1074,7 +620,7 @@ impl<'a, I> de::MapVisitor for DeserializerMapVisitor<'a, I> match self.de.tokens.next() { Some(Token::MapEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1091,33 +637,33 @@ struct DeserializerStructVisitor<'a, I: 'a> where I: Iterator, } -impl<'a, I> de::MapVisitor for DeserializerStructVisitor<'a, I> +impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit_key(&mut self) -> Result, Error> - where K: de::Deserialize, + where K: Deserialize, { match self.de.tokens.peek() { Some(&Token::StructSep) => { self.de.tokens.next(); self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 }); - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::StructEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_value(&mut self) -> Result - where V: de::Deserialize, + where V: Deserialize, { - Ok(try!(de::Deserialize::deserialize(self.de))) + Ok(try!(Deserialize::deserialize(self.de))) } fn end(&mut self) -> Result<(), Error> { @@ -1125,7 +671,7 @@ impl<'a, I> de::MapVisitor for DeserializerStructVisitor<'a, I> match self.de.tokens.next() { Some(Token::StructEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1141,27 +687,27 @@ struct DeserializerVariantVisitor<'a, I: 'a> where I: Iterator, } -impl<'a, I> de::VariantVisitor for DeserializerVariantVisitor<'a, I> +impl<'a, I> VariantVisitor for DeserializerVariantVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit_variant(&mut self) -> Result - where V: de::Deserialize, + where V: Deserialize, { match self.de.tokens.peek() { Some(&Token::EnumUnit(_, v)) | Some(&Token::EnumNewType(_, v)) | Some(&Token::EnumSeqStart(_, v, _)) | Some(&Token::EnumMapStart(_, v, _)) => { - let mut de = ValueDeserializer::::into_deserializer(v); - let value = try!(de::Deserialize::deserialize(&mut de)); + let mut de = de::value::ValueDeserializer::::into_deserializer(v); + let value = try!(Deserialize::deserialize(&mut de)); Ok(value) } Some(_) => { - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1172,31 +718,31 @@ impl<'a, I> de::VariantVisitor for DeserializerVariantVisitor<'a, I> Ok(()) } Some(_) => { - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_newtype(&mut self) -> Result - where T: de::Deserialize, + where T: Deserialize, { match self.de.tokens.peek() { Some(&Token::EnumNewType(_, _)) => { self.de.tokens.next(); - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } Some(_) => { - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_tuple(&mut self, len: usize, visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.de.tokens.peek() { Some(&Token::EnumSeqStart(_, _, Some(enum_len))) => { @@ -1218,16 +764,16 @@ impl<'a, I> de::VariantVisitor for DeserializerVariantVisitor<'a, I> } } Some(_) => { - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_struct(&mut self, fields: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor, + where V: Visitor, { match self.de.tokens.peek() { Some(&Token::EnumMapStart(_, _, Some(enum_len))) => { @@ -1249,9 +795,9 @@ impl<'a, I> de::VariantVisitor for DeserializerVariantVisitor<'a, I> } } Some(_) => { - de::Deserialize::deserialize(self.de) + Deserialize::deserialize(self.de) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } } @@ -1263,33 +809,33 @@ struct DeserializerVariantMapVisitor<'a, I: 'a> where I: Iterator, } -impl<'a, I> de::MapVisitor for DeserializerVariantMapVisitor<'a, I> +impl<'a, I> MapVisitor for DeserializerVariantMapVisitor<'a, I> where I: Iterator>, { type Error = Error; fn visit_key(&mut self) -> Result, Error> - where K: de::Deserialize, + where K: Deserialize, { match self.de.tokens.peek() { Some(&Token::EnumMapSep) => { self.de.tokens.next(); self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 }); - Ok(Some(try!(de::Deserialize::deserialize(self.de)))) + Ok(Some(try!(Deserialize::deserialize(self.de)))) } Some(&Token::EnumMapEnd) => Ok(None), Some(_) => { let token = self.de.tokens.next().unwrap(); Err(Error::UnexpectedToken(token)) } - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } fn visit_value(&mut self) -> Result - where V: de::Deserialize, + where V: Deserialize, { - Ok(try!(de::Deserialize::deserialize(self.de))) + Ok(try!(Deserialize::deserialize(self.de))) } fn end(&mut self) -> Result<(), Error> { @@ -1297,7 +843,7 @@ impl<'a, I> de::MapVisitor for DeserializerVariantMapVisitor<'a, I> match self.de.tokens.next() { Some(Token::EnumMapEnd) => Ok(()), Some(token) => Err(Error::UnexpectedToken(token)), - None => Err(Error::EndOfStreamError), + None => Err(Error::EndOfStream), } } @@ -1306,89 +852,3 @@ impl<'a, I> de::MapVisitor for DeserializerVariantMapVisitor<'a, I> (len, self.len) } } - -////////////////////////////////////////////////////////////////////////////// - -pub fn assert_ser_tokens(value: &T, tokens: &[Token]) - where T: ser::Serialize, -{ - let mut ser = Serializer::new(tokens.iter()); - assert_eq!(ser::Serialize::serialize(value, &mut ser), Ok(())); - assert_eq!(ser.tokens.next(), None); -} - -// Expect an error deserializing tokens into a T -pub fn assert_ser_tokens_error(value: &T, tokens: &[Token], error: Error) - where T: ser::Serialize + PartialEq + fmt::Debug, -{ - let mut ser = Serializer::new(tokens.iter()); - let v: Result<(), Error> = ser::Serialize::serialize(value, &mut ser); - assert_eq!(v.as_ref(), Err(&error)); -} - -pub fn assert_de_tokens(value: &T, tokens: Vec>) - where T: de::Deserialize + PartialEq + fmt::Debug, -{ - let mut de = Deserializer::new(tokens.into_iter()); - let v: Result = de::Deserialize::deserialize(&mut de); - assert_eq!(v.as_ref(), Ok(value)); - assert_eq!(de.tokens.next(), None); -} - -// Expect an error deserializing tokens into a T -pub fn assert_de_tokens_error(tokens: Vec>, error: Error) - where T: de::Deserialize + PartialEq + fmt::Debug, -{ - let mut de = Deserializer::new(tokens.into_iter()); - let v: Result = de::Deserialize::deserialize(&mut de); - assert_eq!(v, Err(error)); -} - -// Tests that the given token stream is ignorable when embedded in -// an otherwise normal struct -pub fn assert_de_tokens_ignore(ignorable_tokens: Vec>) { - #[derive(PartialEq, Debug, Deserialize)] - struct IgnoreBase { - a: i32, - } - - let expected = IgnoreBase{a: 1}; - - // Embed the tokens to be ignored in the normal token - // stream for an IgnoreBase type - let concated_tokens : Vec> = vec![ - Token::MapStart(Some(2)), - Token::MapSep, - Token::Str("a"), - Token::I32(1), - - Token::MapSep, - Token::Str("ignored") - ] - .into_iter() - .chain(ignorable_tokens.into_iter()) - .chain(vec![ - Token::MapEnd, - ].into_iter()) - .collect(); - - let mut de = Deserializer::new(concated_tokens.into_iter()); - let v: Result = de::Deserialize::deserialize(&mut de); - - // We run this test on every token stream for convenience, but - // some token streams don't make sense embedded as a map value, - // so we ignore those. SyntaxError is the real sign of trouble. - if let Err(Error::UnexpectedToken(_)) = v { - return; - } - - assert_eq!(v.as_ref(), Ok(&expected)); - assert_eq!(de.tokens.next(), None); -} - -pub fn assert_tokens(value: &T, tokens: Vec>) - where T: ser::Serialize + de::Deserialize + PartialEq + fmt::Debug, -{ - assert_ser_tokens(value, &tokens[..]); - assert_de_tokens(value, tokens); -} diff --git a/serde_test/src/error.rs b/serde_test/src/error.rs new file mode 100644 index 00000000..d5c4c6e7 --- /dev/null +++ b/serde_test/src/error.rs @@ -0,0 +1,87 @@ +use std::{error, fmt}; + +use serde::{ser, de}; + +use token::Token; + +#[derive(Clone, PartialEq, Debug)] +pub enum Error { + // Shared + Custom(String), + InvalidValue(String), + + // De + EndOfStream, + InvalidType(de::Type), + InvalidLength(usize), + UnknownVariant(String), + UnknownField(String), + MissingField(&'static str), + DuplicateField(&'static str), + InvalidName(&'static str), + UnexpectedToken(Token<'static>), +} + +impl ser::Error for Error { + fn custom>(msg: T) -> Error { + Error::Custom(msg.into()) + } + + fn invalid_value(msg: &str) -> Error { + Error::InvalidValue(msg.to_owned()) + } +} + +impl de::Error for Error { + fn custom>(msg: T) -> Error { + Error::Custom(msg.into()) + } + + fn end_of_stream() -> Error { + Error::EndOfStream + } + + fn invalid_type(ty: de::Type) -> Error { + Error::InvalidType(ty) + } + + fn invalid_value(msg: &str) -> Error { + Error::InvalidValue(msg.to_owned()) + } + + fn invalid_length(len: usize) -> Error { + Error::InvalidLength(len) + } + + fn unknown_variant(variant: &str) -> Error { + Error::UnknownVariant(variant.to_owned()) + } + + fn unknown_field(field: &str) -> Error { + Error::UnknownField(field.to_owned()) + } + + fn missing_field(field: &'static str) -> Error { + Error::MissingField(field) + } + + fn duplicate_field(field: &'static str) -> Error { + Error::DuplicateField(field) + } +} + +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} diff --git a/serde_test/src/lib.rs b/serde_test/src/lib.rs new file mode 100644 index 00000000..945262c0 --- /dev/null +++ b/serde_test/src/lib.rs @@ -0,0 +1,22 @@ +extern crate serde; + +mod assert; +pub use assert::{ + assert_tokens, + assert_ser_tokens, + assert_ser_tokens_error, + assert_de_tokens, + assert_de_tokens_error, +}; + +mod ser; +pub use ser::Serializer; + +mod de; +pub use de::Deserializer; + +mod token; +pub use token::Token; + +mod error; +pub use error::Error; diff --git a/serde_test/src/ser.rs b/serde_test/src/ser.rs new file mode 100644 index 00000000..2f028d3b --- /dev/null +++ b/serde_test/src/ser.rs @@ -0,0 +1,350 @@ +use std::marker::PhantomData; + +use serde::ser::{ + self, + MapVisitor, + SeqVisitor, + Serialize, +}; + +use error::Error; +use token::Token; + +pub struct Serializer<'a, I> + where I: Iterator>, +{ + tokens: I, + phantom: PhantomData<&'a Token<'a>>, +} + +impl<'a, I> Serializer<'a, I> + where I: Iterator>, +{ + pub fn new(tokens: I) -> Serializer<'a, I> { + Serializer { + tokens: tokens, + phantom: PhantomData, + } + } + + pub fn next_token(&mut self) -> Option<&'a Token<'a>> { + self.tokens.next() + } + + fn visit_seq(&mut self, mut visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::SeqEnd)); + + Ok(()) + } + + fn visit_map(&mut self, mut visitor: V) -> Result<(), Error> + where V: MapVisitor + { + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::MapEnd)); + + Ok(()) + } +} + +impl<'a, I> ser::Serializer for Serializer<'a, I> + where I: Iterator>, +{ + type Error = Error; + + fn serialize_unit(&mut self) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Unit)); + Ok(()) + } + + fn serialize_newtype_variant(&mut self, + name: &str, + _variant_index: usize, + variant: &str, + value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant))); + value.serialize(self) + } + + fn serialize_unit_struct(&mut self, name: &str) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name))); + Ok(()) + } + + fn serialize_unit_variant(&mut self, + name: &str, + _variant_index: usize, + variant: &str) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant))); + + Ok(()) + } + + fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Bool(v))); + Ok(()) + } + + fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Isize(v))); + Ok(()) + } + + fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::I8(v))); + Ok(()) + } + + fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::I16(v))); + Ok(()) + } + + fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::I32(v))); + Ok(()) + } + + fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::I64(v))); + Ok(()) + } + + fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Usize(v))); + Ok(()) + } + + fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::U8(v))); + Ok(()) + } + + fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::U16(v))); + Ok(()) + } + + fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::U32(v))); + Ok(()) + } + + fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::U64(v))); + Ok(()) + } + + fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::F32(v))); + Ok(()) + } + + fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::F64(v))); + Ok(()) + } + + fn serialize_char(&mut self, v: char) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Char(v))); + Ok(()) + } + + fn serialize_str(&mut self, v: &str) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Str(v))); + Ok(()) + } + + fn serialize_none(&mut self) -> Result<(), Error> { + assert_eq!(self.tokens.next(), Some(&Token::Option(false))); + Ok(()) + } + + fn serialize_some(&mut self, value: V) -> Result<(), Error> + where V: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::Option(true))); + value.serialize(self) + } + + + fn serialize_seq(&mut self, visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len))); + + self.visit_seq(visitor) + } + + fn serialize_fixed_size_array(&mut self, visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + let len = visitor.len().expect("arrays must have a length"); + + assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len))); + + self.visit_seq(visitor) + } + + fn serialize_seq_elt(&mut self, value: T) -> Result<(), Error> + where T: Serialize + { + assert_eq!(self.tokens.next(), Some(&Token::SeqSep)); + value.serialize(self) + } + + fn serialize_tuple(&mut self, mut visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + let len = visitor.len().expect("arrays must have a length"); + + assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len))); + + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::TupleEnd)); + + Ok(()) + } + + fn serialize_tuple_elt(&mut self, value: T) -> Result<(), Error> + where T: Serialize + { + assert_eq!(self.tokens.next(), Some(&Token::TupleSep)); + value.serialize(self) + } + + fn serialize_newtype_struct(&mut self, + name: &'static str, + value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name))); + value.serialize(self) + } + + fn serialize_tuple_struct(&mut self, name: &str, mut visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len))); + + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd)); + + Ok(()) + } + + fn serialize_tuple_struct_elt(&mut self, value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep)); + value.serialize(self) + } + + fn serialize_tuple_variant(&mut self, + name: &str, + _variant_index: usize, + variant: &str, + mut visitor: V) -> Result<(), Error> + where V: SeqVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len))); + + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd)); + + Ok(()) + } + + fn serialize_tuple_variant_elt(&mut self, value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep)); + value.serialize(self) + } + + fn serialize_map(&mut self, visitor: V) -> Result<(), Error> + where V: MapVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::MapStart(len))); + + self.visit_map(visitor) + } + + fn serialize_map_elt(&mut self, key: K, value: V) -> Result<(), Error> + where K: Serialize, + V: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::MapSep)); + + try!(key.serialize(self)); + value.serialize(self) + } + + fn serialize_struct(&mut self, name: &str, mut visitor: V) -> Result<(), Error> + where V: MapVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len))); + + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::StructEnd)); + + Ok(()) + } + + fn serialize_struct_elt(&mut self, key: &'static str, value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::StructSep)); + + try!(key.serialize(self)); + value.serialize(self) + } + + fn serialize_struct_variant(&mut self, + name: &str, + _variant_index: usize, + variant: &str, + mut visitor: V) -> Result<(), Error> + where V: MapVisitor + { + let len = visitor.len(); + + assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len))); + + while let Some(()) = try!(visitor.visit(self)) { } + + assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd)); + + Ok(()) + } + + fn serialize_struct_variant_elt(&mut self, key: &'static str, value: T) -> Result<(), Error> + where T: Serialize, + { + assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep)); + + try!(key.serialize(self)); + value.serialize(self) + } +} diff --git a/serde_test/src/token.rs b/serde_test/src/token.rs new file mode 100644 index 00000000..dcb1137f --- /dev/null +++ b/serde_test/src/token.rs @@ -0,0 +1,60 @@ +#[derive(Clone, PartialEq, Debug)] +pub enum Token<'a> { + Bool(bool), + Isize(isize), + I8(i8), + I16(i16), + I32(i32), + I64(i64), + Usize(usize), + U8(u8), + U16(u16), + U32(u32), + U64(u64), + F32(f32), + F64(f64), + Char(char), + Str(&'a str), + String(String), + Bytes(&'a [u8]), + + Option(bool), + + Unit, + UnitStruct(&'a str), + + StructNewType(&'a str), + + EnumStart(&'a str), + EnumUnit(&'a str, &'a str), + EnumNewType(&'a str, &'a str), + + SeqStart(Option), + SeqArrayStart(usize), + SeqSep, + SeqEnd, + + TupleStart(usize), + TupleSep, + TupleEnd, + + TupleStructStart(&'a str, Option), + TupleStructSep, + TupleStructEnd, + + MapStart(Option), + MapSep, + MapEnd, + + StructStart(&'a str, Option), + StructSep, + StructEnd, + + EnumSeqStart(&'a str, &'a str, Option), + EnumSeqSep, + EnumSeqEnd, + + EnumMapStart(&'a str, &'a str, Option), + EnumMapSep, + EnumMapEnd, +} diff --git a/serde_tests/Cargo.toml b/testing/Cargo.toml similarity index 91% rename from serde_tests/Cargo.toml rename to testing/Cargo.toml index 2f28417a..a32b2f9f 100644 --- a/serde_tests/Cargo.toml +++ b/testing/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "serde_tests" +name = "serde_testing" version = "0.7.11" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" @@ -20,6 +20,7 @@ serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-sy fnv = "1.0" rustc-serialize = "^0.3.16" serde = { version = "*", path = "../serde" } +serde_test = { version = "*", path = "../serde_test" } [dependencies] clippy = { version = "^0.*", optional = true } diff --git a/serde_tests/benches/bench.rs b/testing/benches/bench.rs similarity index 100% rename from serde_tests/benches/bench.rs rename to testing/benches/bench.rs diff --git a/serde_tests/benches/bench.rs.in b/testing/benches/bench.rs.in similarity index 100% rename from serde_tests/benches/bench.rs.in rename to testing/benches/bench.rs.in diff --git a/serde_tests/benches/bench_enum.rs b/testing/benches/bench_enum.rs similarity index 100% rename from serde_tests/benches/bench_enum.rs rename to testing/benches/bench_enum.rs diff --git a/serde_tests/benches/bench_map.rs b/testing/benches/bench_map.rs similarity index 100% rename from serde_tests/benches/bench_map.rs rename to testing/benches/bench_map.rs diff --git a/serde_tests/benches/bench_struct.rs b/testing/benches/bench_struct.rs similarity index 100% rename from serde_tests/benches/bench_struct.rs rename to testing/benches/bench_struct.rs diff --git a/serde_tests/benches/bench_vec.rs b/testing/benches/bench_vec.rs similarity index 100% rename from serde_tests/benches/bench_vec.rs rename to testing/benches/bench_vec.rs diff --git a/serde_tests/build.rs b/testing/build.rs similarity index 100% rename from serde_tests/build.rs rename to testing/build.rs diff --git a/serde_tests/tests/macros.rs b/testing/tests/macros.rs similarity index 96% rename from serde_tests/tests/macros.rs rename to testing/tests/macros.rs index 2446150b..8ed4bf1d 100644 --- a/serde_tests/tests/macros.rs +++ b/testing/tests/macros.rs @@ -5,7 +5,7 @@ macro_rules! declare_ser_tests { #[test] fn $name() { $( - ::token::assert_ser_tokens(&$value, $tokens); + assert_ser_tokens(&$value, $tokens); )+ } )+ diff --git a/serde_tests/tests/test.rs b/testing/tests/test.rs similarity index 100% rename from serde_tests/tests/test.rs rename to testing/tests/test.rs diff --git a/serde_tests/tests/test.rs.in b/testing/tests/test.rs.in similarity index 91% rename from serde_tests/tests/test.rs.in rename to testing/tests/test.rs.in index f64b84c1..32c00054 100644 --- a/serde_tests/tests/test.rs.in +++ b/testing/tests/test.rs.in @@ -1,8 +1,6 @@ #[macro_use] mod macros; -mod token; - mod test_annotations; mod test_bytes; mod test_de; diff --git a/serde_tests/tests/test_annotations.rs b/testing/tests/test_annotations.rs similarity index 97% rename from serde_tests/tests/test_annotations.rs rename to testing/tests/test_annotations.rs index 79e1683b..0a795725 100644 --- a/serde_tests/tests/test_annotations.rs +++ b/testing/tests/test_annotations.rs @@ -1,7 +1,8 @@ extern crate serde; use self::serde::{Serialize, Serializer, Deserialize, Deserializer}; -use token::{ +extern crate serde_test; +use self::serde_test::{ Error, Token, assert_tokens, @@ -80,7 +81,7 @@ struct DefaultStruct fn test_default_struct() { assert_de_tokens( &DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 }, - vec![ + &[ Token::StructStart("DefaultStruct", Some(3)), Token::StructSep, @@ -109,7 +110,7 @@ fn test_default_struct() { assert_de_tokens( &DefaultStruct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 }, - vec![ + &[ Token::StructStart("DefaultStruct", Some(1)), Token::StructSep, @@ -143,7 +144,7 @@ enum DefaultEnum fn test_default_enum() { assert_de_tokens( &DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 }, - vec![ + &[ Token::EnumMapStart("DefaultEnum", "Struct", Some(5)), Token::EnumMapSep, @@ -172,7 +173,7 @@ fn test_default_enum() { assert_de_tokens( &DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 }, - vec![ + &[ Token::EnumMapStart("DefaultEnum", "Struct", Some(5)), Token::EnumMapSep, @@ -206,7 +207,7 @@ struct ContainsNoStdDefault { fn test_no_std_default() { assert_de_tokens( &ContainsNoStdDefault { a: NoStdDefault(123) }, - vec![ + &[ Token::StructStart("ContainsNoStdDefault", Some(1)), Token::StructEnd, ] @@ -214,7 +215,7 @@ fn test_no_std_default() { assert_de_tokens( &ContainsNoStdDefault { a: NoStdDefault(8) }, - vec![ + &[ Token::StructStart("ContainsNoStdDefault", Some(1)), Token::StructSep, @@ -279,7 +280,7 @@ fn test_elt_not_deserialize() { c: NotDeserializeStruct(123), e: NotDeserializeEnum::Trouble, }, - vec![ + &[ Token::StructStart("ContainsNotDeserialize", Some(3)), Token::StructEnd, ] @@ -297,7 +298,7 @@ fn test_ignore_unknown() { // 'Default' allows unknown. Basic smoke test of ignore... assert_de_tokens( &DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 }, - vec![ + &[ Token::StructStart("DefaultStruct", Some(5)), Token::StructSep, @@ -332,7 +333,7 @@ fn test_ignore_unknown() { ); assert_de_tokens_error::( - vec![ + &[ Token::StructStart("DenyUnknown", Some(2)), Token::StructSep, @@ -341,11 +342,8 @@ fn test_ignore_unknown() { Token::StructSep, Token::Str("whoops"), - Token::I32(2), - - Token::StructEnd, ], - Error::UnknownFieldError("whoops".to_owned()) + Error::UnknownField("whoops".to_owned()) ); } @@ -369,7 +367,7 @@ struct RenameStructSerializeDeserialize { fn test_rename_struct() { assert_tokens( &RenameStruct { a1: 1, a2: 2 }, - vec![ + &[ Token::StructStart("Superhero", Some(2)), Token::StructSep, @@ -403,7 +401,7 @@ fn test_rename_struct() { assert_de_tokens( &RenameStructSerializeDeserialize { a1: 1, a2: 2 }, - vec![ + &[ Token::StructStart("SuperheroDe", Some(2)), Token::StructSep, @@ -451,14 +449,14 @@ enum RenameEnumSerializeDeserialize { fn test_rename_enum() { assert_tokens( &RenameEnum::Batman, - vec![ + &[ Token::EnumUnit("Superhero", "bruce_wayne"), ] ); assert_tokens( &RenameEnum::Superman(0), - vec![ + &[ Token::EnumNewType("Superhero", "clark_kent"), Token::I8(0), ] @@ -466,7 +464,7 @@ fn test_rename_enum() { assert_tokens( &RenameEnum::WonderWoman(0, 1), - vec![ + &[ Token::EnumSeqStart("Superhero", "diana_prince", Some(2)), Token::EnumSeqSep, @@ -481,7 +479,7 @@ fn test_rename_enum() { assert_tokens( &RenameEnum::Flash { a: 1 }, - vec![ + &[ Token::EnumMapStart("Superhero", "barry_allan", Some(1)), Token::EnumMapSep, @@ -517,7 +515,7 @@ fn test_rename_enum() { a: 0, b: String::new(), }, - vec![ + &[ Token::EnumMapStart("SuperheroDe", "jason_todd", Some(2)), Token::EnumMapSep, @@ -807,7 +805,7 @@ fn test_deserialize_with_struct() { a: 1, b: 2, }, - vec![ + &[ Token::StructStart("DeserializeWithStruct", Some(2)), Token::StructSep, @@ -827,7 +825,7 @@ fn test_deserialize_with_struct() { a: 1, b: 123, }, - vec![ + &[ Token::StructStart("DeserializeWithStruct", Some(2)), Token::StructSep, @@ -859,7 +857,7 @@ fn test_deserialize_with_enum() { a: 1, b: 2, }, - vec![ + &[ Token::EnumMapStart("DeserializeWithEnum", "Struct", Some(2)), Token::EnumMapSep, @@ -879,7 +877,7 @@ fn test_deserialize_with_enum() { a: 1, b: 123, }, - vec![ + &[ Token::EnumMapStart("DeserializeWithEnum", "Struct", Some(2)), Token::EnumMapSep, @@ -898,7 +896,7 @@ fn test_deserialize_with_enum() { #[test] fn test_missing_renamed_field_struct() { assert_de_tokens_error::( - vec![ + &[ Token::StructStart("Superhero", Some(2)), Token::StructSep, @@ -907,11 +905,11 @@ fn test_missing_renamed_field_struct() { Token::StructEnd, ], - Error::MissingFieldError("a3"), + Error::MissingField("a3"), ); assert_de_tokens_error::( - vec![ + &[ Token::StructStart("SuperheroDe", Some(2)), Token::StructSep, @@ -920,23 +918,23 @@ fn test_missing_renamed_field_struct() { Token::StructEnd, ], - Error::MissingFieldError("a5"), + Error::MissingField("a5"), ); } #[test] fn test_missing_renamed_field_enum() { assert_de_tokens_error::( - vec![ + &[ Token::EnumMapStart("Superhero", "barry_allan", Some(1)), Token::EnumMapEnd, ], - Error::MissingFieldError("b"), + Error::MissingField("b"), ); assert_de_tokens_error::>( - vec![ + &[ Token::EnumMapStart("SuperheroDe", "jason_todd", Some(2)), Token::EnumMapSep, @@ -945,6 +943,6 @@ fn test_missing_renamed_field_enum() { Token::EnumMapEnd, ], - Error::MissingFieldError("d"), + Error::MissingField("d"), ); } diff --git a/serde_tests/tests/test_bytes.rs b/testing/tests/test_bytes.rs similarity index 100% rename from serde_tests/tests/test_bytes.rs rename to testing/tests/test_bytes.rs diff --git a/serde_tests/tests/test_de.rs b/testing/tests/test_de.rs similarity index 74% rename from serde_tests/tests/test_de.rs rename to testing/tests/test_de.rs index 2b9d0035..fcd17448 100644 --- a/serde_tests/tests/test_de.rs +++ b/testing/tests/test_de.rs @@ -2,14 +2,17 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::net; use std::path::PathBuf; +extern crate serde; +use self::serde::Deserialize; + extern crate fnv; use self::fnv::FnvHasher; -use token::{ +extern crate serde_test; +use self::serde_test::{ Error, Token, assert_de_tokens, - assert_de_tokens_ignore, assert_de_tokens_error, }; @@ -73,107 +76,147 @@ macro_rules! declare_error_tests { } } +fn assert_de_tokens_ignore(ignorable_tokens: &[Token<'static>]) { + #[derive(PartialEq, Debug, Deserialize)] + struct IgnoreBase { + a: i32, + } + + let expected = IgnoreBase{a: 1}; + + // Embed the tokens to be ignored in the normal token + // stream for an IgnoreBase type + let concated_tokens : Vec> = vec![ + Token::MapStart(Some(2)), + Token::MapSep, + Token::Str("a"), + Token::I32(1), + + Token::MapSep, + 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.into_iter()); + let v: Result = Deserialize::deserialize(&mut de); + + // We run this test on every token stream for convenience, but + // some token streams don't make sense embedded as a map value, + // so we ignore those. SyntaxError is the real sign of trouble. + if let Err(Error::UnexpectedToken(_)) = v { + return; + } + + assert_eq!(v.as_ref(), Ok(&expected)); + assert_eq!(de.next_token(), None); +} + ////////////////////////////////////////////////////////////////////////// declare_tests! { test_bool { - true => vec![Token::Bool(true)], - false => vec![Token::Bool(false)], + true => &[Token::Bool(true)], + false => &[Token::Bool(false)], } test_isize { - 0isize => vec![Token::Isize(0)], - 0isize => vec![Token::I8(0)], - 0isize => vec![Token::I16(0)], - 0isize => vec![Token::I32(0)], - 0isize => vec![Token::I64(0)], - 0isize => vec![Token::Usize(0)], - 0isize => vec![Token::U8(0)], - 0isize => vec![Token::U16(0)], - 0isize => vec![Token::U32(0)], - 0isize => vec![Token::U64(0)], - 0isize => vec![Token::F32(0.)], - 0isize => vec![Token::F64(0.)], + 0isize => &[Token::Isize(0)], + 0isize => &[Token::I8(0)], + 0isize => &[Token::I16(0)], + 0isize => &[Token::I32(0)], + 0isize => &[Token::I64(0)], + 0isize => &[Token::Usize(0)], + 0isize => &[Token::U8(0)], + 0isize => &[Token::U16(0)], + 0isize => &[Token::U32(0)], + 0isize => &[Token::U64(0)], + 0isize => &[Token::F32(0.)], + 0isize => &[Token::F64(0.)], } test_ints { - 0isize => vec![Token::Isize(0)], - 0i8 => vec![Token::I8(0)], - 0i16 => vec![Token::I16(0)], - 0i32 => vec![Token::I32(0)], - 0i64 => vec![Token::I64(0)], + 0isize => &[Token::Isize(0)], + 0i8 => &[Token::I8(0)], + 0i16 => &[Token::I16(0)], + 0i32 => &[Token::I32(0)], + 0i64 => &[Token::I64(0)], } test_uints { - 0usize => vec![Token::Usize(0)], - 0u8 => vec![Token::U8(0)], - 0u16 => vec![Token::U16(0)], - 0u32 => vec![Token::U32(0)], - 0u64 => vec![Token::U64(0)], + 0usize => &[Token::Usize(0)], + 0u8 => &[Token::U8(0)], + 0u16 => &[Token::U16(0)], + 0u32 => &[Token::U32(0)], + 0u64 => &[Token::U64(0)], } test_floats { - 0f32 => vec![Token::F32(0.)], - 0f64 => vec![Token::F64(0.)], + 0f32 => &[Token::F32(0.)], + 0f64 => &[Token::F64(0.)], } test_char { - 'a' => vec![Token::Char('a')], - 'a' => vec![Token::Str("a")], - 'a' => vec![Token::String("a".to_owned())], + 'a' => &[Token::Char('a')], + 'a' => &[Token::Str("a")], + 'a' => &[Token::String("a".to_owned())], } test_string { - "abc".to_owned() => vec![Token::Str("abc")], - "abc".to_owned() => vec![Token::String("abc".to_owned())], - "a".to_owned() => vec![Token::Char('a')], + "abc".to_owned() => &[Token::Str("abc")], + "abc".to_owned() => &[Token::String("abc".to_owned())], + "a".to_owned() => &[Token::Char('a')], } test_option { - None:: => vec![Token::Unit], - None:: => vec![Token::Option(false)], - Some(1) => vec![Token::I32(1)], - Some(1) => vec![ + None:: => &[Token::Unit], + None:: => &[Token::Option(false)], + Some(1) => &[Token::I32(1)], + Some(1) => &[ Token::Option(true), Token::I32(1), ], } test_result { - Ok::(0) => vec![ + Ok::(0) => &[ Token::EnumStart("Result"), Token::Str("Ok"), Token::I32(0), ], - Err::(1) => vec![ + Err::(1) => &[ Token::EnumStart("Result"), Token::Str("Err"), Token::I32(1), ], } test_unit { - () => vec![Token::Unit], - () => vec![ + () => &[Token::Unit], + () => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - () => vec![ + () => &[ Token::SeqStart(None), Token::SeqEnd, ], - () => vec![ + () => &[ Token::TupleStructStart("Anything", Some(0)), Token::SeqEnd, ], } test_unit_struct { - UnitStruct => vec![Token::Unit], - UnitStruct => vec![ + UnitStruct => &[Token::Unit], + UnitStruct => &[ Token::UnitStruct("UnitStruct"), ], - UnitStruct => vec![ + UnitStruct => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - UnitStruct => vec![ + UnitStruct => &[ Token::SeqStart(None), Token::SeqEnd, ], } test_tuple_struct { - TupleStruct(1, 2, 3) => vec![ + TupleStruct(1, 2, 3) => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(1), @@ -185,7 +228,7 @@ declare_tests! { Token::I32(3), Token::SeqEnd, ], - TupleStruct(1, 2, 3) => vec![ + TupleStruct(1, 2, 3) => &[ Token::SeqStart(None), Token::SeqSep, Token::I32(1), @@ -197,7 +240,7 @@ declare_tests! { Token::I32(3), Token::SeqEnd, ], - TupleStruct(1, 2, 3) => vec![ + TupleStruct(1, 2, 3) => &[ Token::TupleStructStart("TupleStruct", Some(3)), Token::TupleStructSep, Token::I32(1), @@ -209,7 +252,7 @@ declare_tests! { Token::I32(3), Token::TupleStructEnd, ], - TupleStruct(1, 2, 3) => vec![ + TupleStruct(1, 2, 3) => &[ Token::TupleStructStart("TupleStruct", None), Token::TupleStructSep, Token::I32(1), @@ -223,14 +266,14 @@ declare_tests! { ], } test_btreeset { - BTreeSet::::new() => vec![ + BTreeSet::::new() => &[ Token::Unit, ], - BTreeSet::::new() => vec![ + BTreeSet::::new() => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => vec![ + btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::SeqStart(Some(0)), @@ -252,23 +295,23 @@ declare_tests! { Token::SeqEnd, Token::SeqEnd, ], - BTreeSet::::new() => vec![ + BTreeSet::::new() => &[ Token::UnitStruct("Anything"), ], - BTreeSet::::new() => vec![ + BTreeSet::::new() => &[ Token::TupleStructStart("Anything", Some(0)), Token::SeqEnd, ], } test_hashset { - HashSet::::new() => vec![ + HashSet::::new() => &[ Token::Unit, ], - HashSet::::new() => vec![ + HashSet::::new() => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - hashset![1, 2, 3] => vec![ + hashset![1, 2, 3] => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(1), @@ -280,14 +323,14 @@ declare_tests! { Token::I32(3), Token::SeqEnd, ], - HashSet::::new() => vec![ + HashSet::::new() => &[ Token::UnitStruct("Anything"), ], - HashSet::::new() => vec![ + HashSet::::new() => &[ Token::TupleStructStart("Anything", Some(0)), Token::SeqEnd, ], - hashset![FnvHasher @ 1, 2, 3] => vec![ + hashset![FnvHasher @ 1, 2, 3] => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(1), @@ -301,14 +344,14 @@ declare_tests! { ], } test_vec { - Vec::::new() => vec![ + Vec::::new() => &[ Token::Unit, ], - Vec::::new() => vec![ + Vec::::new() => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - vec![vec![], vec![1], vec![2, 3]] => vec![ + vec![vec![], vec![1], vec![2, 3]] => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::SeqStart(Some(0)), @@ -330,27 +373,27 @@ declare_tests! { Token::SeqEnd, Token::SeqEnd, ], - Vec::::new() => vec![ + Vec::::new() => &[ Token::UnitStruct("Anything"), ], - Vec::::new() => vec![ + Vec::::new() => &[ Token::TupleStructStart("Anything", Some(0)), Token::SeqEnd, ], } test_array { - [0; 0] => vec![ + [0; 0] => &[ Token::Unit, ], - [0; 0] => vec![ + [0; 0] => &[ Token::SeqStart(Some(0)), Token::SeqEnd, ], - [0; 0] => vec![ + [0; 0] => &[ Token::SeqArrayStart(0), Token::SeqEnd, ], - ([0; 0], [1], [2, 3]) => vec![ + ([0; 0], [1], [2, 3]) => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::SeqStart(Some(0)), @@ -372,7 +415,7 @@ declare_tests! { Token::SeqEnd, Token::SeqEnd, ], - ([0; 0], [1], [2, 3]) => vec![ + ([0; 0], [1], [2, 3]) => &[ Token::SeqArrayStart(3), Token::SeqSep, Token::SeqArrayStart(0), @@ -394,22 +437,22 @@ declare_tests! { Token::SeqEnd, Token::SeqEnd, ], - [0; 0] => vec![ + [0; 0] => &[ Token::UnitStruct("Anything"), ], - [0; 0] => vec![ + [0; 0] => &[ Token::TupleStructStart("Anything", Some(0)), Token::SeqEnd, ], } test_tuple { - (1,) => vec![ + (1,) => &[ Token::SeqStart(Some(1)), Token::SeqSep, Token::I32(1), Token::SeqEnd, ], - (1, 2, 3) => vec![ + (1, 2, 3) => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(1), @@ -421,13 +464,13 @@ declare_tests! { Token::I32(3), Token::SeqEnd, ], - (1,) => vec![ + (1,) => &[ Token::TupleStart(1), Token::TupleSep, Token::I32(1), Token::TupleEnd, ], - (1, 2, 3) => vec![ + (1, 2, 3) => &[ Token::TupleStart(3), Token::TupleSep, Token::I32(1), @@ -441,21 +484,21 @@ declare_tests! { ], } test_btreemap { - BTreeMap::::new() => vec![ + BTreeMap::::new() => &[ Token::Unit, ], - BTreeMap::::new() => vec![ + BTreeMap::::new() => &[ Token::MapStart(Some(0)), Token::MapEnd, ], - btreemap![1 => 2] => vec![ + btreemap![1 => 2] => &[ Token::MapStart(Some(1)), Token::MapSep, Token::I32(1), Token::I32(2), Token::MapEnd, ], - btreemap![1 => 2, 3 => 4] => vec![ + btreemap![1 => 2, 3 => 4] => &[ Token::MapStart(Some(2)), Token::MapSep, Token::I32(1), @@ -466,7 +509,7 @@ declare_tests! { Token::I32(4), Token::MapEnd, ], - btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => vec![ + btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => &[ Token::MapStart(Some(2)), Token::MapSep, Token::I32(1), @@ -486,30 +529,30 @@ declare_tests! { Token::MapEnd, Token::MapEnd, ], - BTreeMap::::new() => vec![ + BTreeMap::::new() => &[ Token::UnitStruct("Anything"), ], - BTreeMap::::new() => vec![ + BTreeMap::::new() => &[ Token::StructStart("Anything", Some(0)), Token::MapEnd, ], } test_hashmap { - HashMap::::new() => vec![ + HashMap::::new() => &[ Token::Unit, ], - HashMap::::new() => vec![ + HashMap::::new() => &[ Token::MapStart(Some(0)), Token::MapEnd, ], - hashmap![1 => 2] => vec![ + hashmap![1 => 2] => &[ Token::MapStart(Some(1)), Token::MapSep, Token::I32(1), Token::I32(2), Token::MapEnd, ], - hashmap![1 => 2, 3 => 4] => vec![ + hashmap![1 => 2, 3 => 4] => &[ Token::MapStart(Some(2)), Token::MapSep, Token::I32(1), @@ -520,7 +563,7 @@ declare_tests! { Token::I32(4), Token::MapEnd, ], - hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => vec![ + hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => &[ Token::MapStart(Some(2)), Token::MapSep, Token::I32(1), @@ -540,14 +583,14 @@ declare_tests! { Token::MapEnd, Token::MapEnd, ], - HashMap::::new() => vec![ + HashMap::::new() => &[ Token::UnitStruct("Anything"), ], - HashMap::::new() => vec![ + HashMap::::new() => &[ Token::StructStart("Anything", Some(0)), Token::MapEnd, ], - hashmap![FnvHasher @ 1 => 2, 3 => 4] => vec![ + hashmap![FnvHasher @ 1 => 2, 3 => 4] => &[ Token::MapStart(Some(2)), Token::MapSep, Token::I32(1), @@ -560,7 +603,7 @@ declare_tests! { ], } test_struct { - Struct { a: 1, b: 2, c: 0 } => vec![ + Struct { a: 1, b: 2, c: 0 } => &[ Token::MapStart(Some(3)), Token::MapSep, Token::Str("a"), @@ -571,7 +614,7 @@ declare_tests! { Token::I32(2), Token::MapEnd, ], - Struct { a: 1, b: 2, c: 0 } => vec![ + Struct { a: 1, b: 2, c: 0 } => &[ Token::StructStart("Struct", Some(3)), Token::StructSep, Token::Str("a"), @@ -582,7 +625,7 @@ declare_tests! { Token::I32(2), Token::StructEnd, ], - Struct { a: 1, b: 2, c: 0 } => vec![ + Struct { a: 1, b: 2, c: 0 } => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(1), @@ -593,7 +636,7 @@ declare_tests! { ], } test_struct_with_skip { - Struct { a: 1, b: 2, c: 0 } => vec![ + Struct { a: 1, b: 2, c: 0 } => &[ Token::MapStart(Some(3)), Token::MapSep, Token::Str("a"), @@ -612,7 +655,7 @@ declare_tests! { Token::I32(4), Token::MapEnd, ], - Struct { a: 1, b: 2, c: 0 } => vec![ + Struct { a: 1, b: 2, c: 0 } => &[ Token::StructStart("Struct", Some(3)), Token::StructSep, Token::Str("a"), @@ -633,18 +676,18 @@ declare_tests! { ], } test_enum_unit { - Enum::Unit => vec![ + Enum::Unit => &[ Token::EnumUnit("Enum", "Unit"), ], } test_enum_simple { - Enum::Simple(1) => vec![ + Enum::Simple(1) => &[ Token::EnumNewType("Enum", "Simple"), Token::I32(1), ], } test_enum_seq { - Enum::Seq(1, 2, 3) => vec![ + Enum::Seq(1, 2, 3) => &[ Token::EnumSeqStart("Enum", "Seq", Some(3)), Token::EnumSeqSep, Token::I32(1), @@ -658,7 +701,7 @@ declare_tests! { ], } test_enum_map { - Enum::Map { a: 1, b: 2, c: 3 } => vec![ + Enum::Map { a: 1, b: 2, c: 3 } => &[ Token::EnumMapStart("Enum", "Map", Some(3)), Token::EnumMapSep, Token::Str("a"), @@ -675,24 +718,24 @@ declare_tests! { ], } test_enum_unit_usize { - Enum::Unit => vec![ + Enum::Unit => &[ Token::EnumStart("Enum"), Token::Usize(0), Token::Unit, ], } test_enum_unit_bytes { - Enum::Unit => vec![ + Enum::Unit => &[ Token::EnumStart("Enum"), Token::Bytes(b"Unit"), Token::Unit, ], } test_box { - Box::new(0i32) => vec![Token::I32(0)], + Box::new(0i32) => &[Token::I32(0)], } test_boxed_slice { - Box::new([0, 1, 2]) => vec![ + Box::new([0, 1, 2]) => &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(0), @@ -704,18 +747,18 @@ declare_tests! { ], } test_net_ipv4addr { - "1.2.3.4".parse::().unwrap() => vec![Token::Str("1.2.3.4")], + "1.2.3.4".parse::().unwrap() => &[Token::Str("1.2.3.4")], } test_net_ipv6addr { - "::1".parse::().unwrap() => vec![Token::Str("::1")], + "::1".parse::().unwrap() => &[Token::Str("::1")], } test_net_socketaddr { - "1.2.3.4:1234".parse::().unwrap() => vec![Token::Str("1.2.3.4:1234")], - "1.2.3.4:1234".parse::().unwrap() => vec![Token::Str("1.2.3.4:1234")], - "[::1]:1234".parse::().unwrap() => vec![Token::Str("[::1]:1234")], + "1.2.3.4:1234".parse::().unwrap() => &[Token::Str("1.2.3.4:1234")], + "1.2.3.4:1234".parse::().unwrap() => &[Token::Str("1.2.3.4:1234")], + "[::1]:1234".parse::().unwrap() => &[Token::Str("[::1]:1234")], } test_path_buf { - PathBuf::from("/usr/local/lib") => vec![ + PathBuf::from("/usr/local/lib") => &[ Token::String("/usr/local/lib".to_owned()), ], } @@ -726,30 +769,28 @@ declare_tests! { fn test_net_ipaddr() { assert_de_tokens( "1.2.3.4".parse::().unwrap(), - vec![Token::Str("1.2.3.4")], + &[Token::Str("1.2.3.4")], ); } declare_error_tests! { test_unknown_variant { - vec![ + &[ Token::EnumUnit("Enum", "Foo"), ], - Error::UnknownVariantError("Foo".to_owned()), + Error::UnknownVariant("Foo".to_owned()), } test_struct_seq_too_long { - vec![ + &[ Token::SeqStart(Some(4)), Token::SeqSep, Token::I32(1), Token::SeqSep, Token::I32(2), Token::SeqSep, Token::I32(3), - Token::SeqSep, Token::I32(4), - Token::SeqEnd, ], Error::UnexpectedToken(Token::SeqSep), } test_duplicate_field_struct { - vec![ + &[ Token::MapStart(Some(3)), Token::MapSep, Token::Str("a"), @@ -757,13 +798,11 @@ declare_error_tests! { Token::MapSep, Token::Str("a"), - Token::I32(3), - Token::MapEnd, ], - Error::DuplicateFieldError("a"), + Error::DuplicateField("a"), } test_duplicate_field_enum { - vec![ + &[ Token::EnumMapStart("Enum", "Map", Some(3)), Token::EnumMapSep, Token::Str("a"), @@ -771,9 +810,7 @@ declare_error_tests! { Token::EnumMapSep, Token::Str("a"), - Token::I32(3), - Token::EnumMapEnd, ], - Error::DuplicateFieldError("a"), + Error::DuplicateField("a"), } } diff --git a/serde_tests/tests/test_gen.rs b/testing/tests/test_gen.rs similarity index 100% rename from serde_tests/tests/test_gen.rs rename to testing/tests/test_gen.rs diff --git a/serde_tests/tests/test_macros.rs b/testing/tests/test_macros.rs similarity index 97% rename from serde_tests/tests/test_macros.rs rename to testing/tests/test_macros.rs index 491acca2..ca4dffaa 100644 --- a/serde_tests/tests/test_macros.rs +++ b/testing/tests/test_macros.rs @@ -1,11 +1,12 @@ -use std::marker::PhantomData; -use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens}; +extern crate serde_test; +use self::serde_test::{ + Token, + assert_tokens, + assert_ser_tokens, + assert_de_tokens, +}; -/* -trait Trait { - type Type; -} -*/ +use std::marker::PhantomData; // That tests that the derived Serialize implementation doesn't trigger // any warning about `serializer` not being used, in case of empty enums. @@ -161,7 +162,7 @@ struct DefaultTyParam = i32> { fn test_named_unit() { assert_tokens( &NamedUnit, - vec![Token::UnitStruct("NamedUnit")] + &[Token::UnitStruct("NamedUnit")] ); } @@ -192,7 +193,7 @@ fn test_ser_named_tuple() { fn test_de_named_tuple() { assert_de_tokens( &DeNamedTuple(5, 6, 7), - vec![ + &[ Token::SeqStart(Some(3)), Token::SeqSep, Token::I32(5), @@ -209,7 +210,7 @@ fn test_de_named_tuple() { assert_de_tokens( &DeNamedTuple(5, 6, 7), - vec![ + &[ Token::TupleStructStart("DeNamedTuple", Some(3)), Token::TupleStructSep, Token::I32(5), @@ -265,7 +266,7 @@ fn test_de_named_map() { b: 6, c: 7, }, - vec![ + &[ Token::StructStart("DeNamedMap", Some(3)), Token::StructSep, @@ -379,7 +380,7 @@ fn test_ser_enum_map() { fn test_de_enum_unit() { assert_tokens( &DeEnum::Unit::, - vec![ + &[ Token::EnumUnit("DeEnum", "Unit"), ], ); @@ -403,7 +404,7 @@ fn test_de_enum_seq() { e, //f, ), - vec![ + &[ Token::EnumSeqStart("DeEnum", "Seq", Some(4)), Token::EnumSeqSep, @@ -441,7 +442,7 @@ fn test_de_enum_map() { e: e, //f: f, }, - vec![ + &[ Token::EnumMapStart("DeEnum", "Map", Some(4)), Token::EnumMapSep, @@ -516,7 +517,7 @@ fn test_lifetimes() { fn test_generic_struct() { assert_tokens( &GenericStruct { x: 5u32 }, - vec![ + &[ Token::StructStart("GenericStruct", Some(1)), Token::StructSep, @@ -532,7 +533,7 @@ fn test_generic_struct() { fn test_generic_newtype_struct() { assert_tokens( &GenericNewTypeStruct(5u32), - vec![ + &[ Token::StructNewType("GenericNewTypeStruct"), Token::U32(5), ] @@ -543,7 +544,7 @@ fn test_generic_newtype_struct() { fn test_generic_tuple_struct() { assert_tokens( &GenericTupleStruct(5u32, 6u32), - vec![ + &[ Token::TupleStructStart("GenericTupleStruct", Some(2)), Token::TupleStructSep, @@ -561,7 +562,7 @@ fn test_generic_tuple_struct() { fn test_generic_enum_unit() { assert_tokens( &GenericEnum::Unit::, - vec![ + &[ Token::EnumUnit("GenericEnum", "Unit"), ] ); @@ -571,7 +572,7 @@ fn test_generic_enum_unit() { fn test_generic_enum_newtype() { assert_tokens( &GenericEnum::NewType::(5), - vec![ + &[ Token::EnumNewType("GenericEnum", "NewType"), Token::U32(5), ] @@ -582,7 +583,7 @@ fn test_generic_enum_newtype() { fn test_generic_enum_seq() { assert_tokens( &GenericEnum::Seq::(5, 6), - vec![ + &[ Token::EnumSeqStart("GenericEnum", "Seq", Some(2)), Token::EnumSeqSep, @@ -600,7 +601,7 @@ fn test_generic_enum_seq() { fn test_generic_enum_map() { assert_tokens( &GenericEnum::Map:: { x: 5, y: 6 }, - vec![ + &[ Token::EnumMapStart("GenericEnum", "Map", Some(2)), Token::EnumMapSep, @@ -620,7 +621,7 @@ fn test_generic_enum_map() { fn test_default_ty_param() { assert_tokens( &DefaultTyParam:: { phantom: PhantomData }, - vec![ + &[ Token::StructStart("DefaultTyParam", Some(1)), Token::StructSep, diff --git a/serde_tests/tests/test_ser.rs b/testing/tests/test_ser.rs similarity index 96% rename from serde_tests/tests/test_ser.rs rename to testing/tests/test_ser.rs index 5b3f215b..981ddb86 100644 --- a/serde_tests/tests/test_ser.rs +++ b/testing/tests/test_ser.rs @@ -3,7 +3,13 @@ use std::net; use std::path::{Path, PathBuf}; use std::str; -use token::{self, Token}; +extern crate serde_test; +use self::serde_test::{ + Error, + Token, + assert_ser_tokens, + assert_ser_tokens_error, +}; extern crate fnv; use self::fnv::FnvHasher; @@ -355,16 +361,16 @@ fn test_cannot_serialize_paths() { let path = unsafe { str::from_utf8_unchecked(b"Hello \xF0\x90\x80World") }; - token::assert_ser_tokens_error( + assert_ser_tokens_error( &Path::new(path), - &[Token::Str("Hello �World")], - token::Error::InvalidValue("Path contains invalid UTF-8 characters".to_owned())); + &[], + 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( + assert_ser_tokens_error( &path_buf, - &[Token::Str("Hello �World")], - token::Error::InvalidValue("Path contains invalid UTF-8 characters".to_owned())); + &[], + Error::InvalidValue("Path contains invalid UTF-8 characters".to_owned())); }