David Kubecka
11/22/2024, 3:42 PMassertThat(actual).usingRecursiveComparison().ignoringFields("my.nested.field", MyClass::otherField.name).isEqualTo(expected)
but that is unsatisfactory for two reasons:
• The ignore list is either not type-safe (plain strings) or is too verbose (property references)
• The ignore list needs to be specified on the call site instead of on the declaration side.
The latter point is important in the case of test fixtures which are shared among many tests and which contain some dynamic or computed data, such as dates or hashes. In such cases it would be much more convenient for me if I could ignore the field "on the declaration side, e.g. like this:
my TEST_DATA = MyClass(fixedField = TEST_VALUE, dynamicField = any())
and then use some special assertion function that understands that any()
.
For that to work, I imagine that the return type of any()
should be both a subclass of the dynamicField
type as well some marker type for the assertion function. E.g. in pseudo code
interface AnyMarker
inline fun <reified T> any() = object : T, AnyMarker {}
This looks very similar to how Spring creates proxy classes. That relies on the all-open compiler plugin. But I can't use a similar trick here because
• It would be quite impractical to mark a class with some annotation for the plugin only for the testing use case
• I'm not even sure I can "open" classes such as String
which would be needed for the important ignore-hash use case.
My questions are:
• Does this idea make sense?
• Is it feasible? If yes, how to effectively implement any()
(or something similar)?ephemient
11/22/2024, 3:56 PMspand
11/22/2024, 4:05 PMCLOVIS
11/25/2024, 5:34 PMequals
)?David Kubecka
11/25/2024, 5:41 PMDavid Kubecka
11/25/2024, 5:42 PMCLOVIS
11/25/2024, 5:42 PMDavid Kubecka
12/09/2024, 3:31 PMephemient
12/09/2024, 3:35 PMval MY_ANY_STRING = buildString { ... } // new non-interned non-pooled instance
fun myEquals(a: Any?, b: Any?): Boolean {
if (a === MY_ANY_STRING || b === MY_ANY_STRING) return true
ephemient
12/09/2024, 3:35 PM