Skip to content

Commit

Permalink
Issue checkstyle#7487: add method hasChildren() to DetailAST
Browse files Browse the repository at this point in the history
  • Loading branch information
pbludov committed Jan 20, 2020
1 parent dfed794 commit 547d141
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Expand Up @@ -374,6 +374,11 @@ public DetailAstImpl getFirstChild() {
return (DetailAstImpl) super.getFirstChild();
}

@Override
public boolean hasChildren() {
return getFirstChild() != null;
}

/**
* Clears the child count for the ast instance.
* @param ast The ast to clear.
Expand Down
Expand Up @@ -118,4 +118,10 @@ public interface DetailAST {
*/
int getNumberOfChildren();

/**
* Returns whether this AST has any children.
*
* @return {@code true} if this AST has any children.
*/
boolean hasChildren();
}
Expand Up @@ -19,6 +19,7 @@

package com.puppycrawl.tools.checkstyle;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -134,6 +135,22 @@ public void testGetChildCount() throws Exception {
assertEquals(firstLevelA, firstLevelB.getPreviousSibling(), "Invalid previous sibling");
}

@Test
public void testHasChildren() throws Exception {
final DetailAstImpl root = new DetailAstImpl();
final DetailAstImpl child = new DetailAstImpl();

root.setFirstChild(child);
getSetParentMethod().invoke(child, root);

assertWithMessage("Root node should have children")
.that(root.hasChildren())
.isTrue();
assertWithMessage("Child node should have no children")
.that(child.hasChildren())
.isFalse();
}

@Test
public void testGetChildCountType() throws Exception {
final DetailAstImpl root = new DetailAstImpl();
Expand Down

0 comments on commit 547d141

Please sign in to comment.