use links_notation::LiNo;
#[test]
fn links_group_constructor_test() {
let element = LiNo::Ref("root".to_string());
let children = [
LiNo::Ref("child1".to_string()),
LiNo::Ref("child2".to_string()),
];
assert_eq!(element, LiNo::Ref("root".to_string()));
assert_eq!(children.len(), 2);
assert_eq!(children[0], LiNo::Ref("child1".to_string()));
assert_eq!(children[1], LiNo::Ref("child2".to_string()));
}
#[test]
fn links_group_constructor_equivalent_test() {
let root = LiNo::Ref("root".to_string());
let children = vec![
LiNo::Ref("child1".to_string()),
LiNo::Ref("child2".to_string()),
];
let group = LiNo::Link {
id: Some("group".to_string()),
values: vec![root.clone()]
.into_iter()
.chain(children.clone())
.collect(),
};
if let LiNo::Link { id, values } = group {
assert_eq!(id, Some("group".to_string()));
assert_eq!(values.len(), 3); assert_eq!(values[0], root);
} else {
panic!("Expected Link variant");
}
}
#[test]
fn links_group_to_list_flattens_structure_test() {
let root = LiNo::Ref("root".to_string());
let child1 = LiNo::Ref("child1".to_string());
let child2 = LiNo::Ref("child2".to_string());
let grandchild = LiNo::Ref("grandchild".to_string());
let nested_child = LiNo::Link::<String> {
id: None,
values: vec![child2.clone(), grandchild.clone()],
};
let group = LiNo::Link {
id: None,
values: vec![root.clone(), child1.clone(), nested_child],
};
if let LiNo::Link { id, values } = group {
assert_eq!(id, None);
assert_eq!(values.len(), 3);
assert_eq!(values[0], root);
assert_eq!(values[1], child1);
if let LiNo::Link {
id: nested_id,
values: nested_values,
} = &values[2]
{
assert_eq!(*nested_id, None);
assert_eq!(nested_values.len(), 2);
assert_eq!(nested_values[0], child2);
assert_eq!(nested_values[1], grandchild);
}
}
}
#[test]
fn links_group_to_string_test() {
let root = LiNo::Ref("root".to_string());
let children = vec![
LiNo::Ref("child1".to_string()),
LiNo::Ref("child2".to_string()),
];
let group = LiNo::Link::<String> {
id: None,
values: vec![root].into_iter().chain(children).collect(),
};
let str_output = group.to_string();
assert!(str_output.contains("root"));
assert!(str_output.contains("child1"));
assert!(str_output.contains("child2"));
assert!(str_output.contains("(") && str_output.contains(")")); }
#[test]
fn links_group_append_to_links_list_test() {
let element = LiNo::Ref("root".to_string());
let children = vec![
LiNo::Ref("child1".to_string()),
LiNo::Ref("child2".to_string()),
];
let group = LiNo::Link::<String> {
id: None,
values: vec![element.clone()]
.into_iter()
.chain(children.clone())
.collect(),
};
let mut list: Vec<LiNo<String>> = Vec::new();
list.push(group.clone());
if let LiNo::Link { id: _, values } = group {
for value in values {
list.push(value);
}
}
assert_eq!(list.len(), 4); }