Added more FilteredListCell tests.

This commit is contained in:
Daan Vanden Bosch 2022-11-01 11:13:54 +01:00
parent 597bf5390e
commit e671e27c02
5 changed files with 96 additions and 4 deletions

View File

@ -47,9 +47,9 @@ interface CellWithDependenciesTests : CellTests {
disposer.add(leaf.observeChange { observedChanges++ })
// Change root, which results in both branches changing and thus two dependencies of leaf
// Change root, which results in all branches changing and thus three dependencies of leaf
// changing.
root.value = 7
root.value++
assertEquals(1, observedChanges)
}

View File

@ -5,15 +5,20 @@ import world.phantasmal.cell.CellWithDependenciesTests
import world.phantasmal.cell.cell
import world.phantasmal.cell.map
/**
* In these tests the list dependency of the [FilteredListCell] changes.
*/
@Suppress("unused")
class FilteredListCellListDependencyEmitsTests : ListCellTests, CellWithDependenciesTests {
override fun createListProvider(empty: Boolean) = object : ListCellTests.Provider {
// The list cell changes.
private val dependencyCell =
SimpleListCell(if (empty) mutableListOf(5) else mutableListOf(5, 10))
override val cell =
FilteredListCell(
list = dependencyCell,
// Neither the predicate cell nor the predicate results change.
predicate = cell { cell(it % 2 == 0) },
)

View File

@ -0,0 +1,40 @@
package world.phantasmal.cell.list
import world.phantasmal.cell.*
/**
* In these tests the predicate dependency of the [FilteredListCell] changes.
*/
@Suppress("unused")
class FilteredListCellPredicateDependencyEmitsTests : ListCellTests, CellWithDependenciesTests {
override fun createListProvider(empty: Boolean) = object : ListCellTests.Provider {
private var maxValue = if (empty) 0 else 1
// The predicate cell changes, the predicate results don't.
private val predicateCell = SimpleCell<(Int) -> Cell<Boolean>> { cell(it <= maxValue) }
override val cell =
FilteredListCell(
// The list dependency doesn't change.
list = ImmutableListCell((1..20).toList()),
predicate = predicateCell,
)
override fun addElement() {
maxValue++
val max = maxValue
predicateCell.value = { cell(it <= max) }
}
}
override fun createWithDependencies(
dependency1: Cell<Int>,
dependency2: Cell<Int>,
dependency3: Cell<Int>,
) =
FilteredListCell(
list = listCell(1, 2, 3, 4, 5, 6, 7, 8, 9),
predicate = map(dependency1, dependency2, dependency3) { value1, value2, value3 ->
{ cell((it % 2) == ((value1 + value2 + value3) % 2)) }
},
)
}

View File

@ -0,0 +1,44 @@
package world.phantasmal.cell.list
import world.phantasmal.cell.*
/**
* In these tests the predicate result dependencies of the [FilteredListCell] change.
*/
@Suppress("unused")
class FilteredListCellPredicateResultDependenciesEmitTests : ListCellTests,
CellWithDependenciesTests {
override fun createListProvider(empty: Boolean) = object : ListCellTests.Provider {
private var size = if (empty) 0 else 1
// The predicate results change.
private val predicateResultCells = (1..20).map { mutableCell(it <= size) }
override val cell =
FilteredListCell(
// The list and predicate dependencies don't change.
list = ImmutableListCell((1..20).toList()),
predicate = cell { predicateResultCells[it - 1] },
)
override fun addElement() {
predicateResultCells[size].value = true
size++
}
}
override fun createWithDependencies(
dependency1: Cell<Int>,
dependency2: Cell<Int>,
dependency3: Cell<Int>,
): FilteredListCell<Int> {
val deps = listOf(dependency1, dependency2, dependency3)
return FilteredListCell(
list = listCell(1, 2, 3),
predicate = cell {
deps[it - 1].map { value -> (it % 2) == (value % 2) }
},
)
}
}

View File

@ -4,9 +4,12 @@ import world.phantasmal.cell.Cell
import world.phantasmal.cell.cell
import world.phantasmal.cell.map
// TODO: A test suite that tests FilteredListCell while its predicate dependency is changing.
// TODO: A test suite that tests FilteredListCell while the predicate results are changing.
// TODO: A test suite that tests FilteredListCell while all 3 types of dependencies are changing.
/**
* Standard tests are done by [FilteredListCellListDependencyEmitsTests],
* [FilteredListCellPredicateDependencyEmitsTests] and
* [FilteredListCellPredicateResultDependenciesEmitTests].
*/
@Suppress("unused")
class FilteredListCellTests : AbstractFilteredListCellTests {
override fun <E> createFilteredListCell(list: ListCell<E>, predicate: Cell<(E) -> Boolean>) =