forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
147 lines (127 loc) · 3.79 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cmdline_test
import (
"strings"
"testing"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestLineParser_Parse(t *testing.T) {
args := cmdline.NewParser(`./app top sub -a ddd --xx "msg"`).Parse()
assert.Len(t, args, 7)
assert.Eq(t, "msg", args[6])
args = cmdline.ParseLine(" ")
assert.Len(t, args, 0)
args = cmdline.ParseLine("./app")
assert.Len(t, args, 1)
p := cmdline.NewParser("./app sub ${A_ENV_VAR}")
p.WithParseEnv()
assert.True(t, p.ParseEnv)
testutil.MockEnvValue("A_ENV_VAR", "env-value", func(nv string) {
bin, args := p.BinAndArgs()
assert.Len(t, args, 2)
assert.Eq(t, "./app", bin)
assert.Eq(t, "env-value", args[1])
assert.NotEmpty(t, p.NewExecCmd())
})
p = cmdline.NewParser("./app sub ${A_ENV_VAR2}")
testutil.MockEnvValue("A_ENV_VAR2", "env-value2", func(nv string) {
args := p.AlsoEnvParse()
assert.Len(t, args, 3)
assert.Eq(t, "env-value2", args[2])
})
}
func TestParseLine_Parse_withQuote(t *testing.T) {
tests := []struct {
line string
argN int
index int
value string
}{
{
line: `./app top sub -a ddd --xx "abc
def"`,
argN: 7, index: 6, value: "abc\ndef",
},
{
line: `./app top sub -a ddd --xx "abc
def ghi"`,
argN: 7, index: 6, value: "abc\ndef ghi",
},
{
line: `./app top sub --msg "has multi words"`,
argN: 5, index: 4, value: "has multi words",
},
{
line: `./app top sub --msg "has inner 'quote'"`,
argN: 5, index: 4, value: "has inner 'quote'",
},
{
line: `./app top sub --msg "'has' inner quote"`,
argN: 5, index: 4, value: "'has' inner quote",
},
{
line: `./app top sub --msg "has inner 'quote' words"`,
argN: 5, index: 4, value: "has inner 'quote' words",
},
{
line: `./app top sub --msg "has 'inner quote' words"`,
argN: 5, index: 4, value: "has 'inner quote' words",
},
{
line: `./app top sub --msg "has 'inner quote words'"`,
argN: 5, index: 4, value: "has 'inner quote words'",
},
{
line: `./app top sub --msg "'has inner quote' words"`,
argN: 5, index: 4, value: "'has inner quote' words",
},
}
for _, tt := range tests {
args := cmdline.NewParser(tt.line).Parse()
assert.Len(t, args, tt.argN)
assert.Eq(t, tt.value, args[tt.index])
}
}
func TestParseLine_longLine(t *testing.T) {
line := "git log --pretty=format:'one two three'"
args := cmdline.ParseLine(line)
assert.Len(t, args, 3)
assert.Eq(t, "--pretty=format:'one two three'", args[2])
line = `git log --pretty=format:"one two three""`
args = cmdline.ParseLine(line)
assert.Len(t, args, 3)
assert.Eq(t, `--pretty=format:"one two three""`, args[2])
line = "git log --color --graph --pretty=format:'%Cred%h%Creset:%C(ul yellow)%d%Creset %s (%Cgreen%cr%Creset, %C(bold blue)%an%Creset)' --abbrev-commit -10"
args = cmdline.ParseLine(line)
dump.P(args)
assert.Len(t, args, 7)
assert.Eq(t, "--graph", args[3])
assert.Eq(t, "--abbrev-commit", args[5])
}
func TestParseLine_errLine(t *testing.T) {
// exception line string.
args := cmdline.NewParser(`./app top sub -a ddd --xx msg"`).Parse()
assert.Len(t, args, 7)
assert.Eq(t, "msg\"", args[6])
args = cmdline.ParseLine(`./app top sub -a ddd --xx "msg`)
assert.Len(t, args, 7)
assert.Eq(t, "msg", args[6])
args = cmdline.ParseLine(`./app top sub -a ddd --xx "msg text`)
assert.Len(t, args, 7)
assert.Eq(t, "msg text", args[6])
args = cmdline.ParseLine(`./app top sub -a ddd --xx "msg "text"`)
assert.Len(t, args, 7)
assert.Eq(t, "msg \"text", args[6])
}
func TestLineParser_BinAndArgs(t *testing.T) {
p := cmdline.NewParser("git status")
b, a := p.BinAndArgs()
assert.Eq(t, "git", b)
assert.Eq(t, "status", strings.Join(a, " "))
p = cmdline.NewParser("git")
b, a = p.BinAndArgs()
assert.Eq(t, "git", b)
assert.Empty(t, a)
}