mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
Add test for binary fragment header parsing
This commit is contained in:
@@ -2,6 +2,7 @@ package gitdiff
|
||||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,7 @@ func TestParseBinaryMarker(t *testing.T) {
|
||||
|
||||
isBinary, hasData, err := p.ParseBinaryMarker()
|
||||
if test.Err {
|
||||
if err != nil || err == io.EOF {
|
||||
if err == nil || err == io.EOF {
|
||||
t.Fatalf("expected error parsing binary marker, but got %v", err)
|
||||
}
|
||||
return
|
||||
@@ -52,3 +53,58 @@ func TestParseBinaryMarker(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBinaryFragmentHeader(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
Input string
|
||||
Output *BinaryFragment
|
||||
Err bool
|
||||
}{
|
||||
"delta": {
|
||||
Input: "delta 1234\n",
|
||||
Output: &BinaryFragment{
|
||||
Method: BinaryPatchDelta,
|
||||
Size: 1234,
|
||||
},
|
||||
},
|
||||
"literal": {
|
||||
Input: "literal 1234\n",
|
||||
Output: &BinaryFragment{
|
||||
Method: BinaryPatchLiteral,
|
||||
Size: 1234,
|
||||
},
|
||||
},
|
||||
"unknownMethod": {
|
||||
Input: "compressed 1234\n",
|
||||
Output: nil,
|
||||
},
|
||||
"notAHeader": {
|
||||
Input: "Binary files differ\n",
|
||||
Output: nil,
|
||||
},
|
||||
"invalidSize": {
|
||||
Input: "delta 123abc\n",
|
||||
Err: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
p := newTestParser(test.Input, true)
|
||||
|
||||
frag, err := p.ParseBinaryFragmentHeader()
|
||||
if test.Err {
|
||||
if err == nil || err == io.EOF {
|
||||
t.Fatalf("expected error parsing binary header, but got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error parsing binary header: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(test.Output, frag) {
|
||||
t.Errorf("incorrect binary fragment\nexpected: %+v\n actual: %+v", test.Output, frag)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user