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

Karlo Lozovina

01/25/2022, 11:32 PM
Is there any way that in the same test I have these two assertions, first one passes second one fails? Don't get it...
Copy code
o1 shouldBe o2
(o1 == o2) shouldBe true
s

sam

01/25/2022, 11:41 PM
Shouldbe uses it's own equals for some types
What are the types of o1 and o2
k

Karlo Lozovina

01/25/2022, 11:41 PM
just a regular class ...
I've tried overding equals() to always return false - still, frist one passes, second one fails
s

sam

01/25/2022, 11:42 PM
Data classes ?
Probably need to see the definition and data
k

Karlo Lozovina

01/25/2022, 11:44 PM
nope, not data classes, ordinary class with a single member... I'll try to create a simple example to reproduce
my class implements Set and Iterable, maybe that messes it up?
s

sam

01/25/2022, 11:46 PM
Yep
Those are special cased
You may have found a bug
k

Karlo Lozovina

01/25/2022, 11:46 PM
ah, cool... so I'm not going mad 😆
allright, I'll make a small example then to test it out... thanks!
With the latest kotest (5.1.0) only test #3 fails, first two succeed...
Copy code
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class ArraySet(val orders: Array<String>) : Set<String> {

    override val size get() = orders.size
    override fun iterator() = orders.iterator()
    override fun contains(element: String) = orders.contains(element)
    override fun containsAll(elements: Collection<String>) = orders.toList().containsAll(elements)
    override fun isEmpty() = orders.isEmpty()

    override fun equals(other: Any?): Boolean = false

    override fun hashCode(): Int {
        return orders.sumOf { it.hashCode() }
    }

    companion object {
        fun of(vararg orders: String): ArraySet {
            return ArraySet(orders.distinct().toTypedArray())
        }
    }
}

class ArraySetTest : FunSpec({
    val o1 = ArraySet.of("foo", "bar")
    val o2 = ArraySet.of("bar", "foo")
    val o3 = ArraySet.of("foo", "bar")
    val o4 = ArraySet.of("foo, bar, baz")

    test("1. permutation") {
        o1 shouldBe o2
    }
    test("2. completely equal") {
        o1 shouldBe o3
    }
    test("3. different elements") {
        o1 shouldBe o4
    }
})
s

sam

01/26/2022, 12:21 AM
Can you ticket on website
If you have time
k

Karlo Lozovina

01/26/2022, 12:25 AM
👍 will do a bit later! will link the github issue...
s

sam

01/26/2022, 12:25 AM
👍
Ty