package game_test import ( "strconv" "testing" "github.com/iliadenisov/galaxy/internal/controller" e "github.com/iliadenisov/galaxy/internal/error" "github.com/iliadenisov/galaxy/internal/game" mg "github.com/iliadenisov/galaxy/internal/model/game" "github.com/stretchr/testify/assert" ) func TestCreateScience(t *testing.T) { race := "race_01" typeName := "First Step" g(t, func(p func(*controller.Param), g func() mg.Game) { err := game.DeleteScience(p, race, typeName) assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotExists)) err = game.CreateScience(p, unknownRaceName, " "+typeName+" ", 1, 0, 0, 0) // TODO: test on dead race assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace)) err = game.CreateScience(p, race, " "+typeName+" ", 1, 0, 0, 0) assert.NoError(t, err) sc, err := g().Sciences(race) assert.NoError(t, err) assert.Len(t, sc, 1) assert.Equal(t, sc[0].Name, typeName) assert.Equal(t, sc[0].Drive, 1.) assert.Equal(t, sc[0].Weapons, 0.) assert.Equal(t, sc[0].Shields, 0.) assert.Equal(t, sc[0].Cargo, 0.) // TODO: test delete with existing ship group // TODO: test delete with planet production busy with science err = game.DeleteScience(p, unknownRaceName, typeName) // TODO: test with actial rip race assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace)) err = game.DeleteScience(p, race, typeName) assert.NoError(t, err) sc, err = g().Sciences(race) assert.NoError(t, err) assert.Len(t, sc, 0) }) } func TestCreateScienceValidation(t *testing.T) { race := "race_01" typeName := "First Step" type tc struct { name string d, w, s, c float64 err string } table := []tc{ // correct values {typeName, 1, 0, 0, 0, ""}, {typeName, 0.5, 0.5, 0, 0, ""}, {typeName, 0.25, 0.25, 0.25, 0.25, ""}, {typeName, 0.33, 0.33, 0.34, 0, ""}, {typeName, 0, 0, 0.99, 0.01, ""}, // incorrect values... {"", 1, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)}, {" ", 1, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)}, {typeName, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputScienceSumValues)}, // drive {typeName, -1, 0, 0, 0, e.GenericErrorText(e.ErrInputDriveValue)}, {typeName, -1, 2, 0, 0, e.GenericErrorText(e.ErrInputDriveValue)}, // weapons {typeName, 0, -1, 0, 0, e.GenericErrorText(e.ErrInputWeaponsValue)}, {typeName, 2, -1, 0, 0, e.GenericErrorText(e.ErrInputWeaponsValue)}, // shields {typeName, 0, 0, -1, 0, e.GenericErrorText(e.ErrInputShieldsValue)}, {typeName, 0.5, 0.5, -1, 0.5, e.GenericErrorText(e.ErrInputShieldsValue)}, // cargo {typeName, 0, 0, 0, -1, e.GenericErrorText(e.ErrInputCargoValue)}, {typeName, 0, 1, 1, -1, e.GenericErrorText(e.ErrInputCargoValue)}, } g(t, func(p func(*controller.Param), g func() mg.Game) { for i, tc := range table { if tc.err == "" { err := game.CreateScience(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c) assert.NoError(t, err) err = game.CreateScience(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c) assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameDuplicate)) } else { err := game.CreateScience(p, race, tc.name, tc.d, tc.w, tc.s, tc.c) assert.ErrorContains(t, err, tc.err) } } }) }