Skip to content

Commit

Permalink
Fixes deprecation warnings related to Named.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Mar 17, 2022
1 parent a1be5cf commit 33f585b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.squareup.workflow1.ui

import com.google.common.truth.Truth.assertThat
import org.junit.Test

// If you try to replace isTrue() with isTrue compilation fails.
@OptIn(WorkflowUiExperimentalApi::class)
@Suppress("UsePropertyAccessSyntax")
internal class NamedScreenTest {
object Whut : Screen
object Hey : Screen

@Test fun `same type same name matches`() {
assertThat(compatible(NamedScreen(Hey, "eh"), NamedScreen(Hey, "eh"))).isTrue()
}

@Test fun `same type diff name matches`() {
assertThat(compatible(NamedScreen(Hey, "blam"), NamedScreen(Hey, "bloom"))).isFalse()
}

@Test fun `diff type same name no match`() {
assertThat(compatible(NamedScreen(Hey, "a"), NamedScreen(Whut, "a"))).isFalse()
}

@Test fun recursion() {
assertThat(
compatible(
NamedScreen(NamedScreen(Hey, "one"), "ho"),
NamedScreen(NamedScreen(Hey, "one"), "ho")
)
).isTrue()

assertThat(
compatible(
NamedScreen(NamedScreen(Hey, "one"), "ho"),
NamedScreen(NamedScreen(Hey, "two"), "ho")
)
).isFalse()

assertThat(
compatible(
NamedScreen(NamedScreen(Hey, "a"), "ho"),
NamedScreen(NamedScreen(Whut, "a"), "ho")
)
).isFalse()
}

@Test fun `key recursion`() {
assertThat(NamedScreen(NamedScreen(Hey, "one"), "ho").compatibilityKey)
.isEqualTo(NamedScreen(NamedScreen(Hey, "one"), "ho").compatibilityKey)

assertThat(NamedScreen(NamedScreen(Hey, "one"), "ho").compatibilityKey)
.isNotEqualTo(NamedScreen(NamedScreen(Hey, "two"), "ho").compatibilityKey)

assertThat(NamedScreen(NamedScreen(Hey, "a"), "ho").compatibilityKey)
.isNotEqualTo(NamedScreen(NamedScreen(Whut, "a"), "ho").compatibilityKey)
}

@Test fun `recursive keys are legible`() {
assertThat(NamedScreen(NamedScreen(Hey, "one"), "ho").compatibilityKey)
.isEqualTo("com.squareup.workflow1.ui.NamedScreenTest\$Hey+one+ho")
}

private class Foo(override val compatibilityKey: String) : Compatible, Screen

@Test fun `the test Compatible class actually works`() {
assertThat(compatible(Foo("bar"), Foo("bar"))).isTrue()
assertThat(compatible(Foo("bar"), Foo("baz"))).isFalse()
}

@Test fun `wrapping custom Compatible compatibility works`() {
assertThat(compatible(NamedScreen(Foo("bar"), "name"), NamedScreen(Foo("bar"), "name")))
.isTrue()
assertThat(compatible(NamedScreen(Foo("bar"), "name"), NamedScreen(Foo("baz"), "name")))
.isFalse()
}

@Test fun `wrapping custom Compatible keys work`() {
assertThat(NamedScreen(Foo("bar"), "name").compatibilityKey)
.isEqualTo(NamedScreen(Foo("bar"), "name").compatibilityKey)
assertThat(NamedScreen(Foo("bar"), "name").compatibilityKey)
.isNotEqualTo(NamedScreen(Foo("baz"), "name").compatibilityKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.Test

// If you try to replace isTrue() with isTrue compilation fails.
@OptIn(WorkflowUiExperimentalApi::class)
@Suppress("UsePropertyAccessSyntax")
@Suppress("UsePropertyAccessSyntax", "DEPRECATION")
internal class NamedTest {
object Whut
object Hey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@file:Suppress("DEPRECATION")

package com.squareup.workflow1.ui.radiography

import com.squareup.workflow1.ui.Compatible
import com.squareup.workflow1.ui.Named
import com.squareup.workflow1.ui.NamedScreen
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.getRendering
import radiography.AttributeAppendable
Expand All @@ -28,7 +31,9 @@ private object WorkflowViewRendererImpl : ViewStateRenderer {
}

private fun AttributeAppendable.renderRendering(rendering: Any) {
val actualRendering = (rendering as? Named<*>)?.wrapped ?: rendering
val actualRendering = (rendering as? Named<*>)?.wrapped
?: (rendering as? NamedScreen<*>)?.wrapped
?: rendering
append("workflow-rendering-type:${actualRendering::class.java.name}")

if (rendering is Compatible) {
Expand Down

0 comments on commit 33f585b

Please sign in to comment.