assertThat(child2.getParent(), is(nullValue()));
}
@Test
public void shouldReplaceChild() {
AstNode parentOfReplacement = new AstNode(name("parentOfReplacement"));
AstNode replacement = new AstNode(parentOfReplacement, name("replacement"));
parent = new AstNode(name("parent"));
AstNode child1 = new AstNode(parent, name("childA"));
AstNode child2 = new AstNode(parent, name("childB"));
AstNode child3 = new AstNode(parent, name("childC"));
assertThat(parent.getChild(0), is(sameInstance(child1)));
assertThat(parent.getChild(1), is(sameInstance(child2)));
assertThat(parent.getChild(2), is(sameInstance(child3)));
// Perform the replacement ...
assertThat(parent.replaceChild(child2, replacement), is(true));
assertThat(parent.getChild(0), is(sameInstance(child1)));
assertThat(parent.getChild(1), is(sameInstance(replacement)));
assertThat(parent.getChild(2), is(sameInstance(child3)));
assertThat(replacement.getParent(), is(sameInstance(parent)));
assertThat(child1.getParent(), is(sameInstance(parent)));
assertThat(child2.getParent(), is(nullValue()));
assertThat(child3.getParent(), is(sameInstance(parent)));
// The replacement should no longer be a child of its former parent ...
assertThat(parentOfReplacement.getChildCount(), is(0));
}