| Conditions | 9 |
| Total Lines | 71 |
| Code Lines | 53 |
| 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("PUT", "/cache/1", strings.NewReader("1")), |
||
| 24 | w: httptest.NewRecorder(), |
||
| 25 | expectedStatus: http.StatusCreated, |
||
| 26 | }, |
||
| 27 | { |
||
| 28 | name: "Put 2:2", |
||
| 29 | r: httptest.NewRequest("PUT", "/cache/2", strings.NewReader("2")), |
||
| 30 | w: httptest.NewRecorder(), |
||
| 31 | expectedStatus: http.StatusCreated, |
||
| 32 | }, |
||
| 33 | { |
||
| 34 | name: "Put 3:3", |
||
| 35 | r: httptest.NewRequest("PUT", "/cache/3", strings.NewReader("3")), |
||
| 36 | w: httptest.NewRecorder(), |
||
| 37 | expectedStatus: http.StatusCreated, |
||
| 38 | }, |
||
| 39 | { |
||
| 40 | name: "Get 2:2", |
||
| 41 | r: httptest.NewRequest("GET", "/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("GET", "/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("GET", "/cache/1", nil), |
||
| 56 | w: httptest.NewRecorder(), |
||
| 57 | expectedStatus: http.StatusNotFound, |
||
| 58 | }, |
||
| 59 | } |
||
| 60 | for _, test := range tests { |
||
| 61 | test := test |
||
| 62 | t.Run(test.name, func(t *testing.T) { |
||
| 63 | if strings.HasPrefix(test.name, "Put") { |
||
| 64 | router := mux.NewRouter() |
||
| 65 | router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
||
| 66 | putHandler(w, r, gerdu, false) |
||
| 67 | }) |
||
| 68 | router.ServeHTTP(test.w, test.r) |
||
| 69 | |||
| 70 | if test.w.Code != test.expectedStatus { |
||
| 71 | t.Errorf("Failed to produce expected status code %d, got %d", test.expectedStatus, test.w.Code) |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | router := mux.NewRouter() |
||
| 75 | router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
||
| 76 | getHandler(w, r, gerdu, false) |
||
| 77 | }) |
||
| 78 | router.ServeHTTP(test.w, test.r) |
||
| 79 | |||
| 80 | if test.w.Code != test.expectedStatus || test.expectedResponse != test.w.Body.String() { |
||
| 81 | t.Errorf("Failed to produce expected result %d, %s, got %d, %s", |
||
| 82 | test.expectedStatus, test.expectedResponse, test.w.Code, test.w.Body.String()) |
||
| 83 | } |
||
| 89 |