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 2, 2023
1 parent 19ee05d commit 5544d31
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class DataClassContainsFunctions(config: Config = Config.empty) : Rule(config) {
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 +68,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 5544d31

Please sign in to comment.