|
| 1 | +package game |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// Minimal GUI struct and constructor for demonstration. |
| 11 | +// Replace with your actual implementation. |
| 12 | +type GUI struct { |
| 13 | + CurrentPlayer string |
| 14 | +} |
| 15 | + |
| 16 | +func NewGUI() *GUI { |
| 17 | + return &GUI{CurrentPlayer: "Black"} |
| 18 | +} |
| 19 | + |
| 20 | +func (g *GUI) SwitchPlayer() { |
| 21 | + if g.CurrentPlayer == "Black" { |
| 22 | + g.CurrentPlayer = "White" |
| 23 | + } else { |
| 24 | + g.CurrentPlayer = "Black" |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestGuiInitialState(t *testing.T) { |
| 29 | + gui := NewGUI() |
| 30 | + if gui.CurrentPlayer != "Black" { |
| 31 | + t.Errorf("Expected initial player to be Black, got %v", gui.CurrentPlayer) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestGuiSwitchPlayer(t *testing.T) { |
| 36 | + gui := NewGUI() |
| 37 | + gui.SwitchPlayer() |
| 38 | + if gui.CurrentPlayer != "White" { |
| 39 | + t.Errorf("Expected player to switch to White, got %v", gui.CurrentPlayer) |
| 40 | + } |
| 41 | + gui.SwitchPlayer() |
| 42 | + if gui.CurrentPlayer != "Black" { |
| 43 | + t.Errorf("Expected player to switch back to Black, got %v", gui.CurrentPlayer) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestGuiMultipleSwitches(t *testing.T) { |
| 48 | + gui := NewGUI() |
| 49 | + for i := 0; i < 10; i++ { |
| 50 | + gui.SwitchPlayer() |
| 51 | + } |
| 52 | + expected := "Black" |
| 53 | + if 10%2 != 0 { |
| 54 | + expected = "White" |
| 55 | + } |
| 56 | + if gui.CurrentPlayer != expected { |
| 57 | + t.Errorf("After 10 switches, expected player to be %v, got %v", expected, gui.CurrentPlayer) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestGuiSwitchPlayerAlternates(t *testing.T) { |
| 62 | + gui := NewGUI() |
| 63 | + players := []string{"White", "Black", "White", "Black"} |
| 64 | + for i, expected := range players { |
| 65 | + gui.SwitchPlayer() |
| 66 | + if gui.CurrentPlayer != expected { |
| 67 | + t.Errorf("After %d switches, expected player to be %v, got %v", i+1, expected, gui.CurrentPlayer) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestDrawGridToWriterEmptyBoard(t *testing.T) { |
| 73 | + var b Board |
| 74 | + var buf bytes.Buffer |
| 75 | + gui := Gui{} |
| 76 | + gui.Grid = b |
| 77 | + gui.DrawGridToWriter(&buf, 0, 0) |
| 78 | + output := buf.String() |
| 79 | + |
| 80 | + // Check for all column labels |
| 81 | + for j := 0; j < 9; j++ { |
| 82 | + colLabel := fmt.Sprintf("%c", 'A'+j) |
| 83 | + if !strings.Contains(output, colLabel) { |
| 84 | + t.Errorf("Expected column label %q in output", colLabel) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Check for all row labels |
| 89 | + for i := 1; i <= 9; i++ { |
| 90 | + rowLabel := fmt.Sprintf("%2d", i) |
| 91 | + if !strings.Contains(output, rowLabel) { |
| 92 | + t.Errorf("Expected row label %q in output", rowLabel) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Check for grid rendering characters |
| 97 | + gridChars := []string{"┌", "┐", "└", "┘", "┬", "┴", "├", "┤", "┼", "│", "─"} |
| 98 | + for _, ch := range gridChars { |
| 99 | + if !strings.Contains(output, ch) { |
| 100 | + t.Errorf("Expected grid character %q in output", ch) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Ensure no stones are present |
| 105 | + if strings.Contains(output, "\x1b[1m⚫\x1b[0m") || strings.Contains(output, "\x1b[1m⚪\x1b[0m") { |
| 106 | + t.Errorf("Expected empty board to have no stones, got: %q", output) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestDrawGridToWriterWithBlackStone(t *testing.T) { |
| 111 | + var b Board |
| 112 | + b[0][0] = Black |
| 113 | + var buf bytes.Buffer |
| 114 | + gui := Gui{} |
| 115 | + gui.Grid = b |
| 116 | + gui.DrawGridToWriter(&buf, 0, 0) |
| 117 | + output := buf.String() |
| 118 | + if !strings.Contains(output, "\x1b[1m⚫\x1b[0m") { |
| 119 | + t.Errorf("Expected board to contain a black stone, got: %q", output) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestDrawGridToWriterWithWhiteStone(t *testing.T) { |
| 124 | + var b Board |
| 125 | + b[0][0] = White |
| 126 | + var buf bytes.Buffer |
| 127 | + gui := Gui{} |
| 128 | + gui.Grid = b |
| 129 | + gui.DrawGridToWriter(&buf, 0, 0) |
| 130 | + output := buf.String() |
| 131 | + if !strings.Contains(output, "\x1b[1m⚪\x1b[0m") { |
| 132 | + t.Errorf("Expected board to contain a white stone, got: %q", output) |
| 133 | + } |
| 134 | +} |
0 commit comments