Skip to content

Commit 33820cf

Browse files
authored
Add functions for source and destination diff texts (#1)
1 parent d0cc49b commit 33820cf

File tree

2 files changed

+201
-7
lines changed

2 files changed

+201
-7
lines changed

format.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,60 @@ import (
66
"strings"
77
)
88

9+
// typeSymbol returns the associated symbol of a DiffType.
10+
func typeSymbol(t DiffType) string {
11+
switch t {
12+
case Equal:
13+
return " "
14+
case Insert:
15+
return "+"
16+
case Delete:
17+
return "-"
18+
default:
19+
panic("unknown DiffType")
20+
}
21+
}
22+
923
// DiffText returns the source and destination texts (all equalities, insertions and deletions).
1024
func DiffText(diffs []DiffLine) string {
1125
s := make([]string, len(diffs))
1226
for i, l := range diffs {
1327
if len(l.Text) == 0 && l.Type == Equal {
1428
continue
1529
}
16-
prefix := " "
17-
switch l.Type {
18-
case Insert:
19-
prefix = "+"
20-
case Delete:
21-
prefix = "-"
30+
s[i] = fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text)
31+
}
32+
return strings.Join(s, "\n")
33+
}
34+
35+
// DiffTextA returns the source text (all equalities and deletions).
36+
func DiffTextA(diffs []DiffLine) string {
37+
s := []string{}
38+
for _, l := range diffs {
39+
if l.Type == Insert {
40+
continue
41+
}
42+
if l.Type == Equal && len(l.Text) == 0 {
43+
s = append(s, "")
44+
} else {
45+
s = append(s, fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text))
46+
}
47+
}
48+
return strings.Join(s, "\n")
49+
}
50+
51+
// DiffTextB returns the destination text (all equalities and insertions).
52+
func DiffTextB(diffs []DiffLine) string {
53+
s := []string{}
54+
for _, l := range diffs {
55+
if l.Type == Delete {
56+
continue
57+
}
58+
if l.Type == Equal && len(l.Text) == 0 {
59+
s = append(s, "")
60+
} else {
61+
s = append(s, fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text))
2262
}
23-
s[i] = fmt.Sprintf("%s%s", prefix, l.Text)
2463
}
2564
return strings.Join(s, "\n")
2665
}

format_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Package patience implements the Patience Diff algorithm.
2+
package patience
3+
4+
import (
5+
"testing"
6+
)
7+
8+
func TestDiffText(t *testing.T) {
9+
type args struct {
10+
diffs []DiffLine
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want string
16+
}{
17+
{
18+
name: "TestDiffText",
19+
args: args{
20+
diffs: []DiffLine{
21+
{
22+
Type: Equal,
23+
Text: "a",
24+
},
25+
{
26+
Type: Insert,
27+
Text: "b",
28+
},
29+
{
30+
Type: Equal,
31+
Text: "c",
32+
},
33+
{
34+
Type: Equal,
35+
Text: "",
36+
},
37+
{
38+
Type: Delete,
39+
Text: "d",
40+
},
41+
{
42+
Type: Equal,
43+
Text: "e",
44+
},
45+
},
46+
},
47+
want: " a\n+b\n c\n\n-d\n e",
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
if got := DiffText(tt.args.diffs); got != tt.want {
53+
t.Errorf("DiffText() = %v, want %v", got, tt.want)
54+
}
55+
})
56+
}
57+
}
58+
59+
func TestDiffTextA(t *testing.T) {
60+
type args struct {
61+
diffs []DiffLine
62+
}
63+
tests := []struct {
64+
name string
65+
args args
66+
want string
67+
}{
68+
{
69+
name: "TestDiffText",
70+
args: args{
71+
diffs: []DiffLine{
72+
{
73+
Type: Equal,
74+
Text: "a",
75+
},
76+
{
77+
Type: Insert,
78+
Text: "b",
79+
},
80+
{
81+
Type: Equal,
82+
Text: "c",
83+
},
84+
{
85+
Type: Equal,
86+
Text: "",
87+
},
88+
{
89+
Type: Delete,
90+
Text: "d",
91+
},
92+
{
93+
Type: Equal,
94+
Text: "e",
95+
},
96+
},
97+
},
98+
want: " a\n c\n\n-d\n e",
99+
},
100+
}
101+
for _, tt := range tests {
102+
t.Run(tt.name, func(t *testing.T) {
103+
if got := DiffTextA(tt.args.diffs); got != tt.want {
104+
t.Errorf("DiffTextA() = %v, want %v", got, tt.want)
105+
}
106+
})
107+
}
108+
}
109+
110+
func TestDiffTextB(t *testing.T) {
111+
type args struct {
112+
diffs []DiffLine
113+
}
114+
tests := []struct {
115+
name string
116+
args args
117+
want string
118+
}{
119+
{
120+
name: "TestDiffText",
121+
args: args{
122+
diffs: []DiffLine{
123+
{
124+
Type: Equal,
125+
Text: "a",
126+
},
127+
{
128+
Type: Insert,
129+
Text: "b",
130+
},
131+
{
132+
Type: Equal,
133+
Text: "c",
134+
},
135+
{
136+
Type: Equal,
137+
Text: "",
138+
},
139+
{
140+
Type: Equal,
141+
Text: "e",
142+
},
143+
},
144+
},
145+
want: " a\n+b\n c\n\n e",
146+
},
147+
}
148+
for _, tt := range tests {
149+
t.Run(tt.name, func(t *testing.T) {
150+
if got := DiffTextB(tt.args.diffs); got != tt.want {
151+
t.Errorf("DiffTextB() = %v, want %v", got, tt.want)
152+
}
153+
})
154+
}
155+
}

0 commit comments

Comments
 (0)