elect
elect

Reputation: 7190

Is it possible to retrieve the string within the lambda expression?

Writing tests using String Spec:

class stl : StringSpec() {

    init {

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

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

Upvotes: 2

Views: 136

Answers (2)

mfulton26
mfulton26

Reputation: 31234

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
    }
}

Upvotes: 3

Ruckus T-Boom
Ruckus T-Boom

Reputation: 4786

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
        }
    }
}

Upvotes: 1

Related Questions