2016-09-28 50 views
0

我想使用下面的函數(忽略現在如fixture1等,他們都在底部的一些依賴)來定義我的測試:如何在Scala中鏈接函數參數?

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

這是回答您的問題嗎? http://stackoverflow.com/questions/1987820/how-to-apply-a-function-to-a-tuple –

+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功能。

+0

非常好,非常感謝! –