generated from detekt/detekt-custom-rule-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "NoImplicitFunctionReturnType"
- Loading branch information
1 parent
8e66c3e
commit 3dbb8db
Showing
2 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
src/test/kotlin/com/github/ivy/explicit/rule/NoImplicitFunctionReturnTypeRuleTest.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,57 @@ | ||
package com.github.ivy.explicit.rule | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest | ||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment | ||
import org.junit.jupiter.api.Test | ||
|
||
@KotlinCoreEnvironmentTest | ||
internal class NoImplicitFunctionReturnTypeRuleTest(private val env: KotlinCoreEnvironment) { | ||
|
||
@Test | ||
fun `reports function with implicit return type`() { | ||
val code = """ | ||
fun magicNumber() = 42 | ||
""" | ||
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 1 | ||
val message = findings.first().message | ||
message shouldBe """ | ||
The function 'magicNumber()' should declare an explicit return type. Implicit (missing) return types make the code harder to read and reason about. Changing the implementation of such function is error-prone and can lead to regressions. | ||
""".trimIndent() | ||
} | ||
|
||
@Test | ||
fun `reports class method with implicit return type`() { | ||
val code = """ | ||
class A { | ||
fun a() = "Hello, world!" | ||
} | ||
""" | ||
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 1 | ||
} | ||
|
||
@Test | ||
fun `doesn't report class method with explicit return type`() { | ||
val code = """ | ||
class A { | ||
fun a(): String = "Hello, world!" | ||
} | ||
""" | ||
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 0 | ||
} | ||
|
||
@Test | ||
fun `doesn't report function with explicit return type`() { | ||
val code = """ | ||
fun a(): String = "Hello, world!" | ||
""" | ||
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 0 | ||
} | ||
} |