|
| 1 | +package dialog |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIsBinaryFile(t *testing.T) { |
| 12 | + tempDir := t.TempDir() |
| 13 | + |
| 14 | + // Text file |
| 15 | + textFile := filepath.Join(tempDir, "text.txt") |
| 16 | + err := os.WriteFile(textFile, []byte("Hello world, this is a plain text file."), 0644) |
| 17 | + assert.NoError(t, err) |
| 18 | + assert.False(t, IsBinaryFile(textFile)) |
| 19 | + |
| 20 | + // Binary file (contains null byte) |
| 21 | + binFile := filepath.Join(tempDir, "binary.bin") |
| 22 | + err = os.WriteFile(binFile, []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x57, 0x6f, 0x72, 0x6c, 0x64}, 0644) |
| 23 | + assert.NoError(t, err) |
| 24 | + assert.True(t, IsBinaryFile(binFile)) |
| 25 | + |
| 26 | + // Non-existent file |
| 27 | + assert.False(t, IsBinaryFile(filepath.Join(tempDir, "does-not-exist"))) |
| 28 | + assert.False(t, IsBinaryFile(DevNull)) |
| 29 | +} |
| 30 | + |
| 31 | +func TestFormatDiffText(t *testing.T) { |
| 32 | + diffInput := `--- old.txt |
| 33 | ++++ new.txt |
| 34 | +@@ -1,3 +1,3 @@ |
| 35 | +-Hello |
| 36 | ++World |
| 37 | + Unchanged` |
| 38 | + |
| 39 | + // Test without filtering headers |
| 40 | + outNoFilter := FormatDiffText(diffInput, false) |
| 41 | + assert.Contains(t, outNoFilter, "[red]-Hello[white]") |
| 42 | + assert.Contains(t, outNoFilter, "[green]+World[white]") |
| 43 | + assert.Contains(t, outNoFilter, "--- old.txt") |
| 44 | + assert.Contains(t, outNoFilter, "+++ new.txt") |
| 45 | + |
| 46 | + // Test with filtering headers |
| 47 | + outWithFilter := FormatDiffText(diffInput, true) |
| 48 | + assert.Contains(t, outWithFilter, "[red]-Hello[white]") |
| 49 | + assert.Contains(t, outWithFilter, "[green]+World[white]") |
| 50 | + assert.NotContains(t, outWithFilter, "--- old.txt") |
| 51 | + assert.NotContains(t, outWithFilter, "+++ new.txt") |
| 52 | +} |
0 commit comments