Skip to content

Commit

Permalink
Add property delegation double mutability negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketraman committed Feb 1, 2022
1 parent e81a9a0 commit fb661f5
Showing 1 changed file with 88 additions and 0 deletions.
Expand Up @@ -313,6 +313,36 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment)
assertThat(result).isEmpty()
}
}

@Nested
inner class `ignores declaration with var and property delegation` {

@Test
fun `does not detect var declaration with property delegate`() {
val rule = DoubleMutabilityForCollection(
TestConfig(
mapOf(
MUTABLE_TYPES to listOf("MutableState")
)
)
)

val code = """
data class MutableState<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> remember(calculation: () -> T): T
inline operator fun <T> MutableState<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
this.value = value
}
fun main() {
var myState by remember { mutableStateOf("foo") }
}
"""
val result = rule.compileAndLintWithContext(env, code)
assertThat(result).isEmpty()
}
}
}

@Nested
Expand Down Expand Up @@ -566,6 +596,34 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment)
assertThat(result).isEmpty()
}
}

@Nested
inner class `ignores declaration with var and property delegation` {

@Test
fun `does not detect var declaration with property delegate`() {
val rule = DoubleMutabilityForCollection(
TestConfig(
mapOf(
MUTABLE_TYPES to listOf("MutableState")
)
)
)

val code = """
data class MutableState<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> remember(calculation: () -> T): T
inline operator fun <T> MutableState<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
this.value = value
}
var myState by remember { mutableStateOf("foo") }
"""
val result = rule.compileAndLintWithContext(env, code)
assertThat(result).isEmpty()
}
}
}

@Nested
Expand Down Expand Up @@ -863,6 +921,36 @@ class DoubleMutabilityForCollectionSpec(private val env: KotlinCoreEnvironment)
assertThat(result).isEmpty()
}
}

@Nested
inner class `ignores declaration with var and property delegation` {

@Test
fun `does not detect var declaration with property delegate`() {
val rule = DoubleMutabilityForCollection(
TestConfig(
mapOf(
MUTABLE_TYPES to listOf("MutableState")
)
)
)

val code = """
data class MutableState<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> remember(calculation: () -> T): T
inline operator fun <T> MutableState<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
this.value = value
}
class MyClass {
var myState by remember { mutableStateOf("foo") }
}
"""
val result = rule.compileAndLintWithContext(env, code)
assertThat(result).isEmpty()
}
}
}
}
}

0 comments on commit fb661f5

Please sign in to comment.