Skip to content

Detach inserted node before moving in insert_id_* #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = prev_sibling_id;
new_sibling.node().next_sibling = Some(self.id);
Expand Down Expand Up @@ -508,6 +509,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = Some(self.id);
new_sibling.node().next_sibling = next_sibling_id;
Expand Down
89 changes: 88 additions & 1 deletion tests/tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate ego_tree;

use ego_tree::Tree;
use ego_tree::{tree, Tree};

#[test]
fn new() {
Expand Down Expand Up @@ -69,3 +69,90 @@ fn neq() {
let two = Tree::new('b');
assert_eq!(one, two);
}

#[test]
fn insert_id_after() {
let mut tree = tree! {
"root" => {
"a" => {
"child 1",
},
"b" => {
"child 2",
},
}
};

let a = tree.root().first_child().unwrap().id();
let b = tree.root().last_child().unwrap().id();

assert_eq!(2, tree.root().children().count());
assert_eq!(1, tree.get(a).unwrap().children().count());
assert_eq!(1, tree.get(b).unwrap().children().count());

let child_1 = tree.get(a).unwrap().first_child().unwrap().id();
tree.get_mut(b).unwrap().insert_id_after(child_1);

assert_eq!(
0,
tree.get(a).unwrap().children().count(),
"child 1 should be moved from a"
);
assert_eq!(
1,
tree.get(b).unwrap().children().count(),
"b should be unchanged"
);
assert_eq!(
child_1,
tree.root().last_child().unwrap().id(),
"child 1 should be last child of root"
);
assert_eq!(3, tree.root().children().count());
}

#[test]
fn insert_id_before() {
let mut tree = tree! {
"root" => {
"a" => {
"child 1",
},
"b" => {
"child 2",
},
}
};

let a = tree.root().first_child().unwrap().id();
let b = tree.root().last_child().unwrap().id();

assert_eq!(2, tree.root().children().count());
assert_eq!(1, tree.get(a).unwrap().children().count());
assert_eq!(1, tree.get(b).unwrap().children().count());

let child_1 = tree.get(a).unwrap().first_child().unwrap().id();
tree.get_mut(b).unwrap().insert_id_before(child_1);

assert_eq!(
0,
tree.get(a).unwrap().children().count(),
"child 1 should be moved from a"
);
assert_eq!(
1,
tree.get(b).unwrap().children().count(),
"b should be unchanged"
);
assert_eq!(
b,
tree.root().last_child().unwrap().id(),
"b should be last child of root"
);
assert_eq!(
child_1,
tree.get(b).unwrap().prev_sibling().unwrap().id(),
"child 1 should be between a and b"
);
assert_eq!(3, tree.root().children().count());
}
Loading