Generate tests for ide light classes from the same test data as for compiler light classes

This commit is contained in:
Pavel V. Talanov
2015-10-14 16:47:50 +03:00
parent 35e56993d3
commit 9e9f593a7f
13 changed files with 351 additions and 37 deletions
@@ -2,5 +2,5 @@ public final class Derived implements Base {
public Derived(@org.jetbrains.annotations.NotNull Base x) { /* compiled code */ }
@org.jetbrains.annotations.NotNull
public java.lang.String baz(@org.jetbrains.annotations.NotNull java.lang.String s) { /* compiled code */ }
public java.lang.String baz(@org.jetbrains.annotations.NotNull java.lang.String g) { /* compiled code */ }
}
@@ -1,7 +1,7 @@
// Derived
interface Base {
fun baz(s: String): String
fun baz(g: String): String
}
class Derived(x: Base): Base by x
@@ -1,7 +1,7 @@
public final class C {
@kotlin.jvm.JvmOverloads
@org.jetbrains.annotations.NotNull
public final java.lang.String foo(@org.jetbrains.annotations.NotNull java.lang.String o, @org.jetbrains.annotations.NotNull java.lang.String s1, @org.jetbrains.annotations.NotNull java.lang.String k, @org.jetbrains.annotations.Nullable java.lang.String s2) { /* compiled code */ }
public final java.lang.String foo(@org.jetbrains.annotations.NotNull java.lang.String o, @org.jetbrains.annotations.NotNull java.lang.String o1, @org.jetbrains.annotations.NotNull java.lang.String o3, @org.jetbrains.annotations.Nullable java.lang.String o4) { /* compiled code */ }
@kotlin.jvm.JvmOverloads
@org.jetbrains.annotations.NotNull
@@ -1,7 +1,7 @@
// C
class C {
@[kotlin.jvm.JvmOverloads] public fun foo(o: String = "O", s1: String, k: String = "K", s2: String?): String {
return o + k
@[kotlin.jvm.JvmOverloads] public fun foo(o: String = "O", o1: String, o3: String = "K", o4: String?): String {
return "a"
}
}
@@ -7,5 +7,5 @@ public final class PrivateInClass {
private final java.lang.String bar(java.lang.String a, java.lang.String b) { /* compiled code */ }
private PrivateInClass(java.lang.String s) { /* compiled code */ }
private PrivateInClass(java.lang.String g) { /* compiled code */ }
}
@@ -1,6 +1,6 @@
// PrivateInClass
class PrivateInClass private (s: String?) {
class PrivateInClass private (g: String?) {
private var nn: String
get() = ""
set(value) {}
@@ -4,5 +4,5 @@
@java.lang.Deprecated
public final class _DefaultPackage {
@kotlin.jvm.internal.KotlinDelegatedMethod(implementationClassName = "VoidReturnKt")
public static final void foo(@org.jetbrains.annotations.NotNull java.lang.String s) { /* compiled code */ }
public static final void foo(@org.jetbrains.annotations.NotNull java.lang.String g) { /* compiled code */ }
}
@@ -1,3 +1,3 @@
// _DefaultPackage
fun foo(s: String) {}
fun foo(g: String) {}
@@ -16,10 +16,9 @@
package org.jetbrains.kotlin.asJava;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.compiled.ClsElementImpl;
import com.intellij.psi.search.GlobalSearchScope;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithWithJava;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
@@ -27,18 +26,15 @@ import org.jetbrains.kotlin.config.CompilerConfiguration;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestJdkKind;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractCompilerLightClassTest extends KotlinMultiFileTestWithWithJava<Void, Void> {
private static final Pattern SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE);
@Override
@NotNull
protected CompilerConfiguration createCompilerConfiguration(File javaFilesDir) {
@@ -66,28 +62,17 @@ public abstract class AbstractCompilerLightClassTest extends KotlinMultiFileTest
@Override
protected void doMultiFileTest(File file, Map<String, ModuleAndDependencies> modules, List<Void> files) throws IOException {
String text = FileUtil.loadFile(file, true);
Matcher matcher = SUBJECT_FQ_NAME_PATTERN.matcher(text);
assertTrue("No FqName specified. First line of the form '// f.q.Name' expected", matcher.find());
String fqName = matcher.group(1);
JavaElementFinder finder = createFinder(getEnvironment());
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(getEnvironment().getProject()));
if (!(psiClass instanceof KotlinLightClass)) {
throw new IllegalStateException("Not a light class: " + psiClass + " (" + fqName + ")");
}
PsiClass delegate = ((KotlinLightClass) psiClass).getDelegate();
if (!(delegate instanceof ClsElementImpl)) {
throw new IllegalStateException("Not a CLS element: " + delegate);
}
StringBuilder buffer = new StringBuilder();
((ClsElementImpl) delegate).appendMirrorText(0, buffer);
String actual = buffer.toString();
JetTestUtils.assertEqualsToFile(JetTestUtils.replaceExtension(file, "java"), actual);
LightClassTestCommon.INSTANCE.testLightClass(file, new Function1<String, PsiClass>() {
@Override
public PsiClass invoke(String s) {
try {
return createFinder(getEnvironment()).findClass(s, GlobalSearchScope.allScope(getEnvironment().getProject()));
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
}
}
});
}
@Override
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.asJava
import com.intellij.openapi.util.io.FileUtil
import com.intellij.psi.PsiClass
import com.intellij.psi.impl.compiled.ClsElementImpl
import junit.framework.TestCase
import org.jetbrains.kotlin.test.JetTestUtils
import java.io.File
import java.util.regex.Pattern
object LightClassTestCommon {
private val SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE)
@JvmOverloads
fun testLightClass(
testDataFile: File,
findLightClass: (String) -> PsiClass?,
normalizeText: (String) -> String = { it }
) {
val text = FileUtil.loadFile(testDataFile, true)
val matcher = SUBJECT_FQ_NAME_PATTERN.matcher(text)
TestCase.assertTrue("No FqName specified. First line of the form '// f.q.Name' expected", matcher.find())
val fqName = matcher.group(1)
val lightClass = findLightClass(fqName)
TestCase.assertTrue("Not a light class: $lightClass ($fqName)", lightClass is KotlinLightClass)
val delegate = (lightClass as KotlinLightClass).getDelegate()
TestCase.assertTrue("Not a CLS element: $delegate", delegate is ClsElementImpl)
val buffer = StringBuilder()
(delegate as ClsElementImpl).appendMirrorText(0, buffer)
val actual = normalizeText(buffer.toString())
JetTestUtils.assertEqualsToFile(JetTestUtils.replaceExtension(testDataFile, "java"), actual)
}
}
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.generators.tests.reservedWords.generateTestDataForRe
import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest
import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest
import org.jetbrains.kotlin.idea.actions.AbstractGotoTestOrCodeActionTest
import org.jetbrains.kotlin.idea.caches.resolve.AbstractIdeLightClassTest
import org.jetbrains.kotlin.idea.codeInsight.*
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateActionTest
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateTestSupportMethodActionTest
@@ -747,6 +748,12 @@ fun main(args: Array<String>) {
}
}
testGroup("idea/tests", "compiler/testData") {
testClass<AbstractIdeLightClassTest>() {
model("asJava/lightClasses", excludeDirs = listOf("delegation"))
}
}
testGroup("idea/idea-completion/tests", "idea/idea-completion/testData") {
testClass<AbstractCompiledKotlinInJavaCompletionTest>() {
model("injava", extension = "java")
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.caches.resolve
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.asJava.LightClassTestCommon
import org.jetbrains.kotlin.idea.test.JetLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.JetWithJdkAndRuntimeLightProjectDescriptor
import java.io.File
abstract class AbstractIdeLightClassTest : JetLightCodeInsightFixtureTestCase() {
fun doTest(testDataPath: String) {
myFixture.configureByFile(testDataPath)
val project = project
LightClassTestCommon.testLightClass(
File(testDataPath),
findLightClass = {
JavaPsiFacade.getInstance(project).findClass(it, GlobalSearchScope.allScope(project))
},
normalizeText = {
//NOTE: ide and compiler differ in names generated for parameters with unspecified names
it.replace("java.lang.String s,", "java.lang.String p,").replace("java.lang.String s)", "java.lang.String p)")
.replace("java.lang.String s1", "java.lang.String p1").replace("java.lang.String s2", "java.lang.String p2")
}
)
}
override fun getProjectDescriptor() = JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
}
@@ -0,0 +1,223 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.caches.resolve;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/asJava/lightClasses")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class IdeLightClassTestGenerated extends AbstractIdeLightClassTest {
public void testAllFilesPresentInLightClasses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses"), Pattern.compile("^(.+)\\.kt$"), true, "delegation");
}
@TestMetadata("compiler/testData/asJava/lightClasses/facades")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Facades extends AbstractIdeLightClassTest {
public void testAllFilesPresentInFacades() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/facades"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("SingleFile.kt")
public void testSingleFile() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/facades/SingleFile.kt");
doTest(fileName);
}
@TestMetadata("SingleJvmClassName.kt")
public void testSingleJvmClassName() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/facades/SingleJvmClassName.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class NullabilityAnnotations extends AbstractIdeLightClassTest {
public void testAllFilesPresentInNullabilityAnnotations() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/nullabilityAnnotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Class.kt")
public void testClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Class.kt");
doTest(fileName);
}
@TestMetadata("ClassObjectField.kt")
public void testClassObjectField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassObjectField.kt");
doTest(fileName);
}
@TestMetadata("ClassWithConstructor.kt")
public void testClassWithConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassWithConstructor.kt");
doTest(fileName);
}
@TestMetadata("ClassWithConstructorAndProperties.kt")
public void testClassWithConstructorAndProperties() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassWithConstructorAndProperties.kt");
doTest(fileName);
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Generic.kt");
doTest(fileName);
}
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/IntOverridesAny.kt");
doTest(fileName);
}
@TestMetadata("JvmOverloads.kt")
public void testJvmOverloads() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/JvmOverloads.kt");
doTest(fileName);
}
@TestMetadata("NullableUnitReturn.kt")
public void testNullableUnitReturn() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/NullableUnitReturn.kt");
doTest(fileName);
}
@TestMetadata("OverrideAnyWithUnit.kt")
public void testOverrideAnyWithUnit() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/OverrideAnyWithUnit.kt");
doTest(fileName);
}
@TestMetadata("PlatformTypes.kt")
public void testPlatformTypes() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/PlatformTypes.kt");
doTest(fileName);
}
@TestMetadata("Primitives.kt")
public void testPrimitives() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Primitives.kt");
doTest(fileName);
}
@TestMetadata("PrivateInClass.kt")
public void testPrivateInClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/PrivateInClass.kt");
doTest(fileName);
}
@TestMetadata("PrivateInTrait.kt")
public void testPrivateInTrait() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/PrivateInTrait.kt");
doTest(fileName);
}
@TestMetadata("Synthetic.kt")
public void testSynthetic() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Synthetic.kt");
doTest(fileName);
}
@TestMetadata("Trait.kt")
public void testTrait() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Trait.kt");
doTest(fileName);
}
@TestMetadata("TraitClassObjectField.kt")
public void testTraitClassObjectField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/TraitClassObjectField.kt");
doTest(fileName);
}
@TestMetadata("UnitAsGenericArgument.kt")
public void testUnitAsGenericArgument() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/UnitAsGenericArgument.kt");
doTest(fileName);
}
@TestMetadata("UnitParameter.kt")
public void testUnitParameter() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/UnitParameter.kt");
doTest(fileName);
}
@TestMetadata("VoidReturn.kt")
public void testVoidReturn() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/VoidReturn.kt");
doTest(fileName);
}
@TestMetadata("_DefaultPackage.kt")
public void test_DefaultPackage() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations/_DefaultPackage.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/asJava/lightClasses/object")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Object extends AbstractIdeLightClassTest {
public void testAllFilesPresentInObject() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/object"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("SimpleObject.kt")
public void testSimpleObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/object/SimpleObject.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/asJava/lightClasses/publicField")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class PublicField extends AbstractIdeLightClassTest {
public void testAllFilesPresentInPublicField() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/publicField"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("CompanionObject.kt")
public void testCompanionObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/publicField/CompanionObject.kt");
doTest(fileName);
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/publicField/Simple.kt");
doTest(fileName);
}
}
}