-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove exotic
Modifier
APIs, fix normalization (#1548)
* Remove exotic Modifier APIs, fix normalization As we consider removing/reimagining Modifier as a concept in our composables, remove more of their exotic API which is focused on usage as interceptor-like structures (from Compose UI) rather than as key/value structures (from Redwood). Added tests for remaining API behavior, including that of an inconsistency in how combined modifiers behave. The CombinedModifier type retains association on how it was assembled despite being otherwise a list-like structure. So ((a then b) then c) did not equal (a then (b then c)) in old code. Now, if an CombinedModifier is being added to another, its elements are unpacked forming a normal form. Additionally, since nothing references this type or needs to use it directly, remove it from the public API. * Ensure capturing lambda instance is reused in toString * Include comment * Invoke constructor directly instead of polymorphic function
- Loading branch information
1 parent
8fba2cc
commit d9b6e84
Showing
2 changed files
with
139 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
redwood-runtime/src/commonTest/kotlin/app/cash/redwood/ui/ModifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.ui | ||
|
||
import app.cash.redwood.Modifier | ||
import assertk.assertThat | ||
import assertk.assertions.hashCodeFun | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isSameAs | ||
import assertk.assertions.toStringFun | ||
import kotlin.test.Test | ||
import kotlin.test.fail | ||
|
||
class ModifierTest { | ||
@Test fun forEachUnitModifierNeverInvoked() { | ||
Modifier.forEach { | ||
fail() | ||
} | ||
} | ||
|
||
@Test fun forEachOneElement() { | ||
val a = NamedModifier("A") | ||
var called = 0 | ||
a.forEach { element -> | ||
assertThat(element).isSameAs(a) | ||
called++ | ||
} | ||
assertThat(called).isEqualTo(1) | ||
} | ||
|
||
@Test fun forEachManyElements() { | ||
val a = NamedModifier("A") | ||
val b = NamedModifier("B") | ||
val c = NamedModifier("C") | ||
val expected = listOf(a, b, c) | ||
var called = 0 | ||
(a then b then c).forEach { element -> | ||
assertThat(element).isSameAs(expected[called]) | ||
called++ | ||
} | ||
assertThat(called).isEqualTo(3) | ||
} | ||
|
||
@Test fun thenIgnoresUnitModifier() { | ||
assertThat(Modifier then Modifier).isSameAs(Modifier) | ||
|
||
val a = NamedModifier("A") | ||
assertThat(a then Modifier).isSameAs(a) | ||
assertThat(Modifier then a).isSameAs(a) | ||
} | ||
|
||
@Test fun thenElementsToElements() { | ||
val a = NamedModifier("A") | ||
val b = NamedModifier("B") | ||
val c = NamedModifier("C") | ||
val d = NamedModifier("D") | ||
val expected = listOf(a, b, c, d) | ||
var called = 0 | ||
((a then b) then (c then d)).forEach { element -> | ||
assertThat(element).isSameAs(expected[called]) | ||
called++ | ||
} | ||
assertThat(called).isEqualTo(4) | ||
} | ||
|
||
@Test fun toStringEmpty() { | ||
assertThat(Modifier).toStringFun().isEqualTo("Modifier") | ||
} | ||
|
||
@Test fun toStringOne() { | ||
val a = NamedModifier("A") | ||
assertThat(a).toStringFun().isEqualTo("A") | ||
} | ||
|
||
@Test fun toStringMany() { | ||
val a = NamedModifier("A") | ||
val b = NamedModifier("B") | ||
val c = NamedModifier("C") | ||
assertThat((a then b) then c).toStringFun().isEqualTo("[A, B, C]") | ||
assertThat(a then (b then c)).toStringFun().isEqualTo("[A, B, C]") | ||
} | ||
|
||
@Test fun hashCodeMany() { | ||
val a = NamedModifier("A") | ||
val b = NamedModifier("B") | ||
val c = NamedModifier("C") | ||
assertThat((a then b) then c).hashCodeFun().isEqualTo((a then (b then c)).hashCode()) | ||
} | ||
|
||
@Test fun equalsMany() { | ||
val a = NamedModifier("A") | ||
val b = NamedModifier("B") | ||
val c = NamedModifier("C") | ||
assertThat((a then b) then c).isEqualTo(a then (b then c)) | ||
assertThat(a then (b then c)).isEqualTo((a then b) then c) | ||
} | ||
} | ||
|
||
private class NamedModifier(private val name: String) : Modifier.Element { | ||
override fun toString() = name | ||
} |