Skip to content

Commit

Permalink
(test) add test for valuable
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <[email protected]>
  • Loading branch information
sunng87 committed Jul 19, 2021
1 parent c6df4ef commit 8d44650
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions valuable/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ impl Valuable for Json {
Json::Array(ref array) => array.as_value(),
Json::Bool(ref value) => value.as_value(),
Json::Number(ref num) => {
// TODO: check correctness for this
if num.is_f64() {
Value::F64(num.as_f64().unwrap())
} else if num.is_i64() {
Value::I64(num.as_i64().unwrap())
} else if num.is_u64() {
Value::U64(num.as_u64().unwrap())
} else {
unreachable!()
}
Expand All @@ -30,13 +27,10 @@ impl Valuable for Json {
Json::Array(ref array) => array.visit(visit),
Json::Bool(ref value) => value.visit(visit),
Json::Number(ref num) => {
// TODO: check correctness for this
if num.is_f64() {
num.as_f64().unwrap().visit(visit)
} else if num.is_i64() {
num.as_i64().unwrap().visit(visit)
} else if num.is_u64() {
num.as_u64().unwrap().visit(visit)
} else {
unreachable!()
}
Expand Down Expand Up @@ -66,3 +60,19 @@ impl Mappable for Map<String, Json> {
(len, Some(len))
}
}

#[cfg(test)]
mod test {
use crate::{Valuable, Value};
use serde_json::json;

#[test]
fn test_json() {
let j = json!({"a": 100, "b": 1.0, "c": -1});
let jv = j.as_value();

assert!(matches!(jv, Value::Mappable(_)));

assert!(matches!(json!(100).as_value(), Value::I64(_)));
}
}

0 comments on commit 8d44650

Please sign in to comment.