Implemented TestCreateNextBoardState (#15)

This commit is contained in:
sjbcastro 2020-05-28 19:26:44 +01:00 committed by GitHub
parent 2d62a58c9b
commit 70a8107a6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 1 deletions

View file

@ -185,6 +185,11 @@ func (r *StandardRuleset) CreateNextBoardState(prevState *BoardState, moves []Sn
}
func (r *StandardRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
for i := 0; i < len(b.Snakes); i++ {
if len(b.Snakes[i].Body) == 0 {
return errors.New("found snake with zero size body")
}
}
if len(moves) < len(b.Snakes) {
return errors.New("not enough snake moves")
}

View file

@ -287,7 +287,108 @@ func TestPlaceFood(t *testing.T) {
}
func TestCreateNextBoardState(t *testing.T) {
// TODO
tests := []struct {
prevState *BoardState
moves []SnakeMove
expectedError error
expectedState *BoardState
}{
{
&BoardState{
Width: 10,
Height: 10,
Snakes: []Snake{
{
ID: "one",
Body: []Point{{1, 1}, {1, 2}},
Health: 100,
},
{
ID: "two",
Body: []Point{{3, 4}, {3, 3}},
Health: 100,
},
},
Food: []Point{{0, 0}, {1, 0}},
},
[]SnakeMove{},
errors.New("not enough snake moves"),
nil,
},
{
&BoardState{
Width: 10,
Height: 10,
Snakes: []Snake{
{
ID: "one",
Body: []Point{{1, 1}, {1, 2}},
Health: 100,
},
{
ID: "two",
Body: []Point{},
Health: 100,
},
},
Food: []Point{{0, 0}, {1, 0}},
},
[]SnakeMove{
{ID: "one", Move: MoveUp},
{ID: "two", Move: MoveDown},
},
errors.New("found snake with zero size body"),
nil,
},
{
&BoardState{
Width: 10,
Height: 10,
Snakes: []Snake{
{
ID: "one",
Body: []Point{{1, 1}, {1, 2}},
Health: 100,
},
{
ID: "two",
Body: []Point{{3, 4}, {3, 3}},
Health: 100,
},
},
Food: []Point{{0, 0}, {1, 0}},
},
[]SnakeMove{
{ID: "one", Move: MoveUp},
{ID: "two", Move: MoveDown},
},
nil,
&BoardState{
Width: 10,
Height: 10,
Snakes: []Snake{
{
ID: "one",
Body: []Point{{1, 0}, {1, 1}, {1, 1}},
Health: 100,
},
{
ID: "two",
Body: []Point{{3, 5}, {3, 4}},
Health: 99,
},
},
Food: []Point{{0, 0}},
},
},
}
r := StandardRuleset{}
for _, test := range tests {
nextState, err := r.CreateNextBoardState(test.prevState, test.moves)
require.Equal(t, err, test.expectedError)
require.Equal(t, nextState, test.expectedState)
}
}
func TestMoveSnakes(t *testing.T) {