Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isDataClassEqualTo not working with data objects #552

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -81,7 +81,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) {
Comment on lines -84 to 85
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically removes the expected != null check, but it's essentially still done since we check for equality with kclass which is then later checked not to be null.

for (memberProp in kclass.memberProperties) {
@Suppress("UNCHECKED_CAST")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssertionError> {
Expand Down Expand Up @@ -137,6 +142,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 @@ -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
}