@@ -2,6 +2,7 @@ package gitdiff
22
33import (
44 "io"
5+ "reflect"
56 "testing"
67)
78
@@ -35,7 +36,7 @@ func TestParseBinaryMarker(t *testing.T) {
3536
3637 isBinary , hasData , err := p .ParseBinaryMarker ()
3738 if test .Err {
38- if err ! = nil || err == io .EOF {
39+ if err = = nil || err == io .EOF {
3940 t .Fatalf ("expected error parsing binary marker, but got %v" , err )
4041 }
4142 return
@@ -52,3 +53,58 @@ func TestParseBinaryMarker(t *testing.T) {
5253 })
5354 }
5455}
56+
57+ func TestParseBinaryFragmentHeader (t * testing.T ) {
58+ tests := map [string ]struct {
59+ Input string
60+ Output * BinaryFragment
61+ Err bool
62+ }{
63+ "delta" : {
64+ Input : "delta 1234\n " ,
65+ Output : & BinaryFragment {
66+ Method : BinaryPatchDelta ,
67+ Size : 1234 ,
68+ },
69+ },
70+ "literal" : {
71+ Input : "literal 1234\n " ,
72+ Output : & BinaryFragment {
73+ Method : BinaryPatchLiteral ,
74+ Size : 1234 ,
75+ },
76+ },
77+ "unknownMethod" : {
78+ Input : "compressed 1234\n " ,
79+ Output : nil ,
80+ },
81+ "notAHeader" : {
82+ Input : "Binary files differ\n " ,
83+ Output : nil ,
84+ },
85+ "invalidSize" : {
86+ Input : "delta 123abc\n " ,
87+ Err : true ,
88+ },
89+ }
90+
91+ for name , test := range tests {
92+ t .Run (name , func (t * testing.T ) {
93+ p := newTestParser (test .Input , true )
94+
95+ frag , err := p .ParseBinaryFragmentHeader ()
96+ if test .Err {
97+ if err == nil || err == io .EOF {
98+ t .Fatalf ("expected error parsing binary header, but got %v" , err )
99+ }
100+ return
101+ }
102+ if err != nil {
103+ t .Fatalf ("unexpected error parsing binary header: %v" , err )
104+ }
105+ if ! reflect .DeepEqual (test .Output , frag ) {
106+ t .Errorf ("incorrect binary fragment\n expected: %+v\n actual: %+v" , test .Output , frag )
107+ }
108+ })
109+ }
110+ }
0 commit comments