Skip to content

Commit

Permalink
Fix isDataClassEqualTo not working with data objects (#552)
Browse files Browse the repository at this point in the history
* fix `isDataClassEqualTo` not working with `data object`s
  • Loading branch information
VirtualParticle authored Nov 14, 2024
1 parent 76f4588 commit 6ba5ed3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assertk/src/jvmMain/kotlin/assertk/assertions/any.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun <T : Any> Assert<T>.isDataClassEqualTo(expected: T) = given { actual ->

private fun <T> Assert<T>.isDataClassEqualToImpl(expected: T, kclass: KClass<*>?): Unit = given { actual ->
if (actual == expected) return
val compareProps = actual != null && expected != null
val compareProps = actual != null && expected?.let { it::class } == kclass
if (compareProps && kclass != null && kclass.isData) {
for (memberProp in kclass.memberProperties) {
@Suppress("UNCHECKED_CAST")
Expand Down
20 changes: 20 additions & 0 deletions assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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<AssertionError> {
Expand Down Expand Up @@ -152,6 +157,17 @@ class JavaAnyTest {
""".trimMargin().lines(), error.message!!.lines()
)
}

@Test
fun isDataClassEqualTo_fails_different_data_objects() {
val error = assertFailsWith<AssertionError> {
assertThat(DataObject).isDataClassEqualTo(OtherDataObject)
}
assertEquals(
"expected:<[Other]DataObject> but was:<[]DataObject>"
.lines(), error.message!!.lines()
)
}
//endregion

//region isEqualToIgnoringGivenProperties
Expand Down Expand Up @@ -217,5 +233,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
}

0 comments on commit 6ba5ed3

Please # to comment.