Test for nullability annotations generalized and generated

This commit is contained in:
Andrey Breslav
2014-05-23 08:52:52 +02:00
parent 6617a884e4
commit afca70eb41
30 changed files with 228 additions and 116 deletions
@@ -1,3 +1,5 @@
// Class
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
@@ -1,3 +1,5 @@
// ClassObjectField
class ClassObjectField {
class object {
val x: String? = ""
@@ -1,3 +1,5 @@
// ClassWithConstructor
class ClassWithConstructor(
nullable: String?,
notNull: String
@@ -1,3 +1,5 @@
// ClassWithConstructorAndProperties
class ClassWithConstructorAndProperties(
val nullable: String?,
val notNull: String
@@ -1,3 +1,5 @@
// Generic
trait Generic<N, NN: Any> {
fun a(n: N): N
fun b(nn: NN): NN
@@ -1,3 +1,5 @@
// Primitives
trait Primitives {
fun int(x: Int): Int
@@ -1,3 +1,5 @@
// PrivateInClass
class PrivateInClass private (s: String?) {
private val nn: String = ""
private val n: String? = ""
@@ -1,3 +1,5 @@
// PrivateInTrait
trait PrivateInTrait {
private val nn: String
private val n: String?
@@ -1,3 +1,5 @@
// Synthetic
class Synthetic {
inner class Inner {
fun test() {
@@ -1,3 +1,5 @@
// Trait
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
@@ -1,3 +1,5 @@
// TraitClassObjectField
trait TraitClassObjectField {
class object {
val x: String? = ""
@@ -1,3 +1,5 @@
// _DefaultPackage
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
@@ -425,7 +425,7 @@ public class JetTestUtils {
public static void resolveAllKotlinFiles(JetCoreEnvironment environment) throws IOException {
List<String> paths = environment.getConfiguration().get(CommonConfigurationKeys.SOURCE_ROOTS_KEY);
assert paths != null;
if (paths == null) return;
List<JetFile> jetFiles = Lists.newArrayList();
for (String path : paths) {
jetFiles.add(loadJetFile(environment.getProject(), new File(path)));
@@ -0,0 +1,86 @@
/*
* Copyright 2010-2013 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.jet.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 org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.KotlinTestWithEnvironmentManagement;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractKotlinLightClassTest extends KotlinTestWithEnvironmentManagement {
private static final Pattern SUBJECT_FQ_NAME_PATTERN = Pattern.compile("^//\\s*(.*)$", Pattern.MULTILINE);
@NotNull
private JetCoreEnvironment createEnvironment(File kotlinFile) {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, kotlinFile.getPath());
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.getAnnotationsJar());
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.findMockJdkRtJar());
return JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
}
@NotNull
public static JavaElementFinder createFinder(@NotNull JetCoreEnvironment environment) throws IOException {
// We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport
JetTestUtils.resolveAllKotlinFiles(environment);
return JavaElementFinder.getInstance(environment.getProject());
}
protected void doTest(@NotNull String fileName) throws IOException {
File file = new File(fileName);
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);
JetCoreEnvironment environment = createEnvironment(file);
JavaElementFinder finder = createFinder(environment);
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(environment.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);
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.jet.asJava;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
@@ -51,10 +50,7 @@ public abstract class KotlinAsJavaTestBase extends KotlinTestWithEnvironment {
protected void setUp() throws Exception {
super.setUp();
// We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport
JetTestUtils.resolveAllKotlinFiles(getEnvironment());
finder = JavaElementFinder.getInstance(getProject());
finder = AbstractKotlinLightClassTest.createFinder(getEnvironment());
}
@Override
@@ -0,0 +1,111 @@
/*
* Copyright 2010-2014 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.jet.asJava;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/asJava/lightClasses")
@InnerTestClasses({KotlinLightClassTestGenerated.NullabilityAnnotations.class})
public class KotlinLightClassTestGenerated extends AbstractKotlinLightClassTest {
public void testAllFilesPresentInLightClasses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/asJava/lightClasses"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/asJava/lightClasses/nullabilityAnnotations")
public static class NullabilityAnnotations extends AbstractKotlinLightClassTest {
public void testAllFilesPresentInNullabilityAnnotations() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/asJava/lightClasses/nullabilityAnnotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Class.kt")
public void testClass() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Class.kt");
}
@TestMetadata("ClassObjectField.kt")
public void testClassObjectField() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassObjectField.kt");
}
@TestMetadata("ClassWithConstructor.kt")
public void testClassWithConstructor() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassWithConstructor.kt");
}
@TestMetadata("ClassWithConstructorAndProperties.kt")
public void testClassWithConstructorAndProperties() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/ClassWithConstructorAndProperties.kt");
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Generic.kt");
}
@TestMetadata("Primitives.kt")
public void testPrimitives() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Primitives.kt");
}
@TestMetadata("PrivateInClass.kt")
public void testPrivateInClass() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/PrivateInClass.kt");
}
@TestMetadata("PrivateInTrait.kt")
public void testPrivateInTrait() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/PrivateInTrait.kt");
}
@TestMetadata("Synthetic.kt")
public void testSynthetic() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Synthetic.kt");
}
@TestMetadata("Trait.kt")
public void testTrait() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/Trait.kt");
}
@TestMetadata("TraitClassObjectField.kt")
public void testTraitClassObjectField() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/TraitClassObjectField.kt");
}
@TestMetadata("_DefaultPackage.kt")
public void test_DefaultPackage() throws Exception {
doTest("compiler/testData/asJava/lightClasses/nullabilityAnnotations/_DefaultPackage.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("KotlinLightClassTestGenerated");
suite.addTestSuite(KotlinLightClassTestGenerated.class);
suite.addTestSuite(NullabilityAnnotations.class);
return suite;
}
}
@@ -1,110 +0,0 @@
/*
* Copyright 2010-2013 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.jet.asJava;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.compiled.ClsElementImpl;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
import java.io.File;
import java.util.Collections;
import java.util.List;
public class NullabilityAnnotationsTest extends KotlinAsJavaTestBase {
private final File testDir = new File("compiler/testData/asJava/nullabilityAnnotations");
@Override
protected List<File> getKotlinSourceRoots() {
return Collections.singletonList(new File(testDir, getTestName(false) + ".kt"));
}
@Override
protected void extraConfiguration(@NotNull CompilerConfiguration configuration) {
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.getAnnotationsJar());
configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, JetTestUtils.findMockJdkRtJar());
}
public void test_DefaultPackage() throws Exception {
doTest(getTestName(false));
}
public void testClass() throws Exception {
doTest(getTestName(false));
}
public void testTrait() throws Exception {
doTest(getTestName(false));
}
public void testClassWithConstructorAndProperties() throws Exception {
doTest(getTestName(false));
}
public void testClassWithConstructor() throws Exception {
doTest(getTestName(false));
}
public void testSynthetic() throws Exception {
doTest(getTestName(false));
}
public void testPrimitives() throws Exception {
doTest(getTestName(false));
}
public void testPrivateInClass() throws Exception {
doTest(getTestName(false));
}
public void testPrivateInTrait() throws Exception {
doTest(getTestName(false));
}
public void testClassObjectField() throws Exception {
doTest(getTestName(false));
}
public void testTraitClassObjectField() throws Exception {
doTest(getTestName(false));
}
public void testGeneric() throws Exception {
doTest(getTestName(false));
}
private void doTest(@NotNull String fqName) {
PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(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(new File(testDir, fqName + ".java"), actual);
}
}
@@ -114,6 +114,7 @@ import org.jetbrains.jet.cfg.AbstractPseudoValueTest
import org.jetbrains.jet.plugin.structureView.AbstractKotlinFileStructureTest
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterTest
import org.jetbrains.jet.jps.build.AbstractIncrementalJpsTest
import org.jetbrains.jet.asJava.AbstractKotlinLightClassTest
fun main(args: Array<String>) {
System.setProperty("java.awt.headless", "true")
@@ -263,6 +264,10 @@ fun main(args: Array<String>) {
model("evaluate/isPure", testMethod = "doIsPureTest")
model("evaluate/usesVariableAsConstant", testMethod = "doUsesVariableAsConstantTest")
}
testClass(javaClass<AbstractKotlinLightClassTest>()) {
model("asJava/lightClasses")
}
}
testGroup("idea/tests", "idea/testData") {