|
1 | 1 | extern crate ego_tree;
|
2 | 2 |
|
3 |
| -use ego_tree::Tree; |
| 3 | +use ego_tree::{tree, Tree}; |
4 | 4 |
|
5 | 5 | #[test]
|
6 | 6 | fn new() {
|
@@ -69,3 +69,90 @@ fn neq() {
|
69 | 69 | let two = Tree::new('b');
|
70 | 70 | assert_eq!(one, two);
|
71 | 71 | }
|
| 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