Skip to content

Commit

Permalink
up: use goutil/testutil/assert instead of the stretchr/testify/assert
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2022
1 parent 08d19ee commit f1b474e
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 190 deletions.
79 changes: 39 additions & 40 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"github.com/gookit/color"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/dump"
assert2 "github.com/gookit/goutil/testutil/assert"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

var (
Expand Down Expand Up @@ -74,28 +73,28 @@ func TestApp_MatchByPath(t *testing.T) {

c := app.MatchByPath("simple:sub")
assert.NotNil(t, c)
assert.Equal(t, "sub", c.Name)
assert.Equal(t, "simple", c.ParentName())
assert.Eq(t, "sub", c.Name)
assert.Eq(t, "simple", c.ParentName())

c = appWithMl.FindByPath("top1:sub1")
assert.Equal(t, "sub1", c.Name)
assert.Eq(t, "sub1", c.Name)
}

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

app := gcli.NewApp()
app.Add(gcli.NewCommand("c1", "c1 desc", func(c *gcli.Command) {
is.Equal("c1", c.Name)
is.Eq("c1", c.Name)
}), gcli.NewCommand("c2", "c2 desc", func(c *gcli.Command) {
is.Equal("c2", c.Name)
is.Eq("c2", c.Name)
}))
app.AddCommand(&gcli.Command{
Name: "c3",
Desc: "{$cmd} desc",
Aliases: []string{"alias1"},
Config: func(c *gcli.Command) {
is.Equal("c3", c.Name)
is.Eq("c3", c.Name)
},
})

Expand All @@ -109,9 +108,9 @@ func TestApp_Add(t *testing.T) {
c := gcli.NewCommand("mdl-test", "desc test2")
app.AddCommand(c)

is.Equal("", c.ParentName())
is.Equal("mdl-test", c.Name)
is.Equal("c3", app.ResolveAlias("alias1"))
is.Eq("", c.ParentName())
is.Eq("mdl-test", c.Name)
is.Eq("c3", app.ResolveAlias("alias1"))
is.True(app.IsAlias("alias1"))
}

Expand All @@ -129,14 +128,14 @@ func TestApp_AddCommand(t *testing.T) {
assert.Len(t, app.Commands(), 1)
assert.Len(t, app.CommandNames(), 1)
assert.False(t, app.IsCommand("cmd1"))
assert.Equal(t, "", app.CommandName())
assert.Eq(t, "", app.CommandName())

assert.PanicsWithValue(t, "GCli: the command name can not be empty", func() {
assert.PanicsMsg(t, func() {
app.AddCommand(&gcli.Command{})
})
assert.PanicsWithValue(t, "GCli: the command name '+xdd' is invalid, must match: ^[a-zA-Z][\\w-]*$", func() {
}, "GCli: the command name can not be empty")
assert.PanicsMsg(t, func() {
app.AddCommand(&gcli.Command{Name: "+xdd"})
})
}, "GCli: the command name '+xdd' is invalid, must match: ^[a-zA-Z][\\w-]*$")
}

func TestApp_AddAliases(t *testing.T) {
Expand All @@ -153,12 +152,12 @@ func TestApp_AddAliases(t *testing.T) {
app.AddCommand(gcli.NewCommand("cmd2", "desc"))
assert.True(t, app.IsAlias("alias1"))

assert.PanicsWithValue(t, "GCli: The name 'alias1' is already used as an alias", func() {
assert.PanicsMsg(t, func() {
app.AddCommand(gcli.NewCommand("alias1", "desc"))
})
assert.PanicsWithValue(t, "GCli: The name 'test' has been used as an command name", func() {
}, "GCli: The name 'alias1' is already used as an alias")
assert.PanicsMsg(t, func() {
app.AddAliases("cmd2", "test")
})
}, "GCli: The name 'test' has been used as an command name")
}

func TestApp_Run_noCommands(t *testing.T) {
Expand Down Expand Up @@ -186,14 +185,14 @@ func TestApp_Run_noCommands(t *testing.T) {
str := buf.String()
buf.Reset()

is.Equal(0, code)
is.Eq(0, code)
is.Contains(str, "1.3.9")
is.Contains(str, "Version: 1.3.9")
is.Contains(str, "This is my console application")
is.Contains(str, "Display help information")

err := app.Exec("not-exists", []string{})
is.Error(err)
is.Err(err)
}

func TestApp_Run_command_withArguments(t *testing.T) {
Expand All @@ -220,28 +219,28 @@ func TestApp_Run_command_withArguments(t *testing.T) {

// run an command
code := app.Run([]string{"test"})
is.Equal(0, code)
is.Equal("", argStr)
is.Equal("test", cmdRet)
is.Equal("test", app.CommandName())
is.Eq(0, code)
is.Eq("", argStr)
is.Eq("test", cmdRet)
is.Eq("test", app.CommandName())
// clear
argStr = ""
cmdRet = ""

err := app.Exec("test", []string{"val0", "val1"})
is.NoError(err)
is.Equal("test", cmdRet)
is.Equal("val0,val1", argStr)
is.NoErr(err)
is.Eq("test", cmdRet)
is.Eq("val0,val1", argStr)

err = app.Exec("not-exists", []string{})
is.Error(err)
is.Equal("exec unknown command: 'not-exists'", err.Error())
is.Err(err)
is.Eq("exec unknown command: 'not-exists'", err.Error())
// other
// app.AddError(fmt.Errorf("test error"))
}

func TestApp_Run_command_withOptions(t *testing.T) {
is := assert2.New(t)
is := assert.New(t)
app := gcli.NewApp(gcli.NotExitOnEnd())

// run with command
Expand Down Expand Up @@ -297,7 +296,7 @@ func TestApp_Run_subcommand(t *testing.T) {

c := appWithMl.FindCommand(id)
is.NotEmpty(c)
is.Equal("TestApp_Run_subcommand", c.GetVal("msg"))
is.Eq("TestApp_Run_subcommand", c.GetVal("msg"))
}

func TestApp_Run_by_cmd_ID(t *testing.T) {
Expand All @@ -308,7 +307,7 @@ func TestApp_Run_by_cmd_ID(t *testing.T) {

c := appWithMl.FindCommand("top1:sub1")
is.NotEmpty(c)
is.Equal("TestApp_Run_by_cmd_ID", c.GetVal("msg"))
is.Eq("TestApp_Run_by_cmd_ID", c.GetVal("msg"))
}

func TestApp_AddAliases_and_run(t *testing.T) {
Expand All @@ -321,7 +320,7 @@ func TestApp_AddAliases_and_run(t *testing.T) {

c := appWithMl.FindCommand(id)
is.NotEmpty(c)
is.Equal("TestApp_AddAliases_and_run", c.GetVal("msg"))
is.Eq("TestApp_AddAliases_and_run", c.GetVal("msg"))
}

func TestApp_showCommandHelp(t *testing.T) {
Expand All @@ -345,29 +344,29 @@ func TestApp_showCommandHelp(t *testing.T) {
code := app.Run([]string{"help", "test"})
str := buf.String()
buf.Reset()
is.Equal(0, code)
is.Eq(0, code)
is.Contains(str, "Name: test")
is.Contains(str, "Desc for test command")

// show command help: arg error
code = app.Run([]string{"help", "test", "more"})
str = buf.String()
buf.Reset()
is.Equal(gcli.ERR, code)
is.Eq(gcli.ERR, code)
is.Contains(str, "ERROR: Too many arguments given.")

// show command help for 'help'
code = app.Run([]string{"help", "help"})
str = buf.String()
buf.Reset()
is.Equal(gcli.OK, code)
is.Eq(gcli.OK, code)
is.Contains(str, "Display help message for application or command.")

// show command help: unknown command
code = app.Run([]string{"help", "not-exist"})
str = buf.String()
buf.Reset()
is.Equal(gcli.ERR, code)
is.Eq(gcli.ERR, code)
is.Contains(str, "Unknown command name 'not-exist'")
}

Expand Down Expand Up @@ -427,7 +426,7 @@ func TestApp_showCommandTips(t *testing.T) {
// assert.Len(t, app.Commands(), 2)
// assert.True(t, app.IsCommand("cmd1"))
//
// assert.Equal(t, 1, app.RemoveCommand("cmd1"))
// assert.Eq(t, 1, app.RemoveCommand("cmd1"))
// assert.Len(t, app.Commands(), 1)
// assert.False(t, app.IsCommand("cmd1"))
// }
Expand Down
20 changes: 10 additions & 10 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

var (
Expand All @@ -30,15 +30,15 @@ func TestApp_Hooks_EvtAppInit(t *testing.T) {
return false
})
cli.Add(simpleCmd)
assert.Equal(t, "trigger "+gcli.EvtAppInit, buf.String())
assert.Eq(t, "trigger "+gcli.EvtAppInit, buf.String())

buf.Reset()
cli.On(gcli.EvtGOptionsParsed, func(data ...interface{}) bool {
buf.WriteString("trigger " + gcli.EvtGOptionsParsed + ", args:" + fmt.Sprintf("%v", data[1]))
return false
})
cli.Run([]string{"simple"})
assert.Equal(t, "trigger "+gcli.EvtGOptionsParsed+", args:[simple]", buf.String())
assert.Eq(t, "trigger "+gcli.EvtGOptionsParsed+", args:[simple]", buf.String())
}

func TestApp_Hooks_EvtCmdInit(t *testing.T) {
Expand All @@ -55,10 +55,10 @@ func TestApp_Hooks_EvtCmdInit(t *testing.T) {
})

cli.Add(emptyCmd)
assert.Equal(t, "cmd.init:empty;", buf.String())
assert.Eq(t, "cmd.init:empty;", buf.String())

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

func TestCommand_Hooks_EvtCmdOptParsed(t *testing.T) {
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestApp_On_CmdNotFound(t *testing.T) {
})

cli.Run([]string{"top"})
assert.Equal(t, "trigger: cmd.not.found; command: top", buf.String())
assert.Eq(t, "trigger: cmd.not.found; command: top", buf.String())
buf.Reset()

fmt.Println("--------- dont print command tips ----------")
Expand All @@ -108,13 +108,13 @@ func TestApp_On_CmdNotFound(t *testing.T) {
})

cli.Run([]string{"top"})
assert.Equal(t, "trigger: cmd.not.found; command: top", buf.String())
assert.Eq(t, "trigger: cmd.not.found; command: top", buf.String())
}

func TestApp_On_CmdNotFound_redirect(t *testing.T) {
buf.Reset()
simpleCmd.ClearData()
assert.Equal(t, nil, simpleCmd.GetVal("simple"))
assert.Eq(t, nil, simpleCmd.GetVal("simple"))

cli := newNotExitApp()
cli.Add(simpleCmd)
Expand All @@ -127,12 +127,12 @@ func TestApp_On_CmdNotFound_redirect(t *testing.T) {

app := data[0].(*gcli.App)
err := app.Exec("simple", nil)
assert.NoError(t, err)
assert.NoErr(t, err)
buf.WriteString("value:" + simpleCmd.StrValue("simple"))
return true
})

cli.Run([]string{"top"})
want := "trigger:cmd.not.found - command:top; redirect:simple - value:simple command"
assert.Equal(t, want, buf.String())
assert.Eq(t, want, buf.String())
}
10 changes: 5 additions & 5 deletions builtin/sflag/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestArgsParser_Parse(t *testing.T) {
Expand All @@ -16,7 +16,7 @@ func TestArgsParser_Parse(t *testing.T) {
p := &ArgsParser{}
p.Parse(args)

at.Equal("[arg0 arg1]", fmt.Sprint(p.Args()))
at.Eq("[arg0 arg1]", fmt.Sprint(p.Args()))
assertOptions := func(str string) {
at.Contains(str, `"n":"10"`)
at.Contains(str, `"name":"tom"`)
Expand All @@ -36,7 +36,7 @@ func TestArgsParser_Parse(t *testing.T) {
// 加上 []string{"debug"} 解析器就能正确分别 "--debug arg0"
p = NewArgsParser([]string{"debug"}, nil)
p.Parse(args)
at.Equal("[arg0 arg1]", fmt.Sprint(p.Args()))
at.Eq("[arg0 arg1]", fmt.Sprint(p.Args()))
str = p.OptsString()
assertOptions(str)

Expand All @@ -45,7 +45,7 @@ func TestArgsParser_Parse(t *testing.T) {
args = strings.Split(sample, " ")
// 加上 []string{"name"} 解析器就能正确解析 "--name --name john"
p = ParseArgs(args, nil, []string{"name"})
at.Equal("[arg0 arg1]", fmt.Sprint(p.Args()))
at.Eq("[arg0 arg1]", fmt.Sprint(p.Args()))
str = p.OptsString()
at.Contains(str, `"n":"10"`)
at.Contains(str, `"name":[]string{"tom", "john"}`)
Expand All @@ -60,7 +60,7 @@ func TestArgsParser_Parse(t *testing.T) {
args = strings.Split(sample, " ")

p = ParseArgs(args, []string{"debug"}, []string{"name"})
at.Equal("[arg0 arg1]", fmt.Sprint(p.Args()))
at.Eq("[arg0 arg1]", fmt.Sprint(p.Args()))
str = p.OptsString()
at.Contains(str, `"n":"10"`)
at.Contains(str, `"name":[]string{"tom", "john"}`)
Expand Down
Loading

0 comments on commit f1b474e

Please sign in to comment.