我正在Coursera 上学习Scala 中的函数式编程原理课程。有一个Peano 数字的实现示例,如下所示:
abstract class Nat {
def isZero: Boolean
def predecessor: Nat
def successor: Nat = new Succ(this)
def +(that: Nat): Nat
def -(that: Nat): Nat
}
object Zero extends Nat {
def isZero = true
def predecessor: Nat = throw new Error("0.predecessor")
def +(that: Nat): Nat = that
def -(that: Nat): Nat = if (that.isZero) this else throw new Error("negative number")
}
class Succ(n: Nat) extends Nat {
def isZero: Boolean = false
def predecessor: Nat = n
def +(that: Nat): Nat = new Succ(n + that)
def -(that: Nat): Nat = if (that.isZero) this else n - that.predecessor
}
我写了几个单元测试。大多数通行证,但以下通行证 - 以天真的方式编写 - 因明显原因而失败(不同实例的比较):
trait Fixture {
val one = new Succ(Zero)
val two = new Succ(one)
}
test("successor of zero is one") {
new Fixture {
assert(Zero.successor == one)
}
}
test("successor of one is two") {
new Fixture {
assert(one.successor == two)
}
}
test("one plus zero is one") {
new Fixture {
assert((one + Zero) === one)
}
}
test("one plus one is two") {
new Fixture {
assert((one + one) === two)
}
}
我的问题是:应该如何实施单元测试来成功测试 peano 数字的 + 和 - 操作?
以防万一,在这里您可以找到剩余的单元测试。