forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
53 lines (41 loc) · 1.24 KB
/
builder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cmdline_test
import (
"testing"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/testutil/assert"
)
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))
}