Add RulesetError test case.

Remove old comments.
This commit is contained in:
bvanvugt 2020-12-11 10:05:19 -08:00
parent 2cbf8884bf
commit accb598e29
4 changed files with 41 additions and 36 deletions

View file

@ -1,10 +1,36 @@
package rules package rules
type RulesetError string
func (err RulesetError) Error() string { return string(err) }
const ( const (
MoveUp = "up" MoveUp = "up"
MoveDown = "down" MoveDown = "down"
MoveRight = "right" MoveRight = "right"
MoveLeft = "left" MoveLeft = "left"
BoardSizeSmall = 7
BoardSizeMedium = 11
BoardSizeLarge = 19
SnakeMaxHealth = 100
SnakeStartSize = 3
// bvanvugt - TODO: Just return formatted strings instead of codes?
NotEliminated = ""
EliminatedByCollision = "snake-collision"
EliminatedBySelfCollision = "snake-self-collision"
EliminatedByOutOfHealth = "out-of-health"
EliminatedByHeadToHeadCollision = "head-collision"
EliminatedByOutOfBounds = "wall-collision"
// TODO - Error consts
ErrorTooManySnakes = RulesetError("too many snakes for fixed start positions")
ErrorNoRoomForSnake = RulesetError("not enough space to place snake")
ErrorNoRoomForFood = RulesetError("not enough space to place food")
ErrorNoMoveFound = RulesetError("move not provided for snake")
ErrorZeroLengthSnake = RulesetError("snake is length zero")
) )
type Point struct { type Point struct {
@ -37,7 +63,3 @@ type Ruleset interface {
CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)
IsGameOver(state *BoardState) (bool, error) IsGameOver(state *BoardState) (bool, error)
} }
type RulesetError string
func (err RulesetError) Error() string { return string(err) }

12
ruleset_test.go Normal file
View file

@ -0,0 +1,12 @@
package rules
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRulesetError(t *testing.T) {
err := (error)(RulesetError("test error string"))
require.Equal(t, "test error string", err.Error())
}

View file

@ -1,7 +1,6 @@
package rules package rules
import ( import (
//"errors"
"math/rand" "math/rand"
"sort" "sort"
) )
@ -11,33 +10,6 @@ type StandardRuleset struct {
MinimumFood int32 MinimumFood int32
} }
const (
BoardSizeSmall = 7
BoardSizeMedium = 11
BoardSizeLarge = 19
SnakeMaxHealth = 100
SnakeStartSize = 3
// bvanvugt - TODO: Just return formatted strings instead of codes?
NotEliminated = ""
EliminatedByCollision = "snake-collision"
EliminatedBySelfCollision = "snake-self-collision"
EliminatedByOutOfHealth = "out-of-health"
EliminatedByHeadToHeadCollision = "head-collision"
EliminatedByOutOfBounds = "wall-collision"
// TODO - Error consts
ErrorTooManySnakes = RulesetError("too many snakes for fixed start positions")
ErrorNoRoomForSnake = RulesetError("not enough space to place snake")
ErrorNoRoomForFood = RulesetError("not enough space to place food")
ErrorNoMoveFound = RulesetError("move not provided for snake")
// TODO: These two error codes seem equivalent, Do we only need one ?
ErrorSizeZeroBody = RulesetError("found snake with zero size body")
ErrorZeroLengthSnake = RulesetError("snake is length zero")
)
func (r *StandardRuleset) CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error) { func (r *StandardRuleset) CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error) {
initialBoardState := &BoardState{ initialBoardState := &BoardState{
Height: height, Height: height,
@ -263,7 +235,7 @@ func (r *StandardRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
} }
if len(snake.Body) == 0 { if len(snake.Body) == 0 {
return ErrorSizeZeroBody return ErrorZeroLengthSnake
} }
moveFound := false moveFound := false
for _, move := range moves { for _, move := range moves {

View file

@ -1,7 +1,6 @@
package rules package rules
import ( import (
//"errors"
"math" "math"
"math/rand" "math/rand"
"testing" "testing"
@ -513,7 +512,7 @@ func TestCreateNextBoardState(t *testing.T) {
{ID: "one", Move: MoveUp}, {ID: "one", Move: MoveUp},
{ID: "two", Move: MoveDown}, {ID: "two", Move: MoveDown},
}, },
ErrorSizeZeroBody, ErrorZeroLengthSnake,
nil, nil,
}, },
{ {
@ -940,7 +939,7 @@ func TestMoveSnakesWrongID(t *testing.T) {
r := StandardRuleset{} r := StandardRuleset{}
err := r.moveSnakes(b, moves) err := r.moveSnakes(b, moves)
require.Equal(t, ErrorNoMoveFound, err) // TODO: @bvanvugt is this a place where an "==" comparision should be used ? require.Equal(t, ErrorNoMoveFound, err)
} }
func TestMoveSnakesNotEnoughMoves(t *testing.T) { func TestMoveSnakesNotEnoughMoves(t *testing.T) {