Skip to content

Commit

Permalink
Additional tests via factory and calculation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketraman committed Feb 1, 2022
1 parent 4dcd9a3 commit e81a9a0
Showing 1 changed file with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> 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
Expand Down Expand Up @@ -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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> 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
Expand Down Expand Up @@ -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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
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<T>(var state: T)
fun <T> mutableStateOf(value: T): MutableState<T>
fun <T> 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
Expand Down

0 comments on commit e81a9a0

Please sign in to comment.