is it frowned upon to add custom test context to a...
# kotlintest
b
is it frowned upon to add custom test context to a, say,
AbstractShouldSpec.ShouldScope
in order to make tests a bit cleaner? or is that the idea?
for example this:
Copy code
"Boolean" {
    val config = createConfig {
        "some.boolean.value=true"
    }
    getValue<Boolean>("some.boolean.value", config) shouldBe true
}
becomes:
Copy code
"Boolean2" {
    withConfig {
        "some.boolean.value=true"
    }
    getValue<Boolean>("some.boolean.value") shouldBe true
}
which is subtly different but nicer to read, i think
done via adding:
Copy code
private fun AbstractShouldSpec.ShouldScope.withConfig(block: () -> String) {
        val config = createConfig { block() }
        this.context.putMetaData("config", config)
    }

    private inline fun <reified T : Any> AbstractShouldSpec.ShouldScope.getValue(path: String): T {
        val config = this.context.metaData()["config"] as ConfigSource
        val getter = config.getterFor(T::class)
        return getter(path)
    }
s
Are you using that metadata stuff a lot ?
It’s been removed from 4.0 currently
b
ah interesting...this is the first time i thought to play with it...i do think it's quite nice for this though 🙂
is there another way to accomplish something like this?
s
Yeah I could put it back in
I thnk I removed it when trying to get multiplatform working
Why don’t you just create a private val config = Config() in your test file ?
b
yeah, i could create one big one i guess. i was creating one with just the configuration for each test case
it is nice to have the config close to the test...makes things like typos easier to spot when they're right next to eachother (since this involves freeform strings)
l
Won't you get overloaded with different classes and configurations once your test suite start growing?
I'm a fan of having things doing one specific thing, but when writing a lot of tests, this gets messy for me
b
does this not only add to the test context specific to this test/scope?
i assumed since it's defined on
ShouldScope
it was only local to that
l
I believe it is specific, yes, but my point is that creating so many specific stuff may become cumbersome for you to code, won't it?
b
i think i'm missing what you mean...doing it this way saved me code from how i'd do it without this way. if by "specific" you mean: custom and non-idiomatic for kotlintest, which therefore may go away..then yes i'd agree, that's why i was asking.
l
What I mean is that I don't usually write code this way. I usually just hardcode the boolean inside the test and let go of it instead of creating some abstractions to make it more specific
No issues with your approach
I'm just questioning if you don't feel that when your codebase grows, this configs will get a little bit hard to manage
b
oh, well in this case the parsing of a config value is what i'm testing
l
Ah, I see