1 package exec_test
2
3 import (
4
5
6 "testing"
7
8 "github.com/noirbizarre/gonja/exec"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func failsafe(t *testing.T) {
13 if err := recover(); err != nil {
14 t.Error(err)
15 }
16 }
17
18 func TestVarArgs(t *testing.T) {
19 t.Run("first", testVAFirst)
20 t.Run("GetKwarg", testVAGetKwarg)
21 t.Run("Expect", testVAExpect)
22 }
23
24 func testVAFirst(t *testing.T) {
25 t.Run("nil if empty", func(t *testing.T) {
26 defer failsafe(t)
27 assert := assert.New(t)
28
29 va := exec.VarArgs{}
30 first := va.First()
31 assert.True(first.IsNil())
32 })
33 t.Run("first value", func(t *testing.T) {
34 defer failsafe(t)
35 assert := assert.New(t)
36
37 va := exec.VarArgs{Args: []*exec.Value{exec.AsValue(42)}}
38 first := va.First()
39 assert.Equal(42, first.Integer())
40 })
41 }
42
43 func testVAGetKwarg(t *testing.T) {
44 t.Run("value if found", func(t *testing.T) {
45 defer failsafe(t)
46 assert := assert.New(t)
47
48 va := exec.VarArgs{KwArgs: map[string]*exec.Value{
49 "key": exec.AsValue(42),
50 }}
51 kwarg := va.GetKwarg("key", "not found")
52 assert.Equal(42, kwarg.Integer())
53 })
54 t.Run("defaut if missing", func(t *testing.T) {
55 defer failsafe(t)
56 assert := assert.New(t)
57
58 va := exec.VarArgs{}
59 kwarg := va.GetKwarg("missing", "not found")
60 assert.Equal("not found", kwarg.String())
61 })
62 }
63
64 var nothingCases = []struct {
65 name string
66 va *exec.VarArgs
67 error string
68 }{
69 {"got nothing", &exec.VarArgs{}, ""}, {
70 "got an argument",
71 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42)}},
72 `Unexpected argument '42'`,
73 }, {
74 "got multiples arguments",
75 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42), exec.AsValue(7)}},
76 `Unexpected arguments '42, 7'`,
77 }, {
78 "got a keyword argument",
79 &exec.VarArgs{KwArgs: map[string]*exec.Value{
80 "key": exec.AsValue(42),
81 }},
82 `Unexpected keyword argument 'key=42'`,
83 }, {
84 "got multiple keyword arguments",
85 &exec.VarArgs{KwArgs: map[string]*exec.Value{
86 "key": exec.AsValue(42),
87 "other": exec.AsValue(7),
88 }},
89 `Unexpected keyword arguments 'key=42, other=7'`,
90 }, {
91 "got one of each",
92 &exec.VarArgs{
93 Args: []*exec.Value{exec.AsValue(42)},
94 KwArgs: map[string]*exec.Value{
95 "key": exec.AsValue(42),
96 },
97 },
98 `Unexpected arguments '42, key=42'`,
99 },
100 }
101
102 var argsCases = []struct {
103 name string
104 va *exec.VarArgs
105 args int
106 error string
107 }{
108 {
109 "got expected",
110 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42), exec.AsValue(7)}},
111 2, "",
112 }, {
113 "got less arguments",
114 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42)}},
115 2, `Expected 2 arguments, got 1`,
116 }, {
117 "got less arguments (singular)",
118 &exec.VarArgs{},
119 1, `Expected an argument, got 0`,
120 }, {
121 "got more arguments",
122 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42), exec.AsValue(7)}},
123 1, `Unexpected argument '7'`,
124 }, {
125 "got a keyword argument",
126 &exec.VarArgs{
127 Args: []*exec.Value{exec.AsValue(42)},
128 KwArgs: map[string]*exec.Value{
129 "key": exec.AsValue(42),
130 },
131 },
132 1, `Unexpected keyword argument 'key=42'`,
133 },
134 }
135
136 var kwargsCases = []struct {
137 name string
138 va *exec.VarArgs
139 kwargs []*exec.KwArg
140 error string
141 }{
142 {
143 "got expected",
144 &exec.VarArgs{KwArgs: map[string]*exec.Value{
145 "key": exec.AsValue(42),
146 "other": exec.AsValue(7),
147 }},
148 []*exec.KwArg{
149 {"key", "default key"},
150 {"other", "default other"},
151 },
152 "",
153 }, {
154 "got unexpected arguments",
155 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42), exec.AsValue(7), exec.AsValue("unexpected")}},
156 []*exec.KwArg{
157 {"key", "default key"},
158 {"other", "default other"},
159 },
160 `Unexpected argument 'unexpected'`,
161 }, {
162 "got an unexpected keyword argument",
163 &exec.VarArgs{KwArgs: map[string]*exec.Value{
164 "unknown": exec.AsValue(42),
165 }},
166 []*exec.KwArg{
167 {"key", "default key"},
168 {"other", "default other"},
169 },
170 `Unexpected keyword argument 'unknown=42'`,
171 }, {
172 "got multiple keyword arguments",
173 &exec.VarArgs{KwArgs: map[string]*exec.Value{
174 "unknown": exec.AsValue(42),
175 "seven": exec.AsValue(7),
176 }},
177 []*exec.KwArg{
178 {"key", "default key"},
179 {"other", "default other"},
180 },
181 `Unexpected keyword arguments 'seven=7, unknown=42'`,
182 },
183 }
184
185 var mixedArgsKwargsCases = []struct {
186 name string
187 va *exec.VarArgs
188 args int
189 kwargs []*exec.KwArg
190 expected *exec.VarArgs
191 error string
192 }{
193 {
194 "got expected",
195 &exec.VarArgs{
196 Args: []*exec.Value{exec.AsValue(42)},
197 KwArgs: map[string]*exec.Value{
198 "key": exec.AsValue(42),
199 "other": exec.AsValue(7),
200 },
201 },
202 1,
203 []*exec.KwArg{
204 {"key", "default key"},
205 {"other", "default other"},
206 },
207 &exec.VarArgs{
208 Args: []*exec.Value{exec.AsValue(42)},
209 KwArgs: map[string]*exec.Value{
210 "key": exec.AsValue(42),
211 "other": exec.AsValue(7),
212 },
213 },
214 "",
215 },
216 {
217 "fill with default",
218 &exec.VarArgs{Args: []*exec.Value{exec.AsValue(42)}},
219 1,
220 []*exec.KwArg{
221 {"key", "default key"},
222 {"other", "default other"},
223 },
224 &exec.VarArgs{
225 Args: []*exec.Value{exec.AsValue(42)},
226 KwArgs: map[string]*exec.Value{
227 "key": exec.AsValue("default key"),
228 "other": exec.AsValue("default other"),
229 },
230 },
231 "",
232 },
233 {
234 "keyword as argument",
235 &exec.VarArgs{
236 Args: []*exec.Value{exec.AsValue(42), exec.AsValue(42)},
237 KwArgs: map[string]*exec.Value{
238 "other": exec.AsValue(7),
239 },
240 },
241 1,
242 []*exec.KwArg{
243 {"key", "default key"},
244 {"other", "default other"},
245 },
246 &exec.VarArgs{
247 Args: []*exec.Value{exec.AsValue(42)},
248 KwArgs: map[string]*exec.Value{
249 "key": exec.AsValue(42),
250 "other": exec.AsValue(7),
251 },
252 },
253 "",
254 },
255 {
256 "keyword submitted twice",
257 &exec.VarArgs{
258 Args: []*exec.Value{exec.AsValue(42), exec.AsValue(5)},
259 KwArgs: map[string]*exec.Value{
260 "key": exec.AsValue(42),
261 "other": exec.AsValue(7),
262 },
263 },
264 1,
265 []*exec.KwArg{
266 {"key", "default key"},
267 {"other", "default other"},
268 },
269 &exec.VarArgs{
270 Args: []*exec.Value{exec.AsValue(42), exec.AsValue(5)},
271 KwArgs: map[string]*exec.Value{
272 "key": exec.AsValue(42),
273 "other": exec.AsValue(7),
274 },
275 },
276 `Keyword 'key' has been submitted twice`,
277 },
278 }
279
280 func assertError(t *testing.T, rva *exec.ReducedVarArgs, expected string) {
281 assert := assert.New(t)
282 if len(expected) > 0 {
283 if assert.True(rva.IsError(), "Should have returned an error") {
284 assert.Equal(expected, rva.Error())
285 }
286 } else {
287 assert.Falsef(rva.IsError(), "Unexpected error: %s", rva.Error())
288 }
289 }
290
291 func testVAExpect(t *testing.T) {
292 t.Run("nothing", func(t *testing.T) {
293 for _, tc := range nothingCases {
294 test := tc
295 t.Run(test.name, func(t *testing.T) {
296 defer failsafe(t)
297 rva := test.va.ExpectNothing()
298 assertError(t, rva, test.error)
299 })
300 }
301 })
302 t.Run("arguments", func(t *testing.T) {
303 for _, tc := range argsCases {
304 test := tc
305 t.Run(test.name, func(t *testing.T) {
306 defer failsafe(t)
307 rva := test.va.ExpectArgs(test.args)
308 assertError(t, rva, test.error)
309 })
310 }
311 })
312 t.Run("keyword arguments", func(t *testing.T) {
313 for _, tc := range kwargsCases {
314 test := tc
315 t.Run(test.name, func(t *testing.T) {
316 defer failsafe(t)
317 rva := test.va.Expect(0, test.kwargs)
318 assertError(t, rva, test.error)
319 })
320 }
321 })
322 t.Run("mixed argmuents", func(t *testing.T) {
323 for _, tc := range mixedArgsKwargsCases {
324 test := tc
325 t.Run(test.name, func(t *testing.T) {
326 defer failsafe(t)
327 assert := assert.New(t)
328 rva := test.va.Expect(test.args, test.kwargs)
329 assertError(t, rva, test.error)
330 if assert.Equal(len(test.expected.Args), len(rva.Args)) {
331 for idx, expected := range test.expected.Args {
332 arg := rva.Args[idx]
333 assert.Equalf(expected.Interface(), arg.Interface(),
334 `Argument %d mismatch: expected '%s' got '%s'`,
335 idx, expected.String(), arg.String(),
336 )
337 }
338 }
339 if assert.Equal(len(test.expected.KwArgs), len(rva.KwArgs)) {
340 for key, expected := range test.expected.KwArgs {
341 if assert.Contains(rva.KwArgs, key) {
342 value := rva.KwArgs[key]
343 assert.Equalf(expected.Interface(), value.Interface(),
344 `Keyword argument %s mismatch: expected '%s' got '%s'`,
345 key, expected.String(), value.String(),
346 )
347
348 }
349 }
350 }
351 })
352 }
353 })
354 }
355
View as plain text