Conditions | 8 |
Total Lines | 85 |
Code Lines | 64 |
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 httpserver |
||
12 | func TestIndexHandler(t *testing.T) { |
||
13 | gerdu := lrucache.NewCache(2) |
||
14 | tests := []struct { |
||
15 | name string |
||
16 | r *http.Request |
||
17 | w *httptest.ResponseRecorder |
||
18 | expectedStatus int |
||
19 | expectedResponse string |
||
20 | }{ |
||
21 | { |
||
22 | name: "Put 1:1", |
||
23 | r: httptest.NewRequest(http.MethodPut, "/cache/1", strings.NewReader("1")), |
||
24 | w: httptest.NewRecorder(), |
||
25 | expectedStatus: http.StatusCreated, |
||
26 | }, |
||
27 | { |
||
28 | name: "Put 2:2", |
||
29 | r: httptest.NewRequest(http.MethodPut, "/cache/2", strings.NewReader("2")), |
||
30 | w: httptest.NewRecorder(), |
||
31 | expectedStatus: http.StatusCreated, |
||
32 | }, |
||
33 | { |
||
34 | name: "Put 3:3", |
||
35 | r: httptest.NewRequest(http.MethodPut, "/cache/3", strings.NewReader("3")), |
||
36 | w: httptest.NewRecorder(), |
||
37 | expectedStatus: http.StatusCreated, |
||
38 | }, |
||
39 | { |
||
40 | name: "Get 2:2", |
||
41 | r: httptest.NewRequest(http.MethodGet, "/cache/2", nil), |
||
42 | w: httptest.NewRecorder(), |
||
43 | expectedResponse: "2", |
||
44 | expectedStatus: http.StatusOK, |
||
45 | }, |
||
46 | { |
||
47 | name: "Get 3:3", |
||
48 | r: httptest.NewRequest(http.MethodGet, "/cache/3", nil), |
||
49 | w: httptest.NewRecorder(), |
||
50 | expectedResponse: "3", |
||
51 | expectedStatus: http.StatusOK, |
||
52 | }, |
||
53 | { |
||
54 | name: "Get 1:1", |
||
55 | r: httptest.NewRequest(http.MethodGet, "/cache/1", nil), |
||
56 | w: httptest.NewRecorder(), |
||
57 | expectedStatus: http.StatusNotFound, |
||
58 | }, |
||
59 | { |
||
60 | name: "Delete 3:3", |
||
61 | r: httptest.NewRequest(http.MethodDelete, "/cache/3", nil), |
||
62 | w: httptest.NewRecorder(), |
||
63 | expectedStatus: http.StatusOK, |
||
64 | }, |
||
65 | { |
||
66 | name: "Get 3:3", |
||
67 | r: httptest.NewRequest(http.MethodGet, "/cache/3", nil), |
||
68 | w: httptest.NewRecorder(), |
||
69 | expectedResponse: "3", |
||
70 | expectedStatus: http.StatusNotFound, |
||
71 | }, |
||
72 | { |
||
73 | name: "Delete 3:3", |
||
74 | r: httptest.NewRequest(http.MethodDelete, "/cache/3", nil), |
||
75 | w: httptest.NewRecorder(), |
||
76 | expectedResponse: "3", |
||
77 | expectedStatus: http.StatusNotFound, |
||
78 | }, |
||
79 | } |
||
80 | for _, test := range tests { |
||
81 | test := test |
||
82 | t.Run(test.name, func(t *testing.T) { |
||
83 | router := mux.NewRouter() |
||
84 | router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
||
85 | switch test.r.Method { |
||
86 | case http.MethodPut: |
||
87 | putHandler(w, r, gerdu) |
||
88 | case http.MethodGet: |
||
89 | getHandler(w, r, gerdu) |
||
90 | case http.MethodDelete: |
||
91 | deleteHandler(w, r, gerdu) |
||
92 | } |
||
93 | }) |
||
94 | router.ServeHTTP(test.w, test.r) |
||
95 | if test.w.Code != test.expectedStatus { |
||
96 | t.Errorf("Failed to produce expected status code %d, got %d", test.expectedStatus, test.w.Code) |
||
97 | } |
||
101 |