From e671e27c028a2c03bc6a449a717f5ce95fe22fa1 Mon Sep 17 00:00:00 2001
From: Daan Vanden Bosch <daan.v.d.bosch@gmail.com>
Date: Tue, 1 Nov 2022 11:13:54 +0100
Subject: [PATCH] Added more FilteredListCell tests.

---
 .../cell/CellWithDependenciesTests.kt         |  4 +-
 ...ilteredListCellListDependencyEmitsTests.kt |  5 +++
 ...edListCellPredicateDependencyEmitsTests.kt | 40 +++++++++++++++++
 ...ellPredicateResultDependenciesEmitTests.kt | 44 +++++++++++++++++++
 .../cell/list/FilteredListCellTests.kt        |  7 ++-
 5 files changed, 96 insertions(+), 4 deletions(-)
 create mode 100644 cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateDependencyEmitsTests.kt
 create mode 100644 cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateResultDependenciesEmitTests.kt

diff --git a/cell/src/commonTest/kotlin/world/phantasmal/cell/CellWithDependenciesTests.kt b/cell/src/commonTest/kotlin/world/phantasmal/cell/CellWithDependenciesTests.kt
index eaebd298..8e563577 100644
--- a/cell/src/commonTest/kotlin/world/phantasmal/cell/CellWithDependenciesTests.kt
+++ b/cell/src/commonTest/kotlin/world/phantasmal/cell/CellWithDependenciesTests.kt
@@ -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)
     }
diff --git a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellListDependencyEmitsTests.kt b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellListDependencyEmitsTests.kt
index 6330d942..22884ecc 100644
--- a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellListDependencyEmitsTests.kt
+++ b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellListDependencyEmitsTests.kt
@@ -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) },
             )
 
diff --git a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateDependencyEmitsTests.kt b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateDependencyEmitsTests.kt
new file mode 100644
index 00000000..c93f88a2
--- /dev/null
+++ b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateDependencyEmitsTests.kt
@@ -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)) }
+            },
+        )
+}
diff --git a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateResultDependenciesEmitTests.kt b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateResultDependenciesEmitTests.kt
new file mode 100644
index 00000000..083afdde
--- /dev/null
+++ b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellPredicateResultDependenciesEmitTests.kt
@@ -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) }
+            },
+        )
+    }
+}
diff --git a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellTests.kt b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellTests.kt
index e7e97b49..e665d708 100644
--- a/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellTests.kt
+++ b/cell/src/commonTest/kotlin/world/phantasmal/cell/list/FilteredListCellTests.kt
@@ -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>) =