[Spec tests] Add tests for if-expression

This commit is contained in:
anastasiia.spaseeva
2019-12-10 18:39:02 +03:00
parent a41c4c84a6
commit 744cc54dff
8 changed files with 398 additions and 3 deletions
@@ -0,0 +1,24 @@
{
"1": {
"neg": {
"1": [
{
"specVersion": "0.1-213",
"casesNumber": 2,
"description": "* HELPERS:",
"unexpectedBehaviour": false
}
]
},
"pos": {
"1": [
{
"specVersion": "0.1-213",
"casesNumber": 3,
"description": "* HELPERS: checkType",
"unexpectedBehaviour": false
}
]
}
}
}
@@ -0,0 +1,30 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-213
* PLACE: control--and-data-flow-analysis, control-flow-graph, expressions-1, conditional-expressions -> paragraph 1 -> sentence 1
* NUMBER: 1
* DESCRIPTION: check if-expressions must have both branches.
* HELPERS:
*/
// TESTCASE NUMBER: 1
fun case1() {
val b = true
val a = <!INVALID_IF_AS_EXPRESSION!>if<!> (b) {
"true"
}
}
// TESTCASE NUMBER: 2
fun case2() {
val b = true
val a = <!INVALID_IF_AS_EXPRESSION!>if<!> (b) "true"
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-213
* PLACE: control--and-data-flow-analysis, control-flow-graph, expressions-1, conditional-expressions -> paragraph 1 -> sentence 2
* NUMBER: 1
* DESCRIPTION: check any if-statement in kotlin may be trivially turned into such an expression by replacing the missing branch with a kotlin.Unit object expression.
* HELPERS: checkType
*/
// TESTCASE NUMBER: 1
fun case1() {
val b = true
if (!b) {
println("this is statement")
}
val statement = <!INVALID_IF_AS_EXPRESSION!>if<!> (!b) { println("statement could not be assigned") }
}
// TESTCASE NUMBER: 2
fun case2() {
val a = 1
val b = 2
if (a > b) a else ; //statement
val expression: Any = <!INVALID_IF_AS_EXPRESSION!>if<!> (a > b) a else ;
}
@@ -0,0 +1,63 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION -DEBUG_INFO_SMARTCAST
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-213
* PLACE: control--and-data-flow-analysis, control-flow-graph, expressions-1, conditional-expressions -> paragraph 1 -> sentence 1
* NUMBER: 1
* DESCRIPTION: check if-expressions must have both branches.
* HELPERS: checkType
*/
// TESTCASE NUMBER: 1
fun case1() {
val b = true
val a = if (b) {
"true"
} else {
"false"
}
a checkType { check<String>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>a<!>
}
// TESTCASE NUMBER: 2
fun case2() {
val b = true
val a = if (b) "true" else "false"
a checkType { check<String>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>a<!>
}
// TESTCASE NUMBER: 3
fun case3() {
val b = true
//subcase 1 (ELSE BRANCH)
val case_3_1 = if (!b) {
"true"
} else boo()
case_3_1 checkType { check<Any>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>case_3_1<!>
case_3_1 as kotlin.Unit
case_3_1 checkType { check<Unit>() }
//subcase 2 (IF TRUE BRANCH)
val case_3_2 = if (b) {
"true"
} else boo()
case_3_2 checkType { check<Any>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>case_3_2<!>
case_3_2 as kotlin.String
case_3_2 checkType { check<String>() }
}
fun boo(): Any {
return kotlin.Unit
}
@@ -0,0 +1,43 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE -UNUSED_PARAMETER -UNUSED_EXPRESSION -DEBUG_INFO_SMARTCAST
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-213
* PLACE: control--and-data-flow-analysis, control-flow-graph, expressions-1, conditional-expressions -> paragraph 1 -> sentence 2
* NUMBER: 1
* DESCRIPTION: check any if-statement in kotlin may be trivially turned into such an expression by replacing the missing branch with a kotlin.Unit object expression.
* HELPERS: checkType, functions
*/
// TESTCASE NUMBER: 1
fun case1() {
val b = true
if (!b) { 123 } //statement
val expression: Any = if (!b) { } else kotlin.Unit
expression checkType { check<Any>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>expression<!>
expression as kotlin.Unit
expression checkType { check<Unit>() }
}
// TESTCASE NUMBER: 2
fun case2() {
val a = 1
val b = 2
if (a > b) { a } //statement
val expression: Any = if (a > b) { a } else { }
expression checkType { check<Any>() }
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>expression<!>
expression as kotlin.Unit
expression checkType { check<Unit>() }
}
@@ -0,0 +1,92 @@
{
"1": {
"neg": {
"2": [
{
"specVersion": "0.1-213",
"casesNumber": 2,
"description": "* HELPERS: checkType",
"unexpectedBehaviour": false
}
],
"1": [
{
"specVersion": "0.1-213",
"casesNumber": 2,
"description": "* HELPERS:",
"unexpectedBehaviour": false
}
]
},
"pos": {
"2": [
{
"specVersion": "0.1-213",
"casesNumber": 2,
"description": "* HELPERS: checkType, functions",
"unexpectedBehaviour": false
}
],
"1": [
{
"specVersion": "0.1-213",
"casesNumber": 3,
"description": "* HELPERS: checkType, functions",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "Exhaustive with nullability check before",
"path": "compiler/testData/diagnostics/tests/when/ExhaustiveWithNullabilityCheckBefore.kt",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "Exhaustive with nullability check else",
"path": "compiler/testData/diagnostics/tests/when/ExhaustiveWithNullabilityCheckElse.kt",
"unexpectedBehaviour": false
}
]
}
},
"2": {
"neg": {
"1": [
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "When type disjunctions",
"path": "compiler/testData/diagnostics/tests/when/WhenTypeDisjunctions.kt",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "Exhaustive break continue",
"path": "compiler/testData/diagnostics/tests/when/ExhaustiveBreakContinue.kt",
"unexpectedBehaviour": false
}
]
},
"pos": {
"1": [
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "Exhaustive return throw",
"path": "compiler/testData/diagnostics/tests/when/ExhaustiveReturnThrow.kt",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-152",
"casesNumber": 0,
"description": "Exhaustive return",
"path": "compiler/testData/diagnostics/tests/when/ExhaustiveReturn.kt",
"unexpectedBehaviour": false
}
]
}
}
}
@@ -8,7 +8,7 @@
* SPEC VERSION: 0.1-213 * SPEC VERSION: 0.1-213
* PLACE: expressions, prefix-expressions, prefix-increment-expression -> paragraph 4 -> sentence 1 * PLACE: expressions, prefix-expressions, prefix-increment-expression -> paragraph 4 -> sentence 1
* RELEVANT PLACES: statements, assignments -> paragraph 3 -> sentence 2 * RELEVANT PLACES: statements, assignments -> paragraph 3 -> sentence 2
* NUMBER:1 * NUMBER: 1
* DESCRIPTION: check unsafe prefix increment expression call for an assignable expression * DESCRIPTION: check unsafe prefix increment expression call for an assignable expression
*/ */
class A() { class A() {
@@ -25,7 +25,7 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec {
} }
public void testAllFilesPresentInDiagnostics() throws Exception { public void testAllFilesPresentInDiagnostics() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics"), Pattern.compile("^(.+)\\.kt$"), true, "helpers", "linked/type-inference", "linked/type-system/type-kinds/type-parameters", "linked/type-system/subtyping", "linked/declarations/classifier-declaration/classifier-initialization", "linked/declarations/function-declaration", "linked/annotations", "linked/statements", "linked/inheritance", "linked/expressions/function-literals", "linked/expressions/conditional-expression", "linked/expressions/built-in-types-and-their-semantics/kotlin.nothing-1/p-1/neg", "linked/overload-resolution", "linked/control--and-data-flow-analysis"); KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics"), Pattern.compile("^(.+)\\.kt$"), true, "helpers", "linked/type-inference", "linked/type-system/type-kinds/type-parameters", "linked/type-system/subtyping", "linked/control--and-data-flow-analysis.control-flow-graph.expressions-1.conditional-expressions", "linked/declarations/classifier-declaration/classifier-initialization", "linked/declarations/function-declaration", "linked/annotations", "linked/statements", "linked/inheritance", "linked/expressions/function-literals", "linked/expressions/conditional-expression", "linked/expressions/built-in-types-and-their-semantics/kotlin.nothing-1/p-1/neg", "linked/overload-resolution", "linked/control--and-data-flow-analysis/performing-analysis-on-the-control-flow-graph");
} }
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked") @TestMetadata("compiler/tests-spec/testData/diagnostics/linked")
@@ -37,7 +37,118 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec {
} }
public void testAllFilesPresentInLinked() throws Exception { public void testAllFilesPresentInLinked() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked"), Pattern.compile("^(.+)\\.kt$"), true, "type-inference", "type-system/type-kinds/type-parameters", "type-system/subtyping", "declarations/classifier-declaration/classifier-initialization", "declarations/function-declaration", "annotations", "statements", "inheritance", "expressions/function-literals", "expressions/conditional-expression", "expressions/built-in-types-and-their-semantics/kotlin.nothing-1/p-1/neg", "overload-resolution", "control--and-data-flow-analysis"); KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked"), Pattern.compile("^(.+)\\.kt$"), true, "type-inference", "type-system/type-kinds/type-parameters", "type-system/subtyping", "control--and-data-flow-analysis.control-flow-graph.expressions-1.conditional-expressions", "declarations/classifier-declaration/classifier-initialization", "declarations/function-declaration", "annotations", "statements", "inheritance", "expressions/function-literals", "expressions/conditional-expression", "expressions/built-in-types-and-their-semantics/kotlin.nothing-1/p-1/neg", "overload-resolution", "control--and-data-flow-analysis/performing-analysis-on-the-control-flow-graph");
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Control__and_data_flow_analysis extends AbstractDiagnosticsTestSpec {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInControl__and_data_flow_analysis() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis"), Pattern.compile("^(.+)\\.kt$"), true, "performing-analysis-on-the-control-flow-graph");
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Control_flow_graph extends AbstractDiagnosticsTestSpec {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInControl_flow_graph() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Expressions_1 extends AbstractDiagnosticsTestSpec {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInExpressions_1() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Conditional_expressions extends AbstractDiagnosticsTestSpec {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInConditional_expressions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class P_1 extends AbstractDiagnosticsTestSpec {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInP_1() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/neg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Neg 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/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/neg/1.1.kt");
}
@TestMetadata("2.1.kt")
public void test2_1() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/neg/2.1.kt");
}
public void testAllFilesPresentInNeg() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/neg"), Pattern.compile("^(.+)\\.kt$"), true);
}
}
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/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/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/pos/1.1.kt");
}
@TestMetadata("2.1.kt")
public void test2_1() throws Exception {
runTest("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/pos/2.1.kt");
}
public void testAllFilesPresentInPos() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/linked/control--and-data-flow-analysis/control-flow-graph/expressions-1/conditional-expressions/p-1/pos"), Pattern.compile("^(.+)\\.kt$"), true);
}
}
}
}
}
}
} }
@TestMetadata("compiler/tests-spec/testData/diagnostics/linked/declarations") @TestMetadata("compiler/tests-spec/testData/diagnostics/linked/declarations")