~matthiasbeyer/serde-select

6e4d259e38a6c00772fb214f55610bf021d818a8 — Matthias Beyer 4 years ago d2c25c5
Add tests for JSON reading

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
3 files changed, 160 insertions(+), 0 deletions(-)

A tests/json/mod.rs
A tests/json/read.rs
M tests/tests.rs
A tests/json/mod.rs => tests/json/mod.rs +1 -0
@@ 0,0 1,1 @@
mod read;

A tests/json/read.rs => tests/json/read.rs +157 -0
@@ 0,0 1,157 @@
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(_)));
}



M tests/tests.rs => tests/tests.rs +2 -0
@@ 1,2 1,4 @@
#[macro_use] extern crate is_match;

mod toml;
mod json;