fix: improve TerminalUI coverage and make MoveCommand/ResetCommand immutable
**TerminalUI Coverage Fix:** - Added explicit test for empty input case (lines 64-65 previously uncovered) - Test "TerminalUI should explicitly handle empty input by re-prompting" validates that multiple empty inputs are properly handled by re-prompting - UI module coverage improved to near-100% **MoveCommand Immutability (Anti-pattern Fix):** - Changed MoveCommand fields from var to val: moveResult, previousBoard, previousHistory, previousTurn - Changed ResetCommand fields from var to val: previousBoard, previousHistory, previousTurn - Updated GameEngine to use .copy() instead of direct mutation when updating command state (lines 88, 95, 103, 112) - Removed 3 edge-case tests that relied on command mutation (now impossible with immutable fields) **MoveCommand Immutability Tests:** - Added MoveCommandImmutabilityTest to verify: - Fields cannot be mutated after creation - equals/hashCode respect immutability invariant - .copy() creates new instances with updated values All tests pass; 100% core coverage maintained. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -46,6 +46,30 @@ class TerminalUITest extends AnyFunSuite with Matchers {
|
||||
output.split("White's turn.").length should be > 2
|
||||
}
|
||||
|
||||
test("TerminalUI should explicitly handle empty input by re-prompting") {
|
||||
val in = new ByteArrayInputStream("\n\nq\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
|
||||
val engine = new GameEngine()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withIn(in) {
|
||||
Console.withOut(out) {
|
||||
ui.start()
|
||||
}
|
||||
}
|
||||
|
||||
val output = out.toString
|
||||
// With two empty inputs, prompt should appear at least 4 times:
|
||||
// 1. Initial board display
|
||||
// 2. After first empty input
|
||||
// 3. After second empty input
|
||||
// 4. Before quit
|
||||
val promptCount = output.split("White's turn.").length
|
||||
promptCount should be >= 4
|
||||
output should include("Game over. Goodbye!")
|
||||
}
|
||||
|
||||
test("TerminalUI printPrompt should include undo and redo hints if engine returns true") {
|
||||
val in = new ByteArrayInputStream("\nq\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
|
||||
Reference in New Issue
Block a user