Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69c04cc
commit 02cad0c
Showing
6 changed files
with
189 additions
and
42 deletions.
There are no files selected for viewing
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
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
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
64 changes: 64 additions & 0 deletions
64
...test/java/com/github/javaparser/symbolsolver/resolution/naming/AbstractNameLogicTest.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,64 @@ | ||
package com.github.javaparser.symbolsolver.resolution.naming; | ||
|
||
import com.github.javaparser.*; | ||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public abstract class AbstractNameLogicTest extends AbstractResolutionTest { | ||
|
||
protected Node getNameInCodeTollerant(String code, String name, ParseStart parseStart) { | ||
return getNameInCode(code, name, parseStart, true); | ||
} | ||
|
||
protected Node getNameInCode(String code, String name, ParseStart parseStart) { | ||
return getNameInCode(code, name, parseStart, false); | ||
} | ||
|
||
private Node getNameInCode(String code, String name, ParseStart parseStart, boolean tollerant) { | ||
ParserConfiguration parserConfiguration = new ParserConfiguration(); | ||
parserConfiguration.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_10); | ||
ParseResult<? extends Node> parseResult = new JavaParser(parserConfiguration).parse(parseStart, new StringProvider(code)); | ||
if (!parseResult.isSuccessful()) { | ||
parseResult.getProblems().forEach(p -> System.out.println("ERR: " + p)); | ||
} | ||
assertTrue(parseResult.isSuccessful()); | ||
Node root = parseResult.getResult().get(); | ||
List<Node> allNames = root.findAll(Node.class).stream() | ||
.filter(NameLogic::isAName) | ||
.collect(Collectors.toList()); | ||
List<Node> matchingNames = allNames.stream() | ||
.filter(n -> NameLogic.nameAsString(n).equals(name)) | ||
.collect(Collectors.toList()); | ||
// In case of one name being contained in other as is, we remove it | ||
for (int i=0;i<matchingNames.size();i++) { | ||
Node container = matchingNames.get(i); | ||
for (int j=i+1;j<matchingNames.size();j++) { | ||
Node contained = matchingNames.get(j); | ||
if (contained.getParentNode().isPresent() && contained.getParentNode().get() == container | ||
&& NameLogic.nameAsString(contained).equals(NameLogic.nameAsString(container))) { | ||
matchingNames.remove(j); | ||
j--; | ||
} | ||
} | ||
} | ||
|
||
if (matchingNames.size() == 0) { | ||
throw new IllegalArgumentException("Not found. Names found: " + String.join(", ", | ||
allNames.stream().map(NameLogic::nameAsString).collect(Collectors.toList()))); | ||
} else if (matchingNames.size() > 1) { | ||
if (tollerant) { | ||
return matchingNames.get(matchingNames.size() - 1); | ||
} else { | ||
throw new IllegalArgumentException("Ambiguous: there are several matching."); | ||
} | ||
} else { | ||
return matchingNames.get(0); | ||
} | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...ava/com/github/javaparser/symbolsolver/resolution/naming/NameLogicDisambiguationTest.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,36 @@ | ||
package com.github.javaparser.symbolsolver.resolution.naming; | ||
|
||
import com.github.javaparser.*; | ||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; | ||
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest; | ||
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class NameLogicDisambiguationTest extends AbstractNameLogicTest { | ||
|
||
private void assertNameInCodeIsDisambiguited(String code, String name, | ||
NameCategory syntacticClassification, | ||
NameCategory nameCategory, | ||
ParseStart parseStart, TypeSolver typeSolver) { | ||
Node nameNode = getNameInCodeTollerant(code, name, parseStart); | ||
assertEquals(syntacticClassification, NameLogic.syntacticClassificationAccordingToContext(nameNode)); | ||
assertEquals(nameCategory, NameLogic.classifyReference(nameNode, typeSolver)); | ||
} | ||
|
||
@Test | ||
public void ambiguousNameToLocalVar() { | ||
assertNameInCodeIsDisambiguited("class A { void foo() {\n" + | ||
"SomeClass a; a.aField;" + "\n" + | ||
"} }", "a", NameCategory.AMBIGUOUS_NAME, NameCategory.EXPRESSION_NAME, ParseStart.COMPILATION_UNIT, | ||
new ReflectionTypeSolver()); | ||
} | ||
|
||
|
||
} |
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