here's an interesting case: `obj shouldNotBe null`...
# kotest
p
here's an interesting case:
obj shouldNotBe null
is not equivalent to
obj.shouldNotBeNull()
. The former was used by Junie when
obj
was an iterator, and instead of just asserting on it not being
null
, it consumed all items of the iterator, making it unusable for future assertions. I think both should be equivalent since they read the same, and here in should.kt, we could have another check: if
any
is
null
. If yes, delegate it to
any.shouldNotBeNull()
. The same applies to
shouldBe null
. WDYT?
a
can you post a simple repro for "The former was used by Junie when
obj
was an iterator, and instead of just asserting on it not being
null
, it consumed all items of the iterator, making it unusable for future assertions"
p
for now let me share https://github.com/krzema12/snakeyaml-engine-kmp/pull/540/commits/e24c84c49878b6c9fc2c26cb5cd3264a3889f89c, but it's not a minimal example - I have troubles preparing it for some simple iterators. Let me dig deeper and get back to you
👍🏼 1
Copy code
package it.krzeminski.snakeyaml.engine.kmp.issues.issue46

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

class MyStatefulObject : Iterable<Int> {
    var state: Int? = 5
    override fun iterator(): Iterator<Int> = iterator {
        if (state != null) {
            yield(state!!)
            state = null
        }
    }
}

class ReproductionTest : FunSpec({
    test("reproduction") {
        val obj = MyStatefulObject()

        // Uncomment just this to make the test fail
        // since the iterator is consumed, and after this assertion
        // there are no items to consume
        //  obj shouldNotBe null

        // Uncomment just this to make the test pass
        // since the iterator isn't consumed
        obj.shouldNotBeNull()

        val iterator = obj as Iterable<*>
        iterator.iterator().hasNext() shouldBe true
    }
})
👍🏼 1
gratitude thank you 1
a
make sense. TY. I'll take a look. can you open an issue?
s
Wait. You have Junie running Kotest tests directly? It can’t find the Kotest tests when running. You have to be explicit to it to run using grade
p
No, I just mean the rewrite from Java to Kotlin. I didn't work on making Junie run the tests, maybe then it would catch the problem itself and fix the assertion. Although, I'm glad it made the mistake so that I could report a problem I stumbled upon myself once 😊
🤙 1