https://kotlinlang.org logo
Title
s

snowe

01/29/2019, 6:52 AM
I previously had this,
fun Suite.mapsTo(expected: String) {
        val testObject by memoized<Any>()
        it("test value") {
            pp(testObject)
            outContent shouldRenderLike expected
        }
    }

    fun testObject(obj: Any?) {
        val testObject by memoized { obj }
    }
    
    describe("tiny object should") {
        context("render a single field") {
            testObject(TinyObject(1))
            mapsTo(
                """
                TinyObject(
                  int = 1
                )
                """
            )
        }
    }
but I find it very messy, and I can't use an infix....
this is the cleanest version I can get
fun Suite.validateTestOutput(testObject: Any?, expected: String) {
        it("test value") {
            pp(testObject)
            outContent shouldRenderLike expected
        }
    }

    describe("tiny object should") {
        infix fun Any.mapsTo(expected: String) {
            validateTestOutput(this, expected)
        }

        context("render a single field") {
            TinyObject(1) mapsTo """
                TinyObject(
                  int = 1
                )
                """

        }
    }
a

Alan Evans

01/29/2019, 3:43 PM
infix
requires exactly two parameters. But you seem to need the
Suite
, the
testObject
and the
expected
s

snowe

01/29/2019, 4:13 PM
yeah I need a double infix.
Any
and
Suite
. if I had that then this would be easy.
a

Alan Evans

01/29/2019, 6:25 PM
X to Y mapsTo Z
...
Passing a
Pair
. You needn't use
to
, can make your own, you also needn't use
Pair
, any data class will do.
TinyObject(1) validateWith this thatItMapsTo """...
s

snowe

01/29/2019, 6:47 PM
Hmm. nice idea. I was looking into writing a custom Spek class, following these directions (https://github.com/spekframework/spek/issues/115), but if I can’t get that to work then I’ll look into your suggestion. Biggest thing is that I still have to repeat the stuff every test class. I’d like to just have it in one location.