You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/ncli/cmdline/builder_test.go

53 lines
1.2 KiB
Go

1 year ago
package cmdline_test
import (
"git.noahlan.cn/noahlan/ntool/ncli/cmdline"
"git.noahlan.cn/noahlan/ntool/ntest/assert"
"testing"
)
func TestLineBuild(t *testing.T) {
s := cmdline.LineBuild("myapp", []string{"-a", "val0", "arg0"})
assert.Eq(t, "myapp -a val0 arg0", s)
// case: empty string
b := cmdline.NewBuilder("myapp", "-a", "")
assert.Eq(t, 11, b.Len())
assert.Eq(t, `myapp -a ""`, b.String())
b.Reset()
assert.Eq(t, 0, b.Len())
// case: add first
b.AddArg("myapp")
assert.Eq(t, `myapp`, b.String())
b.AddArgs("-a", "val0")
assert.Eq(t, "myapp -a val0", b.String())
// case: contains `"`
b.Reset()
b.AddArgs("myapp", "-a", `"val0"`)
assert.Eq(t, `myapp -a '"val0"'`, b.String())
b.Reset()
b.AddArgs("myapp", "-a", `the "val0" of option`)
assert.Eq(t, `myapp -a 'the "val0" of option'`, b.String())
// case: contains `'`
b.Reset()
b.AddArgs("myapp", "-a", `'val0'`)
assert.Eq(t, `myapp -a "'val0'"`, b.String())
b.Reset()
b.AddArgs("myapp", "-a", `the 'val0' of option`)
assert.Eq(t, `myapp -a "the 'val0' of option"`, b.String())
}
func TestLineBuild_hasQuote(t *testing.T) {
line := "git log --pretty=format:'one two three'"
args := cmdline.ParseLine(line)
// dump.P(args)
assert.Eq(t, line, cmdline.LineBuild("", args))
}