Skip to content

Commit

Permalink
Use more specific assertions (#6858)
Browse files Browse the repository at this point in the history
* Use isEmpty instead of hasSize(0) in tests

* Use isNull() instead of isEqualTo(null)

* Use fluent assertion for AtomicInteger

* Refactor test to use hasSize assertion

* Use singleElement to replace hasSize(1) and using collection 0 index
  • Loading branch information
3flex committed Apr 5, 2024
1 parent 2256f8a commit 0f32dcd
Show file tree
Hide file tree
Showing 46 changed files with 189 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class ConfigPropertySpec {
repeat(5) {
assertThat(subject.useProperty()).isEqualTo("something with 1")
}
assertThat(subject.counter.get()).isEqualTo(1)
assertThat(subject.counter).hasValue(1)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PropertiesAwareSpec {

@Test
fun `returns null on absent values`() {
assertThat(store.getOrNull<Boolean>("absent")).isEqualTo(null)
assertThat(store.getOrNull<Boolean>("absent")).isNull()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class RuleCollectorSpec {
defaultAndroidValue = null,
deprecated = null
)
assertThat(items[0].configurations[0]).isEqualTo(expectedConfiguration)
assertThat(items[0].configurations).singleElement().isEqualTo(expectedConfiguration)
}

@Test
Expand Down Expand Up @@ -531,7 +531,7 @@ class RuleCollectorSpec {
}
""".trimIndent()
val items = subject.run(code)
assertThat(items[0].configurations[0]).isEqualTo(
assertThat(items[0].configurations).singleElement().isEqualTo(
Configuration(
name = "maxLineLength",
description = "description",
Expand All @@ -555,7 +555,7 @@ class RuleCollectorSpec {
}
""".trimIndent()
val items = subject.run(code)
assertThat(items[0].configurations[0]).isEqualTo(
assertThat(items[0].configurations).singleElement().isEqualTo(
Configuration(
name = "maxLineLength",
description = "description",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ class RuleSetProviderCollectorSpec {
fun `has one rule`() {
val items = subject.run(code)
val provider = items[0]
assertThat(provider.rules).hasSize(1)
assertThat(provider.rules[0]).isEqualTo(ruleName)
assertThat(provider.rules).singleElement().isEqualTo(ruleName)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class PackageCountVisitorSpec {
compileContentForTest(default),
compileContentForTest(emptyEnum)
)
val count = files
val distinctFiles = files
.map { getData(it) }
.distinct()
.count()
assertThat(count).isEqualTo(2)
assertThat(distinctFiles).hasSize(2)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class ComplexConditionSpec {

val actual = ComplexCondition(testConfig).compileAndLint(code)

assertThat(actual).hasSize(1)
assertThat(actual.first()).isThresholded().withValue(5)
assertThat(actual).singleElement()
.isThresholded()
.withValue(5)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ class LongMethodSpec {

val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings[0]).isThresholded().hasValue(6)
assertThat(findings).singleElement()
.isThresholded()
.hasValue(6)
}

@Test
Expand All @@ -100,8 +101,9 @@ class LongMethodSpec {

val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings[0]).isThresholded().hasValue(8)
assertThat(findings).singleElement()
.isThresholded()
.hasValue(8)
}

@Test
Expand Down Expand Up @@ -155,8 +157,9 @@ class LongMethodSpec {

val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings).singleElement()
.isThresholded()
.hasValue(6)
assertThat(findings).hasTextLocations("nestedLongMethod")
assertThat(findings[0]).isThresholded().hasValue(6)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand Down Expand Up @@ -67,7 +67,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -81,7 +81,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -103,7 +103,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
val obj = C(a = 1, b = 2, c= 3)
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -114,7 +114,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
val obj = C(1, 2)
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -127,7 +127,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -141,7 +141,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand Down Expand Up @@ -181,7 +181,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand All @@ -206,7 +206,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
""".trimIndent()
val subject = NamedArguments(TestConfig("threshold" to 1))
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}
}

Expand All @@ -226,7 +226,7 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
assertThat(findings).isEmpty()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class NestedBlockDepthSpec {
}
""".trimIndent()
val findings = subject.lint(code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).isThresholded().hasValue(5)
assertThat(findings).singleElement()
.isThresholded()
.hasValue(5)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class NestedScopeFunctionsSpec(private val env: KotlinCoreEnvironment) {
private fun expectFunctionInMsg(scopeFunction: String) {
val expected = "The scope function '$scopeFunction' is nested too deeply ('2'). " +
"The maximum allowed depth is set to '1'."
assertThat(actual[0]).hasMessage(expected)
assertThat(actual).singleElement().hasMessage(expected)
}

private fun expectNoFindings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SleepInsteadOfDelaySpec(private val env: KotlinCoreEnvironment) {
delay(1000L)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0)
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DeprecatedBlockTagSpec {
*/
val v = 2
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(0)
assertThat(subject.compileAndLint(code)).isEmpty()
}

@Nested
Expand All @@ -33,14 +33,9 @@ class DeprecatedBlockTagSpec {
fun ohNo() { }
""".trimIndent()

@Test
fun `has found something`() {
assertThat(subject.compileAndLint(code)).hasSize(1)
}

@Test
fun `correct message`() {
assertThat(subject.compileAndLint(code)[0]).hasMessage(
assertThat(subject.compileAndLint(code)).singleElement().hasMessage(
"@deprecated tag block does not properly report " +
"deprecation in Kotlin, use @Deprecated annotation instead"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,9 @@ class UndocumentedPublicPropertySpec {
}
""".trimIndent()
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasSourceLocation(9, 13)
assertThat(findings)
.singleElement()
.hasSourceLocation(9, 13)
}

@Test
Expand All @@ -347,8 +348,9 @@ class UndocumentedPublicPropertySpec {
}
""".trimIndent()
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasSourceLocation(2, 9)
assertThat(findings)
.singleElement()
.hasSourceLocation(2, 9)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class CastNullableToNonNullableTypeSpec(private val env: KotlinCoreEnvironment)
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasStartSourceLocation(2, 17)
assertThat(findings[0]).hasMessage(
assertThat(findings).singleElement().hasMessage(
"Use separate `null` assertion and type cast like " +
"('(bar ?: error(\"null assertion message\")) as String') instead of 'bar as String'."
)
assertThat(findings).hasStartSourceLocation(2, 17)
}

@Test
Expand All @@ -40,12 +39,11 @@ class CastNullableToNonNullableTypeSpec(private val env: KotlinCoreEnvironment)
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasStartSourceLocation(2, 11)
assertThat(findings[0]).hasMessage(
assertThat(findings).singleElement().hasMessage(
"Use separate `null` assertion and type cast like " +
"('(bar() ?: error(\"null assertion message\")) as Int') instead of 'bar() as Int'."
)
assertThat(findings).hasStartSourceLocation(2, 11)
}

@Test
Expand Down Expand Up @@ -205,8 +203,7 @@ class CastNullableToNonNullableTypeSpec(private val env: KotlinCoreEnvironment)
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasMessage(
assertThat(findings).singleElement().hasMessage(
"Use separate `null` assertion and type cast like " +
"('(array[0] ?: error(\"null assertion message\")) as T') instead of 'array[0] as T'."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class CastToNullableTypeSpec(private val env: KotlinCoreEnvironment) {
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).singleElement().hasMessage("Use the safe cast ('as? String') instead of 'as String?'.")
assertThat(findings).hasStartSourceLocation(2, 24)
assertThat(findings[0]).hasMessage("Use the safe cast ('as? String') instead of 'as String?'.")
}

@Test
Expand Down
Loading

0 comments on commit 0f32dcd

Please sign in to comment.