Conditions | 3 |
Total Lines | 54 |
Code Lines | 31 |
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 urlpath_test |
||
12 | func TestJoin(t *testing.T) { |
||
13 | testCases := []struct { |
||
14 | input []string |
||
15 | expectedOutput string |
||
16 | }{ |
||
17 | { |
||
18 | input: []string{"/home"}, |
||
19 | expectedOutput: "/home", |
||
20 | }, |
||
21 | { |
||
22 | input: []string{"/home/"}, |
||
23 | expectedOutput: "/home/", |
||
24 | }, |
||
25 | { |
||
26 | input: []string{"/home/", "test"}, |
||
27 | expectedOutput: "/home/test", |
||
28 | }, |
||
29 | { |
||
30 | input: []string{"/home/", "/test"}, |
||
31 | expectedOutput: "/home/test", |
||
32 | }, |
||
33 | { |
||
34 | input: []string{"/home/", "/test/"}, |
||
35 | expectedOutput: "/home/test/", |
||
36 | }, |
||
37 | { |
||
38 | input: []string{"/home", "/test/"}, |
||
39 | expectedOutput: "/home/test/", |
||
40 | }, |
||
41 | { |
||
42 | input: []string{"/home", "test/"}, |
||
43 | expectedOutput: "/home/test/", |
||
44 | }, |
||
45 | { |
||
46 | input: []string{"/home", "test"}, |
||
47 | expectedOutput: "/home/test", |
||
48 | }, |
||
49 | } |
||
50 | |||
51 | for _, tc := range testCases { |
||
52 | tc := tc // Pinning ranged variable, more info: https://github.com/kyoh86/scopelint |
||
53 | |||
54 | scenario := fmt.Sprintf( |
||
55 | "Input: `%s`, Expected output: `%s`", |
||
56 | strings.Join(tc.input, ","), |
||
57 | tc.expectedOutput, |
||
58 | ) |
||
59 | |||
60 | t.Run(scenario, func(t *testing.T) { |
||
61 | t.Parallel() |
||
62 | |||
63 | output := urlpath.Join(tc.input...) |
||
64 | |||
65 | assert.Equal(t, tc.expectedOutput, output) |
||
66 | }) |
||
69 |