cmd: create ship type
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package game
|
||||
|
||||
import "github.com/iliadenisov/galaxy/pkg/model/game"
|
||||
|
||||
func CreateShipType(configure func(*Param), race, typeName string, drive, weapons, shields, cargo float64, armament int) (err error) {
|
||||
control(configure, func(c *ctrl) {
|
||||
c.execute(func(r Repo, g game.Game) {
|
||||
err = createShipType(r, g, race, typeName, drive, weapons, shields, cargo, armament)
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func createShipType(r Repo, g game.Game, race, typeName string, d, w, s, c float64, a int) error {
|
||||
if err := g.CreateShipType(race, typeName, d, w, s, c, a); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.SaveState(g)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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 TestCreateShipType(t *testing.T) {
|
||||
race := "race_01"
|
||||
g(t, func(p func(*game.Param), g func() mg.Game) {
|
||||
err := game.CreateShipType(p, race, " Drone ", 1, 0, 0, 0, 0)
|
||||
assert.NoError(t, err)
|
||||
st, err := g().ShipTypes(race)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, st, 1)
|
||||
assert.Equal(t, st[0].Name, "Drone")
|
||||
assert.Equal(t, st[0].Drive, 1.)
|
||||
assert.Equal(t, st[0].Weapons, 0.)
|
||||
assert.Equal(t, st[0].Shields, 0.)
|
||||
assert.Equal(t, st[0].Cargo, 0.)
|
||||
assert.Equal(t, st[0].Armament, uint(0))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateShipTypeValidation(t *testing.T) {
|
||||
race := "race_01"
|
||||
typeName := "Drone"
|
||||
type tc struct {
|
||||
name string
|
||||
d, w, s, c float64
|
||||
a int
|
||||
err string
|
||||
}
|
||||
table := []tc{
|
||||
// correct values
|
||||
{typeName, 1, 0, 0, 0, 0, ""},
|
||||
{typeName, 1.1, 0, 0, 0, 0, ""},
|
||||
{typeName, 1, 1.2, 0, 0, 1, ""},
|
||||
{typeName, 1, 1.2, 2.5, 0, 1, ""},
|
||||
{typeName, 1, 0, 2.5, 7.7, 0, ""},
|
||||
// incorrect values...
|
||||
{"", 1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)},
|
||||
{" ", 1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)},
|
||||
{typeName, 0, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeZeroValues)},
|
||||
// drive
|
||||
{typeName, -1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeDriveValue)},
|
||||
{typeName, 0.5, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeDriveValue)},
|
||||
// weapons
|
||||
{typeName, 0, -1, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsValue)},
|
||||
{typeName, 0, 0.5, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsValue)},
|
||||
// shields
|
||||
{typeName, 0, 0, -1, 0, 0, e.GenericErrorText(e.ErrInputShipTypeShieldsValue)},
|
||||
{typeName, 0, 0, 0.5, 0, 0, e.GenericErrorText(e.ErrInputShipTypeShieldsValue)},
|
||||
// cargo
|
||||
{typeName, 0, 0, 0, -1, 0, e.GenericErrorText(e.ErrInputShipTypeCargoValue)},
|
||||
{typeName, 0, 0, 0, 0.5, 0, e.GenericErrorText(e.ErrInputShipTypeCargoValue)},
|
||||
// armament (and weapons)
|
||||
{typeName, 0, 0, 0, 0, -1, e.GenericErrorText(e.ErrInputShipTypeArmamentValue)},
|
||||
{typeName, 0, 1, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
|
||||
{typeName, 0, 0, 0, 0, 1, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
|
||||
}
|
||||
g(t, func(p func(*game.Param), g func() mg.Game) {
|
||||
for i, tc := range table {
|
||||
if tc.err == "" {
|
||||
err := game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||
assert.NoError(t, err)
|
||||
err = game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameExists))
|
||||
} else {
|
||||
err := game.CreateShipType(p, race, tc.name, tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||
assert.ErrorContains(t, err, tc.err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -26,7 +26,7 @@ func TestComposeGame(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func g(t *testing.T, f func(func(*game.Param), func() mg.Game)) {
|
||||
func g(t *testing.T, f func(p func(*game.Param), g func() mg.Game)) {
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
races := make([]string, testRaceCount)
|
||||
|
||||
Reference in New Issue
Block a user