Skip to content

Commit

Permalink
Improve unit test on BlockStmtContextResolutionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerbsc committed Mar 23, 2022
1 parent 0b892c2 commit 21eeb61
Showing 1 changed file with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.contexts.BlockStmtContext;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
Expand All @@ -55,7 +51,7 @@ void must_be_resolved_from_previous_declaration(){
String src = "public class Example {\n"
+ " int a = 3;\n"
+ " public void bla() {\n"
+ " a = 7; // 'a' must be resolved as int not String"
+ " a = 7; // 'a' must be resolved as int not String\n"
+ " String a = \"\";\n"
+ " a = \"test\";\n"
+ " }\n"
Expand All @@ -64,20 +60,17 @@ void must_be_resolved_from_previous_declaration(){
.setSymbolResolver(new JavaSymbolSolver(new CombinedTypeSolver(new ReflectionTypeSolver())));
StaticJavaParser.setConfiguration(configuration);
CompilationUnit cu = StaticJavaParser.parse(src);
BlockStmt stmt = cu.findFirst(BlockStmt.class).get();
BlockStmtContext ctx = new BlockStmtContext(stmt, new ReflectionTypeSolver());
SymbolReference<? extends ResolvedValueDeclaration> ref = ctx.solveSymbol("a");

assertEquals(true, ref.isSolved());
assertEquals("int", ref.getCorrespondingDeclaration().getType().asPrimitive().describe());
AssignExpr expr = cu.findFirst(AssignExpr.class).get();
ResolvedType rt = expr.calculateResolvedType();
assertEquals("int", rt.describe());
}

@Test
void must_be_resolved_from_previous_declaration_second_declaration_of_the_same_field_name(){
String src = "public class Example {\n"
+ " int a = 3;\n"
+ " public void bla() {\n"
+ " a = 7; // 'a' must be resolved as int not String"
+ " a = 7; // 'a' must be resolved as int not String\n"
+ " String a = \"\";\n"
+ " a = \"test\";\n"
+ " }\n"
Expand Down

0 comments on commit 21eeb61

Please sign in to comment.