Skip to content

Commit

Permalink
feat(test): added test for MoveService.getQueenMove
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Guimaraes <[email protected]>
  • Loading branch information
gabrielg2020 committed Nov 12, 2024
1 parent 72549ad commit 681d87c
Showing 1 changed file with 131 additions and 32 deletions.
163 changes: 131 additions & 32 deletions api/service/move_service/move_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,38 +1086,137 @@ func Test_MoveService_getRookMove(t *testing.T) {
}
}

//func Test_MoveService_getQueenMove(t *testing.T) {
// testCases := []struct {
// name string
// fromY, fromX int
// setupMock func(m *mocks.MockChessboardEntity)
// expectedMoves []entity.MoveEntityInterface
// expectedError error
// }{
// {
//
// },
// }
//
// for _, tc := range testCases {
// t.Run(tc.name, func(t *testing.T) {
// // Arrange
// mockChessboard := new(mocks.MockChessboardEntity)
// tc.setupMock(mockChessboard)
//
// // Act
// moves, err := getQueenMove(5, tc.fromY, tc.fromX, mockChessboard)
//
// // Assert
// if tc.expectedError != nil {
// assert.EqualError(t, err, tc.expectedError.Error())
// } else {
// assert.NoError(t, err)
// assertMovesEqual(t, tc.expectedMoves, moves)
// }
// })
// }
//}
func Test_MoveService_getQueenMove(t *testing.T) {
testCases := []struct {
name string
fromY, fromX int
setupMock func(m *mocks.MockChessboardEntity)
expectedMoves []entity.MoveEntityInterface
expectedError error
}{
{
name: "Queen in the middle of the board",
fromY: 4,
fromX: 4,
setupMock: func(m *mocks.MockChessboardEntity) {
positions := []struct{ toY, toX int }{
{5, 5}, {6, 6}, {7, 7}, {8, 8}, // Bottom right
{5, 3}, {6, 2}, {7, 1}, {8, 0}, // Bottom left
{3, 5}, {2, 6}, {1, 7}, {0, 8}, // Top right
{3, 3}, {2, 2}, {1, 1}, {0, 0}, {-1, -1}, // Top left
{4, 5}, {4, 6}, {4, 7}, {4, 8}, // Right
{4, 3}, {4, 2}, {4, 1}, {4, 0}, {4, -1}, // Left
{5, 4}, {6, 4}, {7, 4}, {8, 4}, // Down
{3, 4}, {2, 4}, {1, 4}, {0, 4}, {-1, 4}, // Up
}
for _, pos := range positions {
if pos.toY < 0 || pos.toX < 0 || pos.toY > 7 || pos.toX > 7 {
m.On("IsWithinBounds", pos.toY, pos.toX).Return(false)
} else {
m.On("IsWithinBounds", pos.toY, pos.toX).Return(true)
m.On("IsSquareEmpty", pos.toY, pos.toX).Return(true, nil)
}
}
},
expectedMoves: massCreateMoveEntities(4, 4, []struct{ toY, toX int }{
{5, 5}, {6, 6}, {7, 7}, // Bottom right
{5, 3}, {6, 2}, {7, 1}, // Bottom left
{3, 5}, {2, 6}, {1, 7}, // Top right
{3, 3}, {2, 2}, {1, 1}, {0, 0}, // Top left
{4, 5}, {4, 6}, {4, 7}, // Right
{4, 3}, {4, 2}, {4, 1}, {4, 0}, // Left
{5, 4}, {6, 4}, {7, 4}, // Down
{3, 4}, {2, 4}, {1, 4}, {0, 4}, // Up
}, 0, false, false, 0),
expectedError: nil,
},
{
name: "Queen in corner",
fromY: 0,
fromX: 0,
setupMock: func(m *mocks.MockChessboardEntity) {
positions := []struct{ toY, toX int }{
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, // Bottom right
{1, -1}, // Bottom left
{-1, 1}, // Top right
{-1, -1}, // Top left
{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0, 8}, // Right
{0, -1}, // Left
{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, // Down
{-1, 0}, // Up
}
for _, pos := range positions {
if pos.toY < 0 || pos.toX < 0 || pos.toY > 7 || pos.toX > 7 {
m.On("IsWithinBounds", pos.toY, pos.toX).Return(false)
} else {
m.On("IsWithinBounds", pos.toY, pos.toX).Return(true)
m.On("IsSquareEmpty", pos.toY, pos.toX).Return(true, nil)
}
}
},
expectedMoves: massCreateMoveEntities(0, 0, []struct{ toY, toX int }{
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, // Bottom right
{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, // Right
{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, // Down
}, 0, false, false, 0),
expectedError: nil,
},
{
name: "Queen in middle with 3 friendly pieces blocking on up and 5 opponent pieces on bottom, right, left",
fromY: 4,
fromX: 4,
setupMock: func(m *mocks.MockChessboardEntity) {
positions := []struct{ toY, toX int }{
{5, 5}, // Bottom right
{5, 3}, // Bottom left
{3, 5}, // Top right
{3, 3}, // Top left
{4, 5}, // Right
{4, 3}, // Left
{5, 4}, // Down
{3, 4}, // Up
}
for _, pos := range positions {
m.On("IsWithinBounds", pos.toY, pos.toX).Return(true)
m.On("IsSquareEmpty", pos.toY, pos.toX).Return(false, nil)
if pos.toY < 4 {
m.On("IsOpponent", 5, pos.toY, pos.toX).Return(false, nil)
} else {
m.On("IsOpponent", 5, pos.toY, pos.toX).Return(true, nil)
m.On("GetPiece", pos.toY, pos.toX).Return(-1, nil)
}
}
},
expectedMoves: []entity.MoveEntityInterface{
newMockMoveEntity(4, 4, 5, 5, 0, false, false, -1),
newMockMoveEntity(4, 4, 3, 5, 0, false, false, -1),
newMockMoveEntity(4, 4, 5, 4, 0, false, false, -1),
newMockMoveEntity(4, 4, 3, 4, 0, false, false, -1),
newMockMoveEntity(4, 4, 4, 5, 0, false, false, -1),
},
expectedError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Arrange
mockChessboard := new(mocks.MockChessboardEntity)
tc.setupMock(mockChessboard)

// Act
moves, err := getQueenMove(5, tc.fromY, tc.fromX, mockChessboard)

// Assert
if tc.expectedError != nil {
assert.EqualError(t, err, tc.expectedError.Error())
} else {
assert.NoError(t, err)
assertMovesEqual(t, tc.expectedMoves, moves)
}
})
}
}

//func Test_MoveService_getKingMove(t *testing.T) {
// testCases := []struct {
Expand Down

0 comments on commit 681d87c

Please sign in to comment.