mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
ccf0c58db8
Call Next() to advance the parser state until it returns a non-nil error, then check if the error is io.EOF. This makes EOF handling easier and also means that Line() and PeekLine() can be called multiple times without changing state. The next step is to update parse functions to return an internal marker error if they are called on an invalid line. This should improve correctness and remove the duplication of testing a condition and then calling a parse function, which checks the same condition.
127 lines
2.5 KiB
Go
127 lines
2.5 KiB
Go
package gitdiff
|
|
|
|
import (
|
|
"bufio"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLineOperations(t *testing.T) {
|
|
const content = "the first line\nthe second line\nthe third line\n"
|
|
|
|
newParser := func() *parser {
|
|
return &parser{r: bufio.NewReader(strings.NewReader(content))}
|
|
}
|
|
|
|
t.Run("readLine", func(t *testing.T) {
|
|
p := newParser()
|
|
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser: %v", err)
|
|
}
|
|
|
|
line := p.Line()
|
|
if line != "the first line\n" {
|
|
t.Fatalf("incorrect first line: %s", line)
|
|
}
|
|
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser: %v", err)
|
|
}
|
|
|
|
line = p.Line()
|
|
if p.Line() != "the second line\n" {
|
|
t.Fatalf("incorrect second line: %s", line)
|
|
}
|
|
})
|
|
|
|
t.Run("peekLine", func(t *testing.T) {
|
|
p := newParser()
|
|
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser: %v", err)
|
|
}
|
|
|
|
line := p.PeekLine()
|
|
if line != "the second line\n" {
|
|
t.Fatalf("incorrect peek line: %s", line)
|
|
}
|
|
|
|
// test that reading the line returns the same value
|
|
if err := p.Next(); err != nil {
|
|
t.Fatalf("error advancing parser: %v", err)
|
|
}
|
|
|
|
line = p.Line()
|
|
if line != "the second line\n" {
|
|
t.Fatalf("incorrect line: %s", line)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestParseFragmentHeader(t *testing.T) {
|
|
tests := map[string]struct {
|
|
Input string
|
|
Output *Fragment
|
|
Err bool
|
|
}{
|
|
"shortest": {
|
|
Input: "@@ -0,0 +1 @@\n",
|
|
Output: &Fragment{
|
|
OldPosition: 0,
|
|
OldLines: 0,
|
|
NewPosition: 1,
|
|
NewLines: 1,
|
|
},
|
|
},
|
|
"standard": {
|
|
Input: "@@ -21,5 +28,9 @@\n",
|
|
Output: &Fragment{
|
|
OldPosition: 21,
|
|
OldLines: 5,
|
|
NewPosition: 28,
|
|
NewLines: 9,
|
|
},
|
|
},
|
|
"trailingComment": {
|
|
Input: "@@ -21,5 +28,9 @@ func test(n int) {\n",
|
|
Output: &Fragment{
|
|
Comment: "func test(n int) {",
|
|
OldPosition: 21,
|
|
OldLines: 5,
|
|
NewPosition: 28,
|
|
NewLines: 9,
|
|
},
|
|
},
|
|
"incomplete": {
|
|
Input: "@@ -12,3 +2\n",
|
|
Err: true,
|
|
},
|
|
"badNumbers": {
|
|
Input: "@@ -1a,2b +3c,4d @@\n",
|
|
Err: true,
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
var frag Fragment
|
|
err := parseFragmentHeader(&frag, test.Input)
|
|
if test.Err {
|
|
if err == nil {
|
|
t.Fatalf("expected error parsing header, but got nil")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("error parsing header: %v", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(*test.Output, frag) {
|
|
t.Fatalf("incorrect fragment\nexpected: %+v\nactual: %+v", *test.Output, frag)
|
|
}
|
|
})
|
|
}
|
|
}
|