Skip to content

Commit 317e51f

Browse files
pbludovromani
authored andcommitted
Issue #7487: add method hasChildren() to DetailAST
1 parent 89b4dcd commit 317e51f

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/main/java/com/puppycrawl/tools/checkstyle/DetailAstImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ public DetailAstImpl getFirstChild() {
374374
return (DetailAstImpl) super.getFirstChild();
375375
}
376376

377+
@Override
378+
public boolean hasChildren() {
379+
return getFirstChild() != null;
380+
}
381+
377382
/**
378383
* Clears the child count for the ast instance.
379384
* @param ast The ast to clear.

‎src/main/java/com/puppycrawl/tools/checkstyle/api/DetailAST.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,10 @@ public interface DetailAST {
118118
*/
119119
int getNumberOfChildren();
120120

121+
/**
122+
* Returns whether this AST has any children.
123+
*
124+
* @return {@code true} if this AST has any children.
125+
*/
126+
boolean hasChildren();
121127
}

‎src/test/java/com/puppycrawl/tools/checkstyle/DetailAstImplTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.puppycrawl.tools.checkstyle;
2121

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

138+
@Test
139+
public void testHasChildren() throws Exception {
140+
final DetailAstImpl root = new DetailAstImpl();
141+
final DetailAstImpl child = new DetailAstImpl();
142+
143+
root.setFirstChild(child);
144+
getSetParentMethod().invoke(child, root);
145+
146+
assertWithMessage("Root node should have children")
147+
.that(root.hasChildren())
148+
.isTrue();
149+
assertWithMessage("Child node should have no children")
150+
.that(child.hasChildren())
151+
.isFalse();
152+
}
153+
137154
@Test
138155
public void testGetChildCountType() throws Exception {
139156
final DetailAstImpl root = new DetailAstImpl();

0 commit comments

Comments
 (0)