Skip to content

Commit a0803ef

Browse files
newschcfvescovo
authored andcommitted
Add tests for insert_id_*
1 parent b9193a7 commit a0803ef

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

tests/tree.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate ego_tree;
22

3-
use ego_tree::Tree;
3+
use ego_tree::{tree, Tree};
44

55
#[test]
66
fn new() {
@@ -69,3 +69,90 @@ fn neq() {
6969
let two = Tree::new('b');
7070
assert_eq!(one, two);
7171
}
72+
73+
#[test]
74+
fn insert_id_after() {
75+
let mut tree = tree! {
76+
"root" => {
77+
"a" => {
78+
"child 1",
79+
},
80+
"b" => {
81+
"child 2",
82+
},
83+
}
84+
};
85+
86+
let a = tree.root().first_child().unwrap().id();
87+
let b = tree.root().last_child().unwrap().id();
88+
89+
assert_eq!(2, tree.root().children().count());
90+
assert_eq!(1, tree.get(a).unwrap().children().count());
91+
assert_eq!(1, tree.get(b).unwrap().children().count());
92+
93+
let child_1 = tree.get(a).unwrap().first_child().unwrap().id();
94+
tree.get_mut(b).unwrap().insert_id_after(child_1);
95+
96+
assert_eq!(
97+
0,
98+
tree.get(a).unwrap().children().count(),
99+
"child 1 should be moved from a"
100+
);
101+
assert_eq!(
102+
1,
103+
tree.get(b).unwrap().children().count(),
104+
"b should be unchanged"
105+
);
106+
assert_eq!(
107+
child_1,
108+
tree.root().last_child().unwrap().id(),
109+
"child 1 should be last child of root"
110+
);
111+
assert_eq!(3, tree.root().children().count());
112+
}
113+
114+
#[test]
115+
fn insert_id_before() {
116+
let mut tree = tree! {
117+
"root" => {
118+
"a" => {
119+
"child 1",
120+
},
121+
"b" => {
122+
"child 2",
123+
},
124+
}
125+
};
126+
127+
let a = tree.root().first_child().unwrap().id();
128+
let b = tree.root().last_child().unwrap().id();
129+
130+
assert_eq!(2, tree.root().children().count());
131+
assert_eq!(1, tree.get(a).unwrap().children().count());
132+
assert_eq!(1, tree.get(b).unwrap().children().count());
133+
134+
let child_1 = tree.get(a).unwrap().first_child().unwrap().id();
135+
tree.get_mut(b).unwrap().insert_id_before(child_1);
136+
137+
assert_eq!(
138+
0,
139+
tree.get(a).unwrap().children().count(),
140+
"child 1 should be moved from a"
141+
);
142+
assert_eq!(
143+
1,
144+
tree.get(b).unwrap().children().count(),
145+
"b should be unchanged"
146+
);
147+
assert_eq!(
148+
b,
149+
tree.root().last_child().unwrap().id(),
150+
"b should be last child of root"
151+
);
152+
assert_eq!(
153+
child_1,
154+
tree.get(b).unwrap().prev_sibling().unwrap().id(),
155+
"child 1 should be between a and b"
156+
);
157+
assert_eq!(3, tree.root().children().count());
158+
}

0 commit comments

Comments
 (0)