2017-04-13 19:34:42 -05:00
|
|
|
// Copyright 2017 Serde Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-02-24 17:32:13 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
2017-07-09 12:16:49 -05:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
2018-04-30 03:41:22 -05:00
|
|
|
use std::ffi::CString;
|
2018-05-07 13:16:34 -05:00
|
|
|
use std::mem;
|
2016-02-21 19:02:25 -06:00
|
|
|
use std::net;
|
2018-04-30 03:41:22 -05:00
|
|
|
use std::num::Wrapping;
|
2016-01-28 11:45:38 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-05-07 13:16:34 -05:00
|
|
|
use std::rc::{Rc, Weak as RcWeak};
|
|
|
|
use std::sync::{Arc, Weak as ArcWeak};
|
2018-04-30 03:41:22 -05:00
|
|
|
use std::time::{Duration, UNIX_EPOCH};
|
2015-03-03 22:33:17 -06:00
|
|
|
|
2017-04-09 16:11:27 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::str;
|
|
|
|
|
2016-06-28 23:47:54 -05:00
|
|
|
extern crate serde_test;
|
2017-11-03 10:12:43 -05:00
|
|
|
use self::serde_test::{assert_ser_tokens, assert_ser_tokens_error, Configure, Token};
|
2015-03-03 22:33:17 -06:00
|
|
|
|
2016-06-11 02:43:02 -05:00
|
|
|
extern crate fnv;
|
|
|
|
use self::fnv::FnvHasher;
|
|
|
|
|
2017-02-24 17:32:13 -06:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2016-12-24 00:37:23 -06:00
|
|
|
#[derive(Serialize, PartialEq, Debug)]
|
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),
|
2017-12-23 22:24:57 -06:00
|
|
|
Map {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
},
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
SkippedUnit,
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
SkippedOne(i32),
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
SkippedSeq(i32, i32),
|
|
|
|
#[serde(skip_serializing)]
|
|
|
|
SkippedMap {
|
|
|
|
_a: i32,
|
|
|
|
_b: i32,
|
|
|
|
},
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
|
|
|
|
2015-03-03 23:42:33 -06:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-25 22:55:55 -06:00
|
|
|
macro_rules! declare_tests {
|
2017-10-17 11:49:39 -05:00
|
|
|
(
|
2017-11-03 10:12:43 -05:00
|
|
|
$readable:tt
|
2017-10-17 11:49:39 -05:00
|
|
|
$($name:ident { $($value:expr => $tokens:expr,)+ })+
|
|
|
|
) => {
|
2017-01-25 22:55:55 -06:00
|
|
|
$(
|
|
|
|
#[test]
|
|
|
|
fn $name() {
|
|
|
|
$(
|
2017-11-03 10:12:43 -05:00
|
|
|
assert_ser_tokens(&$value.$readable(), $tokens);
|
2017-01-25 22:55:55 -06:00
|
|
|
)+
|
|
|
|
}
|
|
|
|
)+
|
2017-10-17 11:49:39 -05:00
|
|
|
};
|
2017-01-25 22:55:55 -06:00
|
|
|
|
2017-09-11 08:54:49 -05:00
|
|
|
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
|
|
|
$(
|
|
|
|
#[test]
|
|
|
|
fn $name() {
|
|
|
|
$(
|
2017-10-17 11:49:39 -05:00
|
|
|
assert_ser_tokens(&$value, $tokens);
|
2017-09-11 08:54:49 -05:00
|
|
|
)+
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 22:55:55 -06:00
|
|
|
declare_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
|
|
|
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
|
|
|
0u8 => &[Token::U8(0)],
|
|
|
|
0u16 => &[Token::U16(0)],
|
|
|
|
0u32 => &[Token::U32(0)],
|
|
|
|
0u64 => &[Token::U64(0)],
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
|
|
|
test_floats {
|
2015-08-27 22:01:09 -05:00
|
|
|
0f32 => &[Token::F32(0.)],
|
|
|
|
0f64 => &[Token::F64(0.)],
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
|
|
|
test_char {
|
2015-08-27 22:01:09 -05:00
|
|
|
'a' => &[Token::Char('a')],
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
|
|
|
test_str {
|
2015-08-27 22:01:09 -05:00
|
|
|
"abc" => &[Token::Str("abc")],
|
2016-01-18 14:24:03 -06:00
|
|
|
"abc".to_owned() => &[Token::Str("abc")],
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
|
|
|
test_option {
|
2017-04-05 12:40:14 -05:00
|
|
|
None::<i32> => &[Token::None],
|
2015-08-27 22:01:09 -05:00
|
|
|
Some(1) => &[
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::Some,
|
2015-03-03 22:33:17 -06:00
|
|
|
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) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::NewtypeVariant { name: "Result", variant: "Ok" },
|
2015-07-19 13:16:46 -05:00
|
|
|
Token::I32(0),
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
Err::<i32, i32>(1) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::NewtypeVariant { name: "Result", variant: "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] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(0) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
&[1, 2, 3][..] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(3) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2015-05-16 04:51:35 -05:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_array {
|
2015-08-27 22:01:09 -05:00
|
|
|
[0; 0] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 0 },
|
2017-04-17 14:07:49 -05:00
|
|
|
Token::TupleEnd,
|
2015-05-16 04:51:35 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
[1, 2, 3] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 3 },
|
2015-05-16 04:51:35 -05:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(3),
|
2017-04-17 14:07:49 -05:00
|
|
|
Token::TupleEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
|
|
|
}
|
|
|
|
test_vec {
|
2015-08-27 22:01:09 -05:00
|
|
|
Vec::<isize>::new() => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: 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]] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(3) },
|
|
|
|
Token::Seq { len: Some(0) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::SeqEnd,
|
|
|
|
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(1) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(2) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(2),
|
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-07-09 12:16:49 -05:00
|
|
|
test_btreeset {
|
|
|
|
BTreeSet::<isize>::new() => &[
|
|
|
|
Token::Seq { len: Some(0) },
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
btreeset![1] => &[
|
|
|
|
Token::Seq { len: Some(1) },
|
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2016-06-11 11:59:29 -05:00
|
|
|
test_hashset {
|
|
|
|
HashSet::<isize>::new() => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(0) },
|
2016-06-11 11:59:29 -05:00
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
hashset![1] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(1) },
|
2016-06-11 11:59:29 -05:00
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
hashset![FnvHasher @ 1] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(1) },
|
2016-06-11 11:59:29 -05:00
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
test_tuple {
|
2015-08-27 22:01:09 -05:00
|
|
|
(1,) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 1 },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
(1, 2, 3) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 3 },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::I32(3),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
|
|
|
}
|
|
|
|
test_btreemap {
|
2015-08-27 22:01:09 -05:00
|
|
|
btreemap![1 => 2] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
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] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
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]] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(0) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::MapEnd,
|
|
|
|
|
|
|
|
Token::I32(2),
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::I32(4),
|
|
|
|
|
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::MapEnd,
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
2016-06-11 02:43:02 -05:00
|
|
|
test_hashmap {
|
2016-06-11 11:42:02 -05:00
|
|
|
HashMap::<isize, isize>::new() => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(0) },
|
2016-06-11 11:42:02 -05:00
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
hashmap![1 => 2] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
2016-06-11 02:43:02 -05:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2016-06-11 11:42:02 -05:00
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
hashmap![FnvHasher @ 1 => 2] => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
2016-06-11 11:42:02 -05:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2016-06-11 02:43:02 -05:00
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
test_unit_struct {
|
2017-04-19 15:06:31 -05:00
|
|
|
UnitStruct => &[Token::UnitStruct { name: "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) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::TupleStruct { name: "TupleStruct", len: 3 },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::I32(3),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleStructEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
test_struct {
|
2015-08-27 22:01:09 -05:00
|
|
|
Struct { a: 1, b: 2, c: 3 } => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "Struct", len: 3 },
|
2015-03-03 23:42:33 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
2016-02-13 00:05:02 -06:00
|
|
|
Token::StructEnd,
|
2015-03-03 23:42:33 -06:00
|
|
|
],
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
test_enum {
|
2017-04-19 15:06:31 -05:00
|
|
|
Enum::Unit => &[Token::UnitVariant { name: "Enum", variant: "Unit" }],
|
|
|
|
Enum::One(42) => &[Token::NewtypeVariant { name: "Enum", variant: "One" }, Token::I32(42)],
|
2015-08-27 22:01:09 -05:00
|
|
|
Enum::Seq(1, 2) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::TupleVariant { name: "Enum", variant: "Seq", len: 2 },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::TupleVariantEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
Enum::Map { a: 1, b: 2 } => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::StructVariant { name: "Enum", variant: "Map", len: 2 },
|
2015-03-03 22:33:17 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2015-03-03 22:33:17 -06:00
|
|
|
],
|
|
|
|
}
|
2016-04-17 22:59:26 -05:00
|
|
|
test_box {
|
|
|
|
Box::new(0i32) => &[Token::I32(0)],
|
|
|
|
}
|
|
|
|
test_boxed_slice {
|
|
|
|
Box::new([0, 1, 2]) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 3 },
|
2016-04-17 22:59:26 -05:00
|
|
|
Token::I32(0),
|
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
2017-04-17 14:07:49 -05:00
|
|
|
Token::TupleEnd,
|
2016-04-17 22:59:26 -05:00
|
|
|
],
|
|
|
|
}
|
2016-08-03 19:15:14 -05:00
|
|
|
test_duration {
|
|
|
|
Duration::new(1, 2) => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "Duration", len: 2 },
|
2016-08-03 19:15:14 -05:00
|
|
|
Token::Str("secs"),
|
|
|
|
Token::U64(1),
|
|
|
|
|
|
|
|
Token::Str("nanos"),
|
|
|
|
Token::U32(2),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-07-11 23:01:40 -05:00
|
|
|
test_system_time {
|
2017-07-16 05:16:07 -05:00
|
|
|
UNIX_EPOCH + Duration::new(1, 200) => &[
|
2017-07-11 23:01:40 -05:00
|
|
|
Token::Struct { name: "SystemTime", len: 2 },
|
|
|
|
Token::Str("secs_since_epoch"),
|
|
|
|
Token::U64(1),
|
|
|
|
|
|
|
|
Token::Str("nanos_since_epoch"),
|
2017-07-16 05:16:07 -05:00
|
|
|
Token::U32(200),
|
2017-07-11 23:01:40 -05:00
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-03-09 05:18:56 -06:00
|
|
|
test_range {
|
|
|
|
1u32..2u32 => &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "Range", len: 2 },
|
2017-03-09 05:18:56 -06:00
|
|
|
Token::Str("start"),
|
|
|
|
Token::U32(1),
|
|
|
|
|
|
|
|
Token::Str("end"),
|
|
|
|
Token::U32(2),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
}
|
2016-01-28 11:45:38 -06:00
|
|
|
test_path {
|
|
|
|
Path::new("/usr/local/lib") => &[
|
|
|
|
Token::Str("/usr/local/lib"),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_path_buf {
|
|
|
|
PathBuf::from("/usr/local/lib") => &[
|
|
|
|
Token::Str("/usr/local/lib"),
|
|
|
|
],
|
|
|
|
}
|
2017-03-03 17:06:04 -06:00
|
|
|
test_cstring {
|
|
|
|
CString::new("abc").unwrap() => &[
|
|
|
|
Token::Bytes(b"abc"),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_cstr {
|
|
|
|
(&*CString::new("abc").unwrap()) => &[
|
|
|
|
Token::Bytes(b"abc"),
|
|
|
|
],
|
|
|
|
}
|
2017-09-04 12:16:03 -05:00
|
|
|
test_rc {
|
|
|
|
Rc::new(true) => &[
|
|
|
|
Token::Bool(true),
|
|
|
|
],
|
|
|
|
}
|
2018-05-07 13:16:34 -05:00
|
|
|
test_rc_weak_some {
|
|
|
|
{
|
|
|
|
let rc = Rc::new(true);
|
|
|
|
mem::forget(rc.clone());
|
|
|
|
Rc::downgrade(&rc)
|
|
|
|
} => &[
|
|
|
|
Token::Some,
|
|
|
|
Token::Bool(true),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_rc_weak_none {
|
|
|
|
RcWeak::<bool>::new() => &[
|
|
|
|
Token::None,
|
|
|
|
],
|
|
|
|
}
|
2017-09-04 12:16:03 -05:00
|
|
|
test_arc {
|
|
|
|
Arc::new(true) => &[
|
|
|
|
Token::Bool(true),
|
|
|
|
],
|
|
|
|
}
|
2018-05-07 13:16:34 -05:00
|
|
|
test_arc_weak_some {
|
|
|
|
{
|
|
|
|
let arc = Arc::new(true);
|
|
|
|
mem::forget(arc.clone());
|
|
|
|
Arc::downgrade(&arc)
|
|
|
|
} => &[
|
|
|
|
Token::Some,
|
|
|
|
Token::Bool(true),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_arc_weak_none {
|
|
|
|
ArcWeak::<bool>::new() => &[
|
|
|
|
Token::None,
|
|
|
|
],
|
|
|
|
}
|
2017-10-31 11:32:58 -05:00
|
|
|
test_wrapping {
|
|
|
|
Wrapping(1usize) => &[
|
|
|
|
Token::U64(1),
|
|
|
|
],
|
|
|
|
}
|
2017-09-04 12:16:03 -05:00
|
|
|
}
|
|
|
|
|
2017-10-17 11:49:39 -05:00
|
|
|
declare_tests! {
|
2017-11-03 10:12:43 -05:00
|
|
|
readable
|
|
|
|
|
2017-10-17 11:49:39 -05:00
|
|
|
test_net_ipv4addr_readable {
|
|
|
|
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
|
|
|
}
|
|
|
|
test_net_ipv6addr_readable {
|
|
|
|
"::1".parse::<net::Ipv6Addr>().unwrap() => &[Token::Str("::1")],
|
|
|
|
}
|
|
|
|
test_net_ipaddr_readable {
|
|
|
|
"1.2.3.4".parse::<net::IpAddr>().unwrap() => &[Token::Str("1.2.3.4")],
|
|
|
|
}
|
|
|
|
test_net_socketaddr_readable {
|
|
|
|
"1.2.3.4:1234".parse::<net::SocketAddr>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
|
|
|
"1.2.3.4:1234".parse::<net::SocketAddrV4>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
|
|
|
"[::1]:1234".parse::<net::SocketAddrV6>().unwrap() => &[Token::Str("[::1]:1234")],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_tests! {
|
2017-11-03 10:12:43 -05:00
|
|
|
compact
|
|
|
|
|
2017-10-17 11:49:39 -05:00
|
|
|
test_net_ipv4addr_compact {
|
2017-09-11 08:54:49 -05:00
|
|
|
net::Ipv4Addr::from(*b"1234") => &seq![
|
|
|
|
Token::Tuple { len: 4 },
|
|
|
|
seq b"1234".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-10-17 11:49:39 -05:00
|
|
|
test_net_ipv6addr_compact {
|
2017-09-11 08:54:49 -05:00
|
|
|
net::Ipv6Addr::from(*b"1234567890123456") => &seq![
|
|
|
|
Token::Tuple { len: 16 },
|
|
|
|
seq b"1234567890123456".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-10-17 11:49:39 -05:00
|
|
|
test_net_ipaddr_compact {
|
2017-09-11 10:39:57 -05:00
|
|
|
net::IpAddr::from(*b"1234") => &seq![
|
|
|
|
Token::NewtypeVariant { name: "IpAddr", variant: "V4" },
|
|
|
|
|
|
|
|
Token::Tuple { len: 4 },
|
|
|
|
seq b"1234".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
}
|
2017-10-17 11:49:39 -05:00
|
|
|
test_net_socketaddr_compact {
|
2017-09-11 08:54:49 -05:00
|
|
|
net::SocketAddr::from((*b"1234567890123456", 1234)) => &seq![
|
2017-09-11 10:39:57 -05:00
|
|
|
Token::NewtypeVariant { name: "SocketAddr", variant: "V6" },
|
|
|
|
|
2017-09-11 08:54:49 -05:00
|
|
|
Token::Tuple { len: 2 },
|
|
|
|
|
|
|
|
Token::Tuple { len: 16 },
|
|
|
|
seq b"1234567890123456".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
|
|
|
|
Token::U16(1234),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
net::SocketAddrV4::new(net::Ipv4Addr::from(*b"1234"), 1234) => &seq![
|
|
|
|
Token::Tuple { len: 2 },
|
|
|
|
|
|
|
|
Token::Tuple { len: 4 },
|
|
|
|
seq b"1234".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
|
|
|
|
Token::U16(1234),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
net::SocketAddrV6::new(net::Ipv6Addr::from(*b"1234567890123456"), 1234, 0, 0) => &seq![
|
|
|
|
Token::Tuple { len: 2 },
|
|
|
|
|
|
|
|
Token::Tuple { len: 16 },
|
|
|
|
seq b"1234567890123456".iter().map(|&b| Token::U8(b)),
|
|
|
|
Token::TupleEnd,
|
|
|
|
|
|
|
|
Token::U16(1234),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:16:03 -05:00
|
|
|
// Serde's implementation is not unstable, but the constructors are.
|
|
|
|
#[cfg(feature = "unstable")]
|
|
|
|
declare_tests! {
|
|
|
|
test_rc_dst {
|
|
|
|
Rc::<str>::from("s") => &[
|
|
|
|
Token::Str("s"),
|
|
|
|
],
|
|
|
|
Rc::<[bool]>::from(&[true][..]) => &[
|
|
|
|
Token::Seq { len: Some(1) },
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_arc_dst {
|
|
|
|
Arc::<str>::from("s") => &[
|
|
|
|
Token::Str("s"),
|
|
|
|
],
|
|
|
|
Arc::<[bool]>::from(&[true][..]) => &[
|
|
|
|
Token::Seq { len: Some(1) },
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-03 22:33:17 -06:00
|
|
|
}
|
2016-01-28 12:41:21 -06:00
|
|
|
|
|
|
|
#[test]
|
2017-03-27 11:15:22 -05:00
|
|
|
#[cfg(unix)]
|
2016-01-28 12:41:21 -06:00
|
|
|
fn test_cannot_serialize_paths() {
|
2017-04-13 14:28:23 -05:00
|
|
|
let path = unsafe { str::from_utf8_unchecked(b"Hello \xF0\x90\x80World") };
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_ser_tokens_error(
|
2016-01-28 12:41:21 -06:00
|
|
|
&Path::new(path),
|
2016-06-29 00:23:38 -05:00
|
|
|
&[],
|
2017-04-19 13:38:57 -05:00
|
|
|
"path contains invalid UTF-8 characters",
|
2017-04-13 14:28:23 -05:00
|
|
|
);
|
2016-01-28 12:41:21 -06:00
|
|
|
|
|
|
|
let mut path_buf = PathBuf::new();
|
|
|
|
path_buf.push(path);
|
|
|
|
|
2017-11-03 10:12:43 -05:00
|
|
|
assert_ser_tokens_error(&path_buf, &[], "path contains invalid UTF-8 characters");
|
2016-01-28 12:41:21 -06:00
|
|
|
}
|
2016-12-24 00:37:23 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_skipped() {
|
|
|
|
assert_ser_tokens_error(
|
|
|
|
&Enum::SkippedUnit,
|
|
|
|
&[],
|
2017-04-19 13:38:57 -05:00
|
|
|
"the enum variant Enum::SkippedUnit cannot be serialized",
|
2017-04-13 14:28:23 -05:00
|
|
|
);
|
2016-12-24 00:37:23 -06:00
|
|
|
assert_ser_tokens_error(
|
|
|
|
&Enum::SkippedOne(42),
|
|
|
|
&[],
|
2017-04-19 13:38:57 -05:00
|
|
|
"the enum variant Enum::SkippedOne cannot be serialized",
|
2017-04-13 14:28:23 -05:00
|
|
|
);
|
2016-12-24 00:37:23 -06:00
|
|
|
assert_ser_tokens_error(
|
|
|
|
&Enum::SkippedSeq(1, 2),
|
|
|
|
&[],
|
2017-04-19 13:38:57 -05:00
|
|
|
"the enum variant Enum::SkippedSeq cannot be serialized",
|
2017-04-13 14:28:23 -05:00
|
|
|
);
|
2016-12-24 00:37:23 -06:00
|
|
|
assert_ser_tokens_error(
|
|
|
|
&Enum::SkippedMap { _a: 1, _b: 2 },
|
|
|
|
&[],
|
2017-04-19 13:38:57 -05:00
|
|
|
"the enum variant Enum::SkippedMap cannot be serialized",
|
2017-04-13 14:28:23 -05:00
|
|
|
);
|
2016-12-24 00:37:23 -06:00
|
|
|
}
|