【问题标题】:How to factor out main method of Scala objects into a shared location?如何将Scala对象的主要方法分解为共享位置?
【发布时间】:2012-04-20 05:45:11
【问题描述】:

在几个 Scala 对象中,我定义了一个调用 runTests 的 main 方法,它是 Test 中的一个抽象方法。有没有办法将主要方法分解到一个公共位置(特征或其他解决方案),以便我仍然可以通过键入 ctrl-F11在 Eclipse 中运行测试>?

这是我目前拥有的,

https://github.com/janekdb/stair-chess/blob/master/src/chess/model/BoardModelTest.scala

object BoardModelTest extends Test with TestUtils {

  def main(args: Array[String]) {
    runTests
  }

  def runTests {
  ...

https://github.com/janekdb/stair-chess/blob/master/src/test/Test.scala

trait Test {

  def runTests: Unit
  ...

【问题讨论】:

  • 您是否有理由不使用 ScalaTest 或 specs2 并推出自己的测试框架?
  • @tolitius 我目前的重点是学习 Scala 基础知识,所以我为自己做的越多,我就越能实现我的目标。稍后我将关注库和框架的采用。感谢您的关注。

标签: scala refactoring main


【解决方案1】:

我的这台电脑上没有 Eclipse,所以我无法测试它是否可以与 Ctrl+F11 一起使用,但我认为你想要一个 self-type:

trait Main {
  self: Test => 
  def main(args: Array[String]) {
    runTests
  }
}

然后,您只需在 Test 特征之后将其混合:

object BoardModelTest extends Test with TestUtils with Main {
  def runTests {}
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多