| Conditions | 13 |
| Total Lines | 78 |
| Code Lines | 50 |
| 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.TestRead 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 |
||
| 99 | func TestRead(t *testing.T) { |
||
| 100 | // Setup test environment |
||
| 101 | originalDataFolder := dataFolder |
||
| 102 | dataFolder = ".hget_test/" |
||
| 103 | defer func() { |
||
| 104 | dataFolder = originalDataFolder |
||
| 105 | usr, _ := user.Current() |
||
| 106 | testFolder := filepath.Join(usr.HomeDir, dataFolder) |
||
| 107 | os.RemoveAll(testFolder) |
||
| 108 | }() |
||
| 109 | |||
| 110 | // Create test data |
||
| 111 | testURL := "http://example.com/test.zip" |
||
| 112 | testState := &State{ |
||
| 113 | URL: testURL, |
||
| 114 | Parts: []Part{ |
||
| 115 | { |
||
| 116 | Index: 0, |
||
| 117 | URL: testURL, |
||
| 118 | Path: "part0", |
||
| 119 | RangeFrom: 0, |
||
| 120 | RangeTo: 100, |
||
| 121 | }, |
||
| 122 | { |
||
| 123 | Index: 1, |
||
| 124 | URL: testURL, |
||
| 125 | Path: "part1", |
||
| 126 | RangeFrom: 101, |
||
| 127 | RangeTo: 200, |
||
| 128 | }, |
||
| 129 | }, |
||
| 130 | } |
||
| 131 | |||
| 132 | // Set up directory structure |
||
| 133 | usr, _ := user.Current() |
||
| 134 | homeDir := usr.HomeDir |
||
| 135 | taskName := TaskFromURL(testURL) |
||
| 136 | folderPath := filepath.Join(homeDir, dataFolder, taskName) |
||
| 137 | stateFilePath := filepath.Join(folderPath, stateFileName) |
||
| 138 | |||
| 139 | // Create directory |
||
| 140 | err := os.MkdirAll(folderPath, 0755) |
||
| 141 | if err != nil { |
||
| 142 | t.Fatalf("Failed to create test directory: %v", err) |
||
| 143 | } |
||
| 144 | |||
| 145 | // Write test state file |
||
| 146 | stateData, err := json.Marshal(testState) |
||
| 147 | if err != nil { |
||
| 148 | t.Fatalf("Failed to marshal test state: %v", err) |
||
| 149 | } |
||
| 150 | |||
| 151 | err = os.WriteFile(stateFilePath, stateData, 0644) |
||
| 152 | if err != nil { |
||
| 153 | t.Fatalf("Failed to write test state file: %v", err) |
||
| 154 | } |
||
| 155 | |||
| 156 | // Test Read function |
||
| 157 | state, err := Read(testURL) |
||
| 158 | if err != nil { |
||
| 159 | t.Fatalf("Read() failed: %v", err) |
||
| 160 | } |
||
| 161 | |||
| 162 | // Verify the read state matches the test state |
||
| 163 | if state.URL != testState.URL { |
||
| 164 | t.Errorf("Expected URL %s, got %s", testState.URL, state.URL) |
||
| 165 | } |
||
| 166 | |||
| 167 | if len(state.Parts) != len(testState.Parts) { |
||
| 168 | t.Errorf("Expected %d parts, got %d", len(testState.Parts), len(state.Parts)) |
||
| 169 | } |
||
| 170 | |||
| 171 | for i, part := range state.Parts { |
||
| 172 | if part.Index != testState.Parts[i].Index || |
||
| 173 | part.URL != testState.Parts[i].URL || |
||
| 174 | part.RangeFrom != testState.Parts[i].RangeFrom || |
||
| 175 | part.RangeTo != testState.Parts[i].RangeTo { |
||
| 176 | t.Errorf("Part %d does not match expected values", i) |
||
| 177 | } |
||
| 188 |