gpt4 book ai didi

unit-testing - Scala 规范单元测试

转载 作者:行者123 更新时间:2023-12-04 04:39:28 25 4
gpt4 key购买 nike

我有下面的类(class),我想写一些 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
}
}

谢谢!

最佳答案

我只能赞成 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 建议。您还需要更改生成的数据,以便交替使用 xo 生成 Boards。这应该是学习如何使用 ScalaCheck 的好方法!

关于unit-testing - Scala 规范单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com