-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for CastNullableToNonNullableType rule
- Loading branch information
Showing
1 changed file
with
135 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
...c/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastNullableToNonNullableTypeSpec.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,135 @@ | ||
package io.gitlab.arturbosch.detekt.rules.bugs | ||
|
||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest | ||
import io.gitlab.arturbosch.detekt.test.assertThat | ||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext | ||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment | ||
import org.junit.jupiter.api.Test | ||
|
||
@KotlinCoreEnvironmentTest | ||
class CastNullableToNonNullableTypeSpec(private val env: KotlinCoreEnvironment) { | ||
private val subject = CastNullableToNonNullableType() | ||
|
||
@Test | ||
fun `reports casting Nullable type to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = bar as String | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).hasSize(1) | ||
assertThat(findings).hasStartSourceLocation(2, 17) | ||
assertThat(findings[0]).hasMessage( | ||
"Use separate `null` assertion and type cast like " + | ||
"('(bar ?: error(\"null assertion message\")) as String') instead of 'bar as String'." | ||
) | ||
} | ||
|
||
@Test | ||
fun `reports casting Nullable value returned from a function call to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
bar() as Int | ||
} | ||
fun bar(): Int? { | ||
return null | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).hasSize(1) | ||
assertThat(findings).hasStartSourceLocation(2, 11) | ||
assertThat(findings[0]).hasMessage( | ||
"Use separate `null` assertion and type cast like " + | ||
"('(bar() ?: error(\"null assertion message\")) as Int') instead of 'bar() as Int'." | ||
) | ||
} | ||
|
||
@Test | ||
fun `reports casting of platform type to NonNullable type`() { | ||
val code = """ | ||
class Foo { | ||
fun test() { | ||
// getSimpleName() is not annotated with nullability information in the JDK, so compiler treats | ||
// it as a platform type with unknown nullability. | ||
val y = javaClass.simpleName as String | ||
} | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).hasSize(1) | ||
assertThat(findings).hasStartSourceLocation(5, 38) | ||
assertThat(findings[0]).hasMessage( | ||
"Use separate `null` assertion and type cast like " + | ||
"('(javaClass.simpleName ?: error(\"null assertion message\")) as String') instead of " + | ||
"'javaClass.simpleName as String'." | ||
) | ||
} | ||
|
||
@Test | ||
fun `does not report casting of Nullable type to NonNullable expression with assertion to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = (bar ?: error("null not expected")) as String | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `does not report casting of Nullable type to NonNullable expression with !! assertion to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = bar!! as String | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `does not report casting of Nullable type to NonNullable smart casted variable to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = bar?.let { bar as String } | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `does not report casting of NonNullable type to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = bar as String? | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `does not report safe casting of Nullable type to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = bar as? String | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `does not report as compile error will happen when null to NonNullable type`() { | ||
val code = """ | ||
fun foo(bar: Any?) { | ||
val x = null as String | ||
} | ||
""".trimIndent() | ||
val findings = subject.compileAndLintWithContext(env, code) | ||
assertThat(findings).isEmpty() | ||
} | ||
} |