Skip to content

Commit 7ced467

Browse files
authored
Merge pull request #130 from Yoric/short
Resolves #126 - Equality on JsonValue should not differentiate betwee…
2 parents 083a571 + f214c68 commit 7ced467

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/value/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! number_to_signed {
3434
}
3535
}
3636

37-
#[derive(Debug, PartialEq, Clone)]
37+
#[derive(Debug, Clone)]
3838
pub enum JsonValue {
3939
Null,
4040
Short(Short),
@@ -45,6 +45,25 @@ pub enum JsonValue {
4545
Array(Vec<JsonValue>),
4646
}
4747

48+
impl PartialEq for JsonValue {
49+
fn eq(&self, other: &Self) -> bool {
50+
use self::JsonValue::*;
51+
match (self, other) {
52+
(&Null, &Null) => true,
53+
(&Short(ref a), &Short(ref b)) => a == b,
54+
(&String(ref a), &String(ref b)) => a == b,
55+
(&Short(ref a), &String(ref b))
56+
| (&String(ref b), &Short(ref a)) => a.as_str() == b.as_str(),
57+
(&Number(ref a), &Number(ref b)) => a == b,
58+
(&Boolean(ref a), &Boolean(ref b)) => a == b,
59+
(&Object(ref a), &Object(ref b)) => a == b,
60+
(&Array(ref a), &Array(ref b)) => a == b,
61+
_ => false,
62+
}
63+
}
64+
}
65+
66+
impl Eq for JsonValue {}
4867

4968
/// Implements formatting
5069
///

tests/value.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,41 @@ fn pretty_writer_generator() {
621621

622622
assert_eq!(String::from_utf8(buf).unwrap(), "{\n \"foo\": [\n \"bar\",\n 100,\n true\n ]\n}");
623623
}
624+
625+
#[test]
626+
fn equality() {
627+
let left = object!{
628+
"foo" => array!["bar", 100, true]
629+
};
630+
631+
let left_copy = object!{
632+
"foo" => array!["bar", 100, true]
633+
};
634+
635+
let left_string = object!{
636+
"foo" => array![JsonValue::String("bar".to_string()), 100, true]
637+
};
638+
639+
let left_short = object!{
640+
"foo" => array![JsonValue::Short(unsafe { json::short::Short::from_slice("bar") }), 100, true]
641+
};
642+
643+
let change_bool = object!{
644+
"foo" => array!["bar", 100, false]
645+
};
646+
647+
let change_string = object!{
648+
"foo" => array![JsonValue::String("sna".to_string()), 100, true]
649+
};
650+
651+
let change_short = object!{
652+
"foo" => array![JsonValue::Short(unsafe { json::short::Short::from_slice("sna") }), 100, true]
653+
};
654+
655+
assert_eq!(left, left_copy);
656+
assert_eq!(left, left_string);
657+
assert_eq!(left, left_short);
658+
assert_ne!(left, change_bool);
659+
assert_ne!(left, change_string);
660+
assert_ne!(left, change_short);
661+
}

0 commit comments

Comments
 (0)