forked from alecthomas/kingpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
122 lines (104 loc) · 3.05 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
package kingpin
import (
"io/ioutil"
"os"
"testing"
"github.com/alecthomas/assert"
)
func TestParserExpandFromFile(t *testing.T) {
f, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(f.Name())
f.WriteString("hello\nworld\n")
f.Close()
app := New("test", "")
arg0 := app.Arg("arg0", "").String()
arg1 := app.Arg("arg1", "").String()
_, err = app.Parse([]string{"@" + f.Name()})
assert.NoError(t, err)
assert.Equal(t, "hello", *arg0)
assert.Equal(t, "world", *arg1)
}
func TestParserExpandFromFileLeadingArg(t *testing.T) {
f, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(f.Name())
f.WriteString("hello\nworld\n")
f.Close()
app := New("test", "")
arg0 := app.Arg("arg0", "").String()
arg1 := app.Arg("arg1", "").String()
arg2 := app.Arg("arg2", "").String()
_, err = app.Parse([]string{"prefix", "@" + f.Name()})
assert.NoError(t, err)
assert.Equal(t, "prefix", *arg0)
assert.Equal(t, "hello", *arg1)
assert.Equal(t, "world", *arg2)
}
func TestParserExpandFromFileTrailingArg(t *testing.T) {
f, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(f.Name())
f.WriteString("hello\nworld\n")
f.Close()
app := New("test", "")
arg0 := app.Arg("arg0", "").String()
arg1 := app.Arg("arg1", "").String()
arg2 := app.Arg("arg2", "").String()
_, err = app.Parse([]string{"@" + f.Name(), "suffix"})
assert.NoError(t, err)
assert.Equal(t, "hello", *arg0)
assert.Equal(t, "world", *arg1)
assert.Equal(t, "suffix", *arg2)
}
func TestParserExpandFromFileMultipleSurroundingArgs(t *testing.T) {
f, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(f.Name())
f.WriteString("hello\nworld\n")
f.Close()
app := New("test", "")
arg0 := app.Arg("arg0", "").String()
arg1 := app.Arg("arg1", "").String()
arg2 := app.Arg("arg2", "").String()
arg3 := app.Arg("arg3", "").String()
_, err = app.Parse([]string{"prefix", "@" + f.Name(), "suffix"})
assert.NoError(t, err)
assert.Equal(t, "prefix", *arg0)
assert.Equal(t, "hello", *arg1)
assert.Equal(t, "world", *arg2)
assert.Equal(t, "suffix", *arg3)
}
func TestParserExpandFromFileMultipleFlags(t *testing.T) {
f, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer os.Remove(f.Name())
f.WriteString("--flag1=f1\n--flag2=f2\n")
f.Close()
app := New("test", "")
flag0 := app.Flag("flag0", "").String()
flag1 := app.Flag("flag1", "").String()
flag2 := app.Flag("flag2", "").String()
flag3 := app.Flag("flag3", "").String()
_, err = app.Parse([]string{"--flag0=f0", "@" + f.Name(), "--flag3=f3"})
assert.NoError(t, err)
assert.Equal(t, "f0", *flag0)
assert.Equal(t, "f1", *flag1)
assert.Equal(t, "f2", *flag2)
assert.Equal(t, "f3", *flag3)
}
func TestParseContextPush(t *testing.T) {
app := New("test", "")
app.Command("foo", "").Command("bar", "")
c := tokenize([]string{"foo", "bar"}, false)
a := c.Next()
assert.Equal(t, TokenArg, a.Type)
b := c.Next()
assert.Equal(t, TokenArg, b.Type)
c.Push(b)
c.Push(a)
a = c.Next()
assert.Equal(t, "foo", a.Value)
b = c.Next()
assert.Equal(t, "bar", b.Value)
}