Conditions | 8 |
Total Lines | 112 |
Code Lines | 84 |
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 |
||
110 | func TestTerminalsService_List(t *testing.T) { |
||
111 | setEnv() |
||
112 | defer unsetEnv() |
||
113 | |||
114 | type args struct { |
||
115 | ctx context.Context |
||
116 | options *ListTerminalsOptions |
||
117 | } |
||
118 | |||
119 | cases := []struct { |
||
120 | name string |
||
121 | args args |
||
122 | wantErr bool |
||
123 | err error |
||
124 | want string |
||
125 | pre func() |
||
126 | handler http.HandlerFunc |
||
127 | }{ |
||
128 | { |
||
129 | "list terminals correctly", |
||
130 | args{ |
||
131 | context.Background(), |
||
132 | &ListTerminalsOptions{}, |
||
133 | }, |
||
134 | false, |
||
135 | nil, |
||
136 | testdata.GetTerminalResponse, |
||
137 | noPre, |
||
138 | func(w http.ResponseWriter, r *http.Request) { |
||
139 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
140 | testMethod(t, r, "GET") |
||
141 | |||
142 | if _, ok := r.Header[AuthHeader]; !ok { |
||
143 | w.WriteHeader(http.StatusUnauthorized) |
||
144 | } |
||
145 | _, _ = w.Write([]byte(testdata.ListTerminalsResponse)) |
||
146 | }, |
||
147 | }, |
||
148 | { |
||
149 | "list terminals correctly with an access token", |
||
150 | args{ |
||
151 | context.Background(), |
||
152 | &ListTerminalsOptions{}, |
||
153 | }, |
||
154 | false, |
||
155 | nil, |
||
156 | testdata.GetTerminalResponse, |
||
157 | setAccessToken, |
||
158 | func(w http.ResponseWriter, r *http.Request) { |
||
159 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
160 | testMethod(t, r, "GET") |
||
161 | |||
162 | if _, ok := r.Header[AuthHeader]; !ok { |
||
163 | w.WriteHeader(http.StatusUnauthorized) |
||
164 | } |
||
165 | _, _ = w.Write([]byte(testdata.ListTerminalsResponse)) |
||
166 | }, |
||
167 | }, |
||
168 | { |
||
169 | "get terminals list, an error is returned from the server", |
||
170 | args{ |
||
171 | context.Background(), |
||
172 | nil, |
||
173 | }, |
||
174 | true, |
||
175 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
176 | "", |
||
177 | noPre, |
||
178 | errorHandler, |
||
179 | }, |
||
180 | { |
||
181 | "get terminals list, an error occurs when parsing json", |
||
182 | args{ |
||
183 | context.Background(), |
||
184 | nil, |
||
185 | }, |
||
186 | true, |
||
187 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
188 | "", |
||
189 | noPre, |
||
190 | encodingHandler, |
||
191 | }, |
||
192 | { |
||
193 | "get terminals list, invalid url when building request", |
||
194 | args{ |
||
195 | context.Background(), |
||
196 | nil, |
||
197 | }, |
||
198 | true, |
||
199 | errBadBaseURL, |
||
200 | "", |
||
201 | crashSrv, |
||
202 | errorHandler, |
||
203 | }, |
||
204 | } |
||
205 | |||
206 | for _, c := range cases { |
||
207 | setup() |
||
208 | defer teardown() |
||
209 | |||
210 | t.Run(c.name, func(t *testing.T) { |
||
211 | c.pre() |
||
212 | tMux.HandleFunc("/v2/terminals", c.handler) |
||
213 | |||
214 | res, m, err := tClient.Terminals.List(c.args.ctx, c.args.options) |
||
215 | if c.wantErr { |
||
216 | assert.NotNil(t, err) |
||
217 | assert.EqualError(t, err, c.err.Error()) |
||
218 | } else { |
||
219 | assert.Nil(t, err) |
||
220 | assert.IsType(t, &TerminalList{}, m) |
||
221 | assert.IsType(t, &http.Response{}, res.Response) |
||
222 | } |
||
226 |