|
| 1 | +package domain |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewInMemoryClass_StoresName(t *testing.T) { |
| 13 | + class := NewInMemoryClass("Ünîcödé.java", "/path/Ünîcödé.java", "content") |
| 14 | + assert.Equal(t, "Ünîcödé.java", class.Name(), "Class name must match the provided value") |
| 15 | +} |
| 16 | + |
| 17 | +func TestNewInMemoryClass_StoresPath(t *testing.T) { |
| 18 | + class := NewInMemoryClass("Tëst.java", "/ûsr/src/Tëst.java", "content") |
| 19 | + assert.Equal(t, "/ûsr/src/Tëst.java", class.Path(), "Class path must match the provided value") |
| 20 | +} |
| 21 | + |
| 22 | +func TestNewInMemoryClass_StoresContent(t *testing.T) { |
| 23 | + content := "public class Mÿ { String ñ = \"Héllo\"; }" |
| 24 | + class := NewInMemoryClass("Mÿ.java", "/src/Mÿ.java", content) |
| 25 | + assert.Equal(t, content, class.Content(), "Class content must match the provided value") |
| 26 | +} |
| 27 | + |
| 28 | +func TestMemClass_SetContent_UpdatesContent(t *testing.T) { |
| 29 | + class := NewInMemoryClass("Çlàss.java", "/Çlàss.java", "old content") |
| 30 | + err := class.SetContent("new çöntênt") |
| 31 | + require.NoError(t, err, "SetContent unexpectedly returned an error") |
| 32 | + assert.Equal(t, "new çöntênt", class.Content(), "Content must reflect the updated value") |
| 33 | +} |
| 34 | + |
| 35 | +func TestMemClass_SetContent_ReturnsNil(t *testing.T) { |
| 36 | + class := NewInMemoryClass("Àny.java", "/Àny.java", "initial") |
| 37 | + err := class.SetContent("updated") |
| 38 | + assert.NoError(t, err, "SetContent must not return an error for in-memory class") |
| 39 | +} |
| 40 | + |
| 41 | +func TestNewFSClass_StoresName(t *testing.T) { |
| 42 | + class := NewFSClass("Fïlé.java", "/tmp/Fïlé.java") |
| 43 | + assert.Equal(t, "Fïlé.java", class.Name(), "Class name must match the provided value") |
| 44 | +} |
| 45 | + |
| 46 | +func TestNewFSClass_StoresPath(t *testing.T) { |
| 47 | + class := NewFSClass("Päth.java", "/vàr/Päth.java") |
| 48 | + assert.Equal(t, "/vàr/Päth.java", class.Path(), "Class path must match the provided value") |
| 49 | +} |
| 50 | + |
| 51 | +func TestFSClass_Content_ReadsFromFile(t *testing.T) { |
| 52 | + tmp := t.TempDir() |
| 53 | + path := filepath.Join(tmp, "Rëàd.java") |
| 54 | + expected := "class Rëàd { String µ = \"tëst\"; }" |
| 55 | + require.NoError(t, os.WriteFile(path, []byte(expected), 0o600), "failed to write test file") |
| 56 | + class := NewFSClass("Rëàd.java", path) |
| 57 | + assert.Equal(t, expected, class.Content(), "Content must match the file contents") |
| 58 | +} |
| 59 | + |
| 60 | +func TestFSClass_SetContent_WritesToFile(t *testing.T) { |
| 61 | + tmp := t.TempDir() |
| 62 | + path := filepath.Join(tmp, "Wrïtë.java") |
| 63 | + require.NoError(t, os.WriteFile(path, []byte("initial"), 0o600), "failed to write test file") |
| 64 | + class := NewFSClass("Wrïtë.java", path) |
| 65 | + expected := "class Wrïtë { int ä = 42; }" |
| 66 | + err := class.SetContent(expected) |
| 67 | + require.NoError(t, err, "SetContent unexpectedly returned an error") |
| 68 | + content, err := os.ReadFile(path) |
| 69 | + require.NoError(t, err, "failed to read test file") |
| 70 | + assert.Equal(t, expected, string(content), "File content must match the updated value") |
| 71 | +} |
| 72 | + |
| 73 | +func TestFSClass_SetContent_ErrorOnInvalidPath(t *testing.T) { |
| 74 | + class := NewFSClass("Ïnvàlîd.java", "/nönëxîstënt/Ïnvàlîd.java") |
| 75 | + err := class.SetContent("content") |
| 76 | + assert.Error(t, err, "SetContent must return an error for invalid path") |
| 77 | +} |
0 commit comments