Byte-snake-engine/cases_test.go
Torben d378759d58
DEV-1096 - add a new "pipeline" concept (#67)
* add a new "pipeline" concept

- added new Pipeline type which is a series of stages
- added a global registry to facilitate plugin architecture
- 100% test coverage

* Refactor rulesets to provide and use Pipeline

* fix copypasta comments

* fix lint for unused method

* include game over stages in ruleset pipelines

* clean up unused private standard methods

* remove unused private methods in squad ruleset

* remove unused private methods in royale ruleset

* refactor: pipeline clone + return next board state

* YAGNI: remove unused Append

* refactor: improve stage names

* add no-op behavior to stages for initial state

* refactor: no-op decision within stage functions

* remove misleading comment that isn't true

* dont bother checking for init in gameover stages

* remove redundant test

* refactor: provide a combined ruleset/pipeline type

* fix: movement no-op for GameOver check

IsGameOver needs to run pipeline, move snakes needs to no-op for that

* add test coverage

* refactor: improve stage names and use constants

* add Error method

Support error checking before calling Execute()

* update naming to be American style

* panic when overwriting stages in global registry

* rename "Error" method and improve docs

* use testify lib for panic assertion

* remove redundant food stage

* use ruleset-specific logic for game over checks

* re-work Pipeline errors

* rework errors again

* add defensive check for zero length snake

* use old logic which checks current state, not next

* add warning about how PipelineRuleset checks for game over
2022-04-19 15:52:57 -07:00

49 lines
1.3 KiB
Go

package rules
import (
"testing"
"github.com/stretchr/testify/require"
)
type gameTestCase struct {
name string
prevState *BoardState
moves []SnakeMove
expectedError error
expectedState *BoardState
}
func (gc *gameTestCase) clone() *gameTestCase {
return &gameTestCase{
name: gc.name,
expectedError: gc.expectedError,
moves: append([]SnakeMove{}, gc.moves...),
prevState: gc.prevState.Clone(),
expectedState: gc.expectedState.Clone(),
}
}
// requireValidNextState requires that the ruleset produces a valid next state
func (gc *gameTestCase) requireValidNextState(t *testing.T, r Ruleset) {
t.Helper()
t.Run(gc.name, func(t *testing.T) {
t.Helper()
prev := gc.prevState.Clone() // clone to protect against mutation (so we can ru-use test cases)
nextState, err := r.CreateNextBoardState(prev, gc.moves)
require.Equal(t, gc.expectedError, err)
if gc.expectedState != nil {
require.Equal(t, gc.expectedState.Width, nextState.Width)
require.Equal(t, gc.expectedState.Height, nextState.Height)
require.Equal(t, gc.expectedState.Food, nextState.Food)
require.Equal(t, gc.expectedState.Snakes, nextState.Snakes)
require.Equal(t, gc.expectedState.Hazards, nextState.Hazards)
}
})
}
func mockSnakeMoves() []SnakeMove {
return []SnakeMove{
{ID: "test-mock-move", Move: "mocked"},
}
}