-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_test.go
114 lines (102 loc) · 2.16 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
package cli
import (
"reflect"
"testing"
)
func TestContextGet(t *testing.T) {
c := &Context{
flags: []*Flag{
{Name: "f1"},
{Name: "f2", Value: new(bool)},
{Name: "f3", Value: new([]string)},
{Name: "f4"},
},
}
// initialize flags
for _, f := range c.flags {
f.initialize()
}
lookupFlag(c.flags, "f1").SetValue("123")
lookupFlag(c.flags, "f2").SetValue("true")
lookupFlag(c.flags, "f3").SetValue("a")
lookupFlag(c.flags, "f3").SetValue("b")
// IsSet
if !c.IsSet("f1") {
t.Error("f1 is not visited")
}
if c.IsSet("f4") {
t.Error("f4 is visited")
}
// GetXXX
if c.GetString("f1") != "123" {
t.Error("f1 GetString is wrong")
}
if c.GetInt("f1") != 123 {
t.Error("f1 GetInt is wrong")
}
if c.GetInt8("f1") != 123 {
t.Error("f1 GetInt8 is wrong")
}
if c.GetInt16("f1") != 123 {
t.Error("f1 GetInt16 is wrong")
}
if c.GetInt32("f1") != 123 {
t.Error("f1 GetInt32 is wrong")
}
if c.GetInt64("f1") != 123 {
t.Error("f1 GetInt64 is wrong")
}
if c.GetUint("f1") != 123 {
t.Error("f1 GetUint is wrong")
}
if c.GetUint8("f1") != 123 {
t.Error("f1 GetUint8 is wrong")
}
if c.GetUint16("f1") != 123 {
t.Error("f1 GetUint16 is wrong")
}
if c.GetUint32("f1") != 123 {
t.Error("f1 GetUint32 is wrong")
}
if c.GetUint64("f1") != 123 {
t.Error("f1 GetUint64 is wrong")
}
if c.GetFloat32("f1") != 123 {
t.Error("f1 GetFloat32 is wrong")
}
if c.GetFloat64("f1") != 123 {
t.Error("f1 GetFloat64 is wrong")
}
// GetBool
if c.GetBool("f2") != true {
t.Error("f2 GetBool is wrong")
}
// GetStringSlice
if got := c.GetStringSlice("f3"); !reflect.DeepEqual(got, []string{"a", "b"}) {
t.Errorf("f3 GetStringSlice is wrong, got: %v", got)
}
}
func TestContextArg(t *testing.T) {
c := &Context{
args: []string{"a", "b", "c"},
}
if c.NArg() != 3 {
t.Error("NArg() != 3")
}
if c.Arg(0) != "a" {
t.Error("Arg(0) != 'a'")
}
if !reflect.DeepEqual(c.Args(), c.args) {
t.Error("Args() is wrong")
}
}
func TestContextParent(t *testing.T) {
p := &Context{name: "p"}
c := &Context{parent: p}
if c.Parent().Name() != "p" {
t.Error("Parent() is wrong")
}
if c.Global().Name() != "p" {
t.Error("Global() is wrong")
}
}