Пример #1
0
  @Test
  public void test_children_when_one_child() {
    AstNodeType type1 = mock(AstNodeType.class);
    AstNodeType type2 = mock(AstNodeType.class);
    AstNode child = mock(AstNode.class);
    when(node.getNumberOfChildren()).thenReturn(1);

    when(node.getFirstChild()).thenReturn(child);
    AstSelect children = select.children();
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);

    when(node.getChildren()).thenReturn(ImmutableList.of(child));

    children = select.children(type1);
    assertThat((Object) children).isSameAs(AstSelectFactory.empty());

    when(child.getType()).thenReturn(type1);
    children = select.children(type1);
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);

    children = select.children(type1, type2);
    assertThat((Object) children).isSameAs(AstSelectFactory.empty());

    when(child.is(type1, type2)).thenReturn(true);
    children = select.children(type1, type2);
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);
  }
Пример #2
0
 @Test
 public void test_descendants() {
   assertThat((Object) select.descendants(mock(AstNodeType.class)))
       .isSameAs(AstSelectFactory.empty());
   assertThat((Object) select.descendants(mock(AstNodeType.class), mock(AstNodeType.class)))
       .isSameAs(AstSelectFactory.empty());
 }
Пример #3
0
 @Test
 public void test_children_when_no_children() {
   assertThat((Object) select.children()).isSameAs(AstSelectFactory.empty());
   assertThat((Object) select.children(mock(AstNodeType.class)))
       .isSameAs(AstSelectFactory.empty());
   assertThat((Object) select.children(mock(AstNodeType.class), mock(AstNodeType.class)))
       .isSameAs(AstSelectFactory.empty());
 }
Пример #4
0
  @Test
  public void test_filter() {
    Predicate<AstNode> predicate = mock(Predicate.class);
    assertThat((Object) select.filter(predicate)).isSameAs(AstSelectFactory.empty());

    when(predicate.apply(node)).thenReturn(true);
    assertThat((Object) select.filter(predicate)).isSameAs(select);
  }
Пример #5
0
  @Test
  public void test_filter_by_type() {
    AstNodeType type = mock(AstNodeType.class);
    assertThat((Object) select.filter(type)).isSameAs(AstSelectFactory.empty());

    when(node.getType()).thenReturn(type);
    assertThat((Object) select.filter(type)).isSameAs(select);
  }
Пример #6
0
  @Test
  public void test_parent() {
    assertThat((Object) select.parent()).isSameAs(AstSelectFactory.empty());

    AstNode parent = mock(AstNode.class);
    when(node.getParent()).thenReturn(parent);
    assertThat((Object) select.parent()).isInstanceOf(SingleAstSelect.class);
    assertThat(select.parent()).containsOnly(parent);
  }
Пример #7
0
  @Test
  public void test_previousSibling() {
    assertThat((Object) select.previousSibling()).isSameAs(AstSelectFactory.empty());

    AstNode sibling = mock(AstNode.class);
    when(node.getPreviousSibling()).thenReturn(sibling);
    assertThat((Object) select.previousSibling()).isInstanceOf(SingleAstSelect.class);
    assertThat(select.previousSibling()).containsOnly(sibling);
  }
Пример #8
0
  @Test
  public void test_firstAncestor_by_type() {
    AstNodeType type = mock(AstNodeType.class);
    assertThat((Object) select.firstAncestor(type)).isSameAs(AstSelectFactory.empty());

    AstNode parent = mock(AstNode.class);
    when(node.getParent()).thenReturn(parent);
    AstNode ancestor = mock(AstNode.class);
    when(ancestor.getType()).thenReturn(type);
    when(parent.getParent()).thenReturn(ancestor);
    assertThat((Object) select.firstAncestor(type)).isInstanceOf(SingleAstSelect.class);
    assertThat(select.firstAncestor(type)).containsOnly(ancestor);
  }