From 201de77270450c3ca0510622fb2fb2ba48af59bb Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Mon, 13 Nov 2023 14:35:57 -0500 Subject: [PATCH] Migrate to AssertK (#245) --- gradle/libs.versions.toml | 3 +- mosaic-gradle-plugin/build.gradle | 2 +- .../jakewharton/mosaic/gradle/FixtureTest.kt | 3 +- mosaic-runtime/build.gradle | 2 +- .../jakewharton/mosaic/AnsiRenderingTest.kt | 33 +++++++------------ .../jakewharton/mosaic/DebugRenderingTest.kt | 28 ++++++++-------- .../com/jakewharton/mosaic/LayoutTest.kt | 13 ++++---- .../com/jakewharton/mosaic/MosaicTest.kt | 6 ++-- .../com/jakewharton/mosaic/NodeApplierTest.kt | 5 +-- .../mosaic/text/AnnotatedStringBuilderTest.kt | 16 +++++---- .../mosaic/text/AnnotatedStringTest.kt | 19 ++++++----- .../jakewharton/mosaic/text/SpanStyleTest.kt | 4 ++- 12 files changed, 68 insertions(+), 66 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 79a6c28b..af5698cd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -25,5 +25,4 @@ mordant = "com.github.ajalt.mordant:mordant:2.2.0" codepoints = "de.cketti.unicode:kotlin-codepoints:0.6.1" junit4 = "junit:junit:4.13.2" -truth = "com.google.truth:truth:1.1.5" -truthish = "com.varabyte.truthish:truthish:0.6.5" +assertk = "com.willowtreeapps.assertk:assertk:0.27.0" diff --git a/mosaic-gradle-plugin/build.gradle b/mosaic-gradle-plugin/build.gradle index 2b67e51a..02de935d 100644 --- a/mosaic-gradle-plugin/build.gradle +++ b/mosaic-gradle-plugin/build.gradle @@ -20,7 +20,7 @@ dependencies { compileOnly libs.kotlin.gradlePlugin testImplementation libs.junit4 - testImplementation libs.truth + testImplementation libs.assertk testImplementation gradleTestKit() } diff --git a/mosaic-gradle-plugin/src/test/kotlin/com/jakewharton/mosaic/gradle/FixtureTest.kt b/mosaic-gradle-plugin/src/test/kotlin/com/jakewharton/mosaic/gradle/FixtureTest.kt index 450accc0..94fefc94 100644 --- a/mosaic-gradle-plugin/src/test/kotlin/com/jakewharton/mosaic/gradle/FixtureTest.kt +++ b/mosaic-gradle-plugin/src/test/kotlin/com/jakewharton/mosaic/gradle/FixtureTest.kt @@ -1,6 +1,7 @@ package com.jakewharton.mosaic.gradle -import com.google.common.truth.Truth.assertThat +import assertk.assertThat +import assertk.assertions.contains import java.io.File import org.gradle.testkit.runner.GradleRunner import org.junit.Test diff --git a/mosaic-runtime/build.gradle b/mosaic-runtime/build.gradle index adaefbed..b0a0d9f9 100644 --- a/mosaic-runtime/build.gradle +++ b/mosaic-runtime/build.gradle @@ -24,7 +24,7 @@ kotlin { commonTest { dependencies { implementation libs.kotlin.test - implementation libs.truthish + implementation libs.assertk } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/AnsiRenderingTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/AnsiRenderingTest.kt index f78d82dd..67f39525 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/AnsiRenderingTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/AnsiRenderingTest.kt @@ -1,11 +1,12 @@ package com.jakewharton.mosaic +import assertk.assertThat +import assertk.assertions.isEqualTo import com.jakewharton.mosaic.ui.Column import com.jakewharton.mosaic.ui.Row import com.jakewharton.mosaic.ui.Static import com.jakewharton.mosaic.ui.Text import kotlin.test.Test -import kotlin.test.assertEquals class AnsiRenderingTest { private val rendering = AnsiRendering() @@ -19,13 +20,12 @@ class AnsiRenderingTest { } // TODO We should not draw trailing whitespace. - assertEquals( + assertThat(rendering.render(hello).toString()).isEqualTo( """ |Hello$s |World! | """.trimMargin(), - rendering.render(hello).toString(), ) } @@ -37,13 +37,12 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(first).toString()).isEqualTo( """ |Hello$s |World! | """.trimMargin(), - rendering.render(first).toString(), ) val second = mosaicNodes { @@ -55,7 +54,7 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(second).toString()).isEqualTo( """ |$cursorUp${cursorUp}Hel$clearLine |lo $clearLine @@ -63,7 +62,6 @@ class AnsiRenderingTest { |ld! | """.trimMargin(), - rendering.render(second).toString(), ) } @@ -77,7 +75,7 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(first).toString()).isEqualTo( """ |Hel |lo$s @@ -85,7 +83,6 @@ class AnsiRenderingTest { |ld! | """.trimMargin(), - rendering.render(first).toString(), ) val second = mosaicNodes { @@ -95,14 +92,13 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(second).toString()).isEqualTo( """ |$cursorUp$cursorUp$cursorUp${cursorUp}Hello $clearLine |World!$clearLine |$clearLine |$clearLine$cursorUp """.trimMargin(), - rendering.render(second).toString(), ) } @@ -114,13 +110,12 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(hello).toString()).isEqualTo( """ |World! |Hello | """.trimMargin(), - rendering.render(hello).toString(), ) } @@ -132,13 +127,12 @@ class AnsiRenderingTest { Text("Two") } - assertEquals( + assertThat(rendering.render(first).toString()).isEqualTo( """ |One |Two | """.trimMargin(), - rendering.render(first).toString(), ) val second = mosaicNodes { @@ -148,13 +142,12 @@ class AnsiRenderingTest { Text("Four") } - assertEquals( + assertThat(rendering.render(second).toString()).isEqualTo( """ |${cursorUp}Three$clearLine |Four | """.trimMargin(), - rendering.render(second).toString(), ) } @@ -182,7 +175,7 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(hello).toString()).isEqualTo( """ |One |Two @@ -192,7 +185,6 @@ class AnsiRenderingTest { |Sup | """.trimMargin(), - rendering.render(hello).toString(), ) } @@ -209,14 +201,13 @@ class AnsiRenderingTest { } } - assertEquals( + assertThat(rendering.render(hello).toString()).isEqualTo( """ |Static |TopTopTop |LeftLeft$s | """.trimMargin(), - rendering.render(hello).toString(), ) } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/DebugRenderingTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/DebugRenderingTest.kt index 8bca382e..62118577 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/DebugRenderingTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/DebugRenderingTest.kt @@ -1,15 +1,19 @@ package com.jakewharton.mosaic +import assertk.assertFailure +import assertk.assertThat +import assertk.assertions.containsMatch +import assertk.assertions.isEqualTo +import assertk.assertions.isInstanceOf +import assertk.assertions.isNotNull +import assertk.assertions.message import com.jakewharton.mosaic.layout.drawBehind import com.jakewharton.mosaic.modifier.Modifier import com.jakewharton.mosaic.ui.Layout import com.jakewharton.mosaic.ui.Row import com.jakewharton.mosaic.ui.Static import com.jakewharton.mosaic.ui.Text -import com.varabyte.truthish.assertThat import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith import kotlin.time.Duration.Companion.milliseconds import kotlin.time.ExperimentalTime import kotlin.time.TestTimeSource @@ -29,10 +33,11 @@ class DebugRenderingTest { } } - val t = assertFailsWith { + assertFailure { rendering.render(nodes) - } - assertThat(t.message!!) + }.isInstanceOf() + .message() + .isNotNull() .containsMatch( """ |Failed @@ -44,7 +49,7 @@ class DebugRenderingTest { | |OUTPUT: |(kotlin\.|java\.lang\.)?UnsupportedOperationException:? - """.trimMargin(), + """.trimMargin().toRegex(), ) } @@ -56,7 +61,7 @@ class DebugRenderingTest { } } - assertEquals( + assertThat(rendering.render(nodes)).isEqualTo( """ |NODES: |Text("Hello") x=0 y=0 w=5 h=1 DrawBehind @@ -70,7 +75,6 @@ class DebugRenderingTest { |Hello | """.trimMargin(), - rendering.render(nodes), ) } @@ -79,7 +83,7 @@ class DebugRenderingTest { Text("Hello") } - assertEquals( + assertThat(rendering.render(hello)).isEqualTo( """ |NODES: |Text("Hello") x=0 y=0 w=5 h=1 DrawBehind @@ -88,11 +92,10 @@ class DebugRenderingTest { |Hello | """.trimMargin(), - rendering.render(hello), ) timeSource += 100.milliseconds - assertEquals( + assertThat(rendering.render(hello)).isEqualTo( """ |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +100ms |NODES: @@ -102,7 +105,6 @@ class DebugRenderingTest { |Hello | """.trimMargin(), - rendering.render(hello), ) } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/LayoutTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/LayoutTest.kt index 2ae1b9ee..1d3e34b0 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/LayoutTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/LayoutTest.kt @@ -1,5 +1,7 @@ package com.jakewharton.mosaic +import assertk.assertThat +import assertk.assertions.isEqualTo import com.jakewharton.mosaic.layout.drawBehind import com.jakewharton.mosaic.modifier.Modifier import com.jakewharton.mosaic.ui.Column @@ -7,7 +9,6 @@ import com.jakewharton.mosaic.ui.Layout import com.jakewharton.mosaic.ui.Row import com.jakewharton.mosaic.ui.Text import kotlin.test.Test -import kotlin.test.assertEquals class LayoutTest { @Test fun layoutDebugInfo() { @@ -28,7 +29,7 @@ class LayoutTest { | Text("Hi!") x=0 y=0 w=0 h=0 DrawBehind | Text("Hey!") x=0 y=0 w=0 h=0 DrawBehind """.trimMargin() - assertEquals(expected, node.toString()) + assertThat(node.toString()).isEqualTo(expected) } @Test fun noMeasureNoDraw() { @@ -46,7 +47,7 @@ class LayoutTest { | $s | """.trimMargin() - assertEquals(expected, actual) + assertThat(actual).isEqualTo(expected) } @Test fun noPlacementOverlaps() { @@ -67,7 +68,7 @@ class LayoutTest { |ABC | """.trimMargin() - assertEquals(expected, actual) + assertThat(actual).isEqualTo(expected) } @Test fun placementWorks() { @@ -91,7 +92,7 @@ class LayoutTest { |A $s | """.trimMargin() - assertEquals(expected, actual) + assertThat(actual).isEqualTo(expected) } @Test fun canvasIsNotClipped() { @@ -130,6 +131,6 @@ class LayoutTest { |.....X | """.trimMargin() - assertEquals(expected, actual) + assertThat(actual).isEqualTo(expected) } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/MosaicTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/MosaicTest.kt index bb8cee7a..178f1e9a 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/MosaicTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/MosaicTest.kt @@ -1,9 +1,10 @@ package com.jakewharton.mosaic +import assertk.assertThat +import assertk.assertions.isEqualTo import com.jakewharton.mosaic.ui.Column import com.jakewharton.mosaic.ui.Text import kotlin.test.Test -import kotlin.test.assertEquals class MosaicTest { @Test fun render() { @@ -14,14 +15,13 @@ class MosaicTest { Text("Three") } } - assertEquals( + assertThat(actual).isEqualTo( """ |One $s |Two $s |Three | """.trimMargin(), - actual, ) } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/NodeApplierTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/NodeApplierTest.kt index 75f2da3e..2127c939 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/NodeApplierTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/NodeApplierTest.kt @@ -1,11 +1,12 @@ package com.jakewharton.mosaic import androidx.compose.runtime.Applier +import assertk.assertThat +import assertk.assertions.isEqualTo import com.jakewharton.mosaic.layout.DebugPolicy import com.jakewharton.mosaic.layout.MosaicNode import com.jakewharton.mosaic.ui.NodeFactory import kotlin.test.Test -import kotlin.test.assertEquals class NodeApplierTest { private val root = createRootNode() @@ -184,7 +185,7 @@ class NodeApplierTest { } private fun assertChildren(vararg nodes: MosaicNode) { - assertEquals(nodes.toList(), root.children) + assertThat(root.children).isEqualTo(nodes.toList()) } private fun textNode(name: String): MosaicNode { diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringBuilderTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringBuilderTest.kt index fb5e9b97..c9942561 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringBuilderTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringBuilderTest.kt @@ -1,11 +1,15 @@ package com.jakewharton.mosaic.text +import assertk.assertFailure +import assertk.assertThat +import assertk.assertions.hasSize +import assertk.assertions.isEmpty +import assertk.assertions.isEqualTo +import assertk.assertions.isInstanceOf import com.jakewharton.mosaic.text.AnnotatedString.Range import com.jakewharton.mosaic.ui.Color import com.jakewharton.mosaic.ui.TextStyle -import com.varabyte.truthish.assertThat import kotlin.test.Test -import kotlin.test.assertFailsWith class AnnotatedStringBuilderTest { @@ -385,9 +389,9 @@ class AnnotatedStringBuilderTest { } @Test fun pop_when_empty_does_not_throw_exception() { - assertFailsWith { + assertFailure { AnnotatedString.Builder().pop() - } + }.isInstanceOf() } @Test fun pop_in_the_middle() { @@ -455,10 +459,10 @@ class AnnotatedStringBuilderTest { with(AnnotatedString.Builder()) { val styleIndex = pushStyle(style) - assertFailsWith { + assertFailure { // should throw exception pop(styleIndex + 1) - } + }.isInstanceOf() } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringTest.kt index 380bcf5a..c160ed9a 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/AnnotatedStringTest.kt @@ -1,11 +1,13 @@ package com.jakewharton.mosaic.text +import assertk.assertFailure +import assertk.assertThat +import assertk.assertions.isEqualTo +import assertk.assertions.isInstanceOf +import assertk.assertions.isSameAs import com.jakewharton.mosaic.text.AnnotatedString.Range import com.jakewharton.mosaic.ui.Color -import com.varabyte.truthish.assertThat import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith class AnnotatedStringTest { @@ -200,15 +202,15 @@ class AnnotatedStringTest { } @Test fun subSequence_throws_exception_for_start_greater_than_end() { - assertFailsWith { + assertFailure { AnnotatedString("ab").subSequence(1, 0) - } + }.isInstanceOf() } @Test fun creating_item_with_start_greater_than_end_throws_exception() { - assertFailsWith { + assertFailure { Range(SpanStyle(color = Color.Red), 1, 0) - } + }.isInstanceOf() } @Test fun creating_item_with_start_equal_to_end_does_not_throw_exception() { @@ -227,7 +229,6 @@ class AnnotatedStringTest { } @Test fun toString_returns_the_plain_string() { - val text = "abc" - assertEquals(text, AnnotatedString(text).toString()) + assertThat(AnnotatedString("abc").toString()).isEqualTo("abc") } } diff --git a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/SpanStyleTest.kt b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/SpanStyleTest.kt index 281e9a2a..5ccf5a2d 100644 --- a/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/SpanStyleTest.kt +++ b/mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/text/SpanStyleTest.kt @@ -1,8 +1,10 @@ package com.jakewharton.mosaic.text +import assertk.assertThat +import assertk.assertions.isEqualTo +import assertk.assertions.isNull import com.jakewharton.mosaic.ui.Color import com.jakewharton.mosaic.ui.TextStyle -import com.varabyte.truthish.assertThat import kotlin.test.Test class SpanStyleTest {