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 1 commit
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 @@ -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
if (compareProps && kclass != null && kclass.isData) {
if (compareProps && kclass != null && kclass.isData && kclass.objectInstance == null) {
Copy link
Author

Choose a reason for hiding this comment

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

This seemed like the best way to check if kclass is a data object, but there could be a better way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

could we check the types directly? This should always fail if expected::class != actual::class right?

Copy link
Author

Choose a reason for hiding this comment

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

Sure, how does this look?

    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<T, Any?>
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
}