From ef2af3251cfe4e8f0b1a3285445d7d7ba6993983 Mon Sep 17 00:00:00 2001 From: Egor Andreevich Date: Mon, 14 Jul 2025 11:21:57 +0200 Subject: [PATCH] Make isEqualToIgnoringGivenProperties accept subtype properties This change makes the properties parameter covariant on T, which allows passing properties of subtypes of T, eliminating the need to cast the test subject to its runtime type to access its properties. --- .../src/jvmMain/kotlin/assertk/assertions/any.kt | 4 ++-- .../test/assertk/assertions/JavaAnyTest.kt | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt index 39b817df..3b20213f 100644 --- a/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt +++ b/assertk/src/jvmMain/kotlin/assertk/assertions/any.kt @@ -97,13 +97,13 @@ private fun Assert.isDataClassEqualToImpl(expected: T, kclass: KClass<*>? /** * Returns an assert that compares all accessible properties except the given properties on the calling class. * @param other Other value to compare to - * @param properties properties of the type with which been ignored + * @param properties properties of the type which will be ignored during comparison * * ``` * assertThat(person).isEqualToIgnoringGivenProperties(other, Person::name, Person::age) * ``` */ -fun Assert.isEqualToIgnoringGivenProperties(other: T, vararg properties: KProperty1) { +fun Assert.isEqualToIgnoringGivenProperties(other: T, vararg properties: KProperty1) { all { for (prop in other::class.memberProperties) { if (!properties.contains(prop)) { diff --git a/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt b/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt index 8d3de13d..d733e76d 100644 --- a/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt +++ b/assertk/src/jvmTest/kotlin/test/assertk/assertions/JavaAnyTest.kt @@ -207,6 +207,22 @@ class JavaAnyTest { BasicObject::failing ) } + + @Test + fun isEqualToIgnoringGivenProperties_passes_subclass() { + fun testObject(): TestObject { + return BasicObject(str = "Rarity") + } + + assertThat( + testObject() + ).isEqualToIgnoringGivenProperties( + BasicObject(str = "notRarity"), + BasicObject::str, + BasicObject::other, + BasicObject::failing + ) + } //endregion open class TestObject