-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
68 lines (65 loc) · 1.57 KB
/
parser_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package xflags
import (
"testing"
)
func TestNormalize(t *testing.T) {
args := []string{
"-x", "-xVar", "-x=Var", "-x=",
"--x", "--xVar", "--x=Var", "--x=",
"--foo", "--foo=bar", "--foo=",
"", "-", "--",
}
expect := []string{
"-x", "-x", "Var", "-x", "Var", "-x", "",
"--x", "--xVar", "--x", "Var", "--x", "",
"--foo", "--foo", "bar", "--foo", "",
"", "-", "--",
}
t.Run("NoTerminator", func(t *testing.T) {
actual := normalize(args, false)
if len(actual) != len(expect) {
t.Errorf("expected %q, got %q", expect, actual)
return
}
for i := 0; i < len(expect); i++ {
if expect[i] != actual[i] {
t.Errorf("expected %q, got %q", expect, actual)
return
}
}
})
t.Run("WithTerminator", func(t *testing.T) {
tArgs := make([]string, len(args)*2)
copy(tArgs, args)
copy(tArgs[len(args):], args)
tExpect := make([]string, len(expect)+len(args))
copy(tExpect, expect)
copy(tExpect[len(expect):], args)
tActual := normalize(tArgs, true)
assertStrings(t, tExpect, tActual)
})
}
func TestTerminator(t *testing.T) {
var foo string
var bar bool
cmd := NewCommand("test", "").
Flags(
String(&foo, "foo", "", "").Must(),
Bool(&bar, "bar", false, "").Must(),
).
WithTerminator().
Must()
tailArgs := []string{
"baz",
"--baz", "--baz=qux", "--baz", "qux",
"-q", "-q=quux", "-q", "quux",
"--", "-", "",
}
args := append([]string{"--foo=foo", "--bar", "--"}, tailArgs...)
if _, err := cmd.Parse(args); err != nil {
t.Fatal(err)
}
assertString(t, "foo", foo)
assertBool(t, true, bar)
assertStrings(t, tailArgs, cmd.Args())
}