Conditions | 6 |
Total Lines | 111 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package mollie |
||
294 | func TestCapturesService_List(t *testing.T) { |
||
295 | setEnv() |
||
296 | defer unsetEnv() |
||
297 | |||
298 | type args struct { |
||
299 | ctx context.Context |
||
300 | payment string |
||
301 | capture string |
||
302 | options *CaptureOptions |
||
303 | } |
||
304 | |||
305 | type key string |
||
306 | |||
307 | cases := []struct { |
||
308 | name string |
||
309 | args args |
||
310 | wantErr bool |
||
311 | err error |
||
312 | handler http.HandlerFunc |
||
313 | pre func() |
||
314 | }{ |
||
315 | { |
||
316 | "list captures works as expected", |
||
317 | args{ |
||
318 | context.Background(), |
||
319 | "tr_WDqYK6vllg", |
||
320 | "cpt_4qqhO89gsT", |
||
321 | &CaptureOptions{ |
||
322 | Embed: []EmbedValue{EmbedPayments}, |
||
323 | }, |
||
324 | }, |
||
325 | false, |
||
326 | nil, |
||
327 | func(w http.ResponseWriter, r *http.Request) { |
||
328 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
329 | testMethod(t, r, "GET") |
||
330 | testQuery(t, r, "embed=payments") |
||
331 | |||
332 | if _, ok := r.Header[AuthHeader]; !ok { |
||
333 | w.WriteHeader(http.StatusUnauthorized) |
||
334 | } |
||
335 | |||
336 | _, _ = w.Write([]byte(testdata.ListCapturesResponse)) |
||
337 | }, |
||
338 | noPre, |
||
339 | }, |
||
340 | { |
||
341 | "list captures returns an http error from the server", |
||
342 | args{ |
||
343 | context.WithValue(context.Background(), key("test"), "test-value"), |
||
344 | "tr_WDqYK6vllg", |
||
345 | "cpt_4qqhO89gsT", |
||
346 | &CaptureOptions{}, |
||
347 | }, |
||
348 | true, |
||
349 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
350 | errorHandler, |
||
351 | noPre, |
||
352 | }, |
||
353 | { |
||
354 | "list captures returns an error when creating the request", |
||
355 | args{ |
||
356 | context.Background(), |
||
357 | "tr_WDqYK6vllg", |
||
358 | "cpt_4qqhO89gsT", |
||
359 | &CaptureOptions{}, |
||
360 | }, |
||
361 | true, |
||
362 | errBadBaseURL, |
||
363 | errorHandler, |
||
364 | crashSrv, |
||
365 | }, |
||
366 | { |
||
367 | "list captures returns an error when trying to parse the json response", |
||
368 | args{ |
||
369 | context.Background(), |
||
370 | "tr_WDqYK6vllg", |
||
371 | "cpt_4qqhO89gsT", |
||
372 | &CaptureOptions{}, |
||
373 | }, |
||
374 | true, |
||
375 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
376 | encodingHandler, |
||
377 | noPre, |
||
378 | }, |
||
379 | } |
||
380 | |||
381 | for _, c := range cases { |
||
382 | setup() |
||
383 | defer teardown() |
||
384 | |||
385 | t.Run(c.name, func(t *testing.T) { |
||
386 | c.pre() |
||
387 | |||
388 | tMux.HandleFunc( |
||
389 | fmt.Sprintf( |
||
390 | "/v2/payments/%s/captures", |
||
391 | c.args.payment, |
||
392 | ), |
||
393 | c.handler, |
||
394 | ) |
||
395 | |||
396 | res, list, err := tClient.Captures.List(c.args.ctx, c.args.payment, c.args.options) |
||
397 | if c.wantErr { |
||
398 | assert.NotNil(t, err) |
||
399 | assert.EqualError(t, err, c.err.Error()) |
||
400 | } else { |
||
401 | assert.Nil(t, err) |
||
402 | assert.IsType(t, &CapturesList{}, list) |
||
403 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
404 | assert.IsType(t, &http.Response{}, res.Response) |
||
405 | } |
||
409 |