Skip to content

Commit

Permalink
test: fix cmd init event fire error and some test error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 4, 2022
1 parent 96aa894 commit 1b510b3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (app *App) Add(c *Command, more ...*Command) {
func (app *App) AddCommand(c *Command) {
// initialize application before add command
app.initialize()
app.fireWithCmd(events.OnAppCmdAdd, c, nil)

// init command
c.app = app
Expand Down
13 changes: 6 additions & 7 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,22 @@ func TestApp_Hooks_EvtAppInit(t *testing.T) {
assert.Eq(t, "trigger "+events.OnGlobalOptsParsed+", args:[simple]", buf.String())
}

func TestApp_Hooks_EvtCmdInit(t *testing.T) {
func TestApp_Hooks_OnAppCmdAdd(t *testing.T) {
buf.Reset()

cli := newNotExitApp()
cli.On(events.OnCmdInitAfter, func(ctx *gcli.HookCtx) (stop bool) {
buf.WriteString(events.OnCmdInitAfter)
buf.WriteString(":")

cli.On(events.OnAppCmdAdd, func(ctx *gcli.HookCtx) (stop bool) {
buf.WriteString(ctx.Name())
buf.WriteString(" - ")
buf.WriteString(ctx.Cmd.Name + ";")
return
})

cli.Add(emptyCmd)
assert.Eq(t, "cmd.init:empty;", buf.String())
assert.Eq(t, "app.cmd.add.before - empty;", buf.String())

cli.Add(simpleCmd)
assert.Eq(t, "cmd.init:empty;cmd.init:simple;", buf.String())
assert.Eq(t, "app.cmd.add.before - empty;app.cmd.add.before - simple;", buf.String())
}

func TestCommand_Hooks_EvtCmdOptParsed(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions builtin/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !go1.18
// +build !go1.18

package builtin

// alias of interface{}, use for go < 1.18
type any = interface{}
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,14 @@ func (c *Command) initialize() {

// check command name
cName := c.goodName()
c.Fire(events.OnCmdInitBefore, nil)
Debugf("initialize the command '%s': init flags, run config func", cName)

c.initialized = true
c.pathNames = append(c.pathNames, cName)

// init base
c.initCommandBase(cName)
c.Fire(events.OnCmdInitBefore, nil)

// load common subs
if len(c.Subs) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func TestCommand_Run_emptyArgs(t *testing.T) {
is.Eq("arg0", c0.Arg("arg0").Name)
}

func TestCommand_Run_XshowHelp1(t *testing.T) {
func TestCommand_Run_showHelp1(t *testing.T) {
is := assert.New(t)

bf.Reset()
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestCommand_Run_showHelp2(t *testing.T) {
is.StrContains(str, "Arg1 desc")
}

func TestCommand_Run_XparseOptions(t *testing.T) {
func TestCommand_Run_parseOptions(t *testing.T) {
bf.Reset()
is := assert.New(t)

Expand All @@ -355,7 +355,7 @@ func TestCommand_Run_XparseOptions(t *testing.T) {
dump.P(gcli.GOpts())
err := c0.Run([]string{"--int", "10", "--str=abc", "txt"})

dump.P(gcli.GOpts(), c0.Context)
// dump.P(gcli.GOpts(), c0.Context)
is.NoErr(err)
is.Eq("test", c0.Get("name"))
is.Eq([]string{"txt"}, c0.Get("args"))
Expand Down
3 changes: 3 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
OnAppBindOptsBefore = "app.bind.opts.before"
OnAppBindOptsAfter = "app.bind.opts.after"

// OnAppCmdAdd on app cmd add
OnAppCmdAdd = "app.cmd.add.before"

// OnAppCmdAdded on app cmd added
OnAppCmdAdded = "app.cmd.added"

Expand Down
7 changes: 7 additions & 0 deletions gflag/any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !go1.18
// +build !go1.18

package gflag_test

// alias of interface{}, use for go < 1.18
type any = interface{}

0 comments on commit 1b510b3

Please sign in to comment.