-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathchanges_test.go
More file actions
101 lines (89 loc) · 2.18 KB
/
changes_test.go
File metadata and controls
101 lines (89 loc) · 2.18 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
package borges
import (
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/src-d/core-retrieval.v0/model"
"gopkg.in/src-d/go-kallax.v1"
)
func TestNewChanges(t *testing.T) {
for _, ct := range ChangesFixtures {
t.Run(ct.TestName, func(t *testing.T) {
require := require.New(t)
oldRefs := NewModelReferencer(&model.Repository{References: ct.OldReferences})
newRefs := NewModelReferencer(&model.Repository{References: ct.NewReferences})
changes, err := newChanges(timeNow, oldRefs, newRefs)
require.NoError(err)
sortChanges(changes)
sortChanges(ct.Changes)
// IDs in the fixtures and the real ones will not match
// so clear them before comparing
var emptyID kallax.ULID
for _, cmds := range changes {
for _, cmd := range cmds {
if cmd.Old != nil {
cmd.Old.ID = emptyID
}
if cmd.New != nil {
cmd.New.ID = emptyID
}
}
}
require.Equal(ct.Changes, changes)
})
}
}
func BenchmarkNewChanges(b *testing.B) {
for _, ct := range ChangesFixtures {
b.Run(ct.TestName, func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldRefs := NewModelReferencer(&model.Repository{References: ct.OldReferences})
newRefs := NewModelReferencer(&model.Repository{References: ct.NewReferences})
_, err := newChanges(timeNow, oldRefs, newRefs)
require.NoError(b, err)
}
})
}
}
func sortChanges(c Changes) {
for _, cmds := range c {
sort.Sort(cmdSort(cmds))
}
}
type cmdSort []*Command
func (s cmdSort) Len() int { return len(s) }
func (s cmdSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s cmdSort) Less(i, j int) bool {
a, b := s[i], s[j]
switch a.Action() {
case Update:
switch b.Action() {
case Update:
return strings.Compare(a.New.Name, b.New.Name) < 0
case Create:
return true
case Delete:
return true
}
case Create:
switch b.Action() {
case Update:
return false
case Create:
return strings.Compare(a.New.Name, b.New.Name) < 0
case Delete:
return true
}
case Delete:
switch b.Action() {
case Update:
return false
case Create:
return false
case Delete:
return strings.Compare(a.Old.Name, b.Old.Name) < 0
}
}
return false
}