2

Writing tests using String Spec:

class stl : StringSpec() {

    init {

        "triangle.stl" {
             ...
        }
    }
}

Is it possible to retrieve "triangle.stl" within the lambda expression?

elect
  • 6,765
  • 10
  • 53
  • 119

2 Answers2

3

It doesn't look like StringSpec exposes this information but you can extend StringSpec to do so. e.g.:

class Spec : StringSpec() {
    init {
        "triangle.stl" { testCase ->
            println(testCase.name)
        }
    }

    operator fun String.invoke(test: (TestCase) -> Unit): TestCase {
        var tc: TestCase? = null
        tc = invoke(fun() { test(tc!!) })
        return tc
    }
}

Or to avoid function conflicts with the exsting String.invoke you could extend it with your own syntax. e.g.:

class Spec : StringSpec() {
    init {
        "triangle.stl" testCase {
            println(name)
        }
    }

    infix fun String.testCase(test: TestCase.() -> Unit): TestCase {
        var tc: TestCase? = null
        tc = invoke { test(tc!!) }
        return tc
    }
}
mfulton26
  • 29,956
  • 6
  • 64
  • 88
1

You would have to store a reference to the string yourself. Something like

class stl : StringSpec() {
    init {
        val spek = "triangle.stl"
        spek {
             // use spek in here
        }
    }
}
Ruckus T-Boom
  • 4,566
  • 1
  • 28
  • 42