-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
318 lines (250 loc) · 10.2 KB
/
parse_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"io/ioutil"
"testing"
"time"
)
func TestIncrementCounters(t *testing.T) {
c := NewContributor("dummy", []PeriodTS{})
c.Contributions[0].IncrementCounters(1, 1)
if c.Contributions[0].Additions != 1 || c.Contributions[0].Deletions != 1 {
t.Errorf("Additions and Deletions should be at 1 and the were %v, %v", c.Contributions[0].Additions, c.Contributions[0].Deletions)
}
c.Contributions[0].IncrementCounters(0, 0)
if c.Contributions[0].Additions != 1 || c.Contributions[0].Deletions != 1 {
t.Errorf("Additions and Deletions should be at 1 and the were %v, %v", c.Contributions[0].Additions, c.Contributions[0].Deletions)
}
c.Contributions[0].IncrementCounters(5, 4)
if c.Contributions[0].Additions != 6 || c.Contributions[0].Deletions != 5 {
t.Errorf("Additions and Deletions should be at 6 and 5 and the were %v, %v", c.Contributions[0].Additions, c.Contributions[0].Deletions)
}
c.Contributions[0].IncrementCounters(-5, -4)
if c.Contributions[0].Additions != 1 || c.Contributions[0].Deletions != 1 {
t.Errorf("Additions and Deletions should be at 1 and the were %v, %v", c.Contributions[0].Additions, c.Contributions[0].Deletions)
}
}
func TestNewContributor(t *testing.T) {
c := NewContributor("Pouet", []PeriodTS{})
if c.Contributions[0].Name != "Pouet" {
t.Errorf("The expected name was Pouet, however we got %v", c.Contributions[0].Name)
}
}
func TestSetScores(t *testing.T) {
c := NewContributor("", []PeriodTS{})
var diffScore, addScore, commitScore float64
diffScore = 80
addScore = 10
commitScore = 10
c.Contributions[0].SetScores(diffScore, addScore, commitScore)
if c.Contributions[0].DifferenceScore != diffScore || c.Contributions[0].AdditionScore != addScore || c.Contributions[0].CommitScore != commitScore {
t.Errorf("The expected scored should be %v %v %v and were %v %v %v", diffScore, addScore, commitScore, c.Contributions[0].DifferenceScore, c.Contributions[0].AdditionScore, c.Contributions[0].CommitScore)
}
}
func TestGetScores(t *testing.T) {
c := NewContributor("", []PeriodTS{})
c.Contributions[0].SetScores(80.0, 10.0, 50.0)
expectedScore := c.Contributions[0].DifferenceScore*0.8 + c.Contributions[0].AdditionScore*0.1 + c.Contributions[0].CommitScore*0.1
if c.Contributions[0].GetScore() != expectedScore {
t.Errorf("The expected score was %v and we got %v", expectedScore, c.Contributions[0].GetScore())
}
}
func TestHasContributor(t *testing.T) {
report := NewReport()
name := "Pouet"
if report.HasContributor(name) {
t.Errorf("The report shouldn't have any contributor")
}
report.AddContributor(name,make(map[string][]PeriodTS))
if !report.HasContributor(name) {
t.Errorf("The report should have the contributor %v", name)
}
if report.HasContributor("Pouetpouet") {
t.Error("Nah, this contributor wasn't added to the report!")
}
}
func TestIncrementReportCounters(t *testing.T) {
r := NewReport()
name := "Pouet"
err := r.IncrementCounters(name, 0, 0, time.Now())
if err == nil {
t.Errorf("Incrementing a counter on a non existing contributor should return a valid error")
}
r.AddContributor(name, make(map[string][]PeriodTS))
c := r.Contributors[name]
err = r.IncrementCounters(name, 0, 0, time.Now())
if err != nil {
t.Errorf("Incrementing a counter on a valid contributor should not return an error")
}
if c.Contributions[0].Additions != 0 || c.Contributions[0].Deletions != 0 {
t.Errorf("Contributor Additions and Deletions should still be at 0")
}
if r.TotalAdditions != 0 || r.TotalDeletions != 0 {
t.Errorf("Total Additions and Deletions should still be at 0")
}
addDiff := 10
delDiff := 9
r.IncrementCounters(name, addDiff, delDiff, time.Now())
if c.Contributions[0].Additions != addDiff || c.Contributions[0].Deletions != delDiff {
t.Errorf("Contributor Additions and Deletions should be equal to %v and %v", addDiff, delDiff)
}
if r.TotalAdditions != addDiff || r.TotalDeletions != delDiff {
t.Errorf("Total Additions and Deletions should be equal to %v and %v", addDiff, delDiff)
}
name2 := "Pouetpouet"
r.AddContributor(name2, make(map[string][]PeriodTS))
c2 := r.Contributors[name2]
r.IncrementCounters(name2, addDiff, delDiff, time.Now())
if c2.Contributions[0].Additions != addDiff || c2.Contributions[0].Deletions != delDiff {
t.Errorf("Contributor Additions and Deletions should be equal to %v and %v", addDiff, delDiff)
}
if c.Contributions[0].Additions != addDiff || c.Contributions[0].Deletions != delDiff {
t.Errorf("Contributor Additions and Deletions should be equal to %v and %v", addDiff, delDiff)
}
if r.TotalAdditions != 2*addDiff || r.TotalDeletions != 2*delDiff {
t.Errorf("Total Additions and Deletions should equal be to %v and %v", 2*addDiff, 2*delDiff)
}
}
func TestIncrementCommit(t *testing.T) {
r := NewReport()
name := "Pouet"
name2 := "Pouetpouet"
err := r.IncrementCommits(name, time.Now())
if err == nil {
t.Errorf("Incrementing the commits of a non existing user should fail")
}
r.AddContributor(name, make(map[string][]PeriodTS))
c := r.Contributors[name]
err = r.IncrementCommits(name, time.Now())
if err != nil {
t.Errorf("Incrementing the commits of an existing user should not fail")
}
if c.Contributions[0].Commits != 1 {
t.Errorf("Failed to increment the contributor's commits")
}
if r.TotalCommits != 1 {
t.Errorf("The total number of commits should have been incremented")
}
r.AddContributor(name2, make(map[string][]PeriodTS))
c2 := r.Contributors[name2]
r.IncrementCommits(name2, time.Now())
if c2.Contributions[0].Commits != 1 {
t.Errorf("Failed to increment the contributor's commits")
}
if r.TotalCommits != 2 {
t.Errorf("The total number of commits should have been incremented")
}
if c.Contributions[0].Commits != 1 {
t.Errorf("The number of the first contributor should remain at 1")
}
}
func CheckContributors(report *Report, contributors []string) bool {
ret := true
for index := range contributors {
ret = ret && report.HasContributor(contributors[index])
}
return ret
}
func testParse(t *testing.T, file string) {
content, err := ioutil.ReadFile(file)
if err != nil {
t.Errorf("Could not read the test file %v", err)
}
report, err := ParseStats(string(content), "/", *NewPeriodArray())
if err != nil {
t.Errorf("Reading a valid git log should not return an error")
}
contributors := []string{"Contributor1", "Contributor2", "Contributor3"}
if !CheckContributors(report, contributors) {
t.Errorf("There's at least a missing contributor in the output")
}
numOfTotalCommits := 9 // there is a full binary commit
if report.TotalCommits != numOfTotalCommits {
t.Errorf("The total number of commits should be %v and it was %v", numOfTotalCommits, report.TotalCommits)
}
totalAdd := 189
totalDel := 8
if report.TotalAdditions != totalAdd {
t.Errorf("The total number of additions should be %v and it was %v", totalAdd, report.TotalAdditions)
}
if report.TotalDeletions != totalDel {
t.Errorf("The total number of deletions should be %v and it was %v", totalDel, report.TotalDeletions)
}
if report.Contributors["Contributor3"].Contributions[0].Commits != 1 {
t.Errorf("Contributor3 should only have one commit")
}
if report.Contributors["Contributor1"].Contributions[0].Commits != 6 {
t.Errorf("Contributor1 should have 6 commits")
}
if report.Contributors["Contributor2"].Contributions[0].Commits != 2 {
t.Errorf("Contributor2 should have 2 commits")
}
if report.Contributors["Contributor3"].Contributions[0].Additions != 1 {
t.Errorf("Contributor3 should only have one addition")
}
if report.Contributors["Contributor3"].Contributions[0].Deletions != 1 {
t.Errorf("Contributor3 should only have one deletion")
}
if report.Contributors["Contributor1"].Contributions[0].Additions != 159 {
t.Errorf("Contributor1 should have 159 additions")
}
if report.Contributors["Contributor1"].Contributions[0].Deletions != 3 {
t.Errorf("Contributor1 should have 3 deletions")
}
if report.Contributors["Contributor2"].Contributions[0].Additions != 29 {
t.Errorf("Contributor2 should have 29 additions")
}
if report.Contributors["Contributor2"].Contributions[0].Deletions != 4 {
t.Errorf("Contributor2 should have 4 deletions")
}
report, err = ParseStats(string(content), "/test", *NewPeriodArray())
contributors = []string{"Contributor1", "Contributor2"}
if !CheckContributors(report, contributors) {
t.Errorf("There's at least a missing contributor in the output")
}
numOfTotalCommits = 7 // there is a full binary commit
if report.TotalCommits != numOfTotalCommits {
t.Errorf("The total number of commits should be %v and it was %v", numOfTotalCommits, report.TotalCommits)
}
totalAdd = 176
totalDel = 4
if report.TotalAdditions != totalAdd {
t.Errorf("The total number of additions should be %v and it was %v", totalAdd, report.TotalAdditions)
}
if report.TotalDeletions != totalDel {
t.Errorf("The total number of deletions should be %v and it was %v", totalDel, report.TotalDeletions)
}
if report.Contributors["Contributor2"].Contributions[0].Commits != 2 {
t.Errorf("Contributor3 should have 2 commits")
}
if report.Contributors["Contributor1"].Contributions[0].Commits != 5 {
t.Errorf("Contributor1 should have 5 commits")
}
if report.Contributors["Contributor1"].Contributions[0].Additions != 147 {
t.Errorf("Contributor1 should have 147 additions")
}
if report.Contributors["Contributor1"].Contributions[0].Deletions != 0 {
t.Errorf("Contributor1 should have 0 deletions")
}
if report.Contributors["Contributor2"].Contributions[0].Additions != 29 {
t.Errorf("Contributor2 should have 29 additions")
}
if report.Contributors["Contributor2"].Contributions[0].Deletions != 4 {
t.Errorf("Contributor2 should have 4 deletions")
}
report, err = ParseStats(string(content), "/tests", *NewPeriodArray())
if err != nil {
t.Errorf("%v", err)
}
if len(report.Contributors) != 0 {
t.Errorf("This path doesn't exist, there shouldn't be any contributor %v", len(report.Contributors))
}
if report.TotalCommits != 0 || report.TotalAdditions != 0 || report.TotalDeletions != 0 {
t.Errorf("This path does not exist, there should be no additions/deletions/commits")
}
}
func TestParseUnix(t *testing.T) {
testParse(t, "test_assets/test_gitlog.txt")
}
func TestParseWindows(t *testing.T) {
testParse(t, "test_assets/test_gitlogwin.txt")
}