test: multiple commands
This commit is contained in:
@@ -921,3 +921,31 @@ func TestCommandPlanetRouteRemove(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMultipleCommands(t *testing.T) {
|
||||||
|
e := newExecutor()
|
||||||
|
r := setupRouterExecutor(e)
|
||||||
|
|
||||||
|
payload := &rest.Command{
|
||||||
|
Actor: commandDefaultActor,
|
||||||
|
Commands: []json.RawMessage{
|
||||||
|
encodeCommand(&rest.CommandRaceRelation{
|
||||||
|
CommandMeta: rest.CommandMeta{Type: rest.CommandTypeRaceRelation},
|
||||||
|
Acceptor: "Opponent",
|
||||||
|
Relation: "PEACE",
|
||||||
|
}),
|
||||||
|
encodeCommand(&rest.CommandRaceVote{
|
||||||
|
CommandMeta: rest.CommandMeta{Type: rest.CommandTypeRaceVote},
|
||||||
|
Acceptor: "Opponent",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest(apiCommandMethod, apiCommandPath, asBody(payload))
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
assert.Equal(t, commandNoErrorsStatus, w.Code, w.Body)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, e.(*dummyExecutor).CommandsExecuted)
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ import (
|
|||||||
|
|
||||||
type CommandExecutor interface {
|
type CommandExecutor interface {
|
||||||
GenerateGame([]string) (uuid.UUID, error)
|
GenerateGame([]string) (uuid.UUID, error)
|
||||||
Execute(cmd ...Command) error
|
|
||||||
GameState() (rest.StateResponse, error)
|
GameState() (rest.StateResponse, error)
|
||||||
|
Execute(cmd ...Command) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command func(controller.Ctrl) error
|
type Command func(controller.Ctrl) error
|
||||||
@@ -39,10 +39,10 @@ func NewDefaultConfigExecutor(configurer controller.Configurer) CommandExecutor
|
|||||||
return &executor{cfg: configurer}
|
return &executor{cfg: configurer}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *executor) Execute(cmd ...Command) error {
|
func (e *executor) Execute(command ...Command) error {
|
||||||
return controller.ExecuteCommand(e.cfg, func(c controller.Ctrl) error {
|
return controller.ExecuteCommand(e.cfg, func(c controller.Ctrl) error {
|
||||||
for i := range cmd {
|
for i := range command {
|
||||||
if err := cmd[i](c); err != nil {
|
if err := command[i](c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type dummyExecutor struct {
|
type dummyExecutor struct {
|
||||||
|
CommandsExecuted int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *dummyExecutor) Execute(cmd ...handler.Command) error {
|
func (e *dummyExecutor) Execute(command ...handler.Command) error {
|
||||||
|
e.CommandsExecuted = len(command)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,8 +28,16 @@ func (e *dummyExecutor) GameState() (rest.StateResponse, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupRouter() *gin.Engine {
|
func setupRouter() *gin.Engine {
|
||||||
|
return setupRouterExecutor(newExecutor())
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupRouterExecutor(e handler.CommandExecutor) *gin.Engine {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
return router.SetupRouter(&dummyExecutor{})
|
return router.SetupRouter(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExecutor() handler.CommandExecutor {
|
||||||
|
return &dummyExecutor{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeCommand(cmd any) json.RawMessage {
|
func encodeCommand(cmd any) json.RawMessage {
|
||||||
|
|||||||
Reference in New Issue
Block a user