-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
WalkFindTest.java
115 lines (99 loc) · 4.2 KB
/
WalkFindTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.github.javaparser.ast;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.SimpleName;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.github.javaparser.StaticJavaParser.parse;
import static com.github.javaparser.StaticJavaParser.parseExpression;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class WalkFindTest {
@Test
void findCompilationUnit() {
CompilationUnit cu = parse("class X{int x;}");
VariableDeclarator x = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0);
assertEquals(cu, x.findCompilationUnit().get());
}
@Test
void findParent() {
CompilationUnit cu = parse("class X{int x;}");
SimpleName x = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0).getName();
assertEquals("int x;", x.findAncestor(FieldDeclaration.class).get().toString());
}
@Test
void findParentFromTypes() {
CompilationUnit cu = parse("class X{Integer x;}");
VariableDeclarator vd = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0);
assertEquals(FieldDeclaration.class.getName(),
vd.findAncestor(new Class[] { CompilationUnit.class, ClassOrInterfaceDeclaration.class, FieldDeclaration.class }).get().getClass()
.getName());
assertEquals(ClassOrInterfaceDeclaration.class.getName(),
vd.findAncestor(new Class[] { CompilationUnit.class, ClassOrInterfaceDeclaration.class }).get().getClass()
.getName());
}
@Test
void cantFindCompilationUnit() {
VariableDeclarator x = new VariableDeclarator();
assertFalse(x.findCompilationUnit().isPresent());
}
@Test
void genericWalk() {
Expression e = parseExpression("1+1");
StringBuilder b = new StringBuilder();
e.walk(n -> b.append(n.toString()));
assertEquals("1 + 111", b.toString());
}
@Test
void classSpecificWalk() {
Expression e = parseExpression("1+1");
StringBuilder b = new StringBuilder();
e.walk(IntegerLiteralExpr.class, n -> b.append(n.toString()));
assertEquals("11", b.toString());
}
@Test
void conditionalFindAll() {
Expression e = parseExpression("1+2+3");
List<IntegerLiteralExpr> ints = e.findAll(IntegerLiteralExpr.class, n -> n.asInt() > 1);
assertEquals("[2, 3]", ints.toString());
}
@Test
void typeOnlyFindAll() {
Expression e = parseExpression("1+2+3");
List<IntegerLiteralExpr> ints = e.findAll(IntegerLiteralExpr.class);
assertEquals("[1, 2, 3]", ints.toString());
}
@Test
void typeOnlyFindAllMatchesSubclasses() {
Expression e = parseExpression("1+2+3");
List<Node> ints = e.findAll(Node.class);
assertEquals("[1 + 2 + 3, 1 + 2, 1, 2, 3]", ints.toString());
}
@Test
void conditionalTypedFindFirst() {
Expression e = parseExpression("1+2+3");
Optional<IntegerLiteralExpr> ints = e.findFirst(IntegerLiteralExpr.class, n -> n.asInt() > 1);
assertEquals("Optional[2]", ints.toString());
}
@Test
void typeOnlyFindFirst() {
Expression e = parseExpression("1+2+3");
Optional<IntegerLiteralExpr> ints = e.findFirst(IntegerLiteralExpr.class);
assertEquals("Optional[1]", ints.toString());
}
@Test
void stream() {
Expression e = parseExpression("1+2+3");
List<IntegerLiteralExpr> ints = e.stream()
.filter(n -> n instanceof IntegerLiteralExpr)
.map(IntegerLiteralExpr.class::cast)
.filter(i -> i.asInt() > 1)
.collect(Collectors.toList());
assertEquals("[2, 3]", ints.toString());
}
}