New J2K: Add nullability analyser tests
This commit is contained in:
committed by
Ilya Kirillov
parent
f3b6c880d4
commit
bdd773f806
@@ -973,8 +973,9 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
testClass<AbstractNewJavaToKotlinConverterNewSingleFileTest> {
|
testGroup("nj2k/tests", "nj2k/testData/") {
|
||||||
model("newFileOrElement", extension = "java")
|
testClass<AbstractNullabilityAnalysisTest> {
|
||||||
|
model("fileOrElement")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
@@ -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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun <T, E, S> foo (x: T, y: List<E>, z: S) {}
|
||||||
|
|
||||||
|
fun bar() {
|
||||||
|
val lst: List<Int> = listOf<Int>(null)
|
||||||
|
foo<Int, Int, String>(null, lst, "nya")
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun <T, E, S> foo (x: T?, y: List<E?>, z: S) {}
|
||||||
|
|
||||||
|
fun bar() {
|
||||||
|
val lst: List<Int?> = listOf<Int?>(null)
|
||||||
|
foo<Int, Int, String>(null, lst, "nya")
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
fun test() {
|
||||||
|
var x: Int = 1
|
||||||
|
x = null
|
||||||
|
|
||||||
|
var y: Int = 5
|
||||||
|
y = nullableFun()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun nullableFun(): Int {
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun test() {
|
||||||
|
var x: Int? = 1
|
||||||
|
x = null
|
||||||
|
|
||||||
|
var y: Int? = 5
|
||||||
|
y = nullableFun()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun nullableFun(): Int? {
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
val x: Int = null
|
||||||
|
fun foo(p: Int = null) {}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
val x: Int? = null
|
||||||
|
fun foo(p: Int? = null) {}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
fun foo(o: Int) {
|
||||||
|
if (o == null) return
|
||||||
|
val a: Int = o
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun foo(o: Int?) {
|
||||||
|
if (o == null) return
|
||||||
|
val a: Int = o
|
||||||
|
}
|
||||||
+12
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -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
|
||||||
|
}
|
||||||
@@ -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?
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
fun foo() {
|
||||||
|
val l: List<Int> = List<Int>(1, {1})
|
||||||
|
val a: Array<Int> = arrayOfNulls<Int>(42)
|
||||||
|
val b: Array<Int> = arrayOf<Int>(1,2,3)
|
||||||
|
val c: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOfNulls<Int>(42))
|
||||||
|
val d: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOf<Int>(42))
|
||||||
|
val e: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOf<Int>(42, null))
|
||||||
|
val f: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOf<Int>(42, null), null)
|
||||||
|
val g: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOf<Int>(42), null)
|
||||||
|
val h: Array<IntArray> = arrayOfNulls(5)
|
||||||
|
val i: Array<Array<IntArray>> = Array<Array<IntArray>>(5, { arrayOfNulls<IntArray>(5) })
|
||||||
|
val k: Array<Int> = arrayOf<Int>(null)
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
fun foo() {
|
||||||
|
val l: List<Int> = List<Int>(1, {1})
|
||||||
|
val a: Array<Int?> = arrayOfNulls<Int>(42)
|
||||||
|
val b: Array<Int> = arrayOf<Int>(1,2,3)
|
||||||
|
val c: Array<Array<Int?>> = arrayOf<Array<Int?>>(arrayOfNulls<Int>(42))
|
||||||
|
val d: Array<Array<Int>> = arrayOf<Array<Int>>(arrayOf<Int>(42))
|
||||||
|
val e: Array<Array<Int?>> = arrayOf<Array<Int?>>(arrayOf<Int?>(42, null))
|
||||||
|
val f: Array<Array<Int?>?> = arrayOf<Array<Int?>?>(arrayOf<Int?>(42, null), null)
|
||||||
|
val g: Array<Array<Int>?> = arrayOf<Array<Int>?>(arrayOf<Int>(42), null)
|
||||||
|
val h: Array<IntArray?> = arrayOfNulls(5)
|
||||||
|
val i: Array<Array<IntArray>> = Array<Array<IntArray>>(5, { arrayOfNulls<IntArray>(5) })
|
||||||
|
val k: Array<Int?> = arrayOf<Int?>(null)
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user