Since `InstancePerLeaf` is gone, I found myself re...
# kotest
r
Since
InstancePerLeaf
is gone, I found myself restoring a fresh state before each leaf. Wouldn't it be nice to have something similar to the following? Does kotest maybe already have something like this? Or should all before* functions maybe return delegates?
Copy code
fun <T> ContainerScope.runningBeforeAny(initializer: () -> T): ReadOnlyProperty<Any?, T> {
    var value: T? = null

    beforeAny {
        value = initializer()
    }

    @Suppress("UNCHECKED_CAST")
    return ReadOnlyProperty { _, _ -> value as T }
}


object Spec : FreeSpec({
    "Given an empty list of numbers" - {
        val numbers by runningBeforeAny { mutableListOf<Int>() }

        "when adding one" - {
            beforeAny { numbers.add(1) }

            "the sum should be one" {
                numbers.sum() shouldBe 1
            }

            "the size should be one" {
                numbers shouldHaveSize 1
            }

            "when adding another one" - {
                beforeAny { numbers.add(1) }

                "the sum should be two" {
                    numbers.sum() shouldBe 2
                }

                "the size should be two" {
                    numbers shouldHaveSize 2
                }
            }
        }

        "when leaving empty" - {
            "the sum should be 0" {
                numbers.sum() shouldBe 0
            }
        }
    }
})
s
instance per leaf is deprecated but won't ever be removed. It's just deprecated because it gets really confusing on how extensions and callbacks should work. So if it works for you, feel free to keep using it!
r
Hm, that is new to me. If this is so, would it maybe be better to use an opt-in approach via an annotation? For me deprecation means future removal. Opt-In would mean: I have been warned and I know what I am doing. Also, we noticed several InstancePerLeaf tests breaking with kotest 6. So our impression was, it was no longer supported.
Copy code
Received a completed event for test with unknown id '1.18'. Registered test ids: '[1.1, 1.3, 1.2, 1.5, 1.4, :mymodule:test]'
java.lang.IllegalArgumentException: Received a completed event for test with unknown id '1.18'. Registered test ids: '[1.1, 1.3, 1.2, 1.5, 1.4, :mymodule:test]'
@sam Is this maybe also a regression I should file a ticket for?