mchess-server/chess/rook_test.go
2023-06-27 22:32:24 +02: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.GetAllNonBlockedMoves(board, types.Coordinate{Col: 5, Row: 5})
assert.Len(t, squares, 14)
})
t.Run("free 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.GetAllNonBlockedMoves(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)
})
}