-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(release): add some test for 2.0.0
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/test/kotlin/com/compiler/server/KotlinFeatureSince200.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.compiler.server | ||
|
||
import com.compiler.server.base.BaseExecutorTest | ||
import com.compiler.server.base.assertNoErrors | ||
import org.junit.jupiter.api.Test | ||
|
||
class KotlinFeatureSince200 : BaseExecutorTest() { | ||
@Test | ||
fun `smart cast to for variables`() { | ||
run( | ||
// language=kotlin | ||
code = """ | ||
class Cat { | ||
fun purr() { | ||
println("Purr purr") | ||
} | ||
} | ||
fun petAnimal(animal: Any) { | ||
val isCat = animal is Cat | ||
if (isCat) { | ||
// In K2, the compiler can access | ||
// information about isCat, so it knows that | ||
// isCat was smart cast to type Cat. | ||
// Therefore, the purr() function is successfully called. | ||
// In Kotlin 1.9.20, the compiler doesn't know | ||
// about the smart cast so calling the purr() | ||
// function triggers an error. | ||
animal.purr() | ||
} | ||
} | ||
fun main() { | ||
val kitty = Cat() | ||
petAnimal(kitty) | ||
// Purr purr | ||
} | ||
""".trimIndent(), | ||
contains = "Purr purr" | ||
) | ||
} | ||
|
||
@Test | ||
fun `stable replacement of the enum class values function`() { | ||
val result = run( | ||
// language=kotlin | ||
code = """ | ||
fun main() { | ||
var stringInput: String? = null | ||
// stringInput is smart cast to String type | ||
stringInput = "" | ||
try { | ||
// The compiler knows that stringInput isn't null | ||
println(stringInput.length) | ||
// 0 | ||
// The compiler rejects previous smart cast information for | ||
// stringInput. Now stringInput has String? type. | ||
stringInput = null | ||
// Trigger an exception | ||
if (2 > 1) throw Exception() | ||
stringInput = "" | ||
} catch (exception: Exception) { | ||
// In Kotlin %kotlinEapVersion%, the compiler knows stringInput | ||
// can be null so stringInput stays nullable. | ||
println(stringInput?.length) | ||
// null | ||
// In Kotlin 1.9.20, the compiler says that a safe call isn't | ||
// needed, but this is incorrect. | ||
} | ||
} | ||
""".trimIndent(), | ||
contains = "0\nnull" | ||
).assertNoErrors() | ||
} | ||
} | ||
|