tests for SetupBoard and UpdateBoard and a bugfix

This commit is contained in:
Rob O'Dwyer 2022-05-11 16:30:08 -07:00
parent 1dcc1352cc
commit 6fa2da2f01
2 changed files with 162 additions and 1 deletions

View file

@ -33,10 +33,57 @@ func UpdateBoard(mapID string, previousBoardState *rules.BoardState, settings ru
nextBoardState := previousBoardState.Clone() nextBoardState := previousBoardState.Clone()
editor := NewBoardStateEditor(nextBoardState, settings.Rand()) editor := NewBoardStateEditor(nextBoardState, settings.Rand())
err = gameMap.SetupBoard(previousBoardState, settings, editor) err = gameMap.UpdateBoard(previousBoardState, settings, editor)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return nextBoardState, nil return nextBoardState, nil
} }
// An implementation of GameMap that just does predetermined placements, for testing.
type StubMap struct {
Id string
SnakePositions map[string]rules.Point
Food []rules.Point
Hazards []rules.Point
Error error
}
func (m StubMap) ID() string {
return m.Id
}
func (StubMap) Meta() Metadata {
return Metadata{}
}
func (m StubMap) SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error {
if m.Error != nil {
return m.Error
}
for _, snake := range initialBoardState.Snakes {
head := m.SnakePositions[snake.ID]
editor.PlaceSnake(snake.ID, []rules.Point{head, head, head}, 100)
}
for _, food := range m.Food {
editor.AddFood(food)
}
for _, hazard := range m.Hazards {
editor.AddHazard(hazard)
}
return nil
}
func (m StubMap) UpdateBoard(previousBoardState *rules.BoardState, settings rules.Settings, editor Editor) error {
if m.Error != nil {
return m.Error
}
for _, food := range m.Food {
editor.AddFood(food)
}
for _, hazard := range m.Hazards {
editor.AddHazard(hazard)
}
return nil
}

114
maps/helpers_test.go Normal file
View file

@ -0,0 +1,114 @@
package maps
import (
"errors"
"testing"
"github.com/BattlesnakeOfficial/rules"
"github.com/stretchr/testify/require"
)
func TestSetupBoard_NotFound(t *testing.T) {
_, err := SetupBoard("does_not_exist", rules.Settings{}, 10, 10, []string{})
require.EqualError(t, err, rules.ErrorMapNotFound.Error())
}
func TestSetupBoard_Error(t *testing.T) {
testMap := StubMap{
Id: t.Name(),
Error: errors.New("bad map update"),
}
RegisterMap(testMap.ID(), testMap)
_, err := SetupBoard(testMap.ID(), rules.Settings{}, 10, 10, []string{})
require.EqualError(t, err, "bad map update")
}
func TestSetupBoard(t *testing.T) {
testMap := StubMap{
Id: t.Name(),
SnakePositions: map[string]rules.Point{
"1": {X: 3, Y: 4},
"2": {X: 6, Y: 2},
},
Food: []rules.Point{
{X: 1, Y: 1},
{X: 5, Y: 3},
},
Hazards: []rules.Point{
{X: 3, Y: 5},
{X: 2, Y: 2},
},
}
RegisterMap(testMap.ID(), testMap)
boardState, err := SetupBoard(testMap.ID(), rules.Settings{}, 10, 10, []string{"1", "2"})
require.NoError(t, err)
require.Len(t, boardState.Snakes, 2)
require.Equal(t, rules.Snake{
ID: "1",
Body: []rules.Point{{X: 3, Y: 4}, {X: 3, Y: 4}, {X: 3, Y: 4}},
Health: rules.SnakeMaxHealth,
}, boardState.Snakes[0])
require.Equal(t, rules.Snake{
ID: "2",
Body: []rules.Point{{X: 6, Y: 2}, {X: 6, Y: 2}, {X: 6, Y: 2}},
Health: rules.SnakeMaxHealth,
}, boardState.Snakes[1])
require.Equal(t, []rules.Point{{X: 1, Y: 1}, {X: 5, Y: 3}}, boardState.Food)
require.Equal(t, []rules.Point{{X: 3, Y: 5}, {X: 2, Y: 2}}, boardState.Hazards)
}
func TestUpdateBoard(t *testing.T) {
testMap := StubMap{
Id: t.Name(),
SnakePositions: map[string]rules.Point{
"1": {X: 3, Y: 4},
"2": {X: 6, Y: 2},
},
Food: []rules.Point{
{X: 1, Y: 1},
{X: 5, Y: 3},
},
Hazards: []rules.Point{
{X: 3, Y: 5},
{X: 2, Y: 2},
},
}
RegisterMap(testMap.ID(), testMap)
previousBoardState := &rules.BoardState{
Turn: 0,
Food: []rules.Point{{X: 0, Y: 1}},
Hazards: []rules.Point{{X: 3, Y: 4}},
Snakes: []rules.Snake{
{
ID: "1",
Health: 100,
Body: []rules.Point{
{X: 6, Y: 4},
{X: 6, Y: 3},
{X: 6, Y: 2},
},
},
},
}
boardState, err := UpdateBoard(testMap.ID(), previousBoardState, rules.Settings{})
require.NoError(t, err)
require.Len(t, boardState.Snakes, 1)
require.Equal(t, rules.Snake{
ID: "1",
Body: []rules.Point{{X: 6, Y: 4}, {X: 6, Y: 3}, {X: 6, Y: 2}},
Health: rules.SnakeMaxHealth,
}, boardState.Snakes[0])
require.Equal(t, []rules.Point{{X: 0, Y: 1}, {X: 1, Y: 1}, {X: 5, Y: 3}}, boardState.Food)
require.Equal(t, []rules.Point{{X: 3, Y: 4}, {X: 3, Y: 5}, {X: 2, Y: 2}}, boardState.Hazards)
}