package game_test import ( "strconv" "testing" "github.com/iliadenisov/galaxy/internal/controller" "github.com/iliadenisov/galaxy/internal/game" e "github.com/iliadenisov/galaxy/internal/error" "github.com/stretchr/testify/assert" ) func TestCreateScience(t *testing.T) { typeName := "First Step" c(t, func(p func(*controller.Param), g func() *controller.Controller) { err := g().CreateScience(unknownRaceName, " "+typeName+" ", 1, 0, 0, 0) assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace)) err = g().DeleteScience(unknownRaceName, typeName) assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace)) }) } 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)}, } c(t, func(p func(*controller.Param), g func() *controller.Controller) { for i, tc := range table { if tc.err == "" { n := tc.name + strconv.Itoa(i) err := game.CreateScience(p, race, n, tc.d, tc.w, tc.s, tc.c) assert.NoError(t, err, "for name=%q", n) err = game.CreateScience(p, race, n, tc.d, tc.w, tc.s, tc.c) assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameDuplicate), "for name=%q", n) } else { err := game.CreateScience(p, race, tc.name, tc.d, tc.w, tc.s, tc.c) assert.ErrorContains(t, err, tc.err) } } }) }