3

我有以下课程,我想编写一些 Spec 测试用例,但我对它真的很陌生,我不知道如何开始。我的班级确实像这样:

class Board{

  val array = Array.fill(7)(Array.fill(6)(None:Option[Coin]))

  def move(x:Int, coin:Coin) {
    val y = array(x).indexOf(None)
    require(y >= 0) 
    array(x)(y) = Some(coin)
   }

  def apply(x: Int, y: Int):Option[Coin] = 
     if (0 <= x && x < 7 && 0 <= y && y < 6) array(x)(y)
     else None

  def winner: Option[Coin] = winner(Cross).orElse(winner(Naught))

  private def winner(coin:Coin):Option[Coin] = {
    val rows = (0 until 6).map(y => (0 until 7).map( x => apply(x,y)))
    val cols = (0 until 7).map(x => (0 until 6).map( y => apply(x,y)))
    val dia1 = (0 until 4).map(x => (0 until 6).map( y => apply(x+y,y)))
    val dia2 = (3 until 7).map(x => (0 until 6).map( y => apply(x-y,y)))

    val slice = List.fill(4)(Some(coin))
    if((rows ++ cols ++ dia1 ++ dia2).exists(_.containsSlice(slice))) 
      Some(coin)
    else None
  }  

  override def toString = {
    val string = new StringBuilder
    for(y <- 5 to 0 by -1; x <- 0 to 6){
        string.append(apply(x, y).getOrElse("_"))
        if (x == 6) string.append ("\n") 
        else string.append("|")
    }
    string.append("0 1 2 3 4 5 6\n").toString
  }
}

谢谢!

4

2 回答 2

4

我只能赞同 Daniel 的建议,因为您最终会通过使用 TDD 获得更实用的 API。

我还认为您的应用程序可以通过 specs2 和ScalaCheck的混合进行很好的测试。这里是一个规范的草案,让你开始:

  import org.specs2._
  import org.scalacheck.{Arbitrary, Gen}

  class TestSpec extends Specification with ScalaCheck { def is =

    "moving a coin in a column moves the coin to the nearest empty slot" ! e1^
    "a coin wins if"                                                     ^
      "a row contains 4 consecutive coins"                               ! e2^
      "a column contains 4 consecutive coins"                            ! e3^
      "a diagonal contains 4 consecutive coins"                          ! e4^
                                                                         end

    def e1 = check { (b: Board, x: Int, c: Coin) =>
      try { b.move(x, c) } catch { case e => () }
      // either there was a coin before somewhere in that column
      // or there is now after the move
      (0 until 6).exists(y => b(x, y).isDefined)
    }

    def e2 = pending
    def e3 = pending
    def e4 = pending

    /**
     * Random data for Coins, x position and Board
     */
    implicit def arbitraryCoin: Arbitrary[Coin]     = Arbitrary { Gen.oneOf(Cross,       Naught) }
    implicit def arbitraryXPosition: Arbitrary[Int] = Arbitrary { Gen.choose(0, 6) }
    implicit def arbitraryBoardMove: Arbitrary[(Int, Coin)]   = Arbitrary {
      for {
        coin <- arbitraryCoin.arbitrary
        x    <- arbitraryXPosition.arbitrary
      } yield (x, coin)
    }
    implicit def arbitraryBoard: Arbitrary[Board]   = Arbitrary {
      for {
        moves <- Gen.listOf1(arbitraryBoardMove.arbitrary)
      } yield {
        val board = new Board
        moves.foreach { case (x, coin) => 
          try { board.move(x, coin) } catch { case e => () }}
          board
      }
    }


  }

  object Cross extends Coin {
    override def toString = "x"
  }
  object Naught extends Coin {
    override def toString = "o"
  }
  sealed trait Coin

e1我实现的属性不是真实的,因为它并没有真正检查我们是否将硬币移动到最近的空槽,这是您的代码和 API 所建议的。x您还需要更改生成的数据,以便使用和交替生成板o。这应该是学习如何使用 ScalaCheck 的好方法!

于 2012-01-10T21:56:39.907 回答
-1

我建议你把所有的代码都扔掉——好吧,把它保存在某个地方,但是使用 TDD 从零开始。

Specs2站点有很多关于如何编写测试的示例,但使用 TDD(测试驱动设计)来完成。至少可以说,事后添加测试是次优的。

所以,想想最简单的特性你想处理的最简单的情况,为此编写一个测试,看到它失败,编写代码来修复它。如有必要,重构,并重复下一个最简单的情况。

如果您需要有关如何进行 TDD 的一般帮助,我衷心赞同Clean Coders上提供的有关 TDD 的视频。至少,请看第二部分 Bob Martin 写了一个完整的类 TDD 风格,从设计到结束。

如果您知道如何进行一般测试,但对 Scala 或 Specs 感到困惑,请更具体地说明您的问题是什么。

于 2012-01-10T15:32:13.347 回答