package gitea import ( "testing" ) func TestParseDiffLineRanges(t *testing.T) { diff := `diff --git a/main.go b/main.go index abc1234..def5678 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ func main() { fmt.Println("hello") + fmt.Println("new line 11") + fmt.Println("new line 12") fmt.Println("existing") } @@ -30,4 +32,5 @@ func other() { return nil + // added at line 33 } diff --git a/util.go b/util.go new file mode 100644 --- /dev/null +++ b/util.go @@ -0,0 +1,5 @@ +package main + +func helper() string { + return "hi" +} ` ranges := ParseDiffNewLines(diff) // main.go should have lines 10-17 (first hunk) and 32-36 (second hunk) if !ranges.Contains("main.go", 11) { t.Error("expected main.go:11 to be in diff") } if !ranges.Contains("main.go", 12) { t.Error("expected main.go:12 to be in diff") } if !ranges.Contains("main.go", 10) { t.Error("expected main.go:10 to be in diff (context line)") } if !ranges.Contains("main.go", 33) { t.Error("expected main.go:33 to be in diff") } if ranges.Contains("main.go", 25) { t.Error("main.go:25 should NOT be in diff") } // util.go is entirely new, lines 1-5 if !ranges.Contains("util.go", 1) { t.Error("expected util.go:1 to be in diff") } if !ranges.Contains("util.go", 5) { t.Error("expected util.go:5 to be in diff") } if ranges.Contains("util.go", 6) { t.Error("util.go:6 should NOT be in diff") } // Unknown file if ranges.Contains("unknown.go", 1) { t.Error("unknown.go should not be in diff") } } func TestParseDiffNewLines_Empty(t *testing.T) { ranges := ParseDiffNewLines("") if ranges.Contains("any.go", 1) { t.Error("empty diff should contain nothing") } } func TestParseDiffNewLines_NoCommaHunk(t *testing.T) { // Single-line hunks omit the comma: @@ -1 +1 @@ diff := `diff --git a/single.go b/single.go --- a/single.go +++ b/single.go @@ -1 +1 @@ -old line +new line ` ranges := ParseDiffNewLines(diff) if !ranges.Contains("single.go", 1) { t.Error("expected single.go:1 to be in diff (no-comma hunk)") } if ranges.Contains("single.go", 2) { t.Error("single.go:2 should NOT be in diff") } } func TestParseDiffNewLines_NoNewlineMarker(t *testing.T) { // "\ No newline at end of file" should not advance line counter diff := `diff --git a/noeof.go b/noeof.go --- a/noeof.go +++ b/noeof.go @@ -1,2 +1,2 @@ +line one +line two \ No newline at end of file ` ranges := ParseDiffNewLines(diff) if !ranges.Contains("noeof.go", 1) { t.Error("expected noeof.go:1") } if !ranges.Contains("noeof.go", 2) { t.Error("expected noeof.go:2") } if ranges.Contains("noeof.go", 3) { t.Error("noeof.go:3 should NOT be in diff (no-newline marker)") } }