Is there a way to refactor this to something that ...
# kotest
v
Is there a way to refactor this to something that does not produce eye-cancer?
Copy code
class SchemaTest : FunSpec({
    beforeSpec {
        (it as SchemaTest).validate = Ajv(Options(strict = true)).compile(
            JSON.parse(readFile(schemaFile, utf8))
        )
    }

    test("foo") {
        readFile(path.join(dataDir, "foo.yml"), utf8) should (testCase.spec as SchemaTest).beValid()
    }
}) {
    lateinit var validate: ValidateFunction

    fun beValid(): Matcher<String> {
        return Matcher { data ->
            MatcherResult(
                validate(YAML.parse(data)),
                { "validation failed:\n${JSON.stringify(validate.errors, null, 4)}" },
                { "should have failed validation but passed" },
            )
        }
    }
}
🙂
✅ 1
l
Copy code
class SchemaTest : FunSpec({
  beforeSpec {
    validate = Ajv(xxxxxxX)
  }

  test("foo") {
    readFile(path) should beValid()
  }
})
lateinit var validate: ValidateFunction
fun beValid(): Matcher<String> = Matcher { data ->}
I thought something like this
But I don't know enough of your code to make real changes
I'd ditch using the Spec Class and leave it outside of the class
Then you can use everything inside constructor, or should be able to
v
Ah, right, thanks. I started with having those two outside the spec class, but for some reason had to move it inside. I don't know what the reason was, but obviously I rewrote some things so that now it works like that. 🙂
Maybe you also have ideas for my other two questions? 🙂
l
Oh, maybe, I didn't see them 😂
v
Right above this one 🙂