-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_test.go
More file actions
103 lines (95 loc) · 2.37 KB
/
diff_test.go
File metadata and controls
103 lines (95 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package diff
import (
"reflect"
"strings"
"testing"
)
func TestDiff(t *testing.T) {
type TestCase struct {
a, b []string
expected []DiffPart
}
testCases := []TestCase{
{
[]string{"Hello", "world!"},
[]string{"world!"},
[]DiffPart{
{DiffRemoved, "Hello"},
{DiffIdentical, "world!"},
},
},
{
[]string{"A", "B", "C", "C" /**/},
[]string{"A" /**/, "C", "C", "B"},
[]DiffPart{
{DiffIdentical, "A"},
{DiffRemoved, "B"},
{DiffIdentical, "C"},
{DiffIdentical, "C"},
{DiffAdded, "B"},
},
},
{
strings.Split("The quick brown fox jumps over the lazy dog", " "),
strings.Split("A over quick red fox jumps the lazy dog", " "),
[]DiffPart{
{DiffRemoved, "The"},
{DiffAdded, "A"},
{DiffAdded, "over"},
{DiffIdentical, "quick"},
{DiffRemoved, "brown"},
{DiffAdded, "red"},
{DiffIdentical, "fox"},
{DiffIdentical, "jumps"},
{DiffRemoved, "over"},
{DiffIdentical, "the"},
{DiffIdentical, "lazy"},
{DiffIdentical, "dog"},
},
},
}
for _, testCase := range testCases {
result := Diff(testCase.a, testCase.b)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("Expected %v, Got %v", testCase.expected, result)
}
// Verify that the original files can be reconstructed from result
a, b := extractOriginals(result)
if !reflect.DeepEqual(a, testCase.a) {
t.Errorf("Original a was %v, got %v", testCase.a, a)
}
if !reflect.DeepEqual(b, testCase.b) {
t.Errorf("Original b was %v, got %v", testCase.b, b)
}
}
}
func TestDiffActionString(t *testing.T) {
testCases := []struct {
action DiffAction
expectedString string
}{
{DiffAdded, "Added"},
{DiffRemoved, "Removed"},
{DiffIdentical, "Identical"},
}
for _, testCase := range testCases {
if s := testCase.action.String(); s != testCase.expectedString {
t.Errorf("Expected %v, got %v", testCase.expectedString, s)
}
}
}
// Given a diff, extract the original two files.
func extractOriginals(d []DiffPart) (a, b []string) {
for _, part := range d {
if part.Action != DiffAdded {
a = append(a, part.Value)
}
if part.Action != DiffRemoved {
b = append(b, part.Value)
}
}
return
}