forked from mcluseau/go-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_test.go
More file actions
144 lines (123 loc) · 2.67 KB
/
diff_test.go
File metadata and controls
144 lines (123 loc) · 2.67 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package diff
import (
"bytes"
"testing"
)
func TestDiffAllChangeTypes(t *testing.T) {
testChanges(t, []KeyValue{
kvS("k1", "v1"),
kvS("k2", "v2"),
kvS("k3", "v3.1"),
}, []KeyValue{
kvS("k1", "v1"),
kvS("k3", "v3"),
kvS("k4", "v4"),
}, []Change{
{Unchanged, []byte("k1"), nil},
{Created, []byte("k2"), []byte("v2")},
{Modified, []byte("k3"), []byte("v3.1")},
{Deleted, []byte("k4"), nil},
})
}
func TestDiffEmpty(t *testing.T) {
testChanges(t, []KeyValue{}, []KeyValue{}, []Change{})
}
func TestDiffCreate(t *testing.T) {
testChanges(t, []KeyValue{
kvS("k1", "v1"),
}, []KeyValue{}, []Change{
{Created, []byte("k1"), []byte("v1")},
})
}
func TestDiffSame(t *testing.T) {
testChanges(t, []KeyValue{
kvS("k1", "v1"),
}, []KeyValue{
kvS("k1", "v1"),
}, []Change{
{Unchanged, []byte("k1"), nil},
})
}
func TestDiffModify(t *testing.T) {
testChanges(t, []KeyValue{
kvS("k1", "v1.1"),
}, []KeyValue{
kvS("k1", "v1"),
}, []Change{
{Modified, []byte("k1"), []byte("v1.1")},
})
}
func TestDiffDelete(t *testing.T) {
testChanges(t, []KeyValue{}, []KeyValue{
kvS("k1", "v1.1"),
}, []Change{
{Deleted, []byte("k1"), nil},
})
}
func TestDiffIndexStream(t *testing.T) {
cancel := make(chan bool, 1)
changes := make(chan Change, 10)
refIndex := NewIndex(true)
refIndex.Index(stream(kvS("k2", "v2")), nil)
go func() {
DiffIndexStream(refIndex, stream(kvS("k1", "v1")), changes, cancel)
close(changes)
}()
expectChanges(t, changes, []Change{
{Deleted, []byte("k1"), nil},
{Created, []byte("k2"), []byte("v2")},
})
}
func stream(kvs ...KeyValue) <-chan KeyValue {
c := make(chan KeyValue, 1)
go func() {
for _, kv := range kvs {
c <- kv
}
close(c)
}()
return c
}
func kvS(key, value string) KeyValue {
return KeyValue{[]byte(key), []byte(value)}
}
func testChanges(t *testing.T, ref, current []KeyValue, exp []Change) {
t.Helper()
cancel := make(chan bool, 1)
changes := make(chan Change, 10)
go func() {
Diff(stream(ref...), stream(current...), changes, cancel)
close(changes)
}()
expectChanges(t, changes, exp)
}
func expectChanges(t *testing.T, changes <-chan Change, exp []Change) {
t.Helper()
for change := range changes {
found := false
for idx, expChange := range exp {
if eq(change, expChange) {
exp = append(exp[0:idx], exp[idx+1:]...)
found = true
}
}
if !found {
t.Error("unexpected change: ", change)
}
}
for _, noFound := range exp {
t.Error("expected change not found: ", noFound)
}
}
func eq(c1, c2 Change) bool {
if c1.Type != c2.Type {
return false
}
if !bytes.Equal(c1.Key, c2.Key) {
return false
}
if !bytes.Equal(c1.Value, c2.Value) {
return false
}
return true
}