on main help sign in
chore: having some fun with the `go-udiff` package
commit7de40d99cf8197b51a5c3f169292c98b04af6fbc authorAndrey Nering <andreynering@users.noreply.github.com> dateMon 2 Jun 2025, 18:02 (1 year ago) parent05b0570
11 files changed+275−2
go.mod+1 go.sum+2−2 internal/exp/diff/diff.go+1 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLines/Content.golden+11 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLines/JSON.golden+40 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusOne/Content.golden+12 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusOne/JSON.golden+44 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusTwo/Content.golden+13 internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusTwo/JSON.golden+48 internal/exp/diff/testdata/TestUdiff/Unified.golden+11 internal/exp/diff/udiff_test.go+92
go.mod+1
@@ -16,6 +16,7 @@ require (
1616 github.com/charmbracelet/glamour v0.9.1
1717 github.com/charmbracelet/lipgloss v1.1.0
1818 github.com/charmbracelet/x/ansi v0.8.0
19+ github.com/charmbracelet/x/exp/golden v0.0.0-20250602192518-9e722df69bbb
1920 github.com/fsnotify/fsnotify v1.8.0
2021 github.com/go-logfmt/logfmt v0.6.0
2122 github.com/google/uuid v1.6.0
go.sum+2−2
@@ -82,8 +82,8 @@ github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2ll
8282github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
8383github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
8484github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
85−github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
86−github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
85+github.com/charmbracelet/x/exp/golden v0.0.0-20250602192518-9e722df69bbb h1:GT/STWThMsrOfYQnhnIPb165e/g1waAp0gNMFvEO6WI=
86+github.com/charmbracelet/x/exp/golden v0.0.0-20250602192518-9e722df69bbb/go.mod h1:929X+xY3LeoOZrDWIBVZcx/zyS0CYtyLiUIvE4VbKC0=
8787github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
8888github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
8989github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
internal/exp/diff/diff.gonew file+1
@@ -0,0 +1 @@
1+package diff
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLines/Content.goldennew file+11
@@ -0,0 +1,11 @@
1+--- main.go
2++++ main.go
3+@@ -5,5 +5,6 @@
4+ )
5+
6+ func main() {
7+- fmt.Println("Hello, World!")
8++ content := "Hello, World!"
9++ fmt.Println(content)
10+ }
11+\ No newline at end of file
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLines/JSON.goldennew file+40
@@ -0,0 +1,40 @@
1+{
2+ "From": "main.go",
3+ "To": "main.go",
4+ "Hunks": [
5+ {
6+ "FromLine": 5,
7+ "ToLine": 5,
8+ "Lines": [
9+ {
10+ "Kind": 2,
11+ "Content": "\t)\n"
12+ },
13+ {
14+ "Kind": 2,
15+ "Content": "\n"
16+ },
17+ {
18+ "Kind": 2,
19+ "Content": "\tfunc main() {\n"
20+ },
21+ {
22+ "Kind": 0,
23+ "Content": "\t\tfmt.Println(\"Hello, World!\")\n"
24+ },
25+ {
26+ "Kind": 1,
27+ "Content": "\t\tcontent := \"Hello, World!\"\n"
28+ },
29+ {
30+ "Kind": 1,
31+ "Content": "\t\tfmt.Println(content)\n"
32+ },
33+ {
34+ "Kind": 2,
35+ "Content": "\t}"
36+ }
37+ ]
38+ }
39+ ]
40+}
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusOne/Content.goldennew file+12
@@ -0,0 +1,12 @@
1+--- main.go
2++++ main.go
3+@@ -4,6 +4,7 @@
4+ "fmt"
5+ )
6+
7+ func main() {
8+- fmt.Println("Hello, World!")
9++ content := "Hello, World!"
10++ fmt.Println(content)
11+ }
12+\ No newline at end of file
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusOne/JSON.goldennew file+44
@@ -0,0 +1,44 @@
1+{
2+ "From": "main.go",
3+ "To": "main.go",
4+ "Hunks": [
5+ {
6+ "FromLine": 4,
7+ "ToLine": 4,
8+ "Lines": [
9+ {
10+ "Kind": 2,
11+ "Content": "\t\t\"fmt\"\n"
12+ },
13+ {
14+ "Kind": 2,
15+ "Content": "\t)\n"
16+ },
17+ {
18+ "Kind": 2,
19+ "Content": "\n"
20+ },
21+ {
22+ "Kind": 2,
23+ "Content": "\tfunc main() {\n"
24+ },
25+ {
26+ "Kind": 0,
27+ "Content": "\t\tfmt.Println(\"Hello, World!\")\n"
28+ },
29+ {
30+ "Kind": 1,
31+ "Content": "\t\tcontent := \"Hello, World!\"\n"
32+ },
33+ {
34+ "Kind": 1,
35+ "Content": "\t\tfmt.Println(content)\n"
36+ },
37+ {
38+ "Kind": 2,
39+ "Content": "\t}"
40+ }
41+ ]
42+ }
43+ ]
44+}
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusTwo/Content.goldennew file+13
@@ -0,0 +1,13 @@
1+--- main.go
2++++ main.go
3+@@ -3,7 +3,8 @@
4+ import (
5+ "fmt"
6+ )
7+
8+ func main() {
9+- fmt.Println("Hello, World!")
10++ content := "Hello, World!"
11++ fmt.Println(content)
12+ }
13+\ No newline at end of file
internal/exp/diff/testdata/TestUdiff/ToUnifiedDiff/DefaultContextLinesPlusTwo/JSON.goldennew file+48
@@ -0,0 +1,48 @@
1+{
2+ "From": "main.go",
3+ "To": "main.go",
4+ "Hunks": [
5+ {
6+ "FromLine": 3,
7+ "ToLine": 3,
8+ "Lines": [
9+ {
10+ "Kind": 2,
11+ "Content": "\timport (\n"
12+ },
13+ {
14+ "Kind": 2,
15+ "Content": "\t\t\"fmt\"\n"
16+ },
17+ {
18+ "Kind": 2,
19+ "Content": "\t)\n"
20+ },
21+ {
22+ "Kind": 2,
23+ "Content": "\n"
24+ },
25+ {
26+ "Kind": 2,
27+ "Content": "\tfunc main() {\n"
28+ },
29+ {
30+ "Kind": 0,
31+ "Content": "\t\tfmt.Println(\"Hello, World!\")\n"
32+ },
33+ {
34+ "Kind": 1,
35+ "Content": "\t\tcontent := \"Hello, World!\"\n"
36+ },
37+ {
38+ "Kind": 1,
39+ "Content": "\t\tfmt.Println(content)\n"
40+ },
41+ {
42+ "Kind": 2,
43+ "Content": "\t}"
44+ }
45+ ]
46+ }
47+ ]
48+}
internal/exp/diff/testdata/TestUdiff/Unified.goldennew file+11
@@ -0,0 +1,11 @@
1+--- main.go
2++++ main.go
3+@@ -5,5 +5,6 @@
4+ )
5+
6+ func main() {
7+- fmt.Println("Hello, World!")
8++ content := "Hello, World!"
9++ fmt.Println(content)
10+ }
11+\ No newline at end of file
internal/exp/diff/udiff_test.gonew file+92
@@ -0,0 +1,92 @@
1+package diff_test
2+
3+import (
4+ "bytes"
5+ "encoding/json"
6+ "testing"
7+
8+ "github.com/aymanbagabas/go-udiff"
9+ "github.com/aymanbagabas/go-udiff/myers"
10+ "github.com/charmbracelet/x/exp/golden"
11+)
12+
13+func TestUdiff(t *testing.T) {
14+ before := `package main
15+
16+ import (
17+ "fmt"
18+ )
19+
20+ func main() {
21+ fmt.Println("Hello, World!")
22+ }`
23+
24+ after := `package main
25+
26+ import (
27+ "fmt"
28+ )
29+
30+ func main() {
31+ content := "Hello, World!"
32+ fmt.Println(content)
33+ }`
34+
35+ t.Run("Unified", func(t *testing.T) {
36+ content := udiff.Unified("main.go", "main.go", before, after)
37+ golden.RequireEqual(t, []byte(content))
38+ })
39+
40+ t.Run("ToUnifiedDiff", func(t *testing.T) {
41+ toUnifiedDiff := func(t *testing.T, before, after string, contextLines int) udiff.UnifiedDiff {
42+ edits := myers.ComputeEdits(before, after)
43+ unifiedDiff, err := udiff.ToUnifiedDiff("main.go", "main.go", before, edits, contextLines)
44+ if err != nil {
45+ t.Fatalf("ToUnifiedDiff failed: %v", err)
46+ }
47+ return unifiedDiff
48+ }
49+ toJSON := func(t *testing.T, unifiedDiff udiff.UnifiedDiff) []byte {
50+ var buff bytes.Buffer
51+ encoder := json.NewEncoder(&buff)
52+ encoder.SetIndent("", " ")
53+ if err := encoder.Encode(unifiedDiff); err != nil {
54+ t.Fatalf("Failed to encode unified diff: %v", err)
55+ }
56+ return buff.Bytes()
57+ }
58+
59+ t.Run("DefaultContextLines", func(t *testing.T) {
60+ unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines)
61+
62+ t.Run("Content", func(t *testing.T) {
63+ golden.RequireEqual(t, []byte(unifiedDiff.String()))
64+ })
65+ t.Run("JSON", func(t *testing.T) {
66+ golden.RequireEqual(t, toJSON(t, unifiedDiff))
67+ })
68+ })
69+
70+ t.Run("DefaultContextLinesPlusOne", func(t *testing.T) {
71+ unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines+1)
72+
73+ t.Run("Content", func(t *testing.T) {
74+ golden.RequireEqual(t, []byte(unifiedDiff.String()))
75+ })
76+ t.Run("JSON", func(t *testing.T) {
77+ golden.RequireEqual(t, toJSON(t, unifiedDiff))
78+ })
79+ })
80+
81+ t.Run("DefaultContextLinesPlusTwo", func(t *testing.T) {
82+ unifiedDiff := toUnifiedDiff(t, before, after, udiff.DefaultContextLines+2)
83+
84+ t.Run("Content", func(t *testing.T) {
85+ golden.RequireEqual(t, []byte(unifiedDiff.String()))
86+ })
87+ t.Run("JSON", func(t *testing.T) {
88+ golden.RequireEqual(t, toJSON(t, unifiedDiff))
89+ })
90+ })
91+ })
92+}