Files
Chess/testdata/integration_test.go
MSWS 1b197dc642 fix: Restore and enhance move generation and board logic
- Enhance `testdata/integration_test.go` with improved logging and subtest naming for better clarity.
- Refine castle move legality checks in `board/move.go` to ensure accurate column-based validation.

[testdata/integration_test.go]
- Updated log statement in `testData` function to include both the description and FEN string.
- Modified subtest naming to replace slashes in the FEN string with periods.
[board/move.go]
- Adjusted conditional logic for castle-related moves to check for different column coordinates when determining legality.
- No functional changes outside of castle move handling.
2025-03-22 18:10:09 -07:00

96 lines
1.8 KiB
Go

package testdata
import (
"encoding/json"
"io"
"os"
"strings"
"testing"
"github.com/msws/chess/board"
)
type TestData struct {
Description string `json:"description"`
Tests []TestCases `json:"testCases"`
}
type TestCases struct {
Start TestStart `json:"start"`
Expected []TestExpectation `json:"expected"`
}
type TestStart struct {
Fen string `json:"fen"`
Description string `json:"description"`
}
type TestExpectation struct {
Move string `json:"move"`
Fen string `json:"fen"`
}
func TestJSON(t *testing.T) {
data, err := os.ReadDir(".")
if err != nil {
t.Fatal(err)
}
for _, entry := range data {
t.Run(entry.Name(), func(t *testing.T) {
jsonData, err := os.Open(entry.Name())
if err != nil {
t.Error(err)
return
}
defer jsonData.Close()
byteData, err := io.ReadAll(jsonData)
if err != nil {
t.Error(err)
return
}
testJson(t, byteData)
})
}
}
func testJson(t *testing.T, bytes []byte) {
var data TestData
json.Unmarshal(bytes, &data)
t.Log(data.Description)
testData(t, data)
}
func testData(t *testing.T, data TestData) {
for _, test := range data.Tests {
t.Log(test.Start.Description, test.Start.Fen)
t.Run(strings.ReplaceAll(test.Start.Fen, "/", "."), func(t *testing.T) {
testCase(t, test.Start, test.Expected)
})
}
}
func testCase(t *testing.T, start TestStart, expected []TestExpectation) {
t.Run("MoveCount", func(t *testing.T) {
testMoveCount(t, start, expected)
})
}
func testMoveCount(t *testing.T, start TestStart, expected []TestExpectation) {
board, err := board.FromFEN(start.Fen)
if err != nil {
t.Error(err)
return
}
moves := board.GetMoves()
if len(moves) != len(expected) {
t.Errorf("expected %d moves, got %d", len(expected), len(moves))
}
}