From e81a9a0d66509725a8474bc5b764819f83bc7d50 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 e75c8e901b1..6f1505bb4c6 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 @@ -140,6 +140,49 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment) assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(3, 5) } + + fun `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) + } + + fun `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) + } } @Nested @@ -376,6 +419,45 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment) assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(2, 1) } + + fun `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) + } + + fun `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) + } } @Nested @@ -608,6 +690,49 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment) assertThat(result).hasSize(1) assertThat(result).hasSourceLocation(3, 5) } + + fun `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) + } + + fun `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) + } } @Nested