8 "github.com/aymanbagabas/go-udiff"
9 "github.com/charmbracelet/x/exp/golden"
12func TestUdiff(t *testing.T) {
13 before := `package main
20 fmt.Println("Hello, World!")
23 after := `package main
30 content := "Hello, World!"
34 t.Run("Unified", func(t *testing.T) {
35 content := udiff.Unified("main.go", "main.go", before, after)
36 golden.RequireEqual(t, []byte(content))
39 t.Run("ToUnifiedDiff", func(t *testing.T) {
40 toUnifiedDiff := func(t *testing.T, before, after string, contextLines int) udiff.UnifiedDiff {
41 edits := udiff.Strings(before, after)
42 unifiedDiff, err := udiff.ToUnifiedDiff("main.go", "main.go", before, edits, contextLines)
44 t.Fatalf("ToUnifiedDiff failed: %v", err)
48 toJSON := func(t *testing.T, unifiedDiff udiff.UnifiedDiff) []byte {
50 encoder := json.NewEncoder(&buff)
51 encoder.SetIndent("", " ")
52 if err := encoder.Encode(unifiedDiff); err != nil {
53 t.Fatalf("Failed to encode unified diff: %v", err)
58 t.Run("DefaultContextLines", func(t *testing.T) {
59 unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines)
61 t.Run("Content", func(t *testing.T) {
62 golden.RequireEqual(t, []byte(unifiedDiff.String()))
64 t.Run("JSON", func(t *testing.T) {
65 golden.RequireEqual(t, toJSON(t, unifiedDiff))
69 t.Run("DefaultContextLinesPlusOne", func(t *testing.T) {
70 unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines+1)
72 t.Run("Content", func(t *testing.T) {
73 golden.RequireEqual(t, []byte(unifiedDiff.String()))
75 t.Run("JSON", func(t *testing.T) {
76 golden.RequireEqual(t, toJSON(t, unifiedDiff))
80 t.Run("DefaultContextLinesPlusTwo", func(t *testing.T) {
81 unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines+2)
83 t.Run("Content", func(t *testing.T) {
84 golden.RequireEqual(t, []byte(unifiedDiff.String()))
86 t.Run("JSON", func(t *testing.T) {
87 golden.RequireEqual(t, toJSON(t, unifiedDiff))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list