From 0a1b8a928c3f9710dd49794df888c2a909d2f30f Mon Sep 17 00:00:00 2001 From: "anastasiia.spaseeva" Date: Mon, 23 Dec 2019 18:41:33 +0300 Subject: [PATCH] [Spec tests] Add tests for not-null-assertion-expression --- .../p-2/pos/2.1.kt | 36 +++++++++ .../p-2/pos/2.2.kt | 27 +++++++ .../p-2/pos/3.1.kt | 21 ++++++ .../testsMap.json | 44 +++++++++++ .../p-3/pos/1.1.kt | 54 +++++++++++++ .../p-4/pos/1.1.kt | 70 +++++++++++++++++ .../testsMap.json | 26 +++++++ .../DiagnosticsTestSpecGenerated.java | 75 +++++++++++++++++++ .../BlackBoxCodegenTestSpecGenerated.java | 54 +++++++++++++ 9 files changed, 407 insertions(+) create mode 100644 compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt create mode 100644 compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt create mode 100644 compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/3.1.kt create mode 100644 compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/testsMap.json create mode 100644 compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos/1.1.kt create mode 100644 compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos/1.1.kt create mode 100644 compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/testsMap.json diff --git a/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt new file mode 100644 index 00000000000..c093e9415fb --- /dev/null +++ b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt @@ -0,0 +1,36 @@ +// WITH_RUNTIME + +/* + * KOTLIN CODEGEN BOX SPEC TEST (POSITIVE) + * + * SPEC VERSION: 0.1-220 + * PLACE: expressions, not-null-assertion-expression -> paragraph 2 -> sentence 2 + * RELEVANT PLACES: expressions, not-null-assertion-expression -> paragraph 2 -> sentence 1 + * NUMBER: 1 + * DESCRIPTION: For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception. + */ + + +// MODULE: libModule +// FILE: libModule/JavaClass.java +package libModule; + +public class JavaClass { + public static Boolean FALSE; +} + + +// MODULE: mainModule(libModule) +// FILE: KotlinClass.kt +package mainModule +import libModule.* + + +fun box(): String { + try { + val x = JavaClass.FALSE!! + }catch (e: java.lang.NullPointerException){ + return "OK" + } + return "NOK" +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt new file mode 100644 index 00000000000..a79b384ceb1 --- /dev/null +++ b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt @@ -0,0 +1,27 @@ +// WITH_RUNTIME + +/* + * KOTLIN CODEGEN BOX SPEC TEST (POSITIVE) + * + * SPEC VERSION: 0.1-220 + * PLACE: expressions, not-null-assertion-expression -> paragraph 2 -> sentence 2 + * RELEVANT PLACES: expressions, not-null-assertion-expression -> paragraph 2 -> sentence 1 + * NUMBER: 2 + * DESCRIPTION: For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception. + */ + + +fun box(): String { + val x = A(A(A(A()))) + if ( x.a!!.a!!.a!!.a == null) + { + try { + val t = x.a!!.a!!.a!!.a!! + }catch (e: java.lang.NullPointerException){ + return "OK" + } + } + return "NOK" +} + +class A ( val a: A? = null) \ No newline at end of file diff --git a/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/3.1.kt b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/3.1.kt new file mode 100644 index 00000000000..578009c219b --- /dev/null +++ b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/3.1.kt @@ -0,0 +1,21 @@ +// WITH_RUNTIME + +/* + * KOTLIN CODEGEN BOX SPEC TEST (POSITIVE) + * + * SPEC VERSION: 0.1-220 + * PLACE: expressions, not-null-assertion-expression -> paragraph 2 -> sentence 3 + * NUMBER: 1 + * DESCRIPTION: If the evaluation result of e is not equal to null, the result of e!! is the evaluation result of e. + */ + +fun box(): String { + + val x: String? = "str" + try { + val y = x!! + }catch (e: java.lang.NullPointerException){ + return "NOK" + } + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/testsMap.json b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/testsMap.json new file mode 100644 index 00000000000..63b87d0535d --- /dev/null +++ b/compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/testsMap.json @@ -0,0 +1,44 @@ +{ + "2": { + "pos": { + "2": [ + { + "specVersion": "0.1-220", + "casesNumber": 0, + "description": "For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception.", + "unexpectedBehaviour": false + }, + { + "specVersion": "0.1-220", + "casesNumber": 0, + "description": "For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception.", + "unexpectedBehaviour": false + } + ], + "1": [ + { + "specVersion": "0.1-220", + "casesNumber": 0, + "description": "For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception.", + "path": "compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt", + "unexpectedBehaviour": false + }, + { + "specVersion": "0.1-220", + "casesNumber": 0, + "description": "For an expression e!!, if the type of e is nullable, a not-null assertion expression checks, whether the evaluation result of e is equal to null and, if it is, throws a runtime exception.", + "path": "compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt", + "unexpectedBehaviour": false + } + ], + "3": [ + { + "specVersion": "0.1-220", + "casesNumber": 0, + "description": "If the evaluation result of e is not equal to null, the result of e!! is the evaluation result of e.", + "unexpectedBehaviour": false + } + ] + } + } +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos/1.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos/1.1.kt new file mode 100644 index 00000000000..840d9ef922f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos/1.1.kt @@ -0,0 +1,54 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNREACHABLE_CODE -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE) + * + * SPEC VERSION: 0.1-220 + * PLACE: expressions, not-null-assertion-expression -> paragraph 3 -> sentence 1 + * NUMBER: 1 + * DESCRIPTION: If the type of e is non-nullable, not-null assertion expression e!! has no effect. + */ + +// MODULE: libModule +// FILE: libModule/JavaClass.java +package libModule; + +public class JavaClass { + public static final boolean FALSE = false; + public static int obj = 5; +} + + +// MODULE: mainModule(libModule) +// FILE: KotlinClass.kt +package mainModule +import libModule.* + + +// TESTCASE NUMBER: 1 +fun case1() { + val res = JavaClass.FALSE!! +} + +// TESTCASE NUMBER: 2 +fun case2() { + val x = JavaClass.obj!! +} + +// TESTCASE NUMBER: 3 +fun case3() { + val a = false + val x = a!! +} + +// TESTCASE NUMBER: 4 +fun case4() { + val x = "weds"!! +} + +// TESTCASE NUMBER: 5 +fun case5(nothing: Nothing) { + val y = nothing!! +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos/1.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos/1.1.kt new file mode 100644 index 00000000000..7e91778170e --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos/1.1.kt @@ -0,0 +1,70 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNREACHABLE_CODE -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE) + * + * SPEC VERSION: 0.1-220 + * PLACE: expressions, not-null-assertion-expression -> paragraph 4 -> sentence 1 + * NUMBER: 1 + * DESCRIPTION: The type of non-null assertion e!! expression is the non-nullable variant of the type of e. + * HELPERS: checktype + */ + + +// MODULE: libModule +// FILE: libModule/JavaClass.java +package libModule; + +public class JavaClass { + public static final String STR; + public static Object obj; +} + + +// MODULE: mainModule(libModule) +// FILE: KotlinClass.kt +package mainModule +import libModule.* +import checkSubtype + + +// TESTCASE NUMBER: 1 +fun case1() { + val a = JavaClass.STR + a + val res = a!! + res +} + +// TESTCASE NUMBER: 2 +fun case2() { + val a = JavaClass.obj + a + val x = a!! + x +} + +// TESTCASE NUMBER: 3 +fun case3() { + val a : Any? = false + a + val x = a!! + x +} + +// TESTCASE NUMBER: 4 +fun case4() { + val a : String? = "weds" + a + val x = a!! + x +} + +// TESTCASE NUMBER: 5 +fun case5(nothing: Nothing?) { + nothing + val y = nothing!! + y +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/testsMap.json b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/testsMap.json new file mode 100644 index 00000000000..6c6a6016b71 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/testsMap.json @@ -0,0 +1,26 @@ +{ + "4": { + "pos": { + "1": [ + { + "specVersion": "0.1-220", + "casesNumber": 5, + "description": "The type of non-null assertion e!! expression is the non-nullable variant of the type of e.", + "unexpectedBehaviour": false + } + ] + } + }, + "3": { + "pos": { + "1": [ + { + "specVersion": "0.1-220", + "casesNumber": 5, + "description": "If the type of e is non-nullable, not-null assertion expression e!! has no effect.", + "unexpectedBehaviour": false + } + ] + } + } +} \ No newline at end of file diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java index 2918298a9f2..abdf7d51102 100644 --- a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/checkers/DiagnosticsTestSpecGenerated.java @@ -1992,6 +1992,81 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec { } } + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Not_null_assertion_expression extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInNot_null_assertion_expression() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class P_3 extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInP_3() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("1.1.kt") + public void test1_1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos/1.1.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-3/pos"), Pattern.compile("^(.+)\\.kt$"), true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class P_4 extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInP_4() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("1.1.kt") + public void test1_1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos/1.1.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/expressions/not-null-assertion-expression/p-4/pos"), Pattern.compile("^(.+)\\.kt$"), true); + } + } + } + } + @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/expressions/prefix-expressions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/codegen/BlackBoxCodegenTestSpecGenerated.java b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/codegen/BlackBoxCodegenTestSpecGenerated.java index 9b9f71d531c..e30f5fd28c7 100644 --- a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/codegen/BlackBoxCodegenTestSpecGenerated.java +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/codegen/BlackBoxCodegenTestSpecGenerated.java @@ -1481,6 +1481,60 @@ public class BlackBoxCodegenTestSpecGenerated extends AbstractBlackBoxCodegenTes } } + @TestMetadata("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Not_null_assertion_expression extends AbstractBlackBoxCodegenTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInNot_null_assertion_expression() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class P_2 extends AbstractBlackBoxCodegenTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInP_2() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractBlackBoxCodegenTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("2.1.kt") + public void test2_1() throws Exception { + runTest("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.1.kt"); + } + + @TestMetadata("2.2.kt") + public void test2_2() throws Exception { + runTest("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/2.2.kt"); + } + + @TestMetadata("3.1.kt") + public void test3_1() throws Exception { + runTest("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos/3.1.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/codegen/box/linked/expressions/not-null-assertion-expression/p-2/pos"), Pattern.compile("^(.+)\\.kt$"), true); + } + } + } + } + @TestMetadata("compiler/tests-spec/testData/codegen/box/linked/expressions/postfix-operator-expressions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)