Files
PR-Helper/services/review_test.go
T
2026-06-24 18:28:14 +08:00

108 lines
2.0 KiB
Go

package services
import "testing"
func TestCountDiffLines(t *testing.T) {
tests := []struct {
name string
patch string
want int
}{
{
name: "standard diff with additions and deletions",
patch: `--- a/file.go
+++ b/file.go
@@ -1,5 +1,6 @@
package main
+import "fmt"
+import "os"
-func old() {}
+func new() {}
func keep() {}`,
want: 4, // +import, +import, -func, +func
},
{
name: "only additions",
patch: "+++ b/file.go\n@@ -0,0 +1,3 @@\n+line1\n+line2\n+line3",
want: 3,
},
{
name: "only deletions",
patch: "--- a/file.go\n@@ -1,3 +0,0 @@\n-line1\n-line2\n-line3",
want: 3,
},
{
name: "empty patch",
patch: "",
want: 0,
},
{
name: "context only lines (no +/- prefix)",
patch: `@@ -1,3 +1,3 @@
func unchanged() {}
return nil
}`,
want: 0,
},
{
name: "header lines excluded",
patch: `--- a/old.go
+++ b/new.go
@@ -1 +1 @@
-old
+new`,
want: 2, // -old and +new (headers excluded)
},
{
name: "mixed content with no changes",
patch: "just some\nplain text\nlines",
want: 0,
},
{
name: "single addition",
patch: "+added line",
want: 1,
},
{
name: "single deletion",
patch: "-removed line",
want: 1,
},
{
name: "line starting with plus in content context",
patch: " +not a change, just context\n+actual addition",
want: 1, // only the line starting with + at position 0
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := countDiffLines(tt.patch)
if got != tt.want {
t.Errorf("countDiffLines() = %d, want %d", got, tt.want)
}
})
}
}
func TestCountDiffLines_MultiFileDiff(t *testing.T) {
patch := `--- a/file1.go
+++ b/file1.go
@@ -1,3 +1,4 @@
package main
+import "fmt"
func main() {
+ fmt.Println("hello")
}
--- a/file2.go
+++ b/file2.go
@@ -1,2 +1,2 @@
-old line
+new line`
got := countDiffLines(patch)
want := 4 // +import, +fmt.Println, -old line, +new line
if got != want {
t.Errorf("countDiffLines() = %d, want %d", got, want)
}
}