-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
235 lines (212 loc) · 5.79 KB
/
main_test.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bytes"
"io"
"log"
"testing"
)
func init() {
log.SetOutput(io.Discard)
}
func TestInvalidKey(t *testing.T) {
for n, tc := range []struct {
key string
invalid bool
}{
{"commit", true},
{"tree", true},
{"parent", true},
{"author", true},
{"committer", true},
{"encoding", true},
{"commit ", true},
{"non-alphanumeric", true},
{"x", false},
{"f00", false},
} {
if got, want := invalidKey(tc.key), tc.invalid; got != want {
t.Errorf("[%d] invalidKey(%q) = %t, want %t", n, tc.key, got, want)
}
}
}
func TestValidPrefix(t *testing.T) {
for n, tc := range []struct {
prefix string
valid bool
}{
{"", false},
{"x", false},
{"0", true},
{"f00", true},
{"0000000000000000000000000000000000000000", true}, // 40 chars
{"00000000000000000000000000000000000000000", false}, // 41 chars
} {
if got, want := validPrefix(tc.prefix), tc.valid; got != want {
t.Errorf("[%d] validPrefix(%q) = %t, want %t", n, tc.prefix, got, want)
}
}
}
const commit = `tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
Message
`
func TestHeadTail(t *testing.T) {
wantHead := []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100`)
wantTail := []byte("\n\nMessage\n")
head, tail := headTail([]byte(commit))
if !bytes.Equal(head, wantHead) {
t.Errorf("head is %s, want %s", head, wantHead)
}
if !bytes.Equal(tail, wantTail) {
t.Errorf("tail is %s, want %s", tail, wantTail)
}
}
func TestFind(t *testing.T) {
for _, tc := range []struct {
desc string
startN int
wantHash string
wantIteration int
wantNewCommit []byte
}{
{
desc: "Start at 0th iteration",
startN: 0,
wantHash: "034c4f788c4a7522a75e1b86ee3c24eee630e822",
wantIteration: 16,
wantNewCommit: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
foo 16
Message
`),
},
{
desc: "Start at final iteration",
startN: 16,
wantHash: "034c4f788c4a7522a75e1b86ee3c24eee630e822",
wantIteration: 16,
wantNewCommit: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
foo 16
Message
`),
},
{
desc: "Start after would-be final iteration",
startN: 17,
wantHash: "0457314be0b9283e18224b8dfad77741d9f41cdf",
wantIteration: 43,
wantNewCommit: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
foo 43
Message
`),
},
} {
t.Run(tc.desc, func(t *testing.T) {
hash, iteration, newCommit := find("0", "foo", tc.startN, []byte(commit))
if got, want := hash, tc.wantHash; got != want {
t.Errorf("hash = %q, want %q", got, want)
}
if got, want := iteration, tc.wantIteration; got != want {
t.Errorf("iteration = %d, want %d", got, want)
}
if !bytes.Equal(newCommit, tc.wantNewCommit) {
t.Errorf("new commit is:\n%s\n\nwant:\n%s", newCommit, tc.wantNewCommit)
}
})
}
}
func TestTrimHeader(t *testing.T) {
for n, tc := range []struct {
head []byte
header string
want []byte
}{
{
head: []byte{},
header: "f00",
want: []byte{},
},
{
head: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
f00 123`),
header: "f00",
want: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100`),
},
{
head: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
f00 123`),
header: "unknown",
want: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
committer Committer Name <[email protected]> 1577876400 +0100
f00 123`),
},
{
head: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
f00 123
committer Committer Name <[email protected]> 1577876400 +0100`),
header: "f00",
want: []byte(`tree 0000000000000000000000000000000000000000
author Author Name <[email protected]> 1577872800 +0000
f00 123
committer Committer Name <[email protected]> 1577876400 +0100`),
},
} {
got := trimHeader(tc.head, tc.header)
if !bytes.Equal(got, tc.want) {
t.Errorf("[%d] got:\n%s\n\nwant:\n%s", n, got, tc.want)
}
}
}
func TestThousandSeparate(t *testing.T) {
for n, tc := range []struct {
n int
want string
}{
{-1000000000, "-1,000,000,000"},
{-100000000, "-100,000,000"},
{-10000000, "-10,000,000"},
{-1000000, "-1,000,000"},
{-100000, "-100,000"},
{-10000, "-10,000"},
{-1000, "-1,000"},
{-100, "-100"},
{-10, "-10"},
{-1, "-1"},
{0, "0"},
{1, "1"},
{10, "10"},
{100, "100"},
{1000, "1,000"},
{10000, "10,000"},
{100000, "100,000"},
{1000000, "1,000,000"},
{10000000, "10,000,000"},
{100000000, "100,000,000"},
{1000000000, "1,000,000,000"},
} {
if got, want := thousandSeparate(tc.n), tc.want; got != want {
t.Errorf("[%d] thousandSeparate(%d) = %q, want %q", n, tc.n, got, want)
}
}
}
func BenchmarkFind(b *testing.B) {
for b.Loop() {
find("c0ffee", "c0ffee", 0, []byte(commit))
}
}