mchess-server/chess/rook_test.go
Marco af993876fd Introduce check if the game has ended (checkmate/stalemate)
Introduce checkmate check and send out 'gameEnded' message
2024-01-17 23:06:48 +01:00

72 lines
2.0 KiB
Go

package chess
import (
"mchess_server/types"
"testing"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)
func Test_Rook_GetNonBlockedSquares(t *testing.T) {
t.Run("free row and column", func(t *testing.T) {
board := newBoard()
rook := Rook{Color: types.Black}
board.position[types.Coordinate{Col: 5, Row: 5}] = rook
squares := rook.GetAllNonBlockedSquares(board, types.Coordinate{Col: 5, Row: 5})
assert.Len(t, squares, 14)
})
t.Run("blocked row and column", func(t *testing.T) {
board := newBoard()
rook := Rook{Color: types.Black}
rookCoordinate := types.Coordinate{Col: 5, Row: 5}
board.position[rookCoordinate] = rook
board.position[types.Coordinate{Col: 3, Row: 5}] = Pawn{Color: types.White}
board.position[types.Coordinate{Col: 5, Row: 6}] = Pawn{Color: types.White}
board.position[types.Coordinate{Col: 6, Row: 5}] = Pawn{Color: types.Black}
squares := rook.GetAllNonBlockedSquares(board, rookCoordinate)
squaresOnLeft := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
return square.Row == rookCoordinate.Row && square.Col < rookCoordinate.Col
})
assert.Equal(t,
[]types.Coordinate{
{Col: 4, Row: 5},
{Col: 3, Row: 5},
},
squaresOnLeft)
squaresOnRight := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
return square.Row == rookCoordinate.Row && square.Col > rookCoordinate.Col
})
assert.Equal(t,
[]types.Coordinate{},
squaresOnRight)
squaresAbove := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
return square.Col == rookCoordinate.Col && square.Row > rookCoordinate.Row
})
assert.Equal(t,
[]types.Coordinate{
{Col: 5, Row: 6},
},
squaresAbove)
squaresBelow := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
return square.Col == rookCoordinate.Col && square.Row < rookCoordinate.Row
})
assert.Equal(t,
[]types.Coordinate{
{Col: 5, Row: 4},
{Col: 5, Row: 3},
{Col: 5, Row: 2},
{Col: 5, Row: 1},
},
squaresBelow)
})
}