-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test.go
More file actions
185 lines (160 loc) · 4.94 KB
/
git_test.go
File metadata and controls
185 lines (160 loc) · 4.94 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
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
package main
import (
"testing"
"time"
)
func TestParseCommits(t *testing.T) {
input := `a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2|Alice|alice@example.com|2024-12-15T10:30:00+09:00|Add feature X
3 files changed, 45 insertions(+), 12 deletions(-)
b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3|Bob|bob@example.com|2024-12-14T14:00:00+09:00|Fix bug Y
1 file changed, 5 insertions(+), 2 deletions(-)
c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4|Alice|alice@example.com|2024-12-13T09:15:00+09:00|Refactor Z
7 files changed, 120 insertions(+), 80 deletions(-)
`
commits, err := parseCommits(input)
if err != nil {
t.Fatalf("parseCommits failed: %v", err)
}
if len(commits) != 3 {
t.Fatalf("expected 3 commits, got %d", len(commits))
}
// Check first commit
c := commits[0]
if c.Author != "Alice" {
t.Errorf("expected author Alice, got %s", c.Author)
}
if c.Email != "alice@example.com" {
t.Errorf("expected email alice@example.com, got %s", c.Email)
}
if c.Message != "Add feature X" {
t.Errorf("expected message 'Add feature X', got '%s'", c.Message)
}
if c.FilesChanged != 3 {
t.Errorf("expected 3 files changed, got %d", c.FilesChanged)
}
if c.Insertions != 45 {
t.Errorf("expected 45 insertions, got %d", c.Insertions)
}
if c.Deletions != 12 {
t.Errorf("expected 12 deletions, got %d", c.Deletions)
}
// Check second commit
c2 := commits[1]
if c2.Author != "Bob" {
t.Errorf("expected author Bob, got %s", c2.Author)
}
if c2.FilesChanged != 1 {
t.Errorf("expected 1 file changed, got %d", c2.FilesChanged)
}
}
func TestParseCommitsEmpty(t *testing.T) {
commits, err := parseCommits("")
if err != nil {
t.Fatalf("parseCommits failed: %v", err)
}
if len(commits) != 0 {
t.Fatalf("expected 0 commits, got %d", len(commits))
}
}
func TestParseStatLine(t *testing.T) {
tests := []struct {
line string
files int
ins int
del int
}{
{"3 files changed, 45 insertions(+), 12 deletions(-)", 3, 45, 12},
{"1 file changed, 5 insertions(+)", 1, 5, 0},
{"1 file changed, 2 deletions(-)", 1, 0, 2},
{"10 files changed, 100 insertions(+), 50 deletions(-)", 10, 100, 50},
}
for _, tt := range tests {
c := &Commit{}
parseStatLine(tt.line, c)
if c.FilesChanged != tt.files {
t.Errorf("line %q: expected %d files, got %d", tt.line, tt.files, c.FilesChanged)
}
if c.Insertions != tt.ins {
t.Errorf("line %q: expected %d insertions, got %d", tt.line, tt.ins, c.Insertions)
}
if c.Deletions != tt.del {
t.Errorf("line %q: expected %d deletions, got %d", tt.line, tt.del, c.Deletions)
}
}
}
func TestParseFileChurn(t *testing.T) {
input := `a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2|Alice
10 5 src/main.go
3 1 src/util.go
b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3|Bob
8 2 src/main.go
`
files, err := parseFileChurn(input)
if err != nil {
t.Fatalf("parseFileChurn failed: %v", err)
}
if len(files) != 2 {
t.Fatalf("expected 2 files, got %d", len(files))
}
// main.go should be first (most changes)
if files[0].Path != "src/main.go" {
t.Errorf("expected src/main.go first, got %s", files[0].Path)
}
if files[0].Changes != 2 {
t.Errorf("expected 2 changes for main.go, got %d", files[0].Changes)
}
if files[0].Insertions != 18 {
t.Errorf("expected 18 insertions for main.go, got %d", files[0].Insertions)
}
if files[0].Deletions != 7 {
t.Errorf("expected 7 deletions for main.go, got %d", files[0].Deletions)
}
// Check authors
if files[0].Authors["Alice"] != 1 {
t.Errorf("expected Alice to have 1 change, got %d", files[0].Authors["Alice"])
}
if files[0].Authors["Bob"] != 1 {
t.Errorf("expected Bob to have 1 change, got %d", files[0].Authors["Bob"])
}
}
func TestSortFileChurn(t *testing.T) {
files := []FileChurn{
{Path: "a.go", Changes: 3},
{Path: "b.go", Changes: 10},
{Path: "c.go", Changes: 1},
{Path: "d.go", Changes: 7},
}
sortFileChurn(files)
if files[0].Path != "b.go" || files[0].Changes != 10 {
t.Errorf("expected b.go(10) first, got %s(%d)", files[0].Path, files[0].Changes)
}
if files[1].Path != "d.go" || files[1].Changes != 7 {
t.Errorf("expected d.go(7) second, got %s(%d)", files[1].Path, files[1].Changes)
}
if files[3].Path != "c.go" || files[3].Changes != 1 {
t.Errorf("expected c.go(1) last, got %s(%d)", files[3].Path, files[3].Changes)
}
}
func TestFilterByAuthor(t *testing.T) {
commits := []Commit{
{Author: "Alice Smith"},
{Author: "Bob Jones"},
{Author: "Alice Johnson"},
{Author: "Charlie"},
}
filtered := FilterByAuthor(commits, "alice")
if len(filtered) != 2 {
t.Errorf("expected 2 commits for 'alice', got %d", len(filtered))
}
filtered = FilterByAuthor(commits, "CHARLIE")
if len(filtered) != 1 {
t.Errorf("expected 1 commit for 'CHARLIE', got %d", len(filtered))
}
filtered = FilterByAuthor(commits, "nobody")
if len(filtered) != 0 {
t.Errorf("expected 0 commits for 'nobody', got %d", len(filtered))
}
}
func makeTime(year, month, day, hour int) time.Time {
return time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC)
}