Skip to content

Commit

Permalink
Remove delegation related TCs
Browse files Browse the repository at this point in the history
Decouple if checks
  • Loading branch information
atulgpt committed Jan 9, 2023
1 parent 19ee05d commit c6b8fad
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class DataClassContainsFunctions(config: Config = Config.empty) : Rule(config) {
super.visitClass(klass)
}

@Suppress("ReturnCount")
private fun checkFunction(klass: KtClass, function: KtNamedFunction) {
if (function.isOverride()) return

val functionName = function.name
if (functionName != null && (isAllowedConversionFunction(functionName) || checkOperator(function))) return
if (functionName != null && conversionFunctionPrefix.any { functionName.startsWith(it) }) return
if (allowOperators && function.isOperator()) return

report(
CodeSmell(
Expand All @@ -67,12 +69,4 @@ class DataClassContainsFunctions(config: Config = Config.empty) : Rule(config) {
)
)
}

private fun checkOperator(function: KtNamedFunction): Boolean {
return allowOperators && function.isOperator()
}

private fun isAllowedConversionFunction(functionName: String): Boolean {
return conversionFunctionPrefix.any { functionName.startsWith(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,6 @@ class DataClassContainsFunctionsSpec {
val rule = DataClassContainsFunctions(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Nested
inner class `operators in data class via class delegation` {
val code = """
interface VerticalLine {
operator fun get(index: Int): Float
}
class VerticalLineImpl(private val xValue: Float) : VerticalLine {
override fun get(index: Int): Float {
return xValue
}
}
data class VectorOnYAxis(val x: Float) : VerticalLine by VerticalLineImpl(0F)
""".trimIndent()

@Test
fun `does not report delegated operators if not allowed by default`() {
val rule = DataClassContainsFunctions()
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Test
fun `does not report delegated operators if not allowed`() {
val config = TestConfig(mapOf(ALLOW_OPERATORS to false))
val rule = DataClassContainsFunctions(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Test
fun `does not report delegated operators if allowed`() {
val config = TestConfig(mapOf(ALLOW_OPERATORS to true))
val rule = DataClassContainsFunctions(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}
}
}

@Test
Expand Down

0 comments on commit c6b8fad

Please sign in to comment.