cmd: create/delete science

This commit is contained in:
Ilia Denisov
2025-10-01 14:30:36 +03:00
parent bcab29a47f
commit 2295840efe
9 changed files with 385 additions and 155 deletions
+83
View File
@@ -0,0 +1,83 @@
package game_test
import (
"strconv"
"testing"
e "github.com/iliadenisov/galaxy/pkg/error"
"github.com/iliadenisov/galaxy/pkg/game"
mg "github.com/iliadenisov/galaxy/pkg/model/game"
"github.com/stretchr/testify/assert"
)
func TestCreateScience(t *testing.T) {
race := "race_01"
typeName := "First Step"
g(t, func(p func(*game.Param), g func() mg.Game) {
err := game.DeleteScience(p, race, typeName)
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameNotExists))
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 with existing ship group
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(*game.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)
}
}
})
}