| Conditions | 12 |
| Total Lines | 84 |
| Code Lines | 52 |
| 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.TestStateSave 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 |
||
| 11 | func TestStateSave(t *testing.T) { |
||
| 12 | // Setup test environment |
||
| 13 | originalDataFolder := dataFolder |
||
| 14 | dataFolder = ".hget_test/" |
||
| 15 | defer func() { |
||
| 16 | dataFolder = originalDataFolder |
||
| 17 | usr, _ := user.Current() |
||
| 18 | testFolder := filepath.Join(usr.HomeDir, dataFolder) |
||
| 19 | os.RemoveAll(testFolder) |
||
| 20 | }() |
||
| 21 | |||
| 22 | // Create test state |
||
| 23 | testURL := "http://example.com/test.zip" |
||
| 24 | s := &State{ |
||
| 25 | URL: testURL, |
||
| 26 | Parts: []Part{ |
||
| 27 | { |
||
| 28 | Index: 0, |
||
| 29 | URL: testURL, |
||
| 30 | Path: "temp_part0", |
||
| 31 | RangeFrom: 0, |
||
| 32 | RangeTo: 100, |
||
| 33 | }, |
||
| 34 | { |
||
| 35 | Index: 1, |
||
| 36 | URL: testURL, |
||
| 37 | Path: "temp_part1", |
||
| 38 | RangeFrom: 101, |
||
| 39 | RangeTo: 200, |
||
| 40 | }, |
||
| 41 | }, |
||
| 42 | } |
||
| 43 | |||
| 44 | // Create temporary files for parts |
||
| 45 | for _, part := range s.Parts { |
||
| 46 | err := os.WriteFile(part.Path, []byte("test content"), 0644) |
||
| 47 | if err != nil { |
||
| 48 | t.Fatalf("Failed to create test file: %v", err) |
||
| 49 | } |
||
| 50 | defer os.Remove(part.Path) // Cleanup in case the test fails |
||
| 51 | } |
||
| 52 | |||
| 53 | // Test Save method |
||
| 54 | err := s.Save() |
||
| 55 | if err != nil { |
||
| 56 | t.Fatalf("Save() failed: %v", err) |
||
| 57 | } |
||
| 58 | |||
| 59 | // Verify state file was created |
||
| 60 | folder := FolderOf(testURL) |
||
| 61 | stateFilePath := filepath.Join(folder, stateFileName) |
||
| 62 | |||
| 63 | if _, err := os.Stat(stateFilePath); os.IsNotExist(err) { |
||
| 64 | t.Fatalf("State file was not created at %s", stateFilePath) |
||
| 65 | } |
||
| 66 | |||
| 67 | // Verify content of the state file |
||
| 68 | stateBytes, err := os.ReadFile(stateFilePath) |
||
| 69 | if err != nil { |
||
| 70 | t.Fatalf("Could not read state file: %v", err) |
||
| 71 | } |
||
| 72 | |||
| 73 | var savedState State |
||
| 74 | err = json.Unmarshal(stateBytes, &savedState) |
||
| 75 | if err != nil { |
||
| 76 | t.Fatalf("Could not unmarshal state file: %v", err) |
||
| 77 | } |
||
| 78 | |||
| 79 | if savedState.URL != testURL { |
||
| 80 | t.Errorf("Expected URL %s, got %s", testURL, savedState.URL) |
||
| 81 | } |
||
| 82 | |||
| 83 | if len(savedState.Parts) != len(s.Parts) { |
||
| 84 | t.Errorf("Expected %d parts, got %d", len(s.Parts), len(savedState.Parts)) |
||
| 85 | } |
||
| 86 | |||
| 87 | // Verify part files were moved |
||
| 88 | for _, part := range s.Parts { |
||
| 89 | movedPath := filepath.Join(folder, filepath.Base(part.Path)) |
||
| 90 | if _, err := os.Stat(movedPath); os.IsNotExist(err) { |
||
| 91 | t.Errorf("Part file not moved to %s", movedPath) |
||
| 92 | } else { |
||
| 93 | // Clean up moved files |
||
| 94 | os.Remove(movedPath) |
||
| 95 | } |
||
| 188 |