diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 9a9bbedcb4a..3f1038df9a2 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -973,8 +973,9 @@ fun main(args: Array) { } } - testClass { - model("newFileOrElement", extension = "java") + testGroup("nj2k/tests", "nj2k/testData/") { + testClass { + model("fileOrElement") } } diff --git a/nj2k/testData/fileOrElement/compareWithNull.kt b/nj2k/testData/fileOrElement/compareWithNull.kt new file mode 100644 index 00000000000..fbc379d45e7 --- /dev/null +++ b/nj2k/testData/fileOrElement/compareWithNull.kt @@ -0,0 +1,13 @@ +fun a(): Int { + return 42 +} + +val b: Int = 2 + +fun c(p: Int) { + if (p == null); +} + +fun check() { + if (a() == null || b == null); +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/compareWithNull.kt.after b/nj2k/testData/fileOrElement/compareWithNull.kt.after new file mode 100644 index 00000000000..d1c8c886a8e --- /dev/null +++ b/nj2k/testData/fileOrElement/compareWithNull.kt.after @@ -0,0 +1,13 @@ +fun a(): Int? { + return 42 +} + +val b: Int? = 2 + +fun c(p: Int?) { + if (p == null); +} + +fun check() { + if (a() == null || b == null); +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt b/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt new file mode 100644 index 00000000000..d08e635c7ec --- /dev/null +++ b/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt @@ -0,0 +1,6 @@ +fun foo (x: T, y: List, z: S) {} + +fun bar() { + val lst: List = listOf(null) + foo(null, lst, "nya") +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt.after b/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt.after new file mode 100644 index 00000000000..8ff8ad5af68 --- /dev/null +++ b/nj2k/testData/fileOrElement/functionTypeParameterNullability.kt.after @@ -0,0 +1,6 @@ +fun foo (x: T?, y: List, z: S) {} + +fun bar() { + val lst: List = listOf(null) + foo(null, lst, "nya") +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/nullAsAssignment.kt b/nj2k/testData/fileOrElement/nullAsAssignment.kt new file mode 100644 index 00000000000..0a067e7faa2 --- /dev/null +++ b/nj2k/testData/fileOrElement/nullAsAssignment.kt @@ -0,0 +1,11 @@ +fun test() { + var x: Int = 1 + x = null + + var y: Int = 5 + y = nullableFun() +} + +fun nullableFun(): Int { + return null +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/nullAsAssignment.kt.after b/nj2k/testData/fileOrElement/nullAsAssignment.kt.after new file mode 100644 index 00000000000..8f118b7dd24 --- /dev/null +++ b/nj2k/testData/fileOrElement/nullAsAssignment.kt.after @@ -0,0 +1,11 @@ +fun test() { + var x: Int? = 1 + x = null + + var y: Int? = 5 + y = nullableFun() +} + +fun nullableFun(): Int? { + return null +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/nullAsInitializer.kt b/nj2k/testData/fileOrElement/nullAsInitializer.kt new file mode 100644 index 00000000000..bf51fd3c3f1 --- /dev/null +++ b/nj2k/testData/fileOrElement/nullAsInitializer.kt @@ -0,0 +1,2 @@ +val x: Int = null +fun foo(p: Int = null) {} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/nullAsInitializer.kt.after b/nj2k/testData/fileOrElement/nullAsInitializer.kt.after new file mode 100644 index 00000000000..41fc7bb48c4 --- /dev/null +++ b/nj2k/testData/fileOrElement/nullAsInitializer.kt.after @@ -0,0 +1,2 @@ +val x: Int? = null +fun foo(p: Int? = null) {} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/smartCast.kt b/nj2k/testData/fileOrElement/smartCast.kt new file mode 100644 index 00000000000..559192a9fb8 --- /dev/null +++ b/nj2k/testData/fileOrElement/smartCast.kt @@ -0,0 +1,4 @@ +fun foo(o: Int) { + if (o == null) return + val a: Int = o +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/smartCast.kt.after b/nj2k/testData/fileOrElement/smartCast.kt.after new file mode 100644 index 00000000000..039d05d44e6 --- /dev/null +++ b/nj2k/testData/fileOrElement/smartCast.kt.after @@ -0,0 +1,4 @@ +fun foo(o: Int?) { + if (o == null) return + val a: Int = o +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/superMethod.kt b/nj2k/testData/fileOrElement/superMethod.kt new file mode 100644 index 00000000000..06a7226ce21 --- /dev/null +++ b/nj2k/testData/fileOrElement/superMethod.kt @@ -0,0 +1,12 @@ +open class A () { + open fun foo(x: Int): Int { + if (x == null); + return null + } +} + +class B : A() { + override fun foo(x: Int): Int { + return 1 + } +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/superMethod.kt.after b/nj2k/testData/fileOrElement/superMethod.kt.after new file mode 100644 index 00000000000..7259026f01e --- /dev/null +++ b/nj2k/testData/fileOrElement/superMethod.kt.after @@ -0,0 +1,12 @@ +open class A () { + open fun foo(x: Int?): Int? { + if (x == null); + return null + } +} + +class B : A() { + override fun foo(x: Int?): Int? { + return 1 + } +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/typeCast.kt b/nj2k/testData/fileOrElement/typeCast.kt new file mode 100644 index 00000000000..0b48e0ad566 --- /dev/null +++ b/nj2k/testData/fileOrElement/typeCast.kt @@ -0,0 +1,7 @@ +fun foo() { + val cast1: Int = 1 as Int + val cast2: Int = null as Int + val cast3: Float = (1 as Int).toFloat() + val nya: Int = null + val cast4: Int = nya as Int +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/typeCast.kt.after b/nj2k/testData/fileOrElement/typeCast.kt.after new file mode 100644 index 00000000000..6ee36f0edef --- /dev/null +++ b/nj2k/testData/fileOrElement/typeCast.kt.after @@ -0,0 +1,7 @@ +fun foo() { + val cast1: Int = 1 as Int + val cast2: Int? = null as Int? + val cast3: Float = (1 as Int).toFloat() + val nya: Int? = null + val cast4: Int? = nya as Int? +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/typeParameters.kt b/nj2k/testData/fileOrElement/typeParameters.kt new file mode 100644 index 00000000000..25d179aad98 --- /dev/null +++ b/nj2k/testData/fileOrElement/typeParameters.kt @@ -0,0 +1,13 @@ +fun foo() { + val l: List = List(1, {1}) + val a: Array = arrayOfNulls(42) + val b: Array = arrayOf(1,2,3) + val c: Array> = arrayOf>(arrayOfNulls(42)) + val d: Array> = arrayOf>(arrayOf(42)) + val e: Array> = arrayOf>(arrayOf(42, null)) + val f: Array> = arrayOf>(arrayOf(42, null), null) + val g: Array> = arrayOf>(arrayOf(42), null) + val h: Array = arrayOfNulls(5) + val i: Array> = Array>(5, { arrayOfNulls(5) }) + val k: Array = arrayOf(null) +} \ No newline at end of file diff --git a/nj2k/testData/fileOrElement/typeParameters.kt.after b/nj2k/testData/fileOrElement/typeParameters.kt.after new file mode 100644 index 00000000000..fdb5da98553 --- /dev/null +++ b/nj2k/testData/fileOrElement/typeParameters.kt.after @@ -0,0 +1,13 @@ +fun foo() { + val l: List = List(1, {1}) + val a: Array = arrayOfNulls(42) + val b: Array = arrayOf(1,2,3) + val c: Array> = arrayOf>(arrayOfNulls(42)) + val d: Array> = arrayOf>(arrayOf(42)) + val e: Array> = arrayOf>(arrayOf(42, null)) + val f: Array?> = arrayOf?>(arrayOf(42, null), null) + val g: Array?> = arrayOf?>(arrayOf(42), null) + val h: Array = arrayOfNulls(5) + val i: Array> = Array>(5, { arrayOfNulls(5) }) + val k: Array = arrayOf(null) +} \ No newline at end of file diff --git a/nj2k/tests/org/jetbrains/kotlin/nj2k/AbstractNullabilityAnalysisTest.kt b/nj2k/tests/org/jetbrains/kotlin/nj2k/AbstractNullabilityAnalysisTest.kt new file mode 100644 index 00000000000..33c993baf4b --- /dev/null +++ b/nj2k/tests/org/jetbrains/kotlin/nj2k/AbstractNullabilityAnalysisTest.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.nj2k + +import com.intellij.openapi.util.io.FileUtil +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.nj2k.nullabilityAnalysis.AnalysisScope +import org.jetbrains.kotlin.nj2k.nullabilityAnalysis.Nullability +import org.jetbrains.kotlin.nj2k.nullabilityAnalysis.NullabilityAnalysisFacade +import org.jetbrains.kotlin.nj2k.nullabilityAnalysis.preapareTypeElementByMakingAllTypesNullable +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.test.KotlinTestUtils +import java.io.File + +abstract class AbstractNullabilityAnalysisTest : KotlinLightCodeInsightFixtureTestCase() { + + fun doTest(path: String) { + val file = File(path) + val text = FileUtil.loadFile(file, true) + val ktFile = myFixture.configureByText("converterTestFile.kt", text) as KtFile + NullabilityAnalysisFacade( + getTypeElementNullability = { Nullability.UNKNOWN }, + prepareTypeElement = ::preapareTypeElementByMakingAllTypesNullable, + debugPrint = false + ) + .fixNullability(AnalysisScope(ktFile)) + val expectedFile = File(path.replace(".kt", ".kt.after")) + KotlinTestUtils.assertEqualsToFile(expectedFile, ktFile.text) + } + + override fun getProjectDescriptor() = + KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE +} \ No newline at end of file diff --git a/nj2k/tests/org/jetbrains/kotlin/nj2k/NullabilityAnalysisTestGenerated.java b/nj2k/tests/org/jetbrains/kotlin/nj2k/NullabilityAnalysisTestGenerated.java new file mode 100644 index 00000000000..afab2337384 --- /dev/null +++ b/nj2k/tests/org/jetbrains/kotlin/nj2k/NullabilityAnalysisTestGenerated.java @@ -0,0 +1,71 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.nj2k; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TargetBackend; +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("nj2k/testData/fileOrElement") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class NullabilityAnalysisTestGenerated extends AbstractNullabilityAnalysisTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInFileOrElement() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("nj2k/testData/fileOrElement"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compareWithNull.kt") + public void testCompareWithNull() throws Exception { + runTest("nj2k/testData/fileOrElement/compareWithNull.kt"); + } + + @TestMetadata("functionTypeParameterNullability.kt") + public void testFunctionTypeParameterNullability() throws Exception { + runTest("nj2k/testData/fileOrElement/functionTypeParameterNullability.kt"); + } + + @TestMetadata("nullAsAssignment.kt") + public void testNullAsAssignment() throws Exception { + runTest("nj2k/testData/fileOrElement/nullAsAssignment.kt"); + } + + @TestMetadata("nullAsInitializer.kt") + public void testNullAsInitializer() throws Exception { + runTest("nj2k/testData/fileOrElement/nullAsInitializer.kt"); + } + + @TestMetadata("smartCast.kt") + public void testSmartCast() throws Exception { + runTest("nj2k/testData/fileOrElement/smartCast.kt"); + } + + @TestMetadata("superMethod.kt") + public void testSuperMethod() throws Exception { + runTest("nj2k/testData/fileOrElement/superMethod.kt"); + } + + @TestMetadata("typeCast.kt") + public void testTypeCast() throws Exception { + runTest("nj2k/testData/fileOrElement/typeCast.kt"); + } + + @TestMetadata("typeParameters.kt") + public void testTypeParameters() throws Exception { + runTest("nj2k/testData/fileOrElement/typeParameters.kt"); + } +}