Conditions | 14 |
Total Lines | 81 |
Code Lines | 51 |
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:
Complex classes like main.TestPartCalculate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package main |
||
12 | func TestPartCalculate(t *testing.T) { |
||
13 | // Disable progress bar for tests |
||
14 | displayProgress = false |
||
15 | |||
16 | // Setup test environment |
||
17 | originalDataFolder := dataFolder |
||
18 | dataFolder = ".hget_test/" |
||
19 | defer func() { |
||
20 | dataFolder = originalDataFolder |
||
21 | usr, _ := user.Current() |
||
22 | testFolder := filepath.Join(usr.HomeDir, dataFolder) |
||
23 | os.RemoveAll(testFolder) |
||
24 | }() |
||
25 | |||
26 | // Test with different numbers of parts |
||
27 | testCases := []struct { |
||
28 | parts int64 |
||
29 | totalSize int64 |
||
30 | url string |
||
31 | expectParts int |
||
32 | }{ |
||
33 | {10, 100, "http://foo.bar/file", 10}, |
||
34 | {5, 1000, "http://example.com/largefile", 5}, |
||
35 | {1, 50, "http://test.org/smallfile", 1}, |
||
36 | {3, 10, "http://tiny.file/data", 3}, // Small file, multiple parts |
||
37 | } |
||
38 | |||
39 | for _, tc := range testCases { |
||
40 | parts := partCalculate(tc.parts, tc.totalSize, tc.url) |
||
41 | |||
42 | // Check number of parts |
||
43 | if len(parts) != tc.expectParts { |
||
44 | t.Errorf("Expected %d parts, got %d", tc.expectParts, len(parts)) |
||
45 | } |
||
46 | |||
47 | // Check part URLs |
||
48 | for i, part := range parts { |
||
49 | if part.URL != tc.url { |
||
50 | t.Errorf("Part %d: Expected URL %s, got %s", i, tc.url, part.URL) |
||
51 | } |
||
52 | |||
53 | // Check part index |
||
54 | if part.Index != int64(i) { |
||
55 | t.Errorf("Part %d: Expected Index %d, got %d", i, i, part.Index) |
||
56 | } |
||
57 | |||
58 | // Check ranges |
||
59 | expectedSize := tc.totalSize / tc.parts |
||
60 | if i < int(tc.parts-1) { |
||
61 | if part.RangeFrom != expectedSize*int64(i) { |
||
62 | t.Errorf("Part %d: Expected RangeFrom %d, got %d", |
||
63 | i, expectedSize*int64(i), part.RangeFrom) |
||
64 | } |
||
65 | if part.RangeTo != expectedSize*int64(i+1)-1 { |
||
66 | t.Errorf("Part %d: Expected RangeTo %d, got %d", |
||
67 | i, expectedSize*int64(i+1)-1, part.RangeTo) |
||
68 | } |
||
69 | } else { |
||
70 | // Last part might be larger due to division remainder |
||
71 | if part.RangeFrom != expectedSize*int64(i) { |
||
72 | t.Errorf("Part %d: Expected RangeFrom %d, got %d", |
||
73 | i, expectedSize*int64(i), part.RangeFrom) |
||
74 | } |
||
75 | if part.RangeTo != tc.totalSize { |
||
76 | t.Errorf("Part %d: Expected RangeTo %d, got %d", |
||
77 | i, tc.totalSize, part.RangeTo) |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // Check path format |
||
82 | usr, _ := user.Current() |
||
83 | expectedBasePath := filepath.Join(usr.HomeDir, dataFolder) |
||
84 | if !strings.Contains(part.Path, expectedBasePath) { |
||
85 | t.Errorf("Part %d: Path does not contain expected base path: %s", i, part.Path) |
||
86 | } |
||
87 | |||
88 | fileName := filepath.Base(part.Path) |
||
89 | expectedPrefix := TaskFromURL(tc.url) + ".part" |
||
90 | if !strings.HasPrefix(fileName, expectedPrefix) { |
||
91 | t.Errorf("Part %d: Expected filename prefix %s, got %s", |
||
92 | i, expectedPrefix, fileName) |
||
93 | } |
||
334 |