如何在Scala中链接函数参数?

时间:2016-09-28 10:05:58

标签: scala scalatest

我想使用以下函数定义我的测试(暂时忽略一些依赖项,例如它们位于底部的fixture1等):

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

multiTest在我的基本自定义FunSpecLike子类中定义为:

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

如何将元组推送到函数?

一些缺失的部分是:

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")))

1 个答案:

答案 0 :(得分:1)

实际上,你不能。将multitest更改为

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

甚至更好

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

但是为这种情况编写tableFor函数会更难。