Skip to content

Commit 0db1166

Browse files
authored
Merge pull request #28 from rust-scraper/newsch/master
Detach inserted node before moving in insert_id_*
2 parents b9193a7 + 24711d2 commit 0db1166

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
472472

473473
{
474474
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
475+
new_sibling.detach();
475476
new_sibling.node().parent = Some(parent_id);
476477
new_sibling.node().prev_sibling = prev_sibling_id;
477478
new_sibling.node().next_sibling = Some(self.id);
@@ -508,6 +509,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
508509

509510
{
510511
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
512+
new_sibling.detach();
511513
new_sibling.node().parent = Some(parent_id);
512514
new_sibling.node().prev_sibling = Some(self.id);
513515
new_sibling.node().next_sibling = next_sibling_id;

tests/tree.rs

+88-1
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)