Skip to content

Commit

Permalink
Add functions for source and destination diff texts (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Mar 9, 2022
1 parent d0cc49b commit 33820cf
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 7 deletions.
53 changes: 46 additions & 7 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,60 @@ import (
"strings"
)

// typeSymbol returns the associated symbol of a DiffType.
func typeSymbol(t DiffType) string {
switch t {
case Equal:
return " "
case Insert:
return "+"
case Delete:
return "-"
default:
panic("unknown DiffType")
}
}

// DiffText returns the source and destination texts (all equalities, insertions and deletions).
func DiffText(diffs []DiffLine) string {
s := make([]string, len(diffs))
for i, l := range diffs {
if len(l.Text) == 0 && l.Type == Equal {
continue
}
prefix := " "
switch l.Type {
case Insert:
prefix = "+"
case Delete:
prefix = "-"
s[i] = fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text)
}
return strings.Join(s, "\n")
}

// DiffTextA returns the source text (all equalities and deletions).
func DiffTextA(diffs []DiffLine) string {
s := []string{}
for _, l := range diffs {
if l.Type == Insert {
continue
}
if l.Type == Equal && len(l.Text) == 0 {
s = append(s, "")
} else {
s = append(s, fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text))
}
}
return strings.Join(s, "\n")
}

// DiffTextB returns the destination text (all equalities and insertions).
func DiffTextB(diffs []DiffLine) string {
s := []string{}
for _, l := range diffs {
if l.Type == Delete {
continue
}
if l.Type == Equal && len(l.Text) == 0 {
s = append(s, "")
} else {
s = append(s, fmt.Sprintf("%s%s", typeSymbol(l.Type), l.Text))
}
s[i] = fmt.Sprintf("%s%s", prefix, l.Text)
}
return strings.Join(s, "\n")
}
155 changes: 155 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Package patience implements the Patience Diff algorithm.
package patience

import (
"testing"
)

func TestDiffText(t *testing.T) {
type args struct {
diffs []DiffLine
}
tests := []struct {
name string
args args
want string
}{
{
name: "TestDiffText",
args: args{
diffs: []DiffLine{
{
Type: Equal,
Text: "a",
},
{
Type: Insert,
Text: "b",
},
{
Type: Equal,
Text: "c",
},
{
Type: Equal,
Text: "",
},
{
Type: Delete,
Text: "d",
},
{
Type: Equal,
Text: "e",
},
},
},
want: " a\n+b\n c\n\n-d\n e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DiffText(tt.args.diffs); got != tt.want {
t.Errorf("DiffText() = %v, want %v", got, tt.want)
}
})
}
}

func TestDiffTextA(t *testing.T) {
type args struct {
diffs []DiffLine
}
tests := []struct {
name string
args args
want string
}{
{
name: "TestDiffText",
args: args{
diffs: []DiffLine{
{
Type: Equal,
Text: "a",
},
{
Type: Insert,
Text: "b",
},
{
Type: Equal,
Text: "c",
},
{
Type: Equal,
Text: "",
},
{
Type: Delete,
Text: "d",
},
{
Type: Equal,
Text: "e",
},
},
},
want: " a\n c\n\n-d\n e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DiffTextA(tt.args.diffs); got != tt.want {
t.Errorf("DiffTextA() = %v, want %v", got, tt.want)
}
})
}
}

func TestDiffTextB(t *testing.T) {
type args struct {
diffs []DiffLine
}
tests := []struct {
name string
args args
want string
}{
{
name: "TestDiffText",
args: args{
diffs: []DiffLine{
{
Type: Equal,
Text: "a",
},
{
Type: Insert,
Text: "b",
},
{
Type: Equal,
Text: "c",
},
{
Type: Equal,
Text: "",
},
{
Type: Equal,
Text: "e",
},
},
},
want: " a\n+b\n c\n\n e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DiffTextB(tt.args.diffs); got != tt.want {
t.Errorf("DiffTextB() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 33820cf

Please sign in to comment.