-
Notifications
You must be signed in to change notification settings - Fork 44
/
context_test.go
151 lines (139 loc) · 3.87 KB
/
context_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
148
149
150
151
package cli
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContextGetArgvList(t *testing.T) {
type rootT struct {
A string `cli:"a"`
}
type helloT struct {
B int `cli:"b"`
}
type worldT struct {
C bool `cli:"c"`
}
root := &Command{
Name: "root",
Argv: func() interface{} { return new(rootT) },
Global: true,
Fn: func(ctx *Context) error {
rootArgv := ctx.RootArgv().(*rootT)
assert.Equal(t, rootArgv.A, "Root-A")
assert.Nil(t, ctx.GetArgvList(nil))
var pA = &rootT{}
assert.Nil(t, ctx.GetArgvList(pA))
assert.Equal(t, pA.A, "Root-A")
return nil
},
}
hello := &Command{
Name: "hello",
Argv: func() interface{} { return new(helloT) },
Fn: func(ctx *Context) error {
argv := ctx.Argv().(*helloT)
assert.Equal(t, argv.B, 100)
pA := new(rootT)
pB := new(helloT)
assert.Nil(t, ctx.GetArgvList(pB, pA))
assert.Equal(t, pA.A, "Root-A")
assert.Equal(t, pB.B, argv.B)
return nil
},
}
world := &Command{
Name: "world",
Argv: func() interface{} { return new(worldT) },
Fn: func(ctx *Context) error {
argv := ctx.Argv().(*worldT)
assert.Equal(t, argv.C, true)
pA := new(rootT)
pC := new(worldT)
assert.Nil(t, ctx.GetArgvList(pC, nil, pA))
assert.Equal(t, pA.A, "Root-A")
assert.Equal(t, pC.C, true)
assert.Error(t, ctx.GetArgvList(pC, new(helloT), pA))
return nil
},
}
root.Register(hello)
hello.Register(world)
assert.Nil(t, root.RunWith([]string{"-a=Root-A"}, nil, nil))
assert.Nil(t, root.RunWith([]string{"hello", "-a=Root-A", "-b", "100"}, nil, nil))
assert.Nil(t, root.RunWith([]string{"hello", "world", "-a=Root-A", "-c"}, nil, nil))
}
func TestContextMisc(t *testing.T) {
type argT struct {
Hello string `cli:"hello"`
Age int `cli:"age" dft:"10"`
}
root := &Command{Name: "root"}
parent := &Command{Name: "parent", Fn: donothing}
cmd := &Command{
Name: "cmd",
Argv: func() interface{} { return new(argT) },
Fn: func(ctx *Context) error {
argv := ctx.Argv().(*argT)
assert.Equal(t, ctx.Args(), []string{"a", "b", "c"})
assert.Equal(t, argv.Hello, "world")
assert.Equal(t, argv.Age, 10)
assert.Equal(t, ctx.NativeArgs(), []string{"--hello=world", "a", "b", "c"})
assert.Equal(t, ctx.Command().Name, "cmd")
assert.Equal(t, ctx.IsSet("--hello"), true)
assert.Equal(t, ctx.IsSet("--age"), false)
assert.Equal(t, ctx.NArg(), 3)
assert.Equal(t, ctx.Path(), "parent cmd")
assert.Equal(t, ctx.Router(), []string{"parent", "cmd"})
ctx.writer = nil
assert.NotNil(t, ctx.Writer())
return nil
},
}
root.Register(parent)
parent.Register(cmd)
assert.Nil(t, root.RunWith([]string{"parent", "cmd", "--hello=world", "a", "b", "c"}, nil, nil))
}
func TestPositionalArguments(t *testing.T) {
type argT struct{}
root := &Command{Name: "root"}
parent := &Command{Name: "parent", Fn: donothing}
cmd := &Command{
Name: "cmd",
Argv: func() interface{} { return new(argT) },
Fn: func(ctx *Context) error {
assert.Equal(t, ctx.Args(), []string{"a", "b", "c"})
assert.Equal(t, ctx.NativeArgs(), []string{"a", "b", "c"})
assert.Equal(t, ctx.Command().Name, "cmd")
return nil
},
}
root.Register(parent)
parent.Register(cmd)
assert.Nil(t, root.RunWith([]string{"parent", "cmd", "a", "b", "c"}, nil, nil))
}
func TestContextWriter(t *testing.T) {
w := bytes.NewBufferString("")
assert.Nil(t, (&Command{
Name: "root",
CanSubRoute: true,
Fn: func(ctx *Context) error {
ctx.String("String")
ctx.JSON(struct{ A int }{10})
ctx.JSONln(struct{ B int }{10})
ctx.JSONIndent(struct{ C string }{"11"}, "", " ")
ctx.JSONIndentln(struct{ D bool }{true}, "", " ")
assert.Equal(t, w, ctx.Writer())
n, err := ctx.Write([]byte("end"))
assert.Equal(t, 3, n)
return err
},
}).RunWith([]string{"a", "b"}, w, nil))
assert.Equal(t, w.String(), `String{"A":10}{"B":10}
{
"C": "11"
}{
"D": true
}
end`)
}