https://kotlinlang.org logo
#kotest
Title
# kotest
s

Sebastian Schuberth

12/06/2021, 4:57 PM
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

sam

12/06/2021, 5:02 PM
Only fields are used. Are you sure they're exactly the same ?
s

Sebastian Schuberth

12/06/2021, 5:04 PM
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

Emil Kantis

12/06/2021, 5:20 PM
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

Sebastian Schuberth

12/06/2021, 5:22 PM
Right, which is why I was assuming Kotest would compare the properties of a sealed class manually...
e

Emil Kantis

12/06/2021, 10:03 PM
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

Sebastian Schuberth

12/07/2021, 7:16 AM
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

Emil Kantis

12/07/2021, 7:21 AM
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

Sebastian Schuberth

12/07/2021, 7:22 AM
Oh, I need to check
sealed data class
then, thanks!
:-(
Ah, sorry, you probably mean the nested class to be a data class!
👍 1
22 Views