| Conditions | 7 |
| Total Lines | 71 |
| Code Lines | 54 |
| 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 logger |
||
| 14 | func TestLogger(t *testing.T) { |
||
| 15 | tests := []struct { |
||
| 16 | name string |
||
| 17 | level string |
||
| 18 | funcName string |
||
| 19 | message string |
||
| 20 | expectedMsg string |
||
| 21 | }{ |
||
| 22 | { |
||
| 23 | name: "error msg", |
||
| 24 | level: "ERROR", |
||
| 25 | funcName: "Error", |
||
| 26 | message: "This is error message", |
||
| 27 | expectedMsg: "ERROR:This is error message", |
||
| 28 | }, |
||
| 29 | { |
||
| 30 | name: "skipp_warn", |
||
| 31 | level: "ERROR", |
||
| 32 | funcName: "Warn", |
||
| 33 | message: "This is error message", |
||
| 34 | expectedMsg: "", |
||
| 35 | }, |
||
| 36 | { |
||
| 37 | name: "skipp_info", |
||
| 38 | level: "ERROR", |
||
| 39 | funcName: "Info", |
||
| 40 | message: "This is error message", |
||
| 41 | expectedMsg: "", |
||
| 42 | }, |
||
| 43 | { |
||
| 44 | name: "skipp_debug", |
||
| 45 | level: "ERROR", |
||
| 46 | funcName: "Debug", |
||
| 47 | message: "This is error message", |
||
| 48 | expectedMsg: "", |
||
| 49 | }, |
||
| 50 | { |
||
| 51 | name: "debug", |
||
| 52 | level: "DEBUG", |
||
| 53 | funcName: "Debug", |
||
| 54 | message: "This is error message", |
||
| 55 | expectedMsg: "DEBUG:This is error message", |
||
| 56 | }, |
||
| 57 | { |
||
| 58 | name: "skipp_debug2", |
||
| 59 | level: "INFO", |
||
| 60 | funcName: "Debug", |
||
| 61 | message: "This is error message", |
||
| 62 | expectedMsg: "", |
||
| 63 | }, |
||
| 64 | } |
||
| 65 | |||
| 66 | t.Parallel() |
||
| 67 | for _, tc := range tests { |
||
| 68 | t.Run(fmt.Sprintf("case %s", tc.name), func(t *testing.T) { |
||
| 69 | var b bytes.Buffer |
||
| 70 | tc := tc |
||
| 71 | l := NewLogger(tc.level, &b) |
||
| 72 | |||
| 73 | switch tc.funcName { |
||
| 74 | case "Error": |
||
| 75 | l.Errorf(tc.message) |
||
|
|
|||
| 76 | case "Warn": |
||
| 77 | l.Warningf(tc.message) |
||
| 78 | case "Info": |
||
| 79 | l.Infof(tc.message) |
||
| 80 | case "Debug": |
||
| 81 | l.Debugf(tc.message) |
||
| 82 | } |
||
| 83 | |||
| 84 | require.Equal(t, tc.expectedMsg, b.String(), "error output message") |
||
| 85 | }) |
||
| 102 |