From c27461f27c9506f444ce47fa984017c071063e13 Mon Sep 17 00:00:00 2001 From: Mark Vadeika Date: Fri, 25 Oct 2024 10:37:44 -0400 Subject: [PATCH 1/2] fix `isDataClassEqualTo` not working with `data object`s --- .../jvmMain/kotlin/assertk/assertions/any.kt | 2 +- .../test/assertk/assertions/JavaAnyTest.kt | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt index c9a6f741..7efd930c 100644 --- a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt +++ b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt @@ -82,7 +82,7 @@ fun Assert.isDataClassEqualTo(expected: T) = given { actual -> private fun Assert.isDataClassEqualToImpl(expected: T, kclass: KClass<*>?): Unit = given { actual -> if (actual == expected) return val compareProps = actual != null && expected != null - if (compareProps && kclass != null && kclass.isData) { + if (compareProps && kclass != null && kclass.isData && kclass.objectInstance == null) { for (memberProp in kclass.memberProperties) { @Suppress("UNCHECKED_CAST") val force = memberProp as KProperty1 diff --git a/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt b/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt index ed81e3ad..3cad5ad9 100644 --- a/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt +++ b/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt @@ -96,6 +96,11 @@ class JavaAnyTest { .isDataClassEqualTo(DataClass(InnerDataClass("test"), 1, 'a')) } + @Test + fun isDataClassEqualTo_equal_data_objects_passes() { + assertThat(DataObject).isDataClassEqualTo(DataObject) + } + @Test fun isDataClassEqualTo_reports_all_properties_that_differ_on_failure() { val error = assertFailsWith { @@ -137,6 +142,17 @@ class JavaAnyTest { """.trimMargin().lines(), error.message!!.lines() ) } + + @Test + fun isDataClassEqualTo_fails_different_data_objects() { + val error = assertFailsWith { + assertThat(DataObject).isDataClassEqualTo(OtherDataObject) + } + assertEquals( + "expected:<[Other]DataObject> but was:<[]DataObject>" + .lines(), error.message!!.lines() + ) + } //endregion //region isEqualToIgnoringGivenProperties @@ -202,5 +218,9 @@ class JavaAnyTest { data class DataClass(val one: InnerDataClass?, val two: Int, val three: Char) data class InnerDataClass(val inner: String) + + data object DataObject + + data object OtherDataObject } From 283d199d3a1a2a25d18c3ad8117075a3898ed1b0 Mon Sep 17 00:00:00 2001 From: Mark Vadeika Date: Fri, 25 Oct 2024 14:42:22 -0400 Subject: [PATCH 2/2] Update any.kt --- assertk/src/jvmMain/kotlin/assertk/assertions/any.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt index 7efd930c..9e3bd467 100644 --- a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt +++ b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt @@ -81,8 +81,8 @@ fun Assert.isDataClassEqualTo(expected: T) = given { actual -> private fun Assert.isDataClassEqualToImpl(expected: T, kclass: KClass<*>?): Unit = given { actual -> if (actual == expected) return - val compareProps = actual != null && expected != null - if (compareProps && kclass != null && kclass.isData && kclass.objectInstance == null) { + val compareProps = actual != null && expected?.let { it::class } == kclass + if (compareProps && kclass != null && kclass.isData) { for (memberProp in kclass.memberProperties) { @Suppress("UNCHECKED_CAST") val force = memberProp as KProperty1