Skip to content

Commit

Permalink
Finish updating to rust HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Apr 3, 2015
1 parent b30965e commit d0b49d9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
14 changes: 11 additions & 3 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ pub struct Bytes<'a> {
bytes: &'a [u8],
}

impl<'a, T> From<T> for Bytes<'a> where T: Into<&'a [u8]> {
fn from(bytes: T) -> Self {
impl<'a> From<&'a [u8]> for Bytes<'a> {
fn from(bytes: &'a [u8]) -> Self {
Bytes {
bytes: bytes.into(),
bytes: bytes,
}
}
}

impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
fn from(bytes: &'a Vec<u8>) -> Self {
Bytes {
bytes: &bytes,
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(convert, custom_derive, plugin, test)]
#![feature(custom_derive, plugin, test)]
#![plugin(serde_macros)]

extern crate test;
Expand Down Expand Up @@ -192,10 +192,12 @@ fn test_byte_buf_ser_bytes() {
#[test]
fn test_byte_buf_de_json() {
let bytes = ByteBuf::new();
assert_eq!(json::from_str("[]").unwrap(), bytes);
let v: ByteBuf = json::from_str("[]").unwrap();
assert_eq!(v, bytes);

let bytes = ByteBuf::from(vec![1, 2, 3]);
assert_eq!(json::from_str("[1, 2, 3]").unwrap(), bytes);
let v: ByteBuf = json::from_str("[1, 2, 3]").unwrap();
assert_eq!(v, bytes);
}

#[test]
Expand Down
43 changes: 27 additions & 16 deletions tests/test_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,23 +626,20 @@ fn test_parse_ok<T>(errors: Vec<(&'static str, T)>)
where T: Clone + Debug + PartialEq + ser::Serialize + de::Deserialize,
{
for (s, value) in errors {
let v: Result<T, Error> = from_str(s);
assert_eq!(v, Ok(value.clone()));
let v: T = from_str(s).unwrap();
assert_eq!(v, value.clone());

// Make sure we can deserialize into a `Value`.
let json_value: Result<Value, Error> = from_str(s);
assert_eq!(json_value, Ok(to_value(&value)));

let json_value = json_value.unwrap();

let json_value: Value = from_str(s).unwrap();
assert_eq!(json_value, to_value(&value));

// Make sure we can deserialize from a `Value`.
let v: Result<T, Error> = from_value(json_value.clone());
assert_eq!(v, Ok(value));
let v: T = from_value(json_value.clone()).unwrap();
assert_eq!(v, value);

// Make sure we can round trip back to `Value`.
let json_value2: Result<Value, Error> = from_value(json_value.clone());
assert_eq!(json_value2, Ok(json_value));
let json_value2: Value = from_value(json_value.clone()).unwrap();
assert_eq!(json_value2, json_value);
}
}

Expand All @@ -651,8 +648,20 @@ fn test_parse_err<T>(errors: Vec<(&'static str, Error)>)
where T: Debug + PartialEq + de::Deserialize,
{
for (s, err) in errors {
let v: Result<T, Error> = from_str(s);
assert_eq!(v, Err(err));
match (err, from_str::<T>(s).unwrap_err()) {
(
Error::SyntaxError(expected_code, expected_line, expected_col),
Error::SyntaxError(actual_code, actual_line, actual_col),
) => {
assert_eq!(
(expected_code, expected_line, expected_col),
(actual_code, actual_line, actual_col)
)
}
(expected_err, actual_err) => {
panic!("unexpected errors {} != {}", expected_err, actual_err)
}
}
}
}

Expand Down Expand Up @@ -889,11 +898,13 @@ fn test_parse_struct() {
)
]);

let v: Outer = from_str("{}").unwrap();

assert_eq!(
from_str("{}"),
Ok(Outer {
v,
Outer {
inner: vec![],
})
}
);
}

Expand Down
95 changes: 52 additions & 43 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ fn test_named_unit() {
Value::Null
);

let v = json::from_str("null").unwrap();
let v: NamedUnit = json::from_str("null").unwrap();
assert_eq!(v, named_unit);

let v = json::from_value(Value::Null).unwrap();
let v: NamedUnit = json::from_value(Value::Null).unwrap();
assert_eq!(v, named_unit);
}

Expand All @@ -169,13 +169,15 @@ fn test_ser_named_tuple() {

#[test]
fn test_de_named_tuple() {
let v: DeNamedTuple<i32, i32, i32> = json::from_str("[1,2,3]").unwrap();
assert_eq!(
json::from_str("[1,2,3]").unwrap(),
v,
DeNamedTuple(1, 2, 3)
);

let v: Value = json::from_str("[1,2,3]").unwrap();
assert_eq!(
json::from_str("[1,2,3]").unwrap(),
v,
Value::Array(vec![
Value::U64(1),
Value::U64(2),
Expand Down Expand Up @@ -218,19 +220,17 @@ fn test_de_named_map() {
c: 7,
};

assert_eq!(
json::from_str("{\"a\":5,\"b\":6,\"c\":7}").unwrap(),
v
);

assert_eq!(
json::from_value(Value::Object(btreemap![
"a".to_string() => Value::U64(5),
"b".to_string() => Value::U64(6),
"c".to_string() => Value::U64(7)
])).unwrap(),
v
);
let v2: DeNamedMap<i32, i32, i32> = json::from_str(
"{\"a\":5,\"b\":6,\"c\":7}"
).unwrap();
assert_eq!(v, v2);

let v2 = json::from_value(Value::Object(btreemap![
"a".to_string() => Value::U64(5),
"b".to_string() => Value::U64(6),
"c".to_string() => Value::U64(7)
])).unwrap();
assert_eq!(v, v2);
}

#[test]
Expand Down Expand Up @@ -336,15 +336,17 @@ fn test_ser_enum_map() {

#[test]
fn test_de_enum_unit() {
let v: DeEnum<_, _, _> = json::from_str("{\"Unit\":[]}").unwrap();
assert_eq!(
json::from_str("{\"Unit\":[]}").unwrap(),
v,
DeEnum::Unit::<u32, u32, u32>
);

let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
"Unit".to_string() => Value::Array(vec![]))
)).unwrap();
assert_eq!(
json::from_value(Value::Object(btreemap!(
"Unit".to_string() => Value::Array(vec![]))
)).unwrap(),
v,
DeEnum::Unit::<u32, u32, u32>
);
}
Expand All @@ -358,8 +360,9 @@ fn test_de_enum_seq() {
let e = 5;
//let f = 6;

let v: DeEnum<_, _, _> = json::from_str("{\"Seq\":[1,2,3,5]}").unwrap();
assert_eq!(
json::from_str("{\"Seq\":[1,2,3,5]}").unwrap(),
v,
DeEnum::Seq(
a,
b,
Expand All @@ -370,17 +373,18 @@ fn test_de_enum_seq() {
)
);

let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
"Seq".to_string() => Value::Array(vec![
Value::U64(1),
Value::U64(2),
Value::U64(3),
//Value::U64(4),
Value::U64(5),
//Value::U64(6),
])
))).unwrap();
assert_eq!(
json::from_value(Value::Object(btreemap!(
"Seq".to_string() => Value::Array(vec![
Value::U64(1),
Value::U64(2),
Value::U64(3),
//Value::U64(4),
Value::U64(5),
//Value::U64(6),
])
))).unwrap(),
v,
DeEnum::Seq(
a,
b,
Expand All @@ -401,8 +405,11 @@ fn test_de_enum_map() {
let e = 5;
//let f = 6;

let v: DeEnum<_, _, _> = json::from_str(
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}"
).unwrap();
assert_eq!(
json::from_str("{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}").unwrap(),
v,
DeEnum::Map {
a: a,
b: b,
Expand All @@ -413,17 +420,19 @@ fn test_de_enum_map() {
}
);

let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
"Map".to_string() => Value::Object(btreemap![
"a".to_string() => Value::U64(1),
"b".to_string() => Value::U64(2),
"c".to_string() => Value::U64(3),
//"d".to_string() => Value::U64(4)
"e".to_string() => Value::U64(5)
//"f".to_string() => Value::U64(6)
])
))).unwrap();

assert_eq!(
json::from_value(Value::Object(btreemap!(
"Map".to_string() => Value::Object(btreemap![
"a".to_string() => Value::U64(1),
"b".to_string() => Value::U64(2),
"c".to_string() => Value::U64(3),
//"d".to_string() => Value::U64(4)
"e".to_string() => Value::U64(5)
//"f".to_string() => Value::U64(6)
])
))).unwrap(),
v,
DeEnum::Map {
a: a,
b: b,
Expand Down

0 comments on commit d0b49d9

Please sign in to comment.