~matthiasbeyer/serde-select

d2c25c5ca10207efcd90c66a6b205904707f8bc0 — Matthias Beyer 4 years ago 6176f73
Add tests for reading of toml documents

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

A tests/tests.rs
A tests/toml/mod.rs
A tests/toml/read.rs
A tests/tests.rs => tests/tests.rs +2 -0
@@ 0,0 1,2 @@
#[macro_use] extern crate is_match;
mod toml;

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

A tests/toml/read.rs => tests/toml/read.rs +197 -0
@@ 0,0 1,197 @@
use std::str::FromStr;

use toml::from_str as toml_from_str;
use toml::Value;

use serde_select::read::Read;
use serde_select::query::Query;
use serde_select::error::Error;

#[test]
fn test_read_empty() {
    let toml: Value = toml_from_str("").unwrap();

    let query = Query::from_str("a").unwrap();
    let val = toml.read(&query);

    assert!(val.is_ok());
    let val = val.unwrap();

    assert!(val.is_none());
}

#[test]
fn test_read_table() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let query = Query::from_str("table").unwrap();
    let val = toml.read(&query);

    assert!(val.is_ok());
    let val = val.unwrap();

    assert!(val.is_some());
    let val = val.unwrap();

    assert!(is_match!(val, &Value::Table(_)));
    match val {
        Value::Table(ref t) => assert!(t.is_empty()),
        _ => panic!("What just happened?"),
    }
}

#[test]
fn test_read_table_value() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    a = 1
    "#,
    )
    .unwrap();

    let query = Query::from_str("table.a").unwrap();
    let val = toml.read(&query);

    assert!(val.is_ok());
    let val = val.unwrap();

    assert!(val.is_some());
    let val = val.unwrap();

    assert!(is_match!(val, &Value::Integer(1)));
}

#[test]
fn test_read_empty_table_value() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let query = Query::from_str("table.a").unwrap();
    let val = toml.read(&query);
    assert!(val.is_ok());
    let val = val.unwrap();

    assert!(val.is_none());
}

#[test]
fn test_read_table_index() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let query = Query::from_str("table.[0]").unwrap();
    let val = toml.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 toml: Value = toml_from_str("").unwrap();

    let val = toml.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 toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let val = toml.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::Table(_)));
    match val {
        Value::Table(ref t) => assert!(t.is_empty()),
        _ => panic!("What just happened?"),
    }
}

#[test]
fn test_read_table_value_without_seperator() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    a = 1
    "#,
    )
    .unwrap();

    let val = toml.read(&Query::from_str("table.a").unwrap());

    assert!(val.is_ok());
    let val = val.unwrap();

    assert!(val.is_some());
    let val = val.unwrap();

    assert!(is_match!(val, &Value::Integer(1)));
}

#[test]
fn test_read_empty_table_value_without_seperator() {
    let toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let val = toml.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 toml: Value = toml_from_str(
        r#"
    [table]
    "#,
    )
    .unwrap();

    let val = toml.read(&Query::from_str("table.[0]").unwrap());
    assert!(val.is_err());
    let err = val.unwrap_err();

    assert!(is_match!(err, Error::NoIndexInTable(_)));
}