Skip to content

Commit

Permalink
specs2->munit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 18, 2023
1 parent 17f8738 commit 300847c
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 340 deletions.
87 changes: 41 additions & 46 deletions test-kit/src/test/scala/BerserkTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,48 @@ package chess

import scala.language.implicitConversions

class BerserkTest extends ChessSpecs:
class BerserkTest extends ChessTest:

import clockConv.given

def whiteBerserk(minutes: Int, seconds: Int) =
Clock(minutes * 60, seconds).goBerserk(White).remainingTime(White).centis * .01

given Conversion[Int, Clock.LimitSeconds] = Clock.LimitSeconds(_)
given Conversion[Int, Clock.IncrementSeconds] = Clock.IncrementSeconds(_)

"berserkable" should:
"yep" in:
Clock.Config(60 * 60, 0).berserkable must_== true
Clock.Config(1 * 60, 0).berserkable must_== true
Clock.Config(60 * 60, 60).berserkable must_== true
Clock.Config(1 * 60, 0).berserkable must_== true
"nope" in:
Clock.Config(0 * 60, 1).berserkable must_== false
Clock.Config(0 * 60, 10).berserkable must_== false
"berserk flags" should:
"white" in:
Clock(60, 0).berserked(White) must_== false
Clock(60, 0).goBerserk(White).berserked(White) must_== true
"black" in:
Clock(60, 0).berserked(Black) must_== false
Clock(60, 0).goBerserk(Black).berserked(Black) must_== true
"initial time penalty, no increment" should:
"10+0" in:
whiteBerserk(10, 0) must_== 5 * 60
"5+0" in:
whiteBerserk(5, 0) must_== 2.5 * 60
"3+0" in:
whiteBerserk(3, 0) must_== 1.5 * 60
"1+0" in:
whiteBerserk(1, 0) must_== 0.5 * 60
"initial time penalty, with increment" should:
"4+4" in:
whiteBerserk(4, 4) must_== 2 * 60
"3+2" in:
whiteBerserk(3, 2) must_== 1.5 * 60
"2+10" in:
whiteBerserk(2, 10) must_== 2 * 60
"10+5" in:
whiteBerserk(10, 5) must_== 5 * 60
"10+2" in:
whiteBerserk(10, 2) must_== 5 * 60
"1+1" in:
whiteBerserk(1, 1) must_== 0.5 * 60
"1+3" in:
whiteBerserk(1, 3) must_== 1 * 60
"1+5" in:
whiteBerserk(1, 5) must_== 1 * 60
test("berserkable: yep"):
assertEquals(Clock.Config(60 * 60, 0).berserkable , true)
assertEquals(Clock.Config(1 * 60, 0).berserkable , true)
assertEquals(Clock.Config(60 * 60, 60).berserkable , true)
assertEquals(Clock.Config(1 * 60, 0).berserkable , true)
test("berserkable: nope"):
assertEquals(Clock.Config(0 * 60, 1).berserkable , false)
assertEquals(Clock.Config(0 * 60, 10).berserkable , false)
test("berserk flags: white"):
assertEquals(Clock(60, 0).berserked(White) , false)
assertEquals(Clock(60, 0).goBerserk(White).berserked(White) , true)
test("berserk flags: black"):
assertEquals(Clock(60, 0).berserked(Black) , false)
assertEquals(Clock(60, 0).goBerserk(Black).berserked(Black) , true)
test("initial time penalty, no increment: 10+0"):
assertEquals(whiteBerserk(10, 0) , 5 * 60d)
test("initial time penalty, no increment: 5+0"):
assertEquals(whiteBerserk(5, 0) , 2.5 * 60d)
test("initial time penalty, no increment: 3+0"):
assertEquals(whiteBerserk(3, 0) , 1.5 * 60d)
test("initial time penalty, no increment: 1+0"):
assertEquals(whiteBerserk(1, 0) , 0.5 * 60d)
test("initial time penalty, with increment: 4+4"):
assertEquals(whiteBerserk(4, 4) , 2 * 60d)
test("initial time penalty, with increment: 3+2"):
assertEquals(whiteBerserk(3, 2) , 1.5 * 60d)
test("initial time penalty, with increment: 2+10"):
assertEquals(whiteBerserk(2, 10) , 2 * 60d)
test("initial time penalty, with increment: 10+5"):
assertEquals(whiteBerserk(10, 5) , 5 * 60d)
test("initial time penalty, with increment: 10+2"):
assertEquals(whiteBerserk(10, 2) , 5 * 60d)
test("initial time penalty, with increment: 1+1"):
assertEquals(whiteBerserk(1, 1) , 0.5 * 60d)
test("initial time penalty, with increment: 1+3"):
assertEquals(whiteBerserk(1, 3) , 1 * 60d)
test("initial time penalty, with increment: 1+5"):
assertEquals(whiteBerserk(1, 5) , 1 * 60d)
28 changes: 13 additions & 15 deletions test-kit/src/test/scala/BishopTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package chess
import scala.language.implicitConversions
import Square.*

class BishopTest extends ChessSpecs:
class BishopTest extends ChessTest:

"a bishop" should:

"not move to positions that are occupied by the same colour" in:
val board = """
test("not move to positions that are occupied by the same colour"):
val board = """
k B
Expand All @@ -18,9 +16,9 @@ N B P
PPPPPPPP
NBQKBNR
"""
board destsFrom C4 must bePoss(
board,
"""
assertEquals(
visualDests(board, board destsFrom C4),
"""
k B x
x
x x
Expand All @@ -30,10 +28,10 @@ N B P
PPPPPPPP
NBQKBNR
"""
)
)

"capture opponent pieces" in:
val board = """
test("capture opponent pieces"):
val board = """
k B
q
p
Expand All @@ -43,9 +41,9 @@ N B P
PPPPPPPP
NBQKBNR
"""
board destsFrom C4 must bePoss(
board,
"""
assertEquals(
visualDests(board, board destsFrom C4),
"""
k B
x
x x
Expand All @@ -55,4 +53,4 @@ N B P
PPPPPPPP
NBQKBNR
"""
)
)
61 changes: 33 additions & 28 deletions test-kit/src/test/scala/BoardTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package chess
import scala.language.implicitConversions
import Square.*

class BoardTest extends ChessSpecs:
class BoardTest extends ChessTest:

val board = makeBoard

"a board" should:

"position pieces correctly" in:
board.pieces must havePairs(
test("position pieces correctly"):
assertEquals(
board.pieces,
Map(
A1 -> (White - Rook),
B1 -> (White - Knight),
C1 -> (White - Bishop),
Expand Down Expand Up @@ -44,42 +44,47 @@ class BoardTest extends ChessSpecs:
G8 -> (Black - Knight),
H8 -> (Black - Rook)
)
)

"have pieces by default" in:
board.allPieces must not beEmpty
test("have pieces by default"):
assertNot(board.allPieces.isEmpty)

"have castling rights by default" in:
board.history.castles == Castles.init
test("have castling rights by default"):
assertEquals(board.history.castles, Castles.init)

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

"allow a piece to be taken" in:
board take A1 must beSome:
(_: Board)(A1) must beNone
test("allow a piece to be taken"):
board take A1 assertSome: b =>
assertEquals(b(A1), None)

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

"not allow an empty position to move" in:
board.move(E5, E6) must beNone
test("not allow an empty position to move"):
assertEquals(board.move(E5, E6), None)

"not allow a piece to move to an occupied position" in:
board.move(A1, A2) must beNone
test("not allow a piece to move to an occupied position"):
assertEquals(board.move(A1, A2), None)

"allow chaining actions" in:
makeEmptyBoard.seq(
test("allow chaining actions"):
makeEmptyBoard
.seq(
_.place(White - Pawn, A2),
_.place(White - Pawn, A3),
_.move(A2, A4)
) must beSome:
(_: Board)(A4) must_== Option(White - Pawn)
)
.assertSome: b =>
assertEquals(b(A4), Option(White - Pawn))

"fail on bad actions chain" in:
test("fail on bad actions chain"):
assertEquals(
makeEmptyBoard.seq(
_.place(White - Pawn, A2),
_.place(White - Pawn, A3),
_.move(B2, B4)
) must beNone
),
None
)
102 changes: 102 additions & 0 deletions test-kit/src/test/scala/CastlingKingSideTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package chess

import scala.language.implicitConversions
import Square.*
import variant.FromPosition

class CastlingKingSideTest extends ChessTest:

import compare.dests

val goodHist = """
PPPPPPPP
R QK R"""
val badHist = goodHist updateHistory (_ withoutCastles White)
test("impossible"):
assertEquals(goodHist.place(White.bishop, F1).flatMap(_ destsFrom E1), Set())
assertEquals(goodHist.place(White.knight, G1).flatMap(_ destsFrom E1), Set(F1))
assertEquals(badHist destsFrom E1, Set(F1))
val board960 = """
PPPPPPPP
RQK R """.chess960 withHistory History.castle(White, kingSide = true, queenSide = true)
assertEquals(board960.place(White.bishop, D1).flatMap(_ destsFrom C1), Set())
assertEquals(board960.place(White.knight, F1).flatMap(_ destsFrom C1), Set(D1))
test("possible"):
val game = Game(goodHist, White)
assertEquals(game.board destsFrom E1, Set(F1, G1, H1))
assertGame(
game.playMove(E1, G1).get,
"""
PPPPPPPP
R Q RK """
)
val board: Board = """
PPPPP
B KR""".chess960
val g2 = Game(board, White)
assertEquals(board destsFrom G1, Set(F1, H1))
assertGame(
g2.playMove(G1, H1).get,
"""
PPPPP
B RK """
)
test("chess960 close kingside with 2 rooks around"):
val board: Board = """
PPPPPPPP
RKRBB """.chess960
assertEquals(board destsFrom B1, Set())
test("chess960 close queenside"):
val board: Board = """
PPPPPPPP
RK B""".chess960
val game = Game(board, White)
assertEquals(board destsFrom B1, Set(A1, C1))
assertGame(
game.playMove(B1, A1).get,
"""
PPPPPPPP
KR B"""
)
test("chess960 close queenside as black"):
val game = Game(
"""
b rkr q
p pppppp
p n
K""".chess960,
Black
)
assertEquals(game.board destsFrom E8, Set(D8, F8))
assertGame(
game.playMove(E8, D8).get,
"""
bkr r q
p pppppp
p n
K"""
)
test("from position with chess960 castling"):
val game = Game(
makeBoard(
"""rk r
pppbnppp
p n
P Pp
P q
R NP
PP PP
KNQRB""",
FromPosition
),
Black
)
assertEquals(game.board destsFrom B8, Set(A8, C8, E8))
Loading

0 comments on commit 300847c

Please sign in to comment.