mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
d32b889535
These are about to get even longer, so it makes sense to separate them. Also refactor the remaining parser tests to remove some duplication.
146 lines
3.2 KiB
Go
146 lines
3.2 KiB
Go
package gitdiff
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLineOperations(t *testing.T) {
|
|
const content = "the first line\nthe second line\nthe third line\n"
|
|
|
|
t.Run("read", func(t *testing.T) {
|
|
p := newTestParser(content, false)
|
|
|
|
for i, expected := range []string{
|
|
"the first line\n",
|
|
"the second line\n",
|
|
"the third line\n",
|
|
} {
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser after line %d: %v", i, err)
|
|
}
|
|
if p.lineno != int64(i+1) {
|
|
t.Fatalf("incorrect line number: expected %d, actual: %d", i+1, p.lineno)
|
|
}
|
|
|
|
line := p.Line(0)
|
|
if line != expected {
|
|
t.Fatalf("incorrect line %d: expected %q, was %q", i+1, expected, line)
|
|
}
|
|
}
|
|
|
|
// reading after the last line should return EOF
|
|
if err := p.Next(); err != io.EOF {
|
|
t.Fatalf("expected EOF after end, but got: %v", err)
|
|
}
|
|
if p.lineno != 4 {
|
|
t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno)
|
|
}
|
|
|
|
// reading again returns EOF again and does not advance the line
|
|
if err := p.Next(); err != io.EOF {
|
|
t.Fatalf("expected EOF after end, but got: %v", err)
|
|
}
|
|
if p.lineno != 4 {
|
|
t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno)
|
|
}
|
|
})
|
|
|
|
t.Run("peek", func(t *testing.T) {
|
|
p := newTestParser(content, false)
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser: %v", err)
|
|
}
|
|
|
|
line := p.Line(1)
|
|
if line != "the second line\n" {
|
|
t.Fatalf("incorrect peek line: %s", line)
|
|
}
|
|
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser after peek: %v", err)
|
|
}
|
|
|
|
line = p.Line(0)
|
|
if line != "the second line\n" {
|
|
t.Fatalf("incorrect read line: %s", line)
|
|
}
|
|
})
|
|
|
|
t.Run("emptyInput", func(t *testing.T) {
|
|
p := newTestParser("", false)
|
|
if err := p.Next(); err != io.EOF {
|
|
t.Fatalf("expected EOF on first Next(), but got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestParserAdvancment(t *testing.T) {
|
|
tests := map[string]struct {
|
|
Input string
|
|
Parse func(p *parser) error
|
|
EndLine string
|
|
}{
|
|
"ParseGitFileHeader": {
|
|
Input: `diff --git a/dir/file.txt b/dir/file.txt
|
|
index 9540595..30e6333 100644
|
|
--- a/dir/file.txt
|
|
+++ b/dir/file.txt
|
|
@@ -1,2 +1,3 @@
|
|
context line
|
|
`,
|
|
Parse: func(p *parser) error {
|
|
_, err := p.ParseGitFileHeader()
|
|
return err
|
|
},
|
|
EndLine: "@@ -1,2 +1,3 @@\n",
|
|
},
|
|
"ParseTraditionalFileHeader": {
|
|
Input: `--- dir/file.txt
|
|
+++ dir/file.txt
|
|
@@ -1,2 +1,3 @@
|
|
context line
|
|
`,
|
|
Parse: func(p *parser) error {
|
|
_, err := p.ParseTraditionalFileHeader()
|
|
return err
|
|
},
|
|
EndLine: "@@ -1,2 +1,3 @@\n",
|
|
},
|
|
"ParseTextFragmentHeader": {
|
|
Input: `@@ -1,2 +1,3 @@
|
|
context line
|
|
`,
|
|
Parse: func(p *parser) error {
|
|
_, err := p.ParseTextFragmentHeader()
|
|
return err
|
|
},
|
|
EndLine: "context line\n",
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
p := newTestParser(test.Input, true)
|
|
|
|
if err := test.Parse(p); err != nil {
|
|
t.Fatalf("unexpected error while parsing: %v", err)
|
|
}
|
|
|
|
if test.EndLine != p.Line(0) {
|
|
t.Errorf("incorrect position after parsing\nexpected: %q\nactual: %q", test.EndLine, p.Line(0))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func newTestParser(input string, init bool) *parser {
|
|
p := &parser{r: bufio.NewReader(strings.NewReader(input))}
|
|
if init {
|
|
_ = p.Next()
|
|
}
|
|
return p
|
|
}
|