Moved dependency test to CellTests.

This commit is contained in:
Daan Vanden Bosch 2022-06-04 22:16:43 +02:00
parent 82524f1f2d
commit e6ca3b9871
2 changed files with 33 additions and 42 deletions

View File

@ -1,18 +1,44 @@
package world.phantasmal.cell package world.phantasmal.cell
import world.phantasmal.cell.test.CellTestSuite
import world.phantasmal.core.disposable.use import world.phantasmal.core.disposable.use
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotEquals import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertNull import kotlin.test.assertNull
import kotlin.test.assertTrue
/** /**
* Test suite for all [Cell] implementations. There is a subclass of this suite for every [Cell] * Test suite for all [Cell] implementations. There is a subclass of this suite for every [Cell]
* implementation. * implementation.
*/ */
interface CellTests : DependencyTests { interface CellTests : CellTestSuite {
override fun createProvider(): Provider fun createProvider(): Provider
/**
* Tests low level [Dependency] implementation.
*/
@Test
fun correctly_emits_invalidation_notifications_to_its_dependents() = test {
val p = createProvider()
var dependencyInvalidatedCalled: Boolean
p.cell.addDependent(object : Dependent {
override fun dependencyInvalidated(dependency: Dependency<*>) {
assertEquals(p.cell, dependency)
dependencyInvalidatedCalled = true
}
})
repeat(5) { index ->
dependencyInvalidatedCalled = false
p.emit()
assertTrue(dependencyInvalidatedCalled, "repetition $index")
}
}
@Test @Test
fun value_is_accessible_without_observers() = test { fun value_is_accessible_without_observers() = test {
@ -202,10 +228,13 @@ interface CellTests : DependencyTests {
assertEquals(mapped.value, observedValue) assertEquals(mapped.value, observedValue)
} }
interface Provider : DependencyTests.Provider { interface Provider {
val cell: Cell<Any> val cell: Cell<Any>
override val dependency: Dependency<*> get() = cell /**
* Makes [cell] emit a change.
*/
fun emit()
} }
} }

View File

@ -1,38 +0,0 @@
package world.phantasmal.cell
import world.phantasmal.cell.test.CellTestSuite
import kotlin.test.*
interface DependencyTests : CellTestSuite {
fun createProvider(): Provider
@Test
fun correctly_emits_invalidation_notifications_to_its_dependents() = test {
val p = createProvider()
var dependencyInvalidatedCalled: Boolean
p.dependency.addDependent(object : Dependent {
override fun dependencyInvalidated(dependency: Dependency<*>) {
assertEquals(p.dependency, dependency)
dependencyInvalidatedCalled = true
}
})
repeat(5) { index ->
dependencyInvalidatedCalled = false
p.emit()
assertTrue(dependencyInvalidatedCalled, "repetition $index")
}
}
interface Provider {
val dependency: Dependency<*>
/**
* Makes [dependency] call [Dependent.dependencyInvalidated] on its dependents.
*/
fun emit()
}
}