use std::str::FromStr; use serde_json::from_str as json_from_str; use serde_json::Value; use serde_json::Number; use serde_select::read::Read; use serde_select::query::Query; use serde_select::error::Error; #[test] fn test_read_empty() { let json: Value = json_from_str("{}").unwrap(); let query = Query::from_str("a").unwrap(); let val = json.read(&query); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_none()); } #[test] fn test_read_table() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let query = Query::from_str("table").unwrap(); let val = json.read(&query); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_some()); let val = val.unwrap(); assert!(is_match!(val, &Value::Object(_))); match val { Value::Object(ref t) => assert!(t.is_empty()), _ => panic!("What just happened?"), } } #[test] fn test_read_table_value() { let json: Value = json_from_str("{\"table\":{\"a\":1}}").unwrap(); let query = Query::from_str("table.a").unwrap(); let val = json.read(&query); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_some()); let val = val.unwrap(); assert!(match val { Value::Number(n) => n.as_i64().unwrap() == 1, _ => false }); } #[test] fn test_read_empty_table_value() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let query = Query::from_str("table.a").unwrap(); let val = json.read(&query); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_none()); } #[test] fn test_read_table_index() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let query = Query::from_str("table.[0]").unwrap(); let val = json.read(&query); assert!(val.is_err()); let err = val.unwrap_err(); assert!(is_match!(err, Error::NoIndexInTable(_))); } /// /// /// Querying without specifying the seperator /// /// #[test] fn test_read_empty_without_seperator() { let json: Value = json_from_str("{}").unwrap(); let val = json.read(&Query::from_str("a").unwrap()); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_none()); } #[test] fn test_read_table_without_seperator() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let val = json.read(&Query::from_str("table").unwrap()); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_some()); let val = val.unwrap(); assert!(is_match!(val, &Value::Object(_))); match val { Value::Object(ref t) => assert!(t.is_empty()), _ => panic!("What just happened?"), } } #[test] fn test_read_table_value_without_seperator() { let json: Value = json_from_str("{\"table\":{\"a\":1}}").unwrap(); let val = json.read(&Query::from_str("table.a").unwrap()); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_some()); let val = val.unwrap(); assert!(match val { Value::Number(n) => n.as_i64().unwrap() == 1, _ => false }); } #[test] fn test_read_empty_table_value_without_seperator() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let val = json.read(&Query::from_str("table.a").unwrap()); assert!(val.is_ok()); let val = val.unwrap(); assert!(val.is_none()); } #[test] fn test_read_table_index_without_seperator() { let json: Value = json_from_str("{\"table\":{}}").unwrap(); let val = json.read(&Query::from_str("table.[0]").unwrap()); assert!(val.is_err()); let err = val.unwrap_err(); assert!(is_match!(err, Error::NoIndexInTable(_))); }