Resolve test data fixed
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package Foo {
|
||||
package Foo
|
||||
fun bar() = 610
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if (Foo.bar() == 610) "OK" else "fail"
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
package root
|
||||
|
||||
~a~package a {
|
||||
// FILE: f.kt
|
||||
~a~package a
|
||||
import java.*
|
||||
|
||||
~a.a~val a : util.List<Int>? = null
|
||||
|
||||
val y : Any? = `a.b`b
|
||||
|
||||
}
|
||||
|
||||
package a {
|
||||
// FILE: f.kt
|
||||
package a
|
||||
import java.util.*
|
||||
|
||||
~a.b~val b : List<Int>? = null
|
||||
|
||||
val x = `a.a`a
|
||||
}
|
||||
|
||||
// FILE: f.kt
|
||||
package root
|
||||
val x = `a`a.`a.a`a
|
||||
|
||||
val y = `a`a.`a.b`b
|
||||
@@ -1,4 +1,4 @@
|
||||
~ns~package nestedObjects {
|
||||
~ns~package nestedObjects
|
||||
object ~A~A {
|
||||
val b = `A.B`B
|
||||
val d = `A`A.`A.B`B.`A.B.A`A
|
||||
@@ -25,4 +25,3 @@
|
||||
val c = `A`A.`A.B`B
|
||||
val d = A.B.`A.B.A`A
|
||||
val e = B.`!`A.B
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.jetbrains.jet;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
@@ -18,6 +19,9 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
@@ -168,5 +172,43 @@ public class JetTestUtils {
|
||||
mkdirs(file);
|
||||
}
|
||||
|
||||
public static final Pattern FILE_PATTERN = Pattern.compile("//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE);
|
||||
public interface TestFileFactory<F> {
|
||||
F create(String fileName, String text);
|
||||
}
|
||||
|
||||
public static <F> List<F> createTestFiles(String testFileName, String expectedText, TestFileFactory<F> factory) {
|
||||
List<F> testFileFiles = Lists.newArrayList();
|
||||
Matcher matcher = FILE_PATTERN.matcher(expectedText);
|
||||
if (!matcher.find()) {
|
||||
// One file
|
||||
testFileFiles.add(factory.create(testFileName, expectedText));
|
||||
}
|
||||
else {
|
||||
int processedChars = 0;
|
||||
// Many files
|
||||
while (true) {
|
||||
String fileName = matcher.group(1);
|
||||
int start = matcher.start();
|
||||
assert start == processedChars : "Characters skipped from " + processedChars + " to " + matcher.start();
|
||||
|
||||
boolean nextFileExists = matcher.find();
|
||||
int end;
|
||||
if (nextFileExists) {
|
||||
end = matcher.start();
|
||||
}
|
||||
else {
|
||||
end = expectedText.length();
|
||||
}
|
||||
String fileText = expectedText.substring(start, end);
|
||||
processedChars = end;
|
||||
|
||||
testFileFiles.add(factory.create(fileName, fileText));
|
||||
|
||||
if (!nextFileExists) break;
|
||||
}
|
||||
assert processedChars == expectedText.length() : "Characters skipped from " + processedChars + " to " + (expectedText.length() - 1);
|
||||
}
|
||||
return testFileFiles;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetLiteFixture;
|
||||
import org.jetbrains.jet.JetTestCaseBuilder;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.lang.Configuration;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
|
||||
@@ -20,15 +21,12 @@ import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class JetDiagnosticsTest extends JetLiteFixture {
|
||||
private String name;
|
||||
public static final Pattern FILE_PATTERN = Pattern.compile("//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE);
|
||||
|
||||
public JetDiagnosticsTest(@NonNls String dataPath, String name) {
|
||||
super(dataPath);
|
||||
@@ -88,7 +86,12 @@ public class JetDiagnosticsTest extends JetLiteFixture {
|
||||
|
||||
String expectedText = loadFile(testFileName);
|
||||
|
||||
List<TestFile> testFileFiles = createTestFiles(testFileName, expectedText);
|
||||
List<TestFile> testFileFiles = JetTestUtils.createTestFiles(testFileName, expectedText, new JetTestUtils.TestFileFactory<TestFile>() {
|
||||
@Override
|
||||
public TestFile create(String fileName, String text) {
|
||||
return new TestFile(fileName, text);
|
||||
}
|
||||
});
|
||||
|
||||
boolean importJdk = expectedText.contains("+JDK");
|
||||
// Configuration configuration = importJdk ? JavaBridgeConfiguration.createJavaBridgeConfiguration(getProject()) : Configuration.EMPTY;
|
||||
@@ -114,41 +117,6 @@ public class JetDiagnosticsTest extends JetLiteFixture {
|
||||
assertEquals(expectedText, actualText.toString());
|
||||
}
|
||||
|
||||
private List<TestFile> createTestFiles(String testFileName, String expectedText) {
|
||||
List<TestFile> testFileFiles = Lists.newArrayList();
|
||||
Matcher matcher = FILE_PATTERN.matcher(expectedText);
|
||||
if (!matcher.find()) {
|
||||
// One file
|
||||
testFileFiles.add(new TestFile(testFileName, expectedText));
|
||||
}
|
||||
else {
|
||||
int processedChars = 0;
|
||||
// Many files
|
||||
while (true) {
|
||||
String fileName = matcher.group(1);
|
||||
int start = matcher.start();
|
||||
assertTrue("Characters skipped from " + processedChars + " to " + matcher.start(), start == processedChars);
|
||||
|
||||
boolean nextFileExists = matcher.find();
|
||||
int end;
|
||||
if (nextFileExists) {
|
||||
end = matcher.start();
|
||||
}
|
||||
else {
|
||||
end = expectedText.length();
|
||||
}
|
||||
String fileText = expectedText.substring(start, end);
|
||||
processedChars = end;
|
||||
|
||||
testFileFiles.add(new TestFile(fileName, fileText));
|
||||
|
||||
if (!nextFileExists) break;
|
||||
}
|
||||
assertTrue("Characters skipped from " + processedChars + " to " + (expectedText.length() - 1), processedChars == expectedText.length());
|
||||
}
|
||||
return testFileFiles;
|
||||
}
|
||||
|
||||
// private void convert(File src, File dest) throws IOException {
|
||||
// File[] files = src.listFiles();
|
||||
// for (File file : files) {
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package org.jetbrains.jet.resolve;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.lang.JetSemanticServices;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
@@ -12,13 +17,14 @@ import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||
import org.jetbrains.jet.lang.types.ErrorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -31,22 +37,37 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class ExpectedResolveData {
|
||||
public abstract class ExpectedResolveData {
|
||||
|
||||
private final Map<String, Integer> declarationToPosition = new HashMap<String, Integer>();
|
||||
private final Map<Integer, String> positionToReference = new HashMap<Integer, String>();
|
||||
private final Map<Integer, String> positionToType = new HashMap<Integer, String>();
|
||||
private final Map<String, DeclarationDescriptor> nameToDescriptor;
|
||||
private final Map<String, PsiElement> nameToPsiElement;
|
||||
// private final Map<String, JetType> nameToType;
|
||||
private static class Position {
|
||||
private final PsiElement element;
|
||||
|
||||
public ExpectedResolveData(Map<String, DeclarationDescriptor> nameToDescriptor, Map<String, PsiElement> nameToPsiElement/*, Map<String, JetType> nameToType*/) {
|
||||
this.nameToDescriptor = nameToDescriptor;
|
||||
this.nameToPsiElement = nameToPsiElement;
|
||||
// this.nameToType = nameToType;
|
||||
private Position(JetFile file, int offset) {
|
||||
this.element = file.findElementAt(offset);
|
||||
}
|
||||
|
||||
public PsiElement getElement() {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
public String extractData(String text) {
|
||||
private final Map<String, Position> declarationToPosition = Maps.newHashMap();
|
||||
private final Map<Position, String> positionToReference = Maps.newHashMap();
|
||||
private final Map<Position, String> positionToType = Maps.newHashMap();
|
||||
|
||||
private final Map<String, DeclarationDescriptor> nameToDescriptor;
|
||||
private final Map<String, PsiElement> nameToPsiElement;
|
||||
|
||||
public ExpectedResolveData(Map<String, DeclarationDescriptor> nameToDescriptor, Map<String, PsiElement> nameToPsiElement) {
|
||||
this.nameToDescriptor = nameToDescriptor;
|
||||
this.nameToPsiElement = nameToPsiElement;
|
||||
}
|
||||
|
||||
public final JetFile createFileFromMarkedUpText(String fileName, String text) {
|
||||
Map<String, Integer> declarationToIntPosition = Maps.newHashMap();
|
||||
Map<Integer, String> intPositionToReference = Maps.newHashMap();
|
||||
Map<Integer, String> intPositionToType = Maps.newHashMap();
|
||||
|
||||
Pattern pattern = Pattern.compile("(~[^~]+~)|(`[^`]+`)");
|
||||
while (true) {
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
@@ -56,16 +77,16 @@ public class ExpectedResolveData {
|
||||
String name = group.substring(1, group.length() - 1);
|
||||
int start = matcher.start();
|
||||
if (group.startsWith("~")) {
|
||||
if (declarationToPosition.put(name, start) != null) {
|
||||
if (declarationToIntPosition.put(name, start) != null) {
|
||||
throw new IllegalArgumentException("Redeclaration: " + name);
|
||||
}
|
||||
}
|
||||
else if (group.startsWith("`")) {
|
||||
if (name.startsWith(":")) {
|
||||
positionToType.put(start - 1, name.substring(1));
|
||||
intPositionToType.put(start - 1, name.substring(1));
|
||||
}
|
||||
else {
|
||||
positionToReference.put(start, name);
|
||||
intPositionToReference.put(start, name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -75,16 +96,38 @@ public class ExpectedResolveData {
|
||||
text = text.substring(0, start) + text.substring(matcher.end());
|
||||
}
|
||||
|
||||
System.out.println(text);
|
||||
return text;
|
||||
JetFile jetFile = createJetFile(fileName, text);
|
||||
|
||||
for (Map.Entry<Integer, String> entry : intPositionToType.entrySet()) {
|
||||
positionToType.put(new Position(jetFile, entry.getKey()), entry.getValue());
|
||||
}
|
||||
for (Map.Entry<String, Integer> entry : declarationToIntPosition.entrySet()) {
|
||||
declarationToPosition.put(entry.getKey(), new Position(jetFile, entry.getValue()));
|
||||
}
|
||||
for (Map.Entry<Integer, String> entry : intPositionToReference.entrySet()) {
|
||||
positionToReference.put(new Position(jetFile, entry.getKey()), entry.getValue());
|
||||
}
|
||||
return jetFile;
|
||||
}
|
||||
|
||||
public void checkResult(JetFile file) {
|
||||
final Set<PsiElement> unresolvedReferences = new HashSet<PsiElement>();
|
||||
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(file.getProject());
|
||||
protected abstract JetFile createJetFile(String fileName, String text);
|
||||
|
||||
public final void checkResult(List<JetFile> files) {
|
||||
if (files.isEmpty()) {
|
||||
System.err.println("Suspicious: no files");
|
||||
return;
|
||||
}
|
||||
final Set<PsiElement> unresolvedReferences = Sets.newHashSet();
|
||||
Project project = files.iterator().next().getProject();
|
||||
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project);
|
||||
JetStandardLibrary lib = semanticServices.getStandardLibrary();
|
||||
|
||||
BindingContext bindingContext = JetTestUtils.analyzeNamespace(file.getRootNamespace(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
List<JetDeclaration> declarations = Lists.newArrayList();
|
||||
for (JetFile file : files) {
|
||||
declarations.add(file.getRootNamespace());
|
||||
}
|
||||
|
||||
BindingContext bindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(project, declarations, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
|
||||
if (diagnostic instanceof UnresolvedReferenceDiagnostic) {
|
||||
UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (UnresolvedReferenceDiagnostic) diagnostic;
|
||||
@@ -95,20 +138,20 @@ public class ExpectedResolveData {
|
||||
Map<String, JetDeclaration> nameToDeclaration = new HashMap<String, JetDeclaration>();
|
||||
|
||||
Map<JetDeclaration, String> declarationToName = new HashMap<JetDeclaration, String>();
|
||||
for (Map.Entry<String, Integer> entry : declarationToPosition.entrySet()) {
|
||||
for (Map.Entry<String, Position> entry : declarationToPosition.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
Integer position = entry.getValue();
|
||||
PsiElement element = file.findElementAt(position);
|
||||
Position position = entry.getValue();
|
||||
PsiElement element = position.getElement();
|
||||
|
||||
JetDeclaration ancestorOfType = getAncestorOfType(JetDeclaration.class, element);
|
||||
nameToDeclaration.put(name, ancestorOfType);
|
||||
declarationToName.put(ancestorOfType, name);
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, String> entry : positionToReference.entrySet()) {
|
||||
Integer position = entry.getKey();
|
||||
for (Map.Entry<Position, String> entry : positionToReference.entrySet()) {
|
||||
Position position = entry.getKey();
|
||||
String name = entry.getValue();
|
||||
PsiElement element = file.findElementAt(position);
|
||||
PsiElement element = position.getElement();
|
||||
|
||||
JetReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(element, JetReferenceExpression.class);
|
||||
if ("!".equals(name)) {
|
||||
@@ -216,11 +259,11 @@ public class ExpectedResolveData {
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, String> entry : positionToType.entrySet()) {
|
||||
Integer position = entry.getKey();
|
||||
for (Map.Entry<Position, String> entry : positionToType.entrySet()) {
|
||||
Position position = entry.getKey();
|
||||
String typeName = entry.getValue();
|
||||
|
||||
PsiElement element = file.findElementAt(position);
|
||||
PsiElement element = position.getElement();
|
||||
JetExpression expression = getAncestorOfType(JetExpression.class, element);
|
||||
|
||||
JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, expression);
|
||||
@@ -230,15 +273,16 @@ public class ExpectedResolveData {
|
||||
|
||||
assertNotNull("Expected class not found: " + typeName, expectedClass);
|
||||
expectedTypeConstructor = expectedClass.getTypeConstructor();
|
||||
} else {
|
||||
Integer declarationPosition = declarationToPosition.get(typeName);
|
||||
}
|
||||
else {
|
||||
Position declarationPosition = declarationToPosition.get(typeName);
|
||||
assertNotNull("Undeclared: " + typeName, declarationPosition);
|
||||
PsiElement declElement = file.findElementAt(declarationPosition);
|
||||
PsiElement declElement = declarationPosition.getElement();
|
||||
assertNotNull(declarationPosition);
|
||||
JetDeclaration declaration = getAncestorOfType(JetDeclaration.class, declElement);
|
||||
assertNotNull(declaration);
|
||||
if (declaration instanceof JetClass) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, (JetClass) declaration);
|
||||
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, declaration);
|
||||
expectedTypeConstructor = classDescriptor.getTypeConstructor();
|
||||
}
|
||||
else if (declaration instanceof JetTypeParameter) {
|
||||
|
||||
@@ -2,9 +2,10 @@ package org.jetbrains.jet.resolve;
|
||||
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.jet.JetLiteFixture;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
@@ -22,8 +23,12 @@ public abstract class ExtensibleResolveTestCase extends JetLiteFixture {
|
||||
|
||||
protected void doTest(@NonNls String filePath) throws Exception {
|
||||
String text = loadFile(filePath);
|
||||
text = expectedResolveData.extractData(text);
|
||||
JetFile jetFile = createPsiFile(new File(filePath).getName(), text);
|
||||
expectedResolveData.checkResult(jetFile);
|
||||
List<JetFile> files = JetTestUtils.createTestFiles("file.kt", text, new JetTestUtils.TestFileFactory<JetFile>() {
|
||||
@Override
|
||||
public JetFile create(String fileName, String text) {
|
||||
return expectedResolveData.createFileFromMarkedUpText(fileName, text);
|
||||
}
|
||||
});
|
||||
expectedResolveData.checkResult(files);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
|
||||
import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResults;
|
||||
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
|
||||
@@ -54,7 +55,7 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
|
||||
FunctionDescriptor descriptorForSet = standardFunction(lib.getArray(), Collections.singletonList(new TypeProjection(lib.getIntType())), "set", lib.getIntType(), lib.getIntType());
|
||||
nameToDescriptor.put("std::Array.set(Int, Int)", descriptorForSet.getOriginal());
|
||||
|
||||
Map<String,PsiElement> nameToDeclaration = new HashMap<String, PsiElement>();
|
||||
Map<String, PsiElement> nameToDeclaration = new HashMap<String, PsiElement>();
|
||||
PsiClass java_util_Collections = findClass("java.util.Collections");
|
||||
nameToDeclaration.put("java::java.util.Collections.emptyList()", findMethod(java_util_Collections, "emptyList"));
|
||||
nameToDeclaration.put("java::java.util.Collections", java_util_Collections);
|
||||
@@ -78,7 +79,12 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
|
||||
nameToDeclaration.put("java::java.lang.Number", java_lang_Number);
|
||||
nameToDeclaration.put("java::java.lang.Number.intValue()", java_lang_Number.findMethodsByName("intValue", true)[0]);
|
||||
|
||||
return new ExpectedResolveData(nameToDescriptor, nameToDeclaration);
|
||||
return new ExpectedResolveData(nameToDescriptor, nameToDeclaration) {
|
||||
@Override
|
||||
protected JetFile createJetFile(String fileName, String text) {
|
||||
return createCheckAndReturnPsiFile(fileName, text);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -154,6 +160,9 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
// TestSuite suite = new TestSuite();
|
||||
// suite.addTest(new JetResolveTest("/resolve/Basic.jet", "basic"));
|
||||
// return suite;
|
||||
return JetTestCaseBuilder.suiteForDirectory(getHomeDirectory() + "/compiler/testData/", "/resolve/", true, new JetTestCaseBuilder.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user