Skip to content

Commit

Permalink
tests rewrite to munit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 17, 2023
1 parent 3f91c33 commit 90111f1
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 518 deletions.
464 changes: 203 additions & 261 deletions test-kit/src/test/scala/AntichessVariantTest.scala

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions test-kit/src/test/scala/AtomicVariantTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class AtomicVariantTest extends ChessSpecs:
val successGame = game flatMap (_.playMoves((Square.F6, Square.F5), (Square.G4, Square.H3)))

successGame must beRight.like { game =>
game.situation.check === Check.Yes
game.situation.check must_== Check.Yes
}

"Can move into discovered check in order to explode the opponent's king" in:
Expand All @@ -262,7 +262,7 @@ class AtomicVariantTest extends ChessSpecs:
val successGame = game flatMap (_.playMoves((Square.B5, Square.D7)))

successGame must beRight.like { game =>
game.situation.check === Check.No
game.situation.check must_== Check.No
}

"It should not be possible to explode a piece, exploding a piece next to it which would result in a check" in:
Expand Down Expand Up @@ -546,7 +546,7 @@ class AtomicVariantTest extends ChessSpecs:
.toVector
val (game, steps, error) = chess.Replay.gameMoveWhileValid(sans, Atomic.initialFen, Atomic)
error must beNone
steps.size === sans.size
steps.size must_== sans.size

"castlings" should:

Expand Down
6 changes: 3 additions & 3 deletions test-kit/src/test/scala/BoardTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class BoardTest extends ChessSpecs:

"allow a piece to be placed" in:
board.place(White - Rook, E3) must beSome:
(_: Board)(E3) mustEqual Option(White - Rook)
(_: Board)(E3) must_== Option(White - Rook)

"allow a piece to be taken" in:
board take A1 must beSome:
(_: Board)(A1) must beNone

"allow a piece to move" in:
board.move(E2, E4) must beSome:
(_: Board)(E4) mustEqual Option(White - Pawn)
(_: Board)(E4) must_== Option(White - Pawn)

"not allow an empty position to move" in:
board.move(E5, E6) must beNone
Expand All @@ -75,7 +75,7 @@ class BoardTest extends ChessSpecs:
_.place(White - Pawn, A3),
_.move(A2, A4)
) must beSome:
(_: Board)(A4) mustEqual Option(White - Pawn)
(_: Board)(A4) must_== Option(White - Pawn)

"fail on bad actions chain" in:
makeEmptyBoard.seq(
Expand Down
2 changes: 1 addition & 1 deletion test-kit/src/test/scala/Chess960Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Chess960Test extends ChessSpecs:
"""

Reader.full(pgn) must beRight.like { case Reader.Result.Complete(replay) =>
replay.state.situation.legalMoves.find(_.castles).map(_.toUci) === Some(
replay.state.situation.legalMoves.find(_.castles).map(_.toUci) must_== Some(
format.Uci.Move(Square.E1, Square.B1)
)
}
8 changes: 7 additions & 1 deletion test-kit/src/test/scala/ChessTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ trait ChessTestCommon:
def movesAt(s: Square): List[Move] =
sit.moves.getOrElse(s, Nil)

def fenToGame(positionString: EpdFen, variant: Variant) =
def fenToGameEither(positionString: EpdFen, variant: Variant): Either[String, Game] =
Fen
.read(variant, positionString)
.map: sit =>
Expand Down Expand Up @@ -117,6 +117,9 @@ trait ChessTest extends munit.FunSuite with ChessTestCommon:
def assertCloseTo[T](a: T, b: T, delta: Double)(using n: Numeric[T])(using Location) =
assert(isCloseTo(a, b, delta), s"$a is not close to $b by $delta")

def fenToGame(positionString: EpdFen, variant: Variant)(using Location): Game =
fenToGameEither(positionString, variant).get

private def isCloseTo[T](a: T, b: T, delta: Double)(using n: Numeric[T])(using Location) =
(n.toDouble(a) - n.toDouble(b)).abs <= delta

Expand All @@ -143,6 +146,9 @@ end ChessTest

trait ChessSpecs extends Specification with EitherMatchers with ChessTestCommon:

def fenToGame(positionString: EpdFen, variant: Variant): Either[String, Game] =
fenToGameEither(positionString, variant)

def bePoss(poss: Square*) = // : Matcher[Option[Iterable[square]]] =
beSome: (p: Iterable[Square]) =>
sortPoss(p.toList).map(_.key) must_== sortPoss(poss.toList).map(_.key)
Expand Down
24 changes: 12 additions & 12 deletions test-kit/src/test/scala/HashTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,51 @@ class HashTest extends ChessSpecs:
"match on the starting position" in:
val fen = EpdFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("463b96181691fc9c")
hash(game.situation) must_== hexToBytes("463b96181691fc9c")

"match after 1. e4" in:
val fen = EpdFen("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("823c9b50fd114196")
hash(game.situation) must_== hexToBytes("823c9b50fd114196")

"match after 1. e4 d5" in:
val fen = EpdFen("rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("0756b94461c50fb0")
hash(game.situation) must_== hexToBytes("0756b94461c50fb0")

"match after 1. e4 d5 2. e5" in:
val fen = EpdFen("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("662fafb965db29d4")
hash(game.situation) must_== hexToBytes("662fafb965db29d4")

"match after 1. e4 d5 2. e5 f5" in:
// note that en-passant matters
val fen = EpdFen("rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("22a48b5a8e47ff78")
hash(game.situation) must_== hexToBytes("22a48b5a8e47ff78")

"match after 1. e4 d5 2. e5 f5 3. Ke2" in:
// 3. Ke2 forfeits castling rights
val fen = EpdFen("rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPPKPPP/RNBQ1BNR b kq - 1 3")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("652a607ca3f242c1")
hash(game.situation) must_== hexToBytes("652a607ca3f242c1")

"match after 1. e4 d5 2. e5 f5 3. Ke2 Kf7" in:
val fen = EpdFen("rnbq1bnr/ppp1pkpp/8/3pPp2/8/8/PPPPKPPP/RNBQ1BNR w - - 2 4")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("00fdd303c946bdd9")
hash(game.situation) must_== hexToBytes("00fdd303c946bdd9")

"match after 1. a4 b5 2. h4 b4 3. c4" in:
// again, note en-passant matters
val fen = EpdFen("rnbqkbnr/p1pppppp/8/8/PpP4P/8/1P1PPPP1/RNBQKBNR b KQkq c3 0 3")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("3c8123ea7b067637")
hash(game.situation) must_== hexToBytes("3c8123ea7b067637")

"match after 1. a4 b5 2. h4 b4 3. c4 bxc3 4. Ra3" in:
// 4. Ra3 partially forfeits castling rights
val fen = EpdFen("rnbqkbnr/p1pppppp/8/8/P6P/R1p5/1P1PPPP1/1NBQKBNR b Kkq - 1 4")
val game = fenToGame(fen, Standard).toOption.get
hash(game.situation) mustEqual hexToBytes("5c3f9b829b279560")
hash(game.situation) must_== hexToBytes("5c3f9b829b279560")

"Hasher" should:

Expand Down Expand Up @@ -140,7 +140,7 @@ class HashTest extends ChessSpecs:
val situationAfter = Fen.read(Crazyhouse, fenAfter).get
val hashAfter = hash(situationAfter)

hashAfterMove mustEqual hashAfter
hashAfterMove must_== hashAfter

"be consistent when king is captured in antichess" in:
val fen = EpdFen("rnbqkb1r/ppp1pppp/3p1n2/1B6/8/4P3/PPPP1PPP/RNBQK1NR w KQkq - 2 3")
Expand All @@ -153,7 +153,7 @@ class HashTest extends ChessSpecs:
val situationAfter = Fen.read(Antichess, fenAfter).get
val hashAfter = hash(situationAfter)

hashAfterMove mustEqual hashAfter
hashAfterMove must_== hashAfter

"be consistent when rook is exploded in atomic" in:
val fen = EpdFen("rnbqkb1r/ppppp1pp/5p1n/6N1/8/8/PPPPPPPP/RNBQKB1R w KQkq - 2 3")
Expand All @@ -166,7 +166,7 @@ class HashTest extends ChessSpecs:
val situationAfter = Fen.read(Atomic, fenAfter).get
val hashAfter = hash(situationAfter)

hashAfterMove mustEqual hashAfter
hashAfterMove must_== hashAfter

"prod 5 Three-Check games accumulate hash" in:
val gameMoves = format.pgn.Fixtures.prod5threecheck.map { g =>
Expand Down
Loading

0 comments on commit 90111f1

Please sign in to comment.