Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68ed1a4
commit 0fea783
Showing
1 changed file
with
14 additions
and
104 deletions.
There are no files selected for viewing
118 changes: 14 additions & 104 deletions
118
detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/internal/SignaturesSpec.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 |
---|---|---|
@@ -1,128 +1,38 @@ | ||
package io.gitlab.arturbosch.detekt.api.internal | ||
|
||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import io.github.detekt.test.utils.compileContentForTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.junit.jupiter.api.Test | ||
|
||
class SignaturesSpec { | ||
private var result = "" | ||
private val subject = TestRule { result = it } | ||
|
||
@Test | ||
fun `function with type reference`() { | ||
subject.compileAndLint(functionWithTypeReference()) | ||
assertThat(result).isEqualTo(signatureWithTypeReference()) | ||
val result = compileContentForTest("fun data(): Int = 0") | ||
.findChildByClass(KtNamedFunction::class.java)!! | ||
.buildFullSignature() | ||
|
||
assertThat(result).isEqualTo("Test.kt\$fun data(): Int") | ||
} | ||
|
||
@Test | ||
fun `function without type reference`() { | ||
subject.compileAndLint(functionWithoutTypeReference()) | ||
assertThat(result).isEqualTo(signatureWithoutTypeReference()) | ||
val result = compileContentForTest("fun data() = 0") | ||
.findChildByClass(KtNamedFunction::class.java)!! | ||
.buildFullSignature() | ||
|
||
assertThat(result).isEqualTo("Test.kt\$fun data()") | ||
} | ||
|
||
@Test | ||
fun `function throws exception`() { | ||
assertThatThrownBy { | ||
subject.compileAndLint(functionWontCompile()) | ||
compileContentForTest("{ fun data() = 0 }") | ||
.findChildByClass(KtNamedFunction::class.java)!! | ||
.buildFullSignature() | ||
} | ||
.isInstanceOf(IllegalArgumentException::class.java) | ||
.hasMessageContaining("Error building function signature") | ||
} | ||
} | ||
|
||
private class TestRule(val block: (String) -> Unit) : Rule() { | ||
override val issue = Issue(javaClass.simpleName, Severity.CodeSmell, "", Debt.TWENTY_MINS) | ||
|
||
override fun visitNamedFunction(function: KtNamedFunction) { | ||
block(function.buildFullSignature()) | ||
} | ||
} | ||
|
||
private fun signatureWithTypeReference() = """ | ||
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data(): List<Array<out Any?>> | ||
""".trimIndent() | ||
|
||
private fun signatureWithoutTypeReference() = """ | ||
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data() | ||
""".trimIndent() | ||
|
||
private fun functionWithTypeReference() = """ | ||
@RunWith(Parameterized::class) | ||
class TestClass { | ||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "parameterName") | ||
/** | ||
* Function KDoc | ||
*/ | ||
fun data(): List<Array<out Any?>> = | ||
/** | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
*/ | ||
listOf( | ||
arrayOf(1, 2, 3, 4), | ||
arrayOf(11, 22, 33, 44) | ||
) | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
private fun functionWithoutTypeReference() = """ | ||
@RunWith(Parameterized::class) | ||
class TestClass { | ||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "parameterName") | ||
/** | ||
* Function KDoc | ||
*/ | ||
fun data() = | ||
/** | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
*/ | ||
listOf( | ||
arrayOf(1, 2, 3, 4), | ||
arrayOf(11, 22, 33, 44) | ||
) | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
private fun functionWontCompile() = """ | ||
@RunWith(Parameterized.class) | ||
class TestClass { | ||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters(name = "parameterName") | ||
/** | ||
* Function KDoc | ||
*/ | ||
fun data() = | ||
/** | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
* more description | ||
*/ | ||
listOf( | ||
arrayOf(1, 2, 3, 4), | ||
arrayOf(11, 22, 33, 44) | ||
) | ||
} | ||
} | ||
""".trimIndent() |