Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented findAncestor(Class,Predicate)
- Loading branch information
1 parent
dabbd31
commit 3c4c58c
Showing
2 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
javaparser-core-testing/src/test/java/com/github/javaparser/ast/FindNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.github.javaparser.ast; | ||
|
||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.ast.stmt.BlockStmt; | ||
import com.github.javaparser.ast.stmt.TryStmt; | ||
import org.junit.Test; | ||
|
||
import static com.github.javaparser.JavaParser.parse; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Some tests for finding descendant and ancestor nodes. | ||
*/ | ||
public class FindNodeTest { | ||
@Test | ||
public void testFindFirst() { | ||
CompilationUnit cu = parse( | ||
"class Foo {\n" + | ||
" void foo() {\n" + | ||
" try {\n" + | ||
" } catch (Exception e) {\n" + | ||
" } finally {\n" + | ||
" try {\n" + | ||
" } catch (Exception e) {\n" + | ||
" foo();\n" + | ||
" } finally {\n" + | ||
" }\n" + | ||
" }\n" + | ||
"\n" + | ||
" }\n" + | ||
"}\n"); | ||
|
||
// find the method call expression foo() | ||
MethodCallExpr actual = cu.findFirst(MethodCallExpr.class).orElse(null); | ||
|
||
MethodCallExpr expected = cu.getType(0).getMember(0) | ||
.asMethodDeclaration().getBody().get().getStatement(0) | ||
.asTryStmt().getFinallyBlock().get().getStatement(0) | ||
.asTryStmt().getCatchClauses().get(0).getBody().getStatement(0) | ||
.asExpressionStmt().getExpression() | ||
.asMethodCallExpr(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void testFindAncestralFinallyBlock() { | ||
CompilationUnit cu = parse( | ||
"class Foo {\n" + | ||
" void foo() {\n" + | ||
" try {\n" + | ||
" } catch (Exception e) {\n" + | ||
" } finally {\n" + | ||
" try {\n" + | ||
" } catch (Exception e) {\n" + | ||
" foo();\n" + | ||
" } finally {\n" + | ||
" }\n" + | ||
" }\n" + | ||
"\n" + | ||
" }\n" + | ||
"}\n"); | ||
|
||
// find the method call expression foo() | ||
MethodCallExpr methodCallExpr = cu.findFirst(MethodCallExpr.class).orElse(null); | ||
|
||
// find the finally block that the method call expression foo() is in | ||
BlockStmt actual = methodCallExpr.findAncestor(BlockStmt.class, bs -> { | ||
if (bs.getParentNode().isPresent() && bs.getParentNode().get() instanceof TryStmt) { | ||
TryStmt ancestralTryStmt = (TryStmt) bs.getParentNode().get(); | ||
return bs == ancestralTryStmt.getFinallyBlock().orElse(null); | ||
} | ||
return false; | ||
}).orElse(null); | ||
|
||
BlockStmt expected = cu.getType(0).getMember(0) | ||
.asMethodDeclaration().getBody().get().getStatement(0) | ||
.asTryStmt().getFinallyBlock().get(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters