I'm using Kotest's data class matchers to compare ...
# kotest
s
I'm using Kotest's data class matchers to compare two data classes that each contain a field with an instance of a sealed class. While the sealed class instances are different, their data is the same. This makes the test fail. Is this expected? I would have assumed sealed class properties should be compared by their values, just like nested data classes.
s
Only fields are used. Are you sure they're exactly the same ?
s
I'm seeing this as part of Kotest's data class diff output:
Copy code
referenceCategory=OTHER, referenceType=org.ossreviewtoolkit.utils.spdx.model.SpdxExternalReference$Type$Other@aa32f88, referenceLocator=acmecorp/acmenator/4.1.3-alpha), 
referenceCategory=OTHER, referenceType=org.ossreviewtoolkit.utils.spdx.model.SpdxExternalReference$Type$Other@67eaebca, referenceLocator=acmecorp/acmenator/4.1.3-alpha),
where "Other" is a sealed class
and these seem to be compared by instance, not by fields...
e
Looks like it's just a sealed class? For data classes there's auto generated equals method, but that's not the case for arbitrary sealed classes
s
Right, which is why I was assuming Kotest would compare the properties of a sealed class manually...
e
Why not mark
Other
as a data class if you need auto-generated equals by properties? it doesn’t feel like it would sense to automatically compare any sealed class within the same hierarchy using properties by default
s
What do you mean by "mark"? Just "declare"? Well, of course there's a reason why
Other
is a sealed class... we need some sort of enum there. And we usually do not need property-equality for this class, other than the Kotest use-case. That said, should it work if I override equals / hashcode for the sealed class?
e
Sealed and data are not mutually exclusive.
sealed data class
could be used. Anyway, I think adding equals / hash code should also do the trick
s
Oh, I need to check
sealed data class
then, thanks!
:-(
Ah, sorry, you probably mean the nested class to be a data class!
đź‘Ť 1