Skip to content

Commit

Permalink
tests for SetupBoard and UpdateBoard and a bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
robbles committed May 11, 2022
1 parent 1dcc135 commit 6fa2da2
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
49 changes: 48 additions & 1 deletion maps/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,57 @@ func UpdateBoard(mapID string, previousBoardState *rules.BoardState, settings ru
nextBoardState := previousBoardState.Clone()
editor := NewBoardStateEditor(nextBoardState, settings.Rand())

err = gameMap.SetupBoard(previousBoardState, settings, editor)
err = gameMap.UpdateBoard(previousBoardState, settings, editor)
if err != nil {
return nil, err
}

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 changes: 114 additions & 0 deletions maps/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6fa2da2

Please sign in to comment.