build: migrate to ScalaTest and Scoverage, replacing JaCoCo across modules

This commit is contained in:
2026-03-22 15:28:02 +01:00
parent 5a21e57ca9
commit 551e08cef3
13 changed files with 1357 additions and 143 deletions
+10 -2
View File
@@ -50,12 +50,20 @@ dependencies {
implementation(project(":modules:api"))
testImplementation(platform("org.junit:junit-bom:5.13.4"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.scalatest:scalatest_3:${versions["SCALATEST"]!!}")
testImplementation("org.scalatestplus:junit-5-11_3:${versions["SCALATESTPLUS_JUNIT5"]!!}")
testImplementation("co.helmethair:scalatest-junit-runner:${versions["SCALATEST_JUNIT"]!!}")
}
tasks.test {
useJUnitPlatform()
useJUnitPlatform {
includeEngines("scalatest")
testLogging {
events("passed", "skipped", "failed")
}
}
finalizedBy(tasks.reportScoverage)
}
tasks.reportScoverage {
@@ -0,0 +1,103 @@
package de.nowchess.chess.controller
import de.nowchess.api.board.*
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import java.io.ByteArrayInputStream
class GameControllerTest extends AnyFunSuite with Matchers:
private def sq(f: File, r: Rank): Square = Square(f, r)
private val initial = Board.initial
// ──── processMove ────────────────────────────────────────────────────
test("processMove: 'quit' input returns Quit"):
GameController.processMove(initial, Color.White, "quit") shouldBe MoveResult.Quit
test("processMove: 'q' input returns Quit"):
GameController.processMove(initial, Color.White, "q") shouldBe MoveResult.Quit
test("processMove: quit with surrounding whitespace returns Quit"):
GameController.processMove(initial, Color.White, " quit ") shouldBe MoveResult.Quit
test("processMove: unparseable input returns InvalidFormat"):
GameController.processMove(initial, Color.White, "xyz") shouldBe MoveResult.InvalidFormat("xyz")
test("processMove: valid format but empty square returns NoPiece"):
// E3 is empty in the initial position
GameController.processMove(initial, Color.White, "e3e4") shouldBe MoveResult.NoPiece
test("processMove: piece of wrong color returns WrongColor"):
// E7 has a Black pawn; it is White's turn
GameController.processMove(initial, Color.White, "e7e6") shouldBe MoveResult.WrongColor
test("processMove: geometrically illegal move returns IllegalMove"):
// White pawn at E2 cannot jump three squares to E5
GameController.processMove(initial, Color.White, "e2e5") shouldBe MoveResult.IllegalMove
test("processMove: legal pawn move returns Moved with updated board and flipped turn"):
GameController.processMove(initial, Color.White, "e2e4") match
case MoveResult.Moved(newBoard, captured, newTurn) =>
newBoard.pieceAt(sq(File.E, Rank.R4)) shouldBe Some(Piece.WhitePawn)
newBoard.pieceAt(sq(File.E, Rank.R2)) shouldBe None
captured shouldBe None
newTurn shouldBe Color.Black
case other => fail(s"Expected Moved, got $other")
test("processMove: legal capture returns Moved with the captured piece"):
val captureBoard = Board(Map(
sq(File.E, Rank.R5) -> Piece.WhitePawn,
sq(File.D, Rank.R6) -> Piece.BlackPawn
))
GameController.processMove(captureBoard, Color.White, "e5d6") match
case MoveResult.Moved(newBoard, captured, newTurn) =>
captured shouldBe Some(Piece.BlackPawn)
newBoard.pieceAt(sq(File.D, Rank.R6)) shouldBe Some(Piece.WhitePawn)
newTurn shouldBe Color.Black
case other => fail(s"Expected Moved, got $other")
// ──── gameLoop ───────────────────────────────────────────────────────
private def withInput(input: String)(block: => Unit): Unit =
val stream = ByteArrayInputStream(input.getBytes("UTF-8"))
scala.Console.withIn(stream)(scala.Console.withOut(System.out)(block))
test("gameLoop: 'quit' exits cleanly without exception"):
withInput("quit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: EOF (null readLine) exits via quit fallback"):
withInput(""):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: invalid format prints message and recurses until quit"):
withInput("badmove\nquit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: NoPiece prints message and recurses until quit"):
// E3 is empty in the initial position
withInput("e3e4\nquit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: WrongColor prints message and recurses until quit"):
// E7 has a Black pawn; it is White's turn
withInput("e7e6\nquit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: IllegalMove prints message and recurses until quit"):
withInput("e2e5\nquit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: legal non-capture move recurses with new board then quits"):
withInput("e2e4\nquit\n"):
GameController.gameLoop(Board.initial, Color.White)
test("gameLoop: capture move prints capture message then recurses and quits"):
val captureBoard = Board(Map(
sq(File.E, Rank.R5) -> Piece.WhitePawn,
sq(File.D, Rank.R6) -> Piece.BlackPawn
))
withInput("e5d6\nquit\n"):
GameController.gameLoop(captureBoard, Color.White)
@@ -0,0 +1,43 @@
package de.nowchess.chess.controller
import de.nowchess.api.board.{File, Rank, Square}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class ParserTest extends AnyFunSuite with Matchers:
test("parseMove parses valid 'e2e4'"):
Parser.parseMove("e2e4") shouldBe Some((Square(File.E, Rank.R2), Square(File.E, Rank.R4)))
test("parseMove is case-insensitive"):
Parser.parseMove("E2E4") shouldBe Some((Square(File.E, Rank.R2), Square(File.E, Rank.R4)))
test("parseMove trims leading and trailing whitespace"):
Parser.parseMove(" e2e4 ") shouldBe Some((Square(File.E, Rank.R2), Square(File.E, Rank.R4)))
test("parseMove handles corner squares a1h8"):
Parser.parseMove("a1h8") shouldBe Some((Square(File.A, Rank.R1), Square(File.H, Rank.R8)))
test("parseMove handles corner squares h8a1"):
Parser.parseMove("h8a1") shouldBe Some((Square(File.H, Rank.R8), Square(File.A, Rank.R1)))
test("parseMove returns None for empty string"):
Parser.parseMove("") shouldBe None
test("parseMove returns None for input shorter than 4 chars"):
Parser.parseMove("e2e") shouldBe None
test("parseMove returns None for input longer than 4 chars"):
Parser.parseMove("e2e44") shouldBe None
test("parseMove returns None when from-file is out of range"):
Parser.parseMove("z2e4") shouldBe None
test("parseMove returns None when from-rank is out of range"):
Parser.parseMove("e9e4") shouldBe None
test("parseMove returns None when to-file is out of range"):
Parser.parseMove("e2z4") shouldBe None
test("parseMove returns None when to-rank is out of range"):
Parser.parseMove("e2e9") shouldBe None
@@ -0,0 +1,211 @@
package de.nowchess.chess.logic
import de.nowchess.api.board.{Board, Color, File, Piece, Rank, Square}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class MoveValidatorTest extends AnyFunSuite with Matchers:
private def sq(f: File, r: Rank): Square = Square(f, r)
private def board(entries: (Square, Piece)*): Board = Board(entries.toMap)
// ──── Empty square ───────────────────────────────────────────────────
test("legalTargets returns empty set when no piece at from square"):
MoveValidator.legalTargets(Board.initial, sq(File.E, Rank.R4)) shouldBe empty
// ──── isLegal delegates to legalTargets ──────────────────────────────
test("isLegal returns true for a valid pawn move"):
MoveValidator.isLegal(Board.initial, sq(File.E, Rank.R2), sq(File.E, Rank.R4)) shouldBe true
test("isLegal returns false for an invalid move"):
MoveValidator.isLegal(Board.initial, sq(File.E, Rank.R2), sq(File.E, Rank.R5)) shouldBe false
// ──── Pawn – White ───────────────────────────────────────────────────
test("white pawn on starting rank can move forward one square"):
val b = board(sq(File.E, Rank.R2) -> Piece.WhitePawn)
MoveValidator.legalTargets(b, sq(File.E, Rank.R2)) should contain(sq(File.E, Rank.R3))
test("white pawn on starting rank can move forward two squares"):
val b = board(sq(File.E, Rank.R2) -> Piece.WhitePawn)
MoveValidator.legalTargets(b, sq(File.E, Rank.R2)) should contain(sq(File.E, Rank.R4))
test("white pawn not on starting rank cannot move two squares"):
val b = board(sq(File.E, Rank.R3) -> Piece.WhitePawn)
MoveValidator.legalTargets(b, sq(File.E, Rank.R3)) should not contain sq(File.E, Rank.R5)
test("white pawn is blocked by piece directly in front, and cannot jump over it"):
val b = board(
sq(File.E, Rank.R2) -> Piece.WhitePawn,
sq(File.E, Rank.R3) -> Piece.BlackPawn
)
val targets = MoveValidator.legalTargets(b, sq(File.E, Rank.R2))
targets should not contain sq(File.E, Rank.R3)
targets should not contain sq(File.E, Rank.R4)
test("white pawn on starting rank cannot move two squares if destination square is occupied"):
val b = board(
sq(File.E, Rank.R2) -> Piece.WhitePawn,
sq(File.E, Rank.R4) -> Piece.BlackPawn
)
val targets = MoveValidator.legalTargets(b, sq(File.E, Rank.R2))
targets should contain(sq(File.E, Rank.R3))
targets should not contain sq(File.E, Rank.R4)
test("white pawn can capture diagonally when enemy piece is present"):
val b = board(
sq(File.E, Rank.R2) -> Piece.WhitePawn,
sq(File.D, Rank.R3) -> Piece.BlackPawn
)
MoveValidator.legalTargets(b, sq(File.E, Rank.R2)) should contain(sq(File.D, Rank.R3))
test("white pawn cannot capture diagonally when no enemy piece is present"):
val b = board(sq(File.E, Rank.R2) -> Piece.WhitePawn)
MoveValidator.legalTargets(b, sq(File.E, Rank.R2)) should not contain sq(File.D, Rank.R3)
test("white pawn at A-file does not generate diagonal to the left off the board"):
val b = board(sq(File.A, Rank.R2) -> Piece.WhitePawn)
val targets = MoveValidator.legalTargets(b, sq(File.A, Rank.R2))
targets should contain(sq(File.A, Rank.R3))
targets should contain(sq(File.A, Rank.R4))
targets.size shouldBe 2
// ──── Pawn – Black ───────────────────────────────────────────────────
test("black pawn on starting rank can move forward one and two squares"):
val b = board(sq(File.E, Rank.R7) -> Piece.BlackPawn)
val targets = MoveValidator.legalTargets(b, sq(File.E, Rank.R7))
targets should contain(sq(File.E, Rank.R6))
targets should contain(sq(File.E, Rank.R5))
test("black pawn not on starting rank cannot move two squares"):
val b = board(sq(File.E, Rank.R6) -> Piece.BlackPawn)
MoveValidator.legalTargets(b, sq(File.E, Rank.R6)) should not contain sq(File.E, Rank.R4)
test("black pawn can capture diagonally when enemy piece is present"):
val b = board(
sq(File.E, Rank.R7) -> Piece.BlackPawn,
sq(File.F, Rank.R6) -> Piece.WhitePawn
)
MoveValidator.legalTargets(b, sq(File.E, Rank.R7)) should contain(sq(File.F, Rank.R6))
// ──── Knight ─────────────────────────────────────────────────────────
test("knight in center has 8 possible moves"):
val b = board(sq(File.D, Rank.R4) -> Piece.WhiteKnight)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)).size shouldBe 8
test("knight in corner has only 2 possible moves"):
val b = board(sq(File.A, Rank.R1) -> Piece.WhiteKnight)
MoveValidator.legalTargets(b, sq(File.A, Rank.R1)).size shouldBe 2
test("knight cannot land on own piece"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteKnight,
sq(File.F, Rank.R5) -> Piece.WhiteRook
)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)) should not contain sq(File.F, Rank.R5)
test("knight can capture enemy piece"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteKnight,
sq(File.F, Rank.R5) -> Piece.BlackRook
)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)) should contain(sq(File.F, Rank.R5))
// ──── Bishop ─────────────────────────────────────────────────────────
test("bishop slides diagonally across an empty board"):
val b = board(sq(File.D, Rank.R4) -> Piece.WhiteBishop)
val targets = MoveValidator.legalTargets(b, sq(File.D, Rank.R4))
targets should contain(sq(File.E, Rank.R5))
targets should contain(sq(File.H, Rank.R8))
targets should contain(sq(File.C, Rank.R3))
targets should contain(sq(File.A, Rank.R1))
test("bishop is blocked by own piece and squares beyond are unreachable"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteBishop,
sq(File.F, Rank.R6) -> Piece.WhiteRook
)
val targets = MoveValidator.legalTargets(b, sq(File.D, Rank.R4))
targets should contain(sq(File.E, Rank.R5))
targets should not contain sq(File.F, Rank.R6)
targets should not contain sq(File.G, Rank.R7)
test("bishop captures enemy piece and cannot slide further"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteBishop,
sq(File.F, Rank.R6) -> Piece.BlackRook
)
val targets = MoveValidator.legalTargets(b, sq(File.D, Rank.R4))
targets should contain(sq(File.E, Rank.R5))
targets should contain(sq(File.F, Rank.R6))
targets should not contain sq(File.G, Rank.R7)
// ──── Rook ───────────────────────────────────────────────────────────
test("rook slides orthogonally across an empty board"):
val b = board(sq(File.D, Rank.R4) -> Piece.WhiteRook)
val targets = MoveValidator.legalTargets(b, sq(File.D, Rank.R4))
targets should contain(sq(File.D, Rank.R8))
targets should contain(sq(File.D, Rank.R1))
targets should contain(sq(File.A, Rank.R4))
targets should contain(sq(File.H, Rank.R4))
test("rook is blocked by own piece and squares beyond are unreachable"):
val b = board(
sq(File.A, Rank.R1) -> Piece.WhiteRook,
sq(File.C, Rank.R1) -> Piece.WhitePawn
)
val targets = MoveValidator.legalTargets(b, sq(File.A, Rank.R1))
targets should contain(sq(File.B, Rank.R1))
targets should not contain sq(File.C, Rank.R1)
targets should not contain sq(File.D, Rank.R1)
test("rook captures enemy piece and cannot slide further"):
val b = board(
sq(File.A, Rank.R1) -> Piece.WhiteRook,
sq(File.C, Rank.R1) -> Piece.BlackPawn
)
val targets = MoveValidator.legalTargets(b, sq(File.A, Rank.R1))
targets should contain(sq(File.B, Rank.R1))
targets should contain(sq(File.C, Rank.R1))
targets should not contain sq(File.D, Rank.R1)
// ──── Queen ──────────────────────────────────────────────────────────
test("queen combines rook and bishop movement for 27 squares from d4"):
val b = board(sq(File.D, Rank.R4) -> Piece.WhiteQueen)
val targets = MoveValidator.legalTargets(b, sq(File.D, Rank.R4))
targets should contain(sq(File.D, Rank.R8))
targets should contain(sq(File.H, Rank.R4))
targets should contain(sq(File.H, Rank.R8))
targets should contain(sq(File.A, Rank.R1))
targets.size shouldBe 27
// ──── King ───────────────────────────────────────────────────────────
test("king moves one step in all 8 directions from center"):
val b = board(sq(File.D, Rank.R4) -> Piece.WhiteKing)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)).size shouldBe 8
test("king at corner has only 3 reachable squares"):
val b = board(sq(File.A, Rank.R1) -> Piece.WhiteKing)
MoveValidator.legalTargets(b, sq(File.A, Rank.R1)).size shouldBe 3
test("king cannot capture own piece"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteKing,
sq(File.E, Rank.R4) -> Piece.WhiteRook
)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)) should not contain sq(File.E, Rank.R4)
test("king can capture enemy piece"):
val b = board(
sq(File.D, Rank.R4) -> Piece.WhiteKing,
sq(File.E, Rank.R4) -> Piece.BlackRook
)
MoveValidator.legalTargets(b, sq(File.D, Rank.R4)) should contain(sq(File.E, Rank.R4))
@@ -0,0 +1,43 @@
package de.nowchess.chess.view
import de.nowchess.api.board.Piece
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class PieceUnicodeTest extends AnyFunSuite with Matchers:
test("White King maps to "):
Piece.WhiteKing.unicode shouldBe "\u2654"
test("White Queen maps to ♕"):
Piece.WhiteQueen.unicode shouldBe "\u2655"
test("White Rook maps to "):
Piece.WhiteRook.unicode shouldBe "\u2656"
test("White Bishop maps to ♗"):
Piece.WhiteBishop.unicode shouldBe "\u2657"
test("White Knight maps to "):
Piece.WhiteKnight.unicode shouldBe "\u2658"
test("White Pawn maps to ♙"):
Piece.WhitePawn.unicode shouldBe "\u2659"
test("Black King maps to "):
Piece.BlackKing.unicode shouldBe "\u265A"
test("Black Queen maps to ♛"):
Piece.BlackQueen.unicode shouldBe "\u265B"
test("Black Rook maps to "):
Piece.BlackRook.unicode shouldBe "\u265C"
test("Black Bishop maps to ♝"):
Piece.BlackBishop.unicode shouldBe "\u265D"
test("Black Knight maps to "):
Piece.BlackKnight.unicode shouldBe "\u265E"
test("Black Pawn maps to ♟"):
Piece.BlackPawn.unicode shouldBe "\u265F"
@@ -0,0 +1,41 @@
package de.nowchess.chess.view
import de.nowchess.api.board.{Board, File, Piece, Rank, Square}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
class RendererTest extends AnyFunSuite with Matchers:
test("render contains column header with all file labels"):
Renderer.render(Board.initial) should include("a b c d e f g h")
test("render output begins with the column header"):
Renderer.render(Board.initial) should startWith(" a b c d e f g h")
test("render contains rank labels 1 through 8"):
val output = Renderer.render(Board.initial)
for rank <- 1 to 8 do output should include(s"$rank ")
test("render shows white king unicode symbol for initial board"):
Renderer.render(Board.initial) should include("\u2654")
test("render shows black king unicode symbol for initial board"):
Renderer.render(Board.initial) should include("\u265A")
test("render contains ANSI light-square background code"):
Renderer.render(Board.initial) should include("\u001b[48;5;223m")
test("render contains ANSI dark-square background code"):
Renderer.render(Board.initial) should include("\u001b[48;5;130m")
test("render uses white-piece foreground color for white pieces"):
Renderer.render(Board.initial) should include("\u001b[97m")
test("render uses black-piece foreground color for black pieces"):
Renderer.render(Board.initial) should include("\u001b[30m")
test("render of empty board contains no piece unicode"):
val output = Renderer.render(Board(Map.empty))
output should include("a b c d e f g h")
output should not include "\u2654"
output should not include "\u265A"