-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from rust-scraper/shouya/master
Add support for appending and prepending subtree
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[macro_use] | ||
extern crate ego_tree; | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[test] | ||
fn prepend_subtree() { | ||
let mut tree = tree!('a' => { 'b', 'c' => { 'd', 'e' } }); | ||
let node_id = tree.root().first_child().unwrap().id(); | ||
let mut node = tree.get_mut(node_id).unwrap(); | ||
assert_eq!(node.value(), &'b'); | ||
|
||
let subtree = tree!('f' => { 'g', 'h' => { 'i', 'j' } }); | ||
let mut root_subtree = node.prepend_subtree(subtree); | ||
assert_eq!(root_subtree.parent().unwrap().value(), &'b'); | ||
assert_eq!( | ||
root_subtree.parent().unwrap().parent().unwrap().value(), | ||
&'a' | ||
); | ||
|
||
let new_tree = | ||
tree!('a' => { 'b' => { 'f' => { 'g', 'h' => { 'i', 'j' } } }, 'c' => { 'd', 'e' } }); | ||
assert_eq!(format!("{:#?}", tree), format!("{:#?}", new_tree)); | ||
} | ||
|
||
#[test] | ||
fn append_subtree() { | ||
let mut tree = tree!('a' => { 'b', 'c' }); | ||
let mut node = tree.root_mut(); | ||
assert_eq!(node.value(), &'a'); | ||
|
||
let subtree = tree!('d' => { 'e', 'f' }); | ||
let mut root_subtree = node.append_subtree(subtree); | ||
assert_eq!(root_subtree.parent().unwrap().value(), &'a'); | ||
|
||
let new_tree = tree!('a' => { 'b', 'c', 'd' => { 'e', 'f' } }); | ||
assert_eq!(format!("{:#?}", tree), format!("{:#?}", new_tree)); | ||
} | ||
} |