Conditions | 6 |
Total Lines | 89 |
Code Lines | 66 |
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 |
||
13 | func TestMiscellaneousService_ApplePaymentSession(t *testing.T) { |
||
14 | setEnv() |
||
15 | defer unsetEnv() |
||
16 | |||
17 | type args struct { |
||
18 | ctx context.Context |
||
19 | appleSess *ApplePaymentSessionRequest |
||
20 | } |
||
21 | |||
22 | cases := []struct { |
||
23 | name string |
||
24 | args args |
||
25 | wantErr bool |
||
26 | err error |
||
27 | pre func() |
||
28 | handler http.HandlerFunc |
||
29 | }{ |
||
30 | { |
||
31 | "get apple payment session works as expected.", |
||
32 | args{ |
||
33 | context.Background(), |
||
34 | &ApplePaymentSessionRequest{ |
||
35 | Domain: "https://example.com", |
||
36 | }, |
||
37 | }, |
||
38 | false, |
||
39 | nil, |
||
40 | noPre, |
||
41 | func(w http.ResponseWriter, r *http.Request) { |
||
42 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
43 | testMethod(t, r, "POST") |
||
44 | |||
45 | if _, ok := r.Header[AuthHeader]; !ok { |
||
46 | w.WriteHeader(http.StatusUnauthorized) |
||
47 | } |
||
48 | _, _ = w.Write([]byte(testdata.ListMethodsResponse)) |
||
49 | }, |
||
50 | }, |
||
51 | { |
||
52 | "get apple payment session, an error is returned from the server", |
||
53 | args{ |
||
54 | context.Background(), |
||
55 | nil, |
||
56 | }, |
||
57 | true, |
||
58 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
59 | noPre, |
||
60 | errorHandler, |
||
61 | }, |
||
62 | { |
||
63 | "get apple payment session, an error occurs when parsing json", |
||
64 | args{ |
||
65 | context.Background(), |
||
66 | nil, |
||
67 | }, |
||
68 | true, |
||
69 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
70 | noPre, |
||
71 | encodingHandler, |
||
72 | }, |
||
73 | { |
||
74 | "get apple payment session, invalid url when building request", |
||
75 | args{ |
||
76 | context.Background(), |
||
77 | nil, |
||
78 | }, |
||
79 | true, |
||
80 | errBadBaseURL, |
||
81 | crashSrv, |
||
82 | errorHandler, |
||
83 | }, |
||
84 | } |
||
85 | |||
86 | for _, c := range cases { |
||
87 | setup() |
||
88 | defer teardown() |
||
89 | |||
90 | t.Run(c.name, func(t *testing.T) { |
||
91 | c.pre() |
||
92 | tMux.HandleFunc("/v2/wallets/applepay/sessions", c.handler) |
||
93 | |||
94 | res, m, err := tClient.Wallets.ApplePaymentSession(c.args.ctx, c.args.appleSess) |
||
95 | if c.wantErr { |
||
96 | assert.NotNil(t, err) |
||
97 | assert.EqualError(t, err, c.err.Error()) |
||
98 | } else { |
||
99 | assert.Nil(t, err) |
||
100 | assert.IsType(t, &ApplePaymentSession{}, m) |
||
101 | assert.IsType(t, &http.Response{}, res.Response) |
||
102 | } |
||
106 |