Syn*_*sso 3 testing specs scala
给定游戏的类型层次结构,强烈区分下一轮的转弯:
trait Game
trait BlackToPlay extends Game {
def move(p: BlackPiece, s: Square): Either[FinishedGame, WhiteToPlay]
}
trait WhiteToPlay extends Game {
def move(p: WhitePiece, s: Square): Either[FinishedGame, BlackToPlay]
}
Run Code Online (Sandbox Code Playgroud)
我可以在不诉诸反思的情况下做出以下重要断言吗?
"A game with white to play" should {
"not allow black to play" in {
// an instance of whiteToPlay should not
// have the `move(BlackPiece, Square)` method.
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我尝试实施@Martin的解决方案不起作用.对这里有什么不妥的想法?来自REPL:
scala> class B() {
| def b(s: String) = s
| }
defined class B
scala> val b = new B()
b: B = B@420e44
scala> b.c("")
<console>:8: error: value c is not a member of B
b.c("")
^
scala> b match {
| case _: { def c(s: String) } => false
| case _ => true
| }
warning: there were unchecked warnings; re-run with -unchecked for details
res7: Boolean = false
Run Code Online (Sandbox Code Playgroud)
res7应该是真的,因为b结构类型不应该匹配{ def c(s: String) }
您不测试系统已经保证的类型.实际上,类型系统已经是对程序某些属性的测试.
你可以进一步测试你保证某种属性的类型(比如没有玩家连续两次移动),但是这种东西仅限于像Agda和Coq这样的语言.