mchess-server/chess/board_test.go
2023-06-25 16:11:29 +02:00

72 lines
1.9 KiB
Go

package chess
import (
"mchess_server/types"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_CheckMove_validPawnMove(t *testing.T) {
var board = make(Board)
board[types.Coordinate{Col: 1, Row: 1}] = Pawn{Color: types.White}
board[types.Coordinate{Col: 1, Row: 5}] = King{Color: types.White}
board[types.Coordinate{Col: 8, Row: 5}] = King{Color: types.Black}
move := types.Move{
StartSquare: types.Coordinate{Col: 1, Row: 1},
EndSquare: types.Coordinate{Col: 1, Row: 2},
}
good, _ := board.CheckMove(move)
assert.True(t, good)
}
func Test_CheckMove_invalidPawnMoves(t *testing.T) {
var board = make(Board)
board[types.Coordinate{Col: 2, Row: 5}] = Pawn{Color: types.White}
board[types.Coordinate{Col: 1, Row: 5}] = King{Color: types.White}
board[types.Coordinate{Col: 7, Row: 5}] = Queen{Color: types.Black}
board[types.Coordinate{Col: 8, Row: 5}] = King{Color: types.Black}
move := types.Move{
StartSquare: types.Coordinate{Col: 2, Row: 5},
EndSquare: types.Coordinate{Col: 2, Row: 6},
}
t.Run("pawn is blocked", func(t *testing.T) {
testBoard := board.getCopyOfBoard()
testBoard[types.Coordinate{Col: 2, Row: 6}] = Pawn{Color: types.Black}
legalMove, _ := testBoard.CheckMove(move)
assert.False(t, legalMove)
})
t.Run("king of moving color is in check after move", func(t *testing.T) {
good, _ := board.CheckMove(move)
assert.False(t, good)
})
}
func Test_CheckMove_validPromotion(t *testing.T) {
var board Board = make(Board)
board[types.Coordinate{Col: 1, Row: 7}] = Pawn{Color: types.White}
board[types.Coordinate{Col: 1, Row: 1}] = King{Color: types.White}
board[types.Coordinate{Col: 8, Row: 7}] = King{Color: types.Black}
shortName := types.Queen
move := types.Move{
StartSquare: types.Coordinate{Col: 1, Row: 7},
EndSquare: types.Coordinate{Col: 1, Row: 8},
PromotionToPiece: &shortName,
}
good, reason := board.CheckMove(move)
assert.Empty(t, reason)
assert.True(t, good)
}