From 33820cfc0234218f8a243e72a6e6211ceca29c2e Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Wed, 9 Mar 2022 23:34:29 +0900 Subject: [PATCH] Add functions for source and destination diff texts (#1) --- format.go | 53 ++++++++++++++--- format_test.go | 155 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 format_test.go diff --git a/format.go b/format.go index be2b2a6..6e29296 100644 --- a/format.go +++ b/format.go @@ -6,6 +6,20 @@ 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)) @@ -13,14 +27,39 @@ func DiffText(diffs []DiffLine) string { 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") } diff --git a/format_test.go b/format_test.go new file mode 100644 index 0000000..41930d1 --- /dev/null +++ b/format_test.go @@ -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) + } + }) + } +}