From 5c605fafbcdbcf420916fec7edb11ab3e61267e5 Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Tue, 30 Nov 2021 15:59:28 +0100 Subject: [PATCH] Renamed StaticCell to ImmutableCell and StaticListCell to ImmutableListCell. --- .../observable/cell/CellCreation.kt | 18 +++++--- .../cell/{StaticCell.kt => ImmutableCell.kt} | 2 +- .../observable/cell/list/FilteredListCell.kt | 1 - ...StaticListCell.kt => ImmutableListCell.kt} | 6 +-- .../observable/cell/list/ListCellCreation.kt | 7 ++- .../phantasmal/observable/cell/CellTests.kt | 2 +- ...DependentCellDirectDependencyEmitsTests.kt | 6 +-- ...ndentCellTransitiveDependencyEmitsTests.kt | 6 +-- .../observable/cell/ImmutableCellTests.kt | 31 +++++++++++++ .../observable/cell/StaticCellTests.kt | 28 ------------ ...ndentListCellDirectDependencyEmitsTests.kt | 4 +- ...tListCellTransitiveDependencyEmitsTests.kt | 6 +-- .../cell/list/ImmutableListCellTests.kt | 44 +++++++++++++++++++ .../cell/list/StaticListCellTests.kt | 41 ----------------- 14 files changed, 108 insertions(+), 94 deletions(-) rename observable/src/commonMain/kotlin/world/phantasmal/observable/cell/{StaticCell.kt => ImmutableCell.kt} (89%) rename observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/{StaticListCell.kt => ImmutableListCell.kt} (88%) create mode 100644 observable/src/commonTest/kotlin/world/phantasmal/observable/cell/ImmutableCellTests.kt delete mode 100644 observable/src/commonTest/kotlin/world/phantasmal/observable/cell/StaticCellTests.kt create mode 100644 observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/ImmutableListCellTests.kt delete mode 100644 observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/StaticListCellTests.kt diff --git a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/CellCreation.kt b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/CellCreation.kt index 2fb6bcc6..a4e8cf96 100644 --- a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/CellCreation.kt +++ b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/CellCreation.kt @@ -1,21 +1,27 @@ package world.phantasmal.observable.cell -private val TRUE_CELL: Cell = StaticCell(true) -private val FALSE_CELL: Cell = StaticCell(false) -private val NULL_CELL: Cell = StaticCell(null) -private val ZERO_INT_CELL: Cell = StaticCell(0) -private val EMPTY_STRING_CELL: Cell = StaticCell("") +private val TRUE_CELL: Cell = ImmutableCell(true) +private val FALSE_CELL: Cell = ImmutableCell(false) +private val NULL_CELL: Cell = ImmutableCell(null) +private val ZERO_INT_CELL: Cell = ImmutableCell(0) +private val EMPTY_STRING_CELL: Cell = ImmutableCell("") -fun cell(value: T): Cell = StaticCell(value) +/** Returns an immutable cell containing [value]. */ +fun cell(value: T): Cell = ImmutableCell(value) +/** Returns a singleton immutable cell containing the value `true`. */ fun trueCell(): Cell = TRUE_CELL +/** Returns a singleton immutable cell containing the value `false`. */ fun falseCell(): Cell = FALSE_CELL +/** Returns a singleton immutable cell containing the value `null`. */ fun nullCell(): Cell = NULL_CELL +/** Returns a singleton immutable cell containing the integer value `0`. */ fun zeroIntCell(): Cell = ZERO_INT_CELL +/** Returns a singleton immutable cell containing the empty string (""). */ fun emptyStringCell(): Cell = EMPTY_STRING_CELL /** diff --git a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/StaticCell.kt b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/ImmutableCell.kt similarity index 89% rename from observable/src/commonMain/kotlin/world/phantasmal/observable/cell/StaticCell.kt rename to observable/src/commonMain/kotlin/world/phantasmal/observable/cell/ImmutableCell.kt index e07c8881..8a0c27ca 100644 --- a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/StaticCell.kt +++ b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/ImmutableCell.kt @@ -6,7 +6,7 @@ import world.phantasmal.observable.AbstractDependency import world.phantasmal.observable.ChangeEvent import world.phantasmal.observable.Observer -class StaticCell(override val value: T) : AbstractDependency(), Cell { +class ImmutableCell(override val value: T) : AbstractDependency(), Cell { override fun observe(callNow: Boolean, observer: Observer): Disposable { if (callNow) { observer(ChangeEvent(value)) diff --git a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/FilteredListCell.kt b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/FilteredListCell.kt index baeb6828..7761cb31 100644 --- a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/FilteredListCell.kt +++ b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/FilteredListCell.kt @@ -1,6 +1,5 @@ package world.phantasmal.observable.cell.list -import world.phantasmal.core.unsafe.unsafeAssertNotNull import world.phantasmal.observable.ChangeEvent import world.phantasmal.observable.Dependency import world.phantasmal.observable.Dependent diff --git a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/StaticListCell.kt b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ImmutableListCell.kt similarity index 88% rename from observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/StaticListCell.kt rename to observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ImmutableListCell.kt index f89865de..7e8d930b 100644 --- a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/StaticListCell.kt +++ b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ImmutableListCell.kt @@ -8,7 +8,7 @@ import world.phantasmal.observable.ChangeEvent import world.phantasmal.observable.Observer import world.phantasmal.observable.cell.* -class StaticListCell(private val elements: List) : AbstractDependency(), ListCell { +class ImmutableListCell(private val elements: List) : AbstractDependency(), ListCell { private var firstOrNull: Cell? = null override val size: Cell = cell(elements.size) @@ -40,13 +40,13 @@ class StaticListCell(private val elements: List) : AbstractDependency(), L override fun firstOrNull(): Cell { if (firstOrNull == null) { - firstOrNull = StaticCell(elements.firstOrNull()) + firstOrNull = ImmutableCell(elements.firstOrNull()) } return unsafeAssertNotNull(firstOrNull) } override fun emitDependencyChanged() { - error("StaticListCell can't change.") + error("ImmutableListCell can't change.") } } diff --git a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ListCellCreation.kt b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ListCellCreation.kt index 997645c1..5f47e0ac 100644 --- a/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ListCellCreation.kt +++ b/observable/src/commonMain/kotlin/world/phantasmal/observable/cell/list/ListCellCreation.kt @@ -2,12 +2,15 @@ package world.phantasmal.observable.cell.list import world.phantasmal.observable.cell.Cell -private val EMPTY_LIST_CELL = StaticListCell(emptyList()) +private val EMPTY_LIST_CELL = ImmutableListCell(emptyList()) -fun listCell(vararg elements: E): ListCell = StaticListCell(elements.toList()) +/** Returns an immutable list cell containing [elements]. */ +fun listCell(vararg elements: E): ListCell = ImmutableListCell(elements.toList()) +/** Returns a singleton empty immutable cell. */ fun emptyListCell(): ListCell = EMPTY_LIST_CELL +/** Returns a mutable list cell containing [elements]. */ fun mutableListCell( vararg elements: E, extractDependencies: DependenciesExtractor? = null, diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/CellTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/CellTests.kt index 2d9825fa..6de5c9d5 100644 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/CellTests.kt +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/CellTests.kt @@ -55,7 +55,7 @@ interface CellTests : ObservableTests { fun propagates_changes_to_flat_mapped_cell() = test { val p = createProvider() - val mapped = p.observable.flatMap { StaticCell(it.hashCode()) } + val mapped = p.observable.flatMap { ImmutableCell(it.hashCode()) } val initialValue = mapped.value var observedValue: Int? = null diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellDirectDependencyEmitsTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellDirectDependencyEmitsTests.kt index 53d47a71..174ee55b 100644 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellDirectDependencyEmitsTests.kt +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellDirectDependencyEmitsTests.kt @@ -6,7 +6,7 @@ package world.phantasmal.observable.cell class FlatteningDependentCellDirectDependencyEmitsTests : RegularCellTests { override fun createProvider() = object : CellTests.Provider { // The transitive dependency can't change. - val transitiveDependency = StaticCell(5) + val transitiveDependency = ImmutableCell(5) // The direct dependency of the cell under test can change. val directDependency = SimpleCell(transitiveDependency) @@ -17,12 +17,12 @@ class FlatteningDependentCellDirectDependencyEmitsTests : RegularCellTests { override fun emit() { // Update the direct dependency. val oldTransitiveDependency = directDependency.value - directDependency.value = StaticCell(oldTransitiveDependency.value + 5) + directDependency.value = ImmutableCell(oldTransitiveDependency.value + 5) } } override fun createWithValue(value: T): FlatteningDependentCell { - val v = StaticCell(StaticCell(value)) + val v = ImmutableCell(ImmutableCell(value)) return FlatteningDependentCell(v) { v.value } } } diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellTransitiveDependencyEmitsTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellTransitiveDependencyEmitsTests.kt index d401fcd5..e559f561 100644 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellTransitiveDependencyEmitsTests.kt +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/FlatteningDependentCellTransitiveDependencyEmitsTests.kt @@ -10,7 +10,7 @@ class FlatteningDependentCellTransitiveDependencyEmitsTests : override fun createProvider() = Provider() override fun createWithValue(value: T): FlatteningDependentCell { - val dependency = StaticCell(StaticCell(value)) + val dependency = ImmutableCell(ImmutableCell(value)) return FlatteningDependentCell(dependency) { dependency.value } } @@ -19,7 +19,7 @@ class FlatteningDependentCellTransitiveDependencyEmitsTests : private val transitiveDependency = SimpleCell(5) // The direct dependency of the cell under test can't change. - private val directDependency = StaticCell(transitiveDependency) + private val directDependency = ImmutableCell(transitiveDependency) override val observable = FlatteningDependentCell(directDependency) { directDependency.value } @@ -30,6 +30,6 @@ class FlatteningDependentCellTransitiveDependencyEmitsTests : } override fun createWithDependencies(vararg dependencies: Cell): Cell = - FlatteningDependentCell(*dependencies) { StaticCell(dependencies.sumOf { it.value }) } + FlatteningDependentCell(*dependencies) { ImmutableCell(dependencies.sumOf { it.value }) } } } diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/ImmutableCellTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/ImmutableCellTests.kt new file mode 100644 index 00000000..9c409846 --- /dev/null +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/ImmutableCellTests.kt @@ -0,0 +1,31 @@ +package world.phantasmal.observable.cell + +import world.phantasmal.core.disposable.TrackedDisposable +import world.phantasmal.observable.test.ObservableTestSuite +import kotlin.test.Test +import kotlin.test.assertEquals + +class ImmutableCellTests : ObservableTestSuite { + @Test + fun observing_it_should_never_create_leaks() = test { + val cell = ImmutableCell("test value") + + TrackedDisposable.checkNoLeaks { + // We never call dispose on the returned disposables. + cell.observe {} + cell.observe(callNow = false) {} + cell.observe(callNow = true) {} + } + } + + @Test + fun observe_respects_callNow() = test { + val cell = ImmutableCell("test value") + var calls = 0 + + cell.observe(callNow = false) { calls++ } + cell.observe(callNow = true) { calls++ } + + assertEquals(1, calls) + } +} diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/StaticCellTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/StaticCellTests.kt deleted file mode 100644 index 0c06215d..00000000 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/StaticCellTests.kt +++ /dev/null @@ -1,28 +0,0 @@ -package world.phantasmal.observable.cell - -import world.phantasmal.observable.test.ObservableTestSuite -import kotlin.test.Test -import kotlin.test.assertEquals - -class StaticCellTests : ObservableTestSuite { - @Test - fun observing_StaticCell_should_never_create_leaks() = test { - val static = StaticCell("test value") - - // We never call dispose on the returned disposables. - static.observe {} - static.observe(callNow = false) {} - static.observe(callNow = true) {} - } - - @Test - fun observe_respects_callNow() = test { - val static = StaticCell("test value") - var calls = 0 - - static.observe(callNow = false) { calls++ } - static.observe(callNow = true) { calls++ } - - assertEquals(1, calls) - } -} diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellDirectDependencyEmitsTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellDirectDependencyEmitsTests.kt index f5dddebc..57b97353 100644 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellDirectDependencyEmitsTests.kt +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellDirectDependencyEmitsTests.kt @@ -8,7 +8,7 @@ import world.phantasmal.observable.cell.SimpleCell class FlatteningDependentListCellDirectDependencyEmitsTests : ListCellTests { override fun createListProvider(empty: Boolean) = object : ListCellTests.Provider { // The transitive dependency can't change. - private val transitiveDependency = StaticListCell(if (empty) emptyList() else listOf(7)) + private val transitiveDependency = ImmutableListCell(if (empty) emptyList() else listOf(7)) // The direct dependency of the list under test can change. private val directDependency = SimpleCell>(transitiveDependency) @@ -19,7 +19,7 @@ class FlatteningDependentListCellDirectDependencyEmitsTests : ListCellTests { override fun addElement() { // Update the direct dependency. val oldTransitiveDependency: ListCell = directDependency.value - directDependency.value = StaticListCell(oldTransitiveDependency.value + 4) + directDependency.value = ImmutableListCell(oldTransitiveDependency.value + 4) } } } diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellTransitiveDependencyEmitsTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellTransitiveDependencyEmitsTests.kt index 70db664b..97a41eec 100644 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellTransitiveDependencyEmitsTests.kt +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/FlatteningDependentListCellTransitiveDependencyEmitsTests.kt @@ -2,7 +2,7 @@ package world.phantasmal.observable.cell.list import world.phantasmal.observable.cell.Cell import world.phantasmal.observable.cell.CellWithDependenciesTests -import world.phantasmal.observable.cell.StaticCell +import world.phantasmal.observable.cell.ImmutableCell /** * In these tests the dependency of the [FlatteningDependentListCell]'s direct dependency changes. @@ -21,7 +21,7 @@ class FlatteningDependentListCellTransitiveDependencyEmitsTests : SimpleListCell(if (empty) mutableListOf() else mutableListOf(7)) // The direct dependency of the list under test can't change. - private val directDependency = StaticCell>(transitiveDependency) + private val directDependency = ImmutableCell>(transitiveDependency) override val observable = FlatteningDependentListCell(directDependency) { directDependency.value } @@ -33,7 +33,7 @@ class FlatteningDependentListCellTransitiveDependencyEmitsTests : override fun createWithDependencies(vararg dependencies: Cell): Cell = FlatteningDependentListCell(*dependencies) { - StaticListCell(dependencies.map { it.value }) + ImmutableListCell(dependencies.map { it.value }) } } } diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/ImmutableListCellTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/ImmutableListCellTests.kt new file mode 100644 index 00000000..4008fa58 --- /dev/null +++ b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/ImmutableListCellTests.kt @@ -0,0 +1,44 @@ +package world.phantasmal.observable.cell.list + +import world.phantasmal.core.disposable.TrackedDisposable +import world.phantasmal.observable.test.ObservableTestSuite +import kotlin.test.Test +import kotlin.test.assertEquals + +class ImmutableListCellTests : ObservableTestSuite { + @Test + fun observing_it_should_never_create_leaks() = test { + val listCell = ImmutableListCell(listOf(1, 2, 3)) + + TrackedDisposable.checkNoLeaks { + // We never call dispose on the returned disposables. + listCell.observe {} + listCell.observe(callNow = false) {} + listCell.observe(callNow = true) {} + listCell.observeList(callNow = false) {} + listCell.observeList(callNow = true) {} + } + } + + @Test + fun observe_respects_callNow() = test { + val listCell = ImmutableListCell(listOf(1, 2, 3)) + var calls = 0 + + listCell.observe(callNow = false) { calls++ } + listCell.observe(callNow = true) { calls++ } + + assertEquals(1, calls) + } + + @Test + fun observeList_respects_callNow() = test { + val listCell = ImmutableListCell(listOf(1, 2, 3)) + var calls = 0 + + listCell.observeList(callNow = false) { calls++ } + listCell.observeList(callNow = true) { calls++ } + + assertEquals(1, calls) + } +} diff --git a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/StaticListCellTests.kt b/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/StaticListCellTests.kt deleted file mode 100644 index d5e67891..00000000 --- a/observable/src/commonTest/kotlin/world/phantasmal/observable/cell/list/StaticListCellTests.kt +++ /dev/null @@ -1,41 +0,0 @@ -package world.phantasmal.observable.cell.list - -import world.phantasmal.observable.test.ObservableTestSuite -import kotlin.test.Test -import kotlin.test.assertEquals - -class StaticListCellTests : ObservableTestSuite { - @Test - fun observing_StaticListCell_should_never_create_leaks() = test { - val static = StaticListCell(listOf(1, 2, 3)) - - // We never call dispose on the returned disposables. - static.observe {} - static.observe(callNow = false) {} - static.observe(callNow = true) {} - static.observeList(callNow = false) {} - static.observeList(callNow = true) {} - } - - @Test - fun observe_respects_callNow() = test { - val static = StaticListCell(listOf(1, 2, 3)) - var calls = 0 - - static.observe(callNow = false) { calls++ } - static.observe(callNow = true) { calls++ } - - assertEquals(1, calls) - } - - @Test - fun observeList_respects_callNow() = test { - val static = StaticListCell(listOf(1, 2, 3)) - var calls = 0 - - static.observeList(callNow = false) { calls++ } - static.observeList(callNow = true) { calls++ } - - assertEquals(1, calls) - } -}