Move tests to new module
This commit is contained in:
parent
847b65f3b0
commit
ecfe78d87b
2 changed files with 462 additions and 452 deletions
|
@ -1308,8 +1308,6 @@ mod test {
|
|||
extern crate env_logger;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use super::EntryHeader;
|
||||
use super::Token;
|
||||
use storeid::StoreId;
|
||||
|
||||
use toml::Value;
|
||||
|
@ -1459,456 +1457,6 @@ Hai";
|
|||
assert_eq!(TEST_ENTRY, string);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_simple() {
|
||||
let tokens = EntryHeader::tokenize("a", '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
header.insert(String::from("a"), Value::Integer(1));
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_with_array() {
|
||||
let tokens = EntryHeader::tokenize("a.0", '.').unwrap();
|
||||
assert!(tokens.len() == 2, "2 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
let ary = Value::Array(vec![Value::Integer(1)]);
|
||||
header.insert(String::from("a"), ary);
|
||||
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_extract_array() {
|
||||
let tokens = EntryHeader::tokenize("a", '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
let ary = Value::Array(vec![Value::Integer(1)]);
|
||||
header.insert(String::from("a"), ary);
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Array(vec![Value::Integer(1)]), res.unwrap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a big testing header.
|
||||
*
|
||||
* JSON equivalent:
|
||||
*
|
||||
* ```json
|
||||
* {
|
||||
* "a": {
|
||||
* "array": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
||||
* },
|
||||
* "b": {
|
||||
* "array": [ "string1", "string2", "string3", "string4" ]
|
||||
* },
|
||||
* "c": {
|
||||
* "array": [ 1, "string2", 3, "string4" ]
|
||||
* },
|
||||
* "d": {
|
||||
* "array": [
|
||||
* {
|
||||
* "d1": 1
|
||||
* },
|
||||
* {
|
||||
* "d2": 2
|
||||
* },
|
||||
* {
|
||||
* "d3": 3
|
||||
* },
|
||||
* ],
|
||||
*
|
||||
* "something": "else",
|
||||
*
|
||||
* "and": {
|
||||
* "something": {
|
||||
* "totally": "different"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* The sections "a", "b", "c", "d" are created in the respective helper functions
|
||||
* create_header_section_a, create_header_section_b, create_header_section_c and
|
||||
* create_header_section_d.
|
||||
*
|
||||
* These functions can also be used for testing.
|
||||
*
|
||||
*/
|
||||
fn create_header() -> Value {
|
||||
let a = create_header_section_a();
|
||||
let b = create_header_section_b();
|
||||
let c = create_header_section_c();
|
||||
let d = create_header_section_d();
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
header.insert(String::from("a"), a);
|
||||
header.insert(String::from("b"), b);
|
||||
header.insert(String::from("c"), c);
|
||||
header.insert(String::from("d"), d);
|
||||
|
||||
Value::Table(header)
|
||||
}
|
||||
|
||||
fn create_header_section_a() -> Value {
|
||||
// 0..10 is exclusive 10
|
||||
let a_ary = Value::Array((0..10).map(|x| Value::Integer(x)).collect());
|
||||
|
||||
let mut a_obj = BTreeMap::new();
|
||||
a_obj.insert(String::from("array"), a_ary);
|
||||
Value::Table(a_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_b() -> Value {
|
||||
let b_ary = Value::Array((0..9)
|
||||
.map(|x| Value::String(format!("string{}", x)))
|
||||
.collect());
|
||||
|
||||
let mut b_obj = BTreeMap::new();
|
||||
b_obj.insert(String::from("array"), b_ary);
|
||||
Value::Table(b_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_c() -> Value {
|
||||
let c_ary = Value::Array(
|
||||
vec![
|
||||
Value::Integer(1),
|
||||
Value::String(String::from("string2")),
|
||||
Value::Integer(3),
|
||||
Value::String(String::from("string4"))
|
||||
]);
|
||||
|
||||
let mut c_obj = BTreeMap::new();
|
||||
c_obj.insert(String::from("array"), c_ary);
|
||||
Value::Table(c_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_d() -> Value {
|
||||
let d_ary = Value::Array(
|
||||
vec![
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d1"), Value::Integer(1));
|
||||
tab
|
||||
},
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d2"), Value::Integer(2));
|
||||
tab
|
||||
},
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d3"), Value::Integer(3));
|
||||
tab
|
||||
},
|
||||
].into_iter().map(Value::Table).collect());
|
||||
|
||||
let and_obj = Value::Table({
|
||||
let mut tab = BTreeMap::new();
|
||||
let something_tab = Value::Table({
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("totally"), Value::String(String::from("different")));
|
||||
tab
|
||||
});
|
||||
tab.insert(String::from("something"), something_tab);
|
||||
tab
|
||||
});
|
||||
|
||||
let mut d_obj = BTreeMap::new();
|
||||
d_obj.insert(String::from("array"), d_ary);
|
||||
d_obj.insert(String::from("something"), Value::String(String::from("else")));
|
||||
d_obj.insert(String::from("and"), and_obj);
|
||||
Value::Table(d_obj)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_a() {
|
||||
test_walk_header_extract_section("a", &create_header_section_a());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_b() {
|
||||
test_walk_header_extract_section("b", &create_header_section_b());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_c() {
|
||||
test_walk_header_extract_section("c", &create_header_section_c());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_d() {
|
||||
test_walk_header_extract_section("d", &create_header_section_d());
|
||||
}
|
||||
|
||||
fn test_walk_header_extract_section(secname: &str, expected: &Value) {
|
||||
let tokens = EntryHeader::tokenize(secname, '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from(secname)),
|
||||
"'{}' token was expected, {:?} was parsed", secname, tokens.iter().next());
|
||||
|
||||
let mut header = create_header();
|
||||
let res = EntryHeader::walk_header(&mut header, tokens);
|
||||
assert_eq!(expected, res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_extract_numbers() {
|
||||
test_extract_number("a", 0, 0);
|
||||
test_extract_number("a", 1, 1);
|
||||
test_extract_number("a", 2, 2);
|
||||
test_extract_number("a", 3, 3);
|
||||
test_extract_number("a", 4, 4);
|
||||
test_extract_number("a", 5, 5);
|
||||
test_extract_number("a", 6, 6);
|
||||
test_extract_number("a", 7, 7);
|
||||
test_extract_number("a", 8, 8);
|
||||
test_extract_number("a", 9, 9);
|
||||
|
||||
test_extract_number("c", 0, 1);
|
||||
test_extract_number("c", 2, 3);
|
||||
}
|
||||
|
||||
fn test_extract_number(sec: &str, idx: usize, exp: i64) {
|
||||
let tokens = EntryHeader::tokenize(&format!("{}.array.{}", sec, idx)[..], '.').unwrap();
|
||||
assert!(tokens.len() == 3, "3 token was expected, {} were parsed", tokens.len());
|
||||
{
|
||||
let mut iter = tokens.iter();
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Key(String::from(sec));
|
||||
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", sec, tok);
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Key(String::from("array"));
|
||||
assert!(tok == &exp, "'array' token was expected, {:?} was parsed", tok);
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Index(idx);
|
||||
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", idx, tok);
|
||||
}
|
||||
|
||||
let mut header = create_header();
|
||||
let res = EntryHeader::walk_header(&mut header, tokens);
|
||||
assert_eq!(&mut Value::Integer(exp), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_read() {
|
||||
let v = create_header();
|
||||
let h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("c") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("c.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("c.array.1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("c.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.0.d1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.0.d2") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.0.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(None) = h.read("d.array.1.d1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.1.d2") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.1.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(None) = h.read("d.array.2.d1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.2.d2") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.2.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("d.something") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and.something") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("d.and.something.totally") { true } else { false });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_set_override() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
println!("Testing index 0");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Altering index 0");
|
||||
assert_eq!(h.set("a.array.0", Value::Integer(42)).unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Values now: {:?}", h);
|
||||
|
||||
println!("Testing all indexes");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(42));
|
||||
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
||||
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
||||
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
||||
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
||||
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
||||
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
||||
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
||||
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
||||
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_set_new() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(h.read("a.foo").is_ok());
|
||||
assert!(h.read("a.foo").unwrap().is_none());
|
||||
|
||||
{
|
||||
let v = h.set("a.foo", Value::Integer(42));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
||||
}
|
||||
|
||||
{
|
||||
let v = h.set("new", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
let v = h.set("new.subset", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
let v = h.set("new.subset.dest", Value::Integer(1337));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_header_insert_override() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
println!("Testing index 0");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Altering index 0");
|
||||
assert_eq!(h.insert("a.array.0", Value::Integer(42)).unwrap(), false);
|
||||
println!("...should have failed");
|
||||
|
||||
println!("Testing all indexes");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
||||
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
||||
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
||||
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
||||
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
||||
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
||||
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
||||
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
||||
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_insert_new() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(h.read("a.foo").is_ok());
|
||||
assert!(h.read("a.foo").unwrap().is_none());
|
||||
|
||||
{
|
||||
let v = h.insert("a.foo", Value::Integer(42));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
||||
}
|
||||
|
||||
{
|
||||
let v = h.insert("new", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
let v = h.insert("new.subset", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
let v = h.insert("new.subset.dest", Value::Integer(1337));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_delete() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from_table(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Integer(1))) = h.delete("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(9))) = h.delete("a.array.8") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.delete("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.delete("a") { true } else { false });
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -353,3 +353,465 @@ impl Extract for Value {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate env_logger;
|
||||
use store::EntryHeader;
|
||||
use super::{tokenize, walk_header};
|
||||
use super::Token;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use toml::Value;
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_simple() {
|
||||
let tokens = tokenize("a", '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
header.insert(String::from("a"), Value::Integer(1));
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_with_array() {
|
||||
let tokens = tokenize("a.0", '.').unwrap();
|
||||
assert!(tokens.len() == 2, "2 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
let ary = Value::Array(vec![Value::Integer(1)]);
|
||||
header.insert(String::from("a"), ary);
|
||||
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_extract_array() {
|
||||
let tokens = tokenize("a", '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
||||
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
let ary = Value::Array(vec![Value::Integer(1)]);
|
||||
header.insert(String::from("a"), ary);
|
||||
|
||||
let mut v_header = Value::Table(header);
|
||||
let res = walk_header(&mut v_header, tokens);
|
||||
assert_eq!(&mut Value::Array(vec![Value::Integer(1)]), res.unwrap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a big testing header.
|
||||
*
|
||||
* JSON equivalent:
|
||||
*
|
||||
* ```json
|
||||
* {
|
||||
* "a": {
|
||||
* "array": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
||||
* },
|
||||
* "b": {
|
||||
* "array": [ "string1", "string2", "string3", "string4" ]
|
||||
* },
|
||||
* "c": {
|
||||
* "array": [ 1, "string2", 3, "string4" ]
|
||||
* },
|
||||
* "d": {
|
||||
* "array": [
|
||||
* {
|
||||
* "d1": 1
|
||||
* },
|
||||
* {
|
||||
* "d2": 2
|
||||
* },
|
||||
* {
|
||||
* "d3": 3
|
||||
* },
|
||||
* ],
|
||||
*
|
||||
* "something": "else",
|
||||
*
|
||||
* "and": {
|
||||
* "something": {
|
||||
* "totally": "different"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* The sections "a", "b", "c", "d" are created in the respective helper functions
|
||||
* create_header_section_a, create_header_section_b, create_header_section_c and
|
||||
* create_header_section_d.
|
||||
*
|
||||
* These functions can also be used for testing.
|
||||
*
|
||||
*/
|
||||
fn create_header() -> Value {
|
||||
let a = create_header_section_a();
|
||||
let b = create_header_section_b();
|
||||
let c = create_header_section_c();
|
||||
let d = create_header_section_d();
|
||||
|
||||
let mut header = BTreeMap::new();
|
||||
header.insert(String::from("a"), a);
|
||||
header.insert(String::from("b"), b);
|
||||
header.insert(String::from("c"), c);
|
||||
header.insert(String::from("d"), d);
|
||||
|
||||
Value::Table(header)
|
||||
}
|
||||
|
||||
fn create_header_section_a() -> Value {
|
||||
// 0..10 is exclusive 10
|
||||
let a_ary = Value::Array((0..10).map(|x| Value::Integer(x)).collect());
|
||||
|
||||
let mut a_obj = BTreeMap::new();
|
||||
a_obj.insert(String::from("array"), a_ary);
|
||||
Value::Table(a_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_b() -> Value {
|
||||
let b_ary = Value::Array((0..9)
|
||||
.map(|x| Value::String(format!("string{}", x)))
|
||||
.collect());
|
||||
|
||||
let mut b_obj = BTreeMap::new();
|
||||
b_obj.insert(String::from("array"), b_ary);
|
||||
Value::Table(b_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_c() -> Value {
|
||||
let c_ary = Value::Array(
|
||||
vec![
|
||||
Value::Integer(1),
|
||||
Value::String(String::from("string2")),
|
||||
Value::Integer(3),
|
||||
Value::String(String::from("string4"))
|
||||
]);
|
||||
|
||||
let mut c_obj = BTreeMap::new();
|
||||
c_obj.insert(String::from("array"), c_ary);
|
||||
Value::Table(c_obj)
|
||||
}
|
||||
|
||||
fn create_header_section_d() -> Value {
|
||||
let d_ary = Value::Array(
|
||||
vec![
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d1"), Value::Integer(1));
|
||||
tab
|
||||
},
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d2"), Value::Integer(2));
|
||||
tab
|
||||
},
|
||||
{
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("d3"), Value::Integer(3));
|
||||
tab
|
||||
},
|
||||
].into_iter().map(Value::Table).collect());
|
||||
|
||||
let and_obj = Value::Table({
|
||||
let mut tab = BTreeMap::new();
|
||||
let something_tab = Value::Table({
|
||||
let mut tab = BTreeMap::new();
|
||||
tab.insert(String::from("totally"), Value::String(String::from("different")));
|
||||
tab
|
||||
});
|
||||
tab.insert(String::from("something"), something_tab);
|
||||
tab
|
||||
});
|
||||
|
||||
let mut d_obj = BTreeMap::new();
|
||||
d_obj.insert(String::from("array"), d_ary);
|
||||
d_obj.insert(String::from("something"), Value::String(String::from("else")));
|
||||
d_obj.insert(String::from("and"), and_obj);
|
||||
Value::Table(d_obj)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_a() {
|
||||
test_walk_header_extract_section("a", &create_header_section_a());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_b() {
|
||||
test_walk_header_extract_section("b", &create_header_section_b());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_c() {
|
||||
test_walk_header_extract_section("c", &create_header_section_c());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_big_d() {
|
||||
test_walk_header_extract_section("d", &create_header_section_d());
|
||||
}
|
||||
|
||||
fn test_walk_header_extract_section(secname: &str, expected: &Value) {
|
||||
let tokens = tokenize(secname, '.').unwrap();
|
||||
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
||||
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from(secname)),
|
||||
"'{}' token was expected, {:?} was parsed", secname, tokens.iter().next());
|
||||
|
||||
let mut header = create_header();
|
||||
let res = walk_header(&mut header, tokens);
|
||||
assert_eq!(expected, res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_walk_header_extract_numbers() {
|
||||
test_extract_number("a", 0, 0);
|
||||
test_extract_number("a", 1, 1);
|
||||
test_extract_number("a", 2, 2);
|
||||
test_extract_number("a", 3, 3);
|
||||
test_extract_number("a", 4, 4);
|
||||
test_extract_number("a", 5, 5);
|
||||
test_extract_number("a", 6, 6);
|
||||
test_extract_number("a", 7, 7);
|
||||
test_extract_number("a", 8, 8);
|
||||
test_extract_number("a", 9, 9);
|
||||
|
||||
test_extract_number("c", 0, 1);
|
||||
test_extract_number("c", 2, 3);
|
||||
}
|
||||
|
||||
fn test_extract_number(sec: &str, idx: usize, exp: i64) {
|
||||
let tokens = tokenize(&format!("{}.array.{}", sec, idx)[..], '.').unwrap();
|
||||
assert!(tokens.len() == 3, "3 token was expected, {} were parsed", tokens.len());
|
||||
{
|
||||
let mut iter = tokens.iter();
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Key(String::from(sec));
|
||||
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", sec, tok);
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Key(String::from("array"));
|
||||
assert!(tok == &exp, "'array' token was expected, {:?} was parsed", tok);
|
||||
|
||||
let tok = iter.next().unwrap();
|
||||
let exp = Token::Index(idx);
|
||||
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", idx, tok);
|
||||
}
|
||||
|
||||
let mut header = create_header();
|
||||
let res = walk_header(&mut header, tokens);
|
||||
assert_eq!(&mut Value::Integer(exp), res.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_read() {
|
||||
let v = create_header();
|
||||
let h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("c") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("c.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("c.array.1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("c.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.0.d1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.0.d2") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.0.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(None) = h.read("d.array.1.d1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.1.d2") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.1.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(None) = h.read("d.array.2.d1") { true } else { false });
|
||||
assert!(if let Ok(None) = h.read("d.array.2.d2") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.2.d3") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("d.something") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and.something") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::String(_))) = h.read("d.and.something.totally") { true } else { false });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_set_override() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
println!("Testing index 0");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Altering index 0");
|
||||
assert_eq!(h.set("a.array.0", Value::Integer(42)).unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Values now: {:?}", h);
|
||||
|
||||
println!("Testing all indexes");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(42));
|
||||
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
||||
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
||||
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
||||
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
||||
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
||||
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
||||
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
||||
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
||||
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_set_new() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(h.read("a.foo").is_ok());
|
||||
assert!(h.read("a.foo").unwrap().is_none());
|
||||
|
||||
{
|
||||
let v = h.set("a.foo", Value::Integer(42));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
||||
}
|
||||
|
||||
{
|
||||
let v = h.set("new", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
let v = h.set("new.subset", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
let v = h.set("new.subset.dest", Value::Integer(1337));
|
||||
assert!(v.is_ok());
|
||||
assert!(v.unwrap().is_none());
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_header_insert_override() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
println!("Testing index 0");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
|
||||
println!("Altering index 0");
|
||||
assert_eq!(h.insert("a.array.0", Value::Integer(42)).unwrap(), false);
|
||||
println!("...should have failed");
|
||||
|
||||
println!("Testing all indexes");
|
||||
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
||||
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
||||
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
||||
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
||||
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
||||
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
||||
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
||||
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
||||
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
||||
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_insert_new() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(h.read("a.foo").is_ok());
|
||||
assert!(h.read("a.foo").unwrap().is_none());
|
||||
|
||||
{
|
||||
let v = h.insert("a.foo", Value::Integer(42));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
||||
}
|
||||
|
||||
{
|
||||
let v = h.insert("new", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
let v = h.insert("new.subset", Value::Table(BTreeMap::new()));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
let v = h.insert("new.subset.dest", Value::Integer(1337));
|
||||
assert!(v.is_ok());
|
||||
assert_eq!(v.unwrap(), true);
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_delete() {
|
||||
let _ = env_logger::init();
|
||||
let v = create_header();
|
||||
let mut h = match v {
|
||||
Value::Table(t) => EntryHeader::from(t),
|
||||
_ => panic!("create_header() doesn't return a table!"),
|
||||
};
|
||||
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
||||
|
||||
assert!(if let Ok(Some(Value::Integer(1))) = h.delete("a.array.1") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Integer(9))) = h.delete("a.array.8") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Array(_))) = h.delete("a.array") { true } else { false });
|
||||
assert!(if let Ok(Some(Value::Table(_))) = h.delete("a") { true } else { false });
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue