From b56155adee92925f27e0af1ef3b6066b16a10e91 Mon Sep 17 00:00:00 2001 From: Raman Gupta Date: Sun, 30 Jan 2022 02:06:40 -0500 Subject: [PATCH] Additional tests via factory and calculation functions --- .../bugs/DoubleMutabilityForCollectionSpec.kt | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DoubleMutabilityForCollectionSpec.kt b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DoubleMutabilityForCollectionSpec.kt index 4171493ccd40..343a2c37d386 100644 --- a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DoubleMutabilityForCollectionSpec.kt +++ b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/DoubleMutabilityForCollectionSpec.kt @@ -130,6 +130,49 @@ class DoubleMutabilityForCollectionSpec : Spek({ assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(3, 5) } + + it("detects var declaration with MutableState via factory function, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + fun main() { + var myState = mutableStateOf("foo") + } + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(4, 5) + } + + it("detects var declaration with MutableState via calculation lambda, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + fun remember(calculation: () -> T): T + fun main() { + var myState = remember { mutableStateOf("foo") } + } + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(5, 5) + } } describe("ignores declaration with val") { @@ -342,6 +385,45 @@ class DoubleMutabilityForCollectionSpec : Spek({ assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(2, 1) } + + it("detects var declaration with MutableState via factory function, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + var myState = mutableStateOf("foo") + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(3, 1) + } + + it("detects var declaration with MutableState via calculation lambda, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + fun remember(calculation: () -> T): T + var myState = remember { mutableStateOf("foo") } + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(4, 1) + } } describe("ignores declaration with val") { @@ -550,6 +632,49 @@ class DoubleMutabilityForCollectionSpec : Spek({ assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(3, 5) } + + it("detects var declaration with MutableState via factory function, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + class MyClass { + var myState = mutableStateOf("foo") + } + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(4, 5) + } + + it("detects var declaration with MutableState via calculation lambda, when configured") { + val rule = DoubleMutabilityForCollection( + TestConfig( + mapOf( + MUTABLE_TYPES to listOf("MutableState") + ) + ) + ) + + val code = """ + data class MutableState(var state: T) + fun mutableStateOf(value: T): MutableState + fun remember(calculation: () -> T): T + class MyClass { + var myState = remember { mutableStateOf("foo") } + } + """ + val result = rule.compileAndLintWithContext(env, code) + assertThat(result).hasSize(1) + assertThat(result).hasSourceLocation(5, 5) + } } describe("ignores declaration with val") {