Newer
Older
tree.add(child_a, Some(root));
tree.add(child_b, Some(root));
tree.add(grandchild_a, Some(child_a));
tree.add(grandchild_b, Some(child_b));
let mut expected = Tree::default();
let expected_root = WrappedIndex(Entity::from_raw(5));
let expected_child_a = WrappedIndex(Entity::from_raw(6));
let expected_child_b = WrappedIndex(Entity::from_raw(7));
let expected_grandchild_a = WrappedIndex(Entity::from_raw(8));
let expected_grandchild_b = WrappedIndex(Entity::from_raw(9));
expected.add(expected_root, None);
expected.add(expected_child_a, Some(expected_root));
expected.add(expected_child_b, Some(expected_root));
expected.add(expected_grandchild_a, Some(expected_child_a));
expected.add(expected_grandchild_b, Some(expected_child_b));
tree.replace(grandchild_b, expected_grandchild_b);
assert!(tree
.children
.get(&child_b)
.unwrap()
.contains(&expected_grandchild_b));
assert!(!tree.children.get(&child_b).unwrap().contains(&grandchild_b));
tree.replace(grandchild_a, expected_grandchild_a);
assert!(tree
.children
.get(&child_a)
.unwrap()
.contains(&expected_grandchild_a));
assert!(!tree.children.get(&child_a).unwrap().contains(&grandchild_a));
tree.replace(child_a, expected_child_a);
assert!(tree
.children
.get(&root)
.unwrap()
.contains(&expected_child_a));
assert!(!tree.children.get(&root).unwrap().contains(&child_a));
assert_eq!(
expected_child_a,
tree.get_parent(expected_grandchild_a).unwrap()
);
tree.replace(child_b, expected_child_b);
assert!(tree
.children
.get(&root)
.unwrap()
.contains(&expected_child_b));
assert!(!tree.children.get(&root).unwrap().contains(&child_b));
assert_eq!(
expected_child_b,
tree.get_parent(expected_grandchild_b).unwrap()
);
tree.replace(root, expected_root);
assert_eq!(Some(expected_root), tree.root_node);
assert_eq!(expected_root, tree.get_parent(expected_child_a).unwrap());
assert_eq!(expected_root, tree.get_parent(expected_child_b).unwrap());
assert_eq!(expected, tree);
}
#[test]
fn should_remove() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child_a = WrappedIndex(Entity::from_raw(1));
let child_b = WrappedIndex(Entity::from_raw(2));
let grandchild_a = WrappedIndex(Entity::from_raw(3));
let grandchild_b = WrappedIndex(Entity::from_raw(4));
tree.add(root, None);
tree.add(child_a, Some(root));
tree.add(child_b, Some(root));
tree.add(grandchild_a, Some(child_a));
tree.add(grandchild_b, Some(child_b));
let mut expected = Tree::default();
expected.add(root, None);
expected.add(child_a, Some(root));
expected.add(grandchild_a, Some(child_a));
tree.remove(child_b);
assert!(!tree.children.get(&root).unwrap().contains(&child_b));
assert_eq!(expected, tree);
}
#[test]
fn should_remove_root() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child_a = WrappedIndex(Entity::from_raw(1));
let child_b = WrappedIndex(Entity::from_raw(2));
let grandchild_a = WrappedIndex(Entity::from_raw(3));
let grandchild_b = WrappedIndex(Entity::from_raw(4));
tree.add(root, None);
tree.add(child_a, Some(root));
tree.add(child_b, Some(root));
tree.add(grandchild_a, Some(child_a));
tree.add(grandchild_b, Some(child_b));
let expected = Tree::default();
tree.remove(root);
assert_eq!(None, tree.root_node);
assert_eq!(expected, tree);
}
#[test]
fn should_remove_and_reparent() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child_a = WrappedIndex(Entity::from_raw(1));
let child_b = WrappedIndex(Entity::from_raw(2));
let grandchild_a = WrappedIndex(Entity::from_raw(3));
let grandchild_b = WrappedIndex(Entity::from_raw(4));
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
tree.add(root, None);
tree.add(child_a, Some(root));
tree.add(child_b, Some(root));
tree.add(grandchild_a, Some(child_a));
tree.add(grandchild_b, Some(child_b));
let mut expected = Tree::default();
expected.add(root, None);
expected.add(child_a, Some(root));
expected.add(grandchild_a, Some(child_a));
expected.add(grandchild_b, Some(root));
tree.remove_and_reparent(child_b);
assert_eq!(root, tree.get_parent(grandchild_b).unwrap());
assert!(tree.children.get(&root).unwrap().contains(&grandchild_b));
assert!(!tree.children.get(&root).unwrap().contains(&child_b));
assert_eq!(expected, tree);
}
#[test]
fn should_contain_root() {
let mut tree = Tree::default();
tree.add(root, None);
assert!(tree.contains(root));
}
#[test]
fn should_contain_child() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child = WrappedIndex(Entity::from_raw(1));
tree.add(root, None);
tree.add(child, Some(root));
assert!(tree.contains(root));
assert!(tree.contains(child));
}
#[test]
fn should_be_empty() {
let mut tree = Tree::default();
assert!(tree.is_empty());
tree.add(WrappedIndex(Entity::from_raw(0)), None);
assert!(!tree.is_empty())
}
#[test]
fn should_be_descendant() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child = WrappedIndex(Entity::from_raw(1));
let grandchild = WrappedIndex(Entity::from_raw(2));
tree.add(root, None);
tree.add(child, Some(root));
tree.add(grandchild, Some(child));
assert!(!tree.is_descendant(root, root));
assert!(tree.is_descendant(child, root));
assert!(tree.is_descendant(grandchild, root));
}
#[test]
fn should_give_len() {
let mut tree = Tree::default();
let root = WrappedIndex(Entity::from_raw(0));
let child = WrappedIndex(Entity::from_raw(1));
let grandchild = WrappedIndex(Entity::from_raw(2));
assert_eq!(0, tree.len());
tree.add(root, None);
assert_eq!(1, tree.len());
tree.add(child, Some(root));
assert_eq!(2, tree.len());
tree.add(grandchild, Some(child));
assert_eq!(3, tree.len());
}