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(_))); }