-
Notifications
You must be signed in to change notification settings - Fork 16
/
context_test.go
148 lines (128 loc) · 3.53 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
package climax
import (
"reflect"
"testing"
)
func TestContext(t *testing.T) {
app := &Application{}
check := func(c string, f []Flag, a []string, exp Context) {
exp.app = app
ctx, err := app.parseContext(f, a)
if err != nil {
t.Errorf(`case "%s" didn't finish well:`, c)
t.Logf(`error: %s`, err)
return
}
if !reflect.DeepEqual(*ctx, exp) {
t.Errorf(`case "%s" didn't finish well:`, c)
t.Logf("- expected:\n%s", exp.String())
t.Logf("- recieved:\n%s", ctx.String())
}
}
mustFail := func(c string, f []Flag, a []string) {
_, err := app.parseContext(f, a)
if err == nil {
t.Errorf(`invalid case "%s" resulted in valid context`, c)
}
}
// PASS TESTS
// ==========
check("no args", []Flag{}, []string{}, *newContext(app))
check("no flags", []Flag{}, []string{"argument", "a thing"}, Context{
Args: []string{"argument", "a thing"},
NonVariable: map[string]bool{},
Variable: map[string]string{},
})
check("single non-var flag", []Flag{
Flag{Name: "force"},
}, []string{"--force", "hard life in ghetto"}, Context{
Args: []string{"hard life in ghetto"},
NonVariable: map[string]bool{
"force": true,
},
Variable: map[string]string{},
})
check("single shortened non-var flag", []Flag{
Flag{Name: "force", Short: "f"},
}, []string{"-f", "hard life in ghetto"}, Context{
Args: []string{"hard life in ghetto"},
NonVariable: map[string]bool{
"force": true,
},
Variable: map[string]string{},
})
check("separated variable flag", []Flag{
Flag{Name: "filter", Variable: true},
}, []string{"--filter", "token here"}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "token here",
},
NonVariable: map[string]bool{},
})
check("joined empty variable flag", []Flag{
Flag{Name: "filter", Variable: true},
}, []string{`--filter=`}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "",
},
NonVariable: map[string]bool{},
})
check("joined single-word variable flag", []Flag{
Flag{Name: "filter", Variable: true},
}, []string{`--filter=token`}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "token",
},
NonVariable: map[string]bool{},
})
check("joined multi-word variable flag", []Flag{
Flag{Name: "filter", Variable: true},
}, []string{`--filter=token here`}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "token here",
},
NonVariable: map[string]bool{},
})
check("joined single-word shortened variable flag", []Flag{
Flag{Name: "filter", Short: "f", Variable: true},
}, []string{`-f=token`}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "token",
},
NonVariable: map[string]bool{},
})
check("joined multi-word shortened variable flag", []Flag{
Flag{Name: "filter", Short: "f", Variable: true},
}, []string{`-f=token here`}, Context{
Args: []string{},
Variable: map[string]string{
"filter": "token here",
},
NonVariable: map[string]bool{},
})
check("sophisticated", []Flag{
Flag{Name: "force", Short: "f", Variable: false},
Flag{Name: "slug", Variable: true},
}, []string{"-f", "--slug", "dog_03", "Dog Doggson"}, Context{
Args: []string{"Dog Doggson"},
NonVariable: map[string]bool{
"force": true,
},
Variable: map[string]string{
"slug": "dog_03",
},
})
// FAIL TESTS
// ==========
mustFail("setting non-var flag", []Flag{
Flag{Name: "force", Variable: false},
}, []string{"--force=value"})
mustFail("missing var flag value", []Flag{
Flag{Name: "filter", Variable: true},
}, []string{"--filter"})
}