Giovanni Azua Giovanni Azua - 2 months ago 11
Scala Question

How to chain function arguments in Scala?

I'd like to define my tests using the following function (ignore some dependencies for now e.g.

fixture1
etc they are at the bottom):

multiTest("my test name", fixture1) { case (x: Double, y: Int, z: String) =>
// test body
}


and
multiTest
is defined in my base custom
FunSpecLike
subclass as:

def multiTest(testName: String, fixture: FixtureTable)(fun: => Unit)(implicit pos: source.Position): Unit = {
val heading = fixture.heading
fixture.values.foreach { tuple =>
it(autoGenerateDesc(heading, tuple)) {
fun tuple // <<<<<< how can I pass the tuple to the definition above?
}
}
}


How can I push the tuple to the function?

Some of the missing pieces are:

case class FixtureTable(heading: Map[String, String], values: Seq[Any])
// tableFor generates the permutations of all paramater values
val fixture1 : FixtureTable = tableFor(
("x", List(1e-1, 1e-2)),
("y", List(1, 2, 3)),
("z", List("a", "b")))

Answer

As it is, you can't. Change multitest to

def multiTest(testName: String, fixture: FixtureTable)(fun: Any => Unit)(implicit pos: source.Position): Unit = {
    val heading = fixture.heading
    fixture.values.foreach { tuple =>
        it(autoGenerateDesc(heading, tuple)) {
            fun(tuple)
        }
    }
}

or even better

case class FixtureTable[A](heading: Map[String, String], values: Seq[A])
def multiTest[A](testName: String, fixture: FixtureTable[A])(fun: A => Unit)(implicit pos: source.Position): Unit = {
    val heading = fixture.heading
    fixture.values.foreach { tuple =>
        it(autoGenerateDesc(heading, tuple)) {
            fun(tuple)
        }
    }
}

but it would be harder to write the tableFor function for this case.