Refactoring tests a bit

cat-town
Dan Buch 12 years ago
parent 0ac0b4cab9
commit 078efbe057

@ -30,9 +30,30 @@ func init() {
s.Set(15, 15, 1)
}
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
func newTestGameState() (*GameState, error) {
state := NewGameState(16, 16)
err := state.Import(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
return nil, err
}
return state, nil
}
func newTestGameOfLife() (*GameOfLife, error) {
game := NewGameOfLife(16, 16)
err := game.State.Import(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
return nil, err
}
return game, nil
}
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
return
@ -53,8 +74,7 @@ func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
}
func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
@ -76,8 +96,7 @@ func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) {
}
func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
@ -99,8 +118,7 @@ func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) {
}
func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
@ -122,9 +140,8 @@ func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) {
}
func TestGameOfLifeCanDisplayItself(t *testing.T) {
game := NewGameOfLife(16, 16)
game, err := newTestGameOfLife()
err := game.ImportState(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
t.Error(err)
return
@ -140,14 +157,24 @@ func TestGameOfLifeCanDisplayItself(t *testing.T) {
}
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
game := NewGameOfLife(16, 16)
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
return
}
if game.State.Height() != 16 || game.State.Width() != 16 {
t.Fail()
}
}
func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) {
game := NewGameOfLife(16, 16)
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
return
}
if len(game.State.Rows) < 16 {
t.Fail()
@ -159,8 +186,8 @@ func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) {
}
func TestGameStateCanImportState(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.State.Import(TEST_SIMPLE_INITIAL_STATE)
game, err := newTestGameOfLife()
if err != nil {
t.Fail()
}
@ -187,9 +214,13 @@ func TestGameStateCanImportState(t *testing.T) {
}
func TestGameOfLifeCanImportRandomState(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.ImportRandomState()
game, err := newTestGameOfLife()
if err != nil {
t.Error(err)
return
}
err = game.ImportRandomState()
if err != nil {
t.Error(err)
return
@ -217,9 +248,7 @@ func TestGameOfLifeCanImportRandomState(t *testing.T) {
}
func TestGameStateCanDisplayItself(t *testing.T) {
state := NewGameState(16, 16)
err := state.Import(TEST_SIMPLE_INITIAL_STATE)
state, err := newTestGameState()
if err != nil {
t.Error(err)
return
@ -314,9 +343,8 @@ func TestNewGenerationScoreCardIsAllZeros(t *testing.T) {
func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T) {
genScore := NewGenerationScoreCard(16, 16)
state := NewGameState(16, 16)
err := state.Import(TEST_SIMPLE_INITIAL_STATE)
state, err := newTestGameState()
if err != nil {
t.Error(err)
return
@ -343,9 +371,7 @@ func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T
}
func TestStateCanMutate(t *testing.T) {
state := NewGameState(16, 16)
err := state.Import(TEST_SIMPLE_INITIAL_STATE)
state, err := newTestGameState()
if err != nil {
t.Error(err)
return

Loading…
Cancel
Save