[Spec tests] Add tests for when-expression (p-3, 4)

This commit is contained in:
anastasiia.spaseeva
2019-12-18 13:54:24 +03:00
parent bd979a12de
commit dcfcc9c7b6
22 changed files with 843 additions and 4 deletions
@@ -0,0 +1 @@
java.lang.IllegalStateException: NO_ELSE_IN_WHEN: 'when' expression must be exhaustive, add necessary 'Val_1', 'Val_2' branches or 'else' branch instead (4,17) in /KotlinClass.kt
@@ -0,0 +1,29 @@
// WITH_RUNTIME
// FULL_JDK
/*
* KOTLIN CODEGEN BOX SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 1
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Enum)
* EXCEPTION: compiletime
*/
// FILE: JavaEnum.java
enum JavaEnum {
Val_1,
Val_2,
Val_3,
}
// FILE: KotlinClass.kt
fun box(): String {
val z = JavaEnum.Val_3
val when3 = when (z) {
JavaEnum.Val_3 -> { "OK" }
}
return when3
}
@@ -0,0 +1 @@
java.lang.IllegalStateException: NO_ELSE_IN_WHEN: 'when' expression must be exhaustive, add necessary 'false' branch or 'else' branch instead (15,17) in /1.2.kt
@@ -0,0 +1,19 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 2
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Boolean)
* EXCEPTION: compiletime
*/
fun box(): String {
val b = true
val when2 = when (b) {
!false -> { "OK" }
}
return when2
}
@@ -0,0 +1 @@
java.lang.IllegalStateException: NO_ELSE_IN_WHEN: 'when' expression must be exhaustive, add necessary 'true' branch or 'else' branch instead (15,17) in /1.3.kt
@@ -0,0 +1,20 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 3
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Boolean)
* EXCEPTION: compiletime
*/
fun box(): String {
val a = false
val when2 = when (a) {
false -> { "OK" }
false -> { "NOK" }
}
return when2
}
@@ -0,0 +1 @@
java.lang.IllegalStateException: NO_ELSE_IN_WHEN: 'when' expression must be exhaustive, add necessary 'is C' branch or 'else' branch instead (22,17) in /1.4.kt
@@ -0,0 +1,28 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 4
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (sealed class)
* EXCEPTION: compiletime
*/
sealed class SClass {
class A : SClass()
class B : SClass()
class C : SClass()
}
fun box(): String {
val x: SClass = SClass.B()
val when1 = when (x){
is SClass.A ->{ "NOK"}
is SClass.B ->{ "OK" }
is SClass.B ->{ "NOK" }
}
return when1
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
// FULL_JDK
/*
* KOTLIN CODEGEN BOX SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 1
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Enum)
*/
// FILE: JavaEnum.java
enum JavaEnum {
Val_1,
Val_2,
Val_3,
}
// FILE: KotlinClass.kt
fun box(): String {
val z = JavaEnum.Val_3
val when3 = when (z) {
JavaEnum.Val_1 -> { "NOK" }
JavaEnum.Val_3 -> { "OK" }
JavaEnum.Val_3 -> { "NOK" }
JavaEnum.Val_2 -> { "NOK" }
}
return when3
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 2
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Boolean)
*/
fun box(): String {
val b = true
val when2 = when (b) {
false -> { "NOK" }
!false -> { "OK" }
!false -> { "NOK" }
}
return when2
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 3
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (Boolean)
*/
fun box(): String {
val a = false
val when2 = when (a) {
true -> { "NOK" }
false -> { "OK" }
false -> { "NOK" }
}
return when2
}
@@ -0,0 +1,28 @@
// WITH_RUNTIME
/*
* KOTLIN CODEGEN BOX SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 4 -> sentence 1
* NUMBER: 4
* DESCRIPTION: it is possible to replace the else condition with an always-true condition (sealed class)
*/
sealed class SClass {
class A : SClass()
class B : SClass()
class C : SClass()
}
fun box(): String {
val x: SClass = SClass.B()
val when1 = when (x){
is SClass.A ->{ "NOK"}
is SClass.B ->{ "OK" }
is SClass.B ->{ "NOK" }
is SClass.C ->{ "NOK" }
}
return when1
}
@@ -0,0 +1 @@
java.lang.IllegalStateException: ELSE_MISPLACED_IN_WHEN: 'else' entry must be the last one in a when-expression (6,9) in /KotlinClass.kt
@@ -0,0 +1,32 @@
// WITH_RUNTIME
// FULL_JDK
/*
* KOTLIN CODEGEN BOX SPEC TEST (NEGATIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 5 -> sentence 2
* NUMBER: 1
* DESCRIPTION: The else condition is a special condition which evaluates to true if none of the branches above it evaluated to true.
* EXCEPTION: compiletime
*/
// FILE: JavaEnum.java
enum JavaEnum {
Val_1,
Val_2,
Val_3,
}
// FILE: KotlinClass.kt
fun box(): String {
val z = JavaEnum.Val_3
val when1 = when (z) {
JavaEnum.Val_1 -> { false }
else -> {true}
JavaEnum.Val_2 -> { false }
}
return "NOK"
}
@@ -0,0 +1,33 @@
// WITH_RUNTIME
// FULL_JDK
/*
* KOTLIN CODEGEN BOX SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-218
* PLACE: expressions, when-expression -> paragraph 5 -> sentence 1
* NUMBER: 1
* DESCRIPTION: The else condition must also be in the last when entry of when expression, otherwise it is a compile-time error.
*/
// FILE: JavaEnum.java
enum JavaEnum {
Val_1,
Val_2,
Val_3,
}
// FILE: KotlinClass.kt
fun box(): String {
val z = JavaEnum.Val_3
val when1 = when (z) {
JavaEnum.Val_1 -> { false }
else -> {true}
}
val when2 = when (z) {
JavaEnum.Val_3 -> { true }
else -> {false}
}
return if (when1 && when2) "OK" else "NOK"
}
@@ -0,0 +1,82 @@
{
"4": {
"neg": {
"1": [
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (Boolean)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (Boolean)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (sealed class)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": "it is possible to replace the else condition with an always-true condition (Enum)",
"unexpectedBehaviour": false
}
]
},
"pos": {
"1": [
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (Boolean)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (Boolean)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": " it is possible to replace the else condition with an always-true condition (sealed class)",
"unexpectedBehaviour": false
},
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": "it is possible to replace the else condition with an always-true condition (Enum)",
"unexpectedBehaviour": false
}
]
}
},
"5": {
"neg": {
"2": [
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": "The else condition is a special condition which evaluates to true if none of the branches above it evaluated to true.",
"unexpectedBehaviour": false
}
]
},
"pos": {
"1": [
{
"specVersion": "0.1-218",
"casesNumber": 0,
"description": "The else condition must also be in the last when entry of when expression, otherwise it is a compile-time error.",
"unexpectedBehaviour": false
}
]
}
}
}