recator, test: race relation

This commit is contained in:
Ilia Denisov
2026-02-08 10:11:58 +02:00
parent fc73cbf83a
commit c36857e702
6 changed files with 49 additions and 69 deletions
+16 -37
View File
@@ -4,7 +4,6 @@ import (
"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"
@@ -13,66 +12,46 @@ import (
func TestDeclarePeaceAndWarSingle(t *testing.T) {
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
hostRace := "race_05"
opponentRace := "race_01"
hostRace := 5
opponentRace := 1
r, err := ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationWar, r)
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(5, 1))
r, err = ctrl().Relation(unknownRaceName, opponentRace) // TODO: test on dead race
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace))
r, err = ctrl().Relation(hostRace, unknownRaceName) // TODO: test on dead race
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace))
assert.NoError(t, game.DeclarePeace(f, raceNum(hostRace), raceNum(opponentRace)))
assert.Equal(t, mg.RelationPeace, ctrl().Cache.Relation(hostRace, opponentRace))
assert.NoError(t, game.DeclarePeace(f, hostRace, opponentRace))
r, err = ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationPeace, r)
assert.NoError(t, game.DeclareWar(f, hostRace, opponentRace))
r, err = ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationWar, r)
assert.NoError(t, game.DeclareWar(f, raceNum(hostRace), raceNum(opponentRace)))
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, opponentRace))
})
}
func TestDeclarePeaceAndWarAll(t *testing.T) {
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
hostRace := "race_07"
hostRace := 7
for i := range testRaceCount {
opponentRace := raceNum(i)
if opponentRace == hostRace {
if i == hostRace {
continue
}
r, err := ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationWar, r)
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, i))
}
assert.NoError(t, game.DeclarePeace(f, hostRace, hostRace))
assert.NoError(t, game.DeclarePeace(f, raceNum(hostRace), raceNum(hostRace)))
for i := range testRaceCount {
opponentRace := raceNum(i)
if opponentRace == hostRace {
if i == hostRace {
continue
}
r, err := ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationPeace, r)
assert.Equal(t, mg.RelationPeace, ctrl().Cache.Relation(hostRace, i))
}
assert.NoError(t, game.DeclareWar(f, hostRace, hostRace))
assert.NoError(t, game.DeclareWar(f, raceNum(hostRace), raceNum(hostRace)))
for i := range testRaceCount {
opponentRace := raceNum(i)
if opponentRace == hostRace {
if i == hostRace {
continue
}
r, err := ctrl().Relation(hostRace, opponentRace)
assert.NoError(t, err)
assert.Equal(t, mg.RelationWar, r)
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, i))
}
})
}
+4 -4
View File
@@ -29,7 +29,7 @@ func TestComposeGame(t *testing.T) {
})
}
func g(t *testing.T, f func(p func(*controller.Param), g func() *mg.Game)) {
func g(t *testing.T, f func(func(*controller.Param), func() *mg.Game)) {
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
races := make([]string, testRaceCount)
@@ -53,7 +53,7 @@ func g(t *testing.T, f func(p func(*controller.Param), g func() *mg.Game)) {
f(p, g)
}
func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Controller)) {
func c(t *testing.T, f func(func(*controller.Param), func() *controller.Controller)) {
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
races := make([]string, testRaceCount)
@@ -66,7 +66,7 @@ func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Cont
assert.FailNow(t, "c: GenerateGame", err)
return
}
ctl := func() *controller.Controller {
ctrl := func() *controller.Controller {
c, err := controller.NewController(p)
if err != nil {
assert.FailNow(t, "c: NewController", err)
@@ -80,5 +80,5 @@ func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Cont
c.Cache = controller.NewCache(g)
return c
}
f(p, ctl)
f(p, ctrl)
}