Skip to content

Commit

Permalink
Adjust tests to account for none as a default
Browse files Browse the repository at this point in the history
  • Loading branch information
drawers committed Apr 6, 2023
1 parent 525956e commit 53e8e7c
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,29 @@ class DoubleNegativeLambdaSpec {
assertThat(subject.compileAndLint(code)).isEmpty()
}

@Test
fun `reports double negatives with none by default`() {
val code = """
val isAllEven = listOf(1, 2, 3).none { it % 2 != 0 }
""".trimIndent()

assertThat(subject.compileAndLint(code)).hasSize(1)
}

@Test
fun `reports negative function name from config`() {
val config =
TestConfig(
DoubleNegativeLambda.NEGATIVE_FUNCTIONS to listOf(
ValueWithReason(value = "none", reason = "any").toConfig(),
ValueWithReason(value = "filterNot", reason = "filter").toConfig(),
ValueWithReason(value = "filterNot", reason = "Use `filter` instead.").toConfig(),
)
)
val code = """
fun Int.isEven() = this % 2 == 0
val isValid = listOf(1, 2, 3).filterNot { !it.isEven() }.none { it != 0 }
val isValid = listOf(1, 2, 3).filterNot { !it.isEven() }
""".trimIndent()

assertThat(DoubleNegativeLambda(config).compileAndLint(code)).hasSize(2)
assertThat(DoubleNegativeLambda(config).compileAndLint(code)).hasSize(1)
}

@Test
Expand Down Expand Up @@ -222,17 +230,18 @@ class DoubleNegativeLambdaSpec {
val config =
TestConfig(
DoubleNegativeLambda.NEGATIVE_FUNCTIONS to listOf(
ValueWithReason(value = "none", reason = null).toConfig(),
ValueWithReason(value = "never", reason = null).toConfig(),
)
)
val code = """
fun List<T>.never(predicate: () -> Boolean) = this.none(predicate)
val list = listOf(1, 2, 3)
val result = list.none { it != 0 }
val result = list.never { it != 0 }
""".trimIndent()

val findings = DoubleNegativeLambda(config).compileAndLint(code)
assertThat(findings[0]).hasMessage(
"Double negative through using `!=` inside a `none` lambda. Rewrite in the positive."
"Double negative through using `!=` inside a `never` lambda. Rewrite in the positive."
)
}
}

0 comments on commit 53e8e7c

Please sign in to comment.