From d2c25c5ca10207efcd90c66a6b205904707f8bc0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 10 Jan 2020 13:29:46 +0100 Subject: [PATCH] Add tests for reading of toml documents Signed-off-by: Matthias Beyer --- tests/tests.rs | 2 + tests/toml/mod.rs | 1 + tests/toml/read.rs | 197 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 tests/tests.rs create mode 100644 tests/toml/mod.rs create mode 100644 tests/toml/read.rs diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..9c7a9f3 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,2 @@ +#[macro_use] extern crate is_match; +mod toml; diff --git a/tests/toml/mod.rs b/tests/toml/mod.rs new file mode 100644 index 0000000..0bec210 --- /dev/null +++ b/tests/toml/mod.rs @@ -0,0 +1 @@ +mod read; diff --git a/tests/toml/read.rs b/tests/toml/read.rs new file mode 100644 index 0000000..6e90289 --- /dev/null +++ b/tests/toml/read.rs @@ -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(_))); +} + -- 2.45.2