2015-03-08 13:05:01 -05:00
|
|
|
#![feature(custom_derive, plugin, test)]
|
2015-03-09 00:56:33 -05:00
|
|
|
#![plugin(serde_macros)]
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
extern crate test;
|
2015-03-09 00:56:33 -05:00
|
|
|
extern crate serde;
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::de;
|
|
|
|
use serde::ser;
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::json::{
|
2015-03-03 22:33:25 -06:00
|
|
|
self,
|
|
|
|
Value,
|
|
|
|
from_str,
|
|
|
|
from_value,
|
|
|
|
to_value,
|
|
|
|
};
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::json::error::{Error, ErrorCode};
|
2015-03-03 22:33:25 -06:00
|
|
|
|
|
|
|
macro_rules! treemap {
|
|
|
|
($($k:expr => $v:expr),*) => ({
|
2015-03-05 21:49:17 -06:00
|
|
|
let mut _m = BTreeMap::new();
|
2015-03-03 22:33:25 -06:00
|
|
|
$(_m.insert($k, $v);)*
|
|
|
|
_m
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2015-03-03 22:33:25 -06:00
|
|
|
#[derive_serialize]
|
|
|
|
#[derive_deserialize]
|
|
|
|
enum Animal {
|
|
|
|
Dog,
|
2015-03-04 09:37:40 -06:00
|
|
|
Frog(String, Vec<isize>),
|
|
|
|
Cat { age: usize, name: String },
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2015-03-03 22:33:25 -06:00
|
|
|
#[derive_serialize]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: usize,
|
2015-03-05 21:49:17 -06:00
|
|
|
c: Vec<String>,
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2015-03-03 22:33:25 -06:00
|
|
|
#[derive_serialize]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct Outer {
|
|
|
|
inner: Vec<Inner>,
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
fn test_encode_ok<T>(errors: &[(T, &str)])
|
|
|
|
where T: PartialEq + Debug + ser::Serialize,
|
|
|
|
{
|
2015-03-03 22:33:25 -06:00
|
|
|
for &(ref value, out) in errors {
|
|
|
|
let out = out.to_string();
|
|
|
|
|
|
|
|
let s = json::to_string(value).unwrap();
|
|
|
|
assert_eq!(s, out);
|
|
|
|
|
|
|
|
let v = to_value(&value);
|
|
|
|
let s = json::to_string(&v).unwrap();
|
|
|
|
assert_eq!(s, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
fn test_pretty_encode_ok<T>(errors: &[(T, &str)])
|
|
|
|
where T: PartialEq + Debug + ser::Serialize,
|
|
|
|
{
|
2015-03-03 22:33:25 -06:00
|
|
|
for &(ref value, out) in errors {
|
|
|
|
let out = out.to_string();
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
let s = json::to_string_pretty(value).unwrap();
|
2015-03-03 22:33:25 -06:00
|
|
|
assert_eq!(s, out);
|
|
|
|
|
2015-03-06 00:16:11 -06:00
|
|
|
let v = to_value(&value);
|
|
|
|
let s = json::to_string_pretty(&v).unwrap();
|
2015-03-03 22:33:25 -06:00
|
|
|
assert_eq!(s, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_null() {
|
|
|
|
let tests = &[
|
|
|
|
((), "null"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_i64() {
|
|
|
|
let tests = &[
|
2015-03-03 23:55:48 -06:00
|
|
|
(3i64, "3"),
|
|
|
|
(-2i64, "-2"),
|
|
|
|
(-1234i64, "-1234"),
|
2015-03-03 22:33:25 -06:00
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_f64() {
|
|
|
|
let tests = &[
|
2015-03-03 23:55:48 -06:00
|
|
|
(3.0, "3"),
|
2015-03-03 22:33:25 -06:00
|
|
|
(3.1, "3.1"),
|
|
|
|
(-1.5, "-1.5"),
|
|
|
|
(0.5, "0.5"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_str() {
|
|
|
|
let tests = &[
|
|
|
|
("", "\"\""),
|
|
|
|
("foo", "\"foo\""),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_bool() {
|
|
|
|
let tests = &[
|
|
|
|
(true, "true"),
|
|
|
|
(false, "false"),
|
|
|
|
];
|
|
|
|
test_encode_ok(tests);
|
2015-03-06 00:16:11 -06:00
|
|
|
test_pretty_encode_ok(tests);
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_list() {
|
|
|
|
test_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(vec![], "[]"),
|
|
|
|
(vec![true], "[true]"),
|
|
|
|
(vec![true, false], "[true,false]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
test_encode_ok(&[
|
|
|
|
(vec![vec![], vec![], vec![]], "[[],[],[]]"),
|
|
|
|
(vec![vec![1, 2, 3], vec![], vec![]], "[[1,2,3],[],[]]"),
|
|
|
|
(vec![vec![], vec![1, 2, 3], vec![]], "[[],[1,2,3],[]]"),
|
|
|
|
(vec![vec![], vec![], vec![1, 2, 3]], "[[],[],[1,2,3]]"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
vec![vec![], vec![], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![1, 2, 3], vec![], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ],\n",
|
|
|
|
" [],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![], vec![1, 2, 3], vec![]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ],\n",
|
|
|
|
" []\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
vec![vec![], vec![], vec![1, 2, 3]],
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" [],\n",
|
|
|
|
" [],\n",
|
|
|
|
" [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
test_pretty_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(vec![], "[]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
vec![true],
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" true\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
vec![true, false],
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" true,\n",
|
|
|
|
" false\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let long_test_list = Value::Array(vec![
|
|
|
|
Value::Bool(false),
|
|
|
|
Value::Null,
|
|
|
|
Value::Array(vec![Value::String("foo\nbar".to_string()), Value::F64(3.5)])]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
2015-03-06 00:16:11 -06:00
|
|
|
(
|
|
|
|
long_test_list.clone(),
|
|
|
|
"[false,null,[\"foo\\nbar\",3.5]]",
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
long_test_list,
|
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" false,\n",
|
|
|
|
" null,\n",
|
|
|
|
" [\n",
|
|
|
|
" \"foo\\nbar\",\n",
|
|
|
|
" 3.5\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
2015-03-06 00:16:11 -06:00
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_object() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(treemap!(), "{}"),
|
|
|
|
(treemap!("a".to_string() => true), "{\"a\":true}"),
|
|
|
|
(
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => true,
|
|
|
|
"b".to_string() => false
|
|
|
|
),
|
|
|
|
"{\"a\":true,\"b\":false}"),
|
|
|
|
]);
|
|
|
|
|
2015-03-17 00:30:02 -05:00
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"b\":{},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"c\":{}}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"{\"a\":{},\"b\":{},\"c\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}}}",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" },\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap![
|
|
|
|
"a".to_string() => treemap![],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![
|
|
|
|
"a".to_string() => treemap!["a" => vec![1,2,3]],
|
|
|
|
"b".to_string() => treemap![],
|
|
|
|
"c".to_string() => treemap![]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": {},\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {\n",
|
|
|
|
" \"a\": {\n",
|
|
|
|
" \"a\": [\n",
|
|
|
|
" 1,\n",
|
|
|
|
" 2,\n",
|
|
|
|
" 3\n",
|
|
|
|
" ]\n",
|
|
|
|
" },\n",
|
|
|
|
" \"b\": {},\n",
|
|
|
|
" \"c\": {}\n",
|
|
|
|
" }\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(treemap!(), "{}"),
|
|
|
|
(
|
|
|
|
treemap!("a".to_string() => true),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": true\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => true,
|
|
|
|
"b".to_string() => false
|
|
|
|
),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"a\": true,\n",
|
|
|
|
" \"b\": false\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let complex_obj = Value::Object(treemap!(
|
2015-03-06 00:16:11 -06:00
|
|
|
"b".to_string() => Value::Array(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
Value::Object(treemap!("c".to_string() => Value::String("\x0c\r".to_string()))),
|
|
|
|
Value::Object(treemap!("d".to_string() => Value::String("".to_string())))
|
2015-03-06 00:16:11 -06:00
|
|
|
])
|
2015-03-03 22:33:25 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
|
|
|
complex_obj.clone(),
|
|
|
|
"{\
|
|
|
|
\"b\":[\
|
|
|
|
{\"c\":\"\\f\\r\"},\
|
|
|
|
{\"d\":\"\"}\
|
|
|
|
]\
|
|
|
|
}"
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
complex_obj.clone(),
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"b\": [\n",
|
|
|
|
" {\n",
|
|
|
|
" \"c\": \"\\f\\r\"\n",
|
|
|
|
" },\n",
|
|
|
|
" {\n",
|
|
|
|
" \"d\": \"\"\n",
|
|
|
|
" }\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_tuple() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5,),
|
2015-03-03 22:33:25 -06:00
|
|
|
"[5]",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5,),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" 5\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5, (6, "abc")),
|
2015-03-03 22:33:25 -06:00
|
|
|
"[5,[6,\"abc\"]]",
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
2015-03-03 23:55:48 -06:00
|
|
|
(5, (6, "abc")),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" 5,\n",
|
|
|
|
" [\n",
|
|
|
|
" 6,\n",
|
|
|
|
" \"abc\"\n",
|
|
|
|
" ]\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_enum() {
|
|
|
|
test_encode_ok(&[
|
2015-03-04 09:37:40 -06:00
|
|
|
(
|
|
|
|
Animal::Dog,
|
|
|
|
"{\"Dog\":[]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[]]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[349]]}",
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-04 09:37:40 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[349,102]]}",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
|
|
|
"{\"Cat\":{\"age\":5,\"name\":\"Kate\"}}"
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(
|
|
|
|
Animal::Dog,
|
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Dog\": []\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" []\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" [\n",
|
|
|
|
" 349\n",
|
|
|
|
" ]\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"{\n",
|
|
|
|
" \"Frog\": [\n",
|
|
|
|
" \"Henry\",\n",
|
|
|
|
" [\n",
|
|
|
|
" 349,\n",
|
|
|
|
" 102\n",
|
|
|
|
" ]\n",
|
|
|
|
" ]\n",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_option() {
|
|
|
|
test_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(Some("jodhpurs"), "\"jodhpurs\""),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_encode_ok(&[
|
|
|
|
(None, "null"),
|
2015-03-06 00:16:11 -06:00
|
|
|
(Some(vec!["foo", "bar"]), "[\"foo\",\"bar\"]"),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(Some("jodhpurs"), "\"jodhpurs\""),
|
|
|
|
]);
|
|
|
|
|
|
|
|
test_pretty_encode_ok(&[
|
|
|
|
(None, "null"),
|
|
|
|
(
|
2015-03-06 00:16:11 -06:00
|
|
|
Some(vec!["foo", "bar"]),
|
2015-03-03 22:33:25 -06:00
|
|
|
concat!(
|
|
|
|
"[\n",
|
|
|
|
" \"foo\",\n",
|
|
|
|
" \"bar\"\n",
|
|
|
|
"]"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_ok<T>(errors: Vec<(&'static str, T)>)
|
|
|
|
where T: Clone + Debug + PartialEq + ser::Serialize + de::Deserialize,
|
2015-03-04 11:12:32 -06:00
|
|
|
{
|
2015-03-18 09:35:05 -05:00
|
|
|
for (s, value) in errors {
|
|
|
|
let v: Result<T, Error> = from_str(s);
|
|
|
|
assert_eq!(v, Ok(value.clone()));
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can deserialize into a `Value`.
|
2015-03-18 09:35:05 -05:00
|
|
|
let json_value: Result<Value, Error> = from_str(s);
|
|
|
|
assert_eq!(json_value, Ok(to_value(&value)));
|
|
|
|
|
|
|
|
let json_value = json_value.unwrap();
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can deserialize from a `Value`.
|
2015-03-18 09:35:05 -05:00
|
|
|
let v: Result<T, Error> = from_value(json_value.clone());
|
|
|
|
assert_eq!(v, Ok(value));
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-04 11:12:32 -06:00
|
|
|
// Make sure we can round trip back to `Value`.
|
2015-03-18 09:35:05 -05:00
|
|
|
let json_value2: Result<Value, Error> = from_value(json_value.clone());
|
|
|
|
assert_eq!(json_value2, Ok(json_value));
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:49:17 -06:00
|
|
|
// FIXME (#5527): these could be merged once UFCS is finished.
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_err<T>(errors: Vec<(&'static str, Error)>)
|
|
|
|
where T: Debug + PartialEq + de::Deserialize,
|
2015-03-05 21:49:17 -06:00
|
|
|
{
|
2015-03-18 09:35:05 -05:00
|
|
|
for (s, err) in errors {
|
2015-03-05 21:49:17 -06:00
|
|
|
let v: Result<T, Error> = from_str(s);
|
2015-03-18 09:35:05 -05:00
|
|
|
assert_eq!(v, Err(err));
|
2015-03-05 21:49:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_null() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<()>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("n", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 2)),
|
|
|
|
("nul", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 4)),
|
|
|
|
("nulla", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 5)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("null", ()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_bool() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<bool>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("t", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 2)),
|
|
|
|
("truz", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 4)),
|
|
|
|
("f", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 2)),
|
|
|
|
("faz", Error::SyntaxError(ErrorCode::ExpectedSomeIdent, 1, 3)),
|
|
|
|
("truea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 5)),
|
|
|
|
("falsea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("true", true),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" true ", true),
|
2015-03-03 22:33:25 -06:00
|
|
|
("false", false),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" false ", false),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_number_errors() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<f64>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("+", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 1)),
|
|
|
|
(".", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 1)),
|
|
|
|
("-", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)),
|
|
|
|
("00", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)),
|
|
|
|
("1.", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 3)),
|
|
|
|
("1e", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 3)),
|
|
|
|
("1e+", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 4)),
|
|
|
|
("1a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 2)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_i64() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("-2", -2),
|
|
|
|
("-1234", -1234),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" -1234 ", -1234),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:51:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_parse_u64() {
|
|
|
|
test_parse_ok(vec![
|
|
|
|
("3", 3u64),
|
|
|
|
("1234", 1234),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_f64() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("3.0", 3.0f64),
|
|
|
|
("3.1", 3.1),
|
|
|
|
("-1.2", -1.2),
|
|
|
|
("0.4", 0.4),
|
|
|
|
("0.4e5", 0.4e5),
|
|
|
|
("0.4e15", 0.4e15),
|
|
|
|
("0.4e-01", 0.4e-01),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" 0.4e-01 ", 0.4e-01),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-05 09:36:30 -06:00
|
|
|
fn test_parse_string() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<String>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("\"", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 2)),
|
|
|
|
("\"lol", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 5)),
|
|
|
|
("\"lol\"a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"\"", "".to_string()),
|
|
|
|
("\"foo\"", "foo".to_string()),
|
2015-03-05 09:36:30 -06:00
|
|
|
(" \"foo\" ", "foo".to_string()),
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"\\\"\"", "\"".to_string()),
|
|
|
|
("\"\\b\"", "\x08".to_string()),
|
|
|
|
("\"\\n\"", "\n".to_string()),
|
|
|
|
("\"\\r\"", "\r".to_string()),
|
|
|
|
("\"\\t\"", "\t".to_string()),
|
|
|
|
("\"\\u12ab\"", "\u{12ab}".to_string()),
|
|
|
|
("\"\\uAB12\"", "\u{AB12}".to_string()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_list() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<Vec<f64>>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("[", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)),
|
|
|
|
("[ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)),
|
|
|
|
("[1", Error::SyntaxError(ErrorCode::EOFWhileParsingList, 1, 3)),
|
|
|
|
("[1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 4)),
|
|
|
|
("[1,]", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 4)),
|
|
|
|
("[1 2]", Error::SyntaxError(ErrorCode::ExpectedListCommaOrEnd, 1, 4)),
|
|
|
|
("[]a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("[]", vec![]),
|
|
|
|
("[ ]", vec![]),
|
|
|
|
("[null]", vec![()]),
|
|
|
|
(" [ null ] ", vec![()]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("[true]", vec![true]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[3,1]", vec![3u64, 1]),
|
2015-03-05 21:49:17 -06:00
|
|
|
(" [ 3 , 1 ] ", vec![3, 1]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[[3], [1, 2]]", vec![vec![3u64], vec![1, 2]]),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1]", (1u64,)),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2]", (1u64, 2u64)),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
2015-03-03 22:33:25 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2, 3]", (1u64, 2u64, 3u64)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
2015-03-05 21:49:17 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, [2, 3]]", (1u64, (2u64, 3u64))),
|
2015-03-05 21:49:17 -06:00
|
|
|
]);
|
|
|
|
|
|
|
|
let v: () = from_str("[]").unwrap();
|
|
|
|
assert_eq!(v, ());
|
2015-03-03 22:33:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_object() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<BTreeMap<String, u32>>(vec![
|
2015-03-05 21:49:17 -06:00
|
|
|
("{", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)),
|
|
|
|
("{ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)),
|
|
|
|
("{1", Error::SyntaxError(ErrorCode::KeyMustBeAString, 1, 2)),
|
|
|
|
("{ \"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 6)),
|
|
|
|
("{\"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 5)),
|
|
|
|
("{\"a\" ", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 6)),
|
|
|
|
("{\"a\" 1", Error::SyntaxError(ErrorCode::ExpectedColon, 1, 6)),
|
|
|
|
("{\"a\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 6)),
|
|
|
|
("{\"a\":1", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 7)),
|
|
|
|
("{\"a\":1 1", Error::SyntaxError(ErrorCode::ExpectedObjectCommaOrEnd, 1, 8)),
|
|
|
|
("{\"a\":1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 8)),
|
|
|
|
("{}a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
("{}", treemap!()),
|
|
|
|
("{ }", treemap!()),
|
|
|
|
(
|
|
|
|
"{\"a\":3}",
|
2015-03-18 22:51:48 -05:00
|
|
|
treemap!("a".to_string() => 3u64)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"{ \"a\" : 3 }",
|
2015-03-04 11:12:32 -06:00
|
|
|
treemap!("a".to_string() => 3)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"{\"a\":3,\"b\":4}",
|
2015-03-03 23:55:48 -06:00
|
|
|
treemap!("a".to_string() => 3, "b".to_string() => 4)
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
" { \"a\" : 3 , \"b\" : 4 } ",
|
2015-03-03 23:55:48 -06:00
|
|
|
treemap!("a".to_string() => 3, "b".to_string() => 4),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
"{\"a\": {\"b\": 3, \"c\": 4}}",
|
2015-03-18 22:51:48 -05:00
|
|
|
treemap!(
|
|
|
|
"a".to_string() => treemap!(
|
|
|
|
"b".to_string() => 3u64,
|
|
|
|
"c".to_string() => 4
|
|
|
|
)
|
|
|
|
),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_struct() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<Outer>(vec![
|
2015-03-05 22:06:03 -06:00
|
|
|
("[]", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
2015-03-12 22:44:16 -05:00
|
|
|
("{}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
2015-03-05 22:06:03 -06:00
|
|
|
("{\"inner\": true}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
"{
|
|
|
|
\"inner\": []
|
|
|
|
}",
|
|
|
|
Outer {
|
|
|
|
inner: vec![]
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"{
|
|
|
|
\"inner\": [
|
|
|
|
{ \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] }
|
|
|
|
]
|
|
|
|
}",
|
|
|
|
Outer {
|
|
|
|
inner: vec![
|
|
|
|
Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_option() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-04 11:42:51 -06:00
|
|
|
("null", None::<String>),
|
2015-03-03 22:33:25 -06:00
|
|
|
("\"jodhpurs\"", Some("jodhpurs".to_string())),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2015-03-03 22:33:25 -06:00
|
|
|
#[derive_serialize]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct Foo {
|
|
|
|
x: Option<isize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-04 11:42:51 -06:00
|
|
|
("{\"x\": null}", Foo { x: None }),
|
|
|
|
("{\"x\": 5}", Foo { x: Some(5) }),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-18 09:35:05 -05:00
|
|
|
fn test_parse_enum_errors() {
|
|
|
|
test_parse_err::<Animal>(vec![
|
2015-03-16 00:04:17 -05:00
|
|
|
("{}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 2)),
|
2015-03-18 09:35:05 -05:00
|
|
|
("{\"Dog\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 8)),
|
|
|
|
("{\"Dog\":}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 8)),
|
2015-03-05 22:06:03 -06:00
|
|
|
("{\"unknown\":[]}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
|
|
|
("{\"Dog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
2015-03-18 09:35:05 -05:00
|
|
|
("{\"Frog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
|
|
|
("{\"Cat\":[]}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
|
2015-03-05 09:36:30 -06:00
|
|
|
]);
|
2015-03-18 09:35:05 -05:00
|
|
|
}
|
2015-03-05 09:36:30 -06:00
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
#[test]
|
|
|
|
fn test_parse_enum() {
|
|
|
|
test_parse_ok(vec![
|
2015-03-05 09:36:30 -06:00
|
|
|
("{\"Dog\":[]}", Animal::Dog),
|
|
|
|
(" { \"Dog\" : [ ] } ", Animal::Dog),
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
"{\"Frog\":[\"Henry\",[]]}",
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![]),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
" { \"Frog\": [ \"Henry\" , [ 349, 102 ] ] } ",
|
2015-03-06 00:16:11 -06:00
|
|
|
Animal::Frog("Henry".to_string(), vec![349, 102]),
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
(
|
2015-03-05 09:36:30 -06:00
|
|
|
"{\"Cat\": {\"age\": 5, \"name\": \"Kate\"}}",
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
" { \"Cat\" : { \"age\" : 5 , \"name\" : \"Kate\" } } ",
|
|
|
|
Animal::Cat { age: 5, name: "Kate".to_string() },
|
2015-03-03 22:33:25 -06:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-03 22:33:25 -06:00
|
|
|
(
|
|
|
|
concat!(
|
|
|
|
"{",
|
|
|
|
" \"a\": {\"Dog\": []},",
|
|
|
|
" \"b\": {\"Frog\":[\"Henry\", []]}",
|
|
|
|
"}"
|
|
|
|
),
|
|
|
|
treemap!(
|
|
|
|
"a".to_string() => Animal::Dog,
|
2015-03-06 00:16:11 -06:00
|
|
|
"b".to_string() => Animal::Frog("Henry".to_string(), vec![])
|
2015-03-03 22:33:25 -06:00
|
|
|
)
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:49:17 -06:00
|
|
|
#[test]
|
|
|
|
fn test_parse_trailing_whitespace() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_ok(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
("[1, 2] ", vec![1u64, 2]),
|
2015-03-05 21:49:17 -06:00
|
|
|
("[1, 2]\n", vec![1, 2]),
|
|
|
|
("[1, 2]\t", vec![1, 2]),
|
|
|
|
("[1, 2]\t \n", vec![1, 2]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_multiline_errors() {
|
2015-03-18 09:35:05 -05:00
|
|
|
test_parse_err::<BTreeMap<String, String>>(vec![
|
2015-03-05 22:06:03 -06:00
|
|
|
("{\n \"foo\":\n \"bar\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 3, 8)),
|
2015-03-03 22:33:25 -06:00
|
|
|
]);
|
|
|
|
}
|
2015-03-12 22:44:16 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_field() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct Foo {
|
|
|
|
x: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value: Foo = from_str("{}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_str("{\"x\": 5}").unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: None });
|
|
|
|
|
|
|
|
let value: Foo = from_value(Value::Object(treemap!(
|
|
|
|
"x".to_string() => Value::I64(5)
|
|
|
|
))).unwrap();
|
|
|
|
assert_eq!(value, Foo { x: Some(5) });
|
|
|
|
}
|