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 Jan 30, 2022
1 parent 7588d61 commit b56155a
Showing 1 changed file with 125 additions and 0 deletions.
Expand Up @@ -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<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)
}

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<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)
}
}

describe("ignores declaration with val") {
Expand Down Expand Up @@ -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<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)
}

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<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)
}
}

describe("ignores declaration with val") {
Expand Down Expand Up @@ -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<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)
}

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<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)
}
}

describe("ignores declaration with val") {
Expand Down

0 comments on commit b56155a

Please sign in to comment.