Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyVPinchuk committed Jun 17, 2022
1 parent 68ed1a4 commit 0fea783
Showing 1 changed file with 14 additions and 104 deletions.
@@ -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()

0 comments on commit 0fea783

Please sign in to comment.