@@ -73,11 +73,8 @@ func TestApplierInvariants(t *testing.T) {
7373 })
7474}
7575
76- func TestTextFragmentApplyStrict (t * testing.T ) {
77- tests := map [string ]struct {
78- Files applyFiles
79- Err error
80- }{
76+ func TestApplyTextFragment (t * testing.T ) {
77+ tests := map [string ]applyTest {
8178 "createFile" : {Files : getApplyFiles ("text_fragment_new" )},
8279 "deleteFile" : {Files : getApplyFiles ("text_fragment_delete_all" )},
8380
@@ -131,43 +128,18 @@ func TestTextFragmentApplyStrict(t *testing.T) {
131128
132129 for name , test := range tests {
133130 t .Run (name , func (t * testing.T ) {
134- src , patch , out := test .Files .Load (t )
135-
136- files , _ , err := Parse (bytes .NewReader (patch ))
137- if err != nil {
138- t .Fatalf ("failed to parse patch file: %v" , err )
139- }
140- if len (files ) != 1 {
141- t .Fatalf ("patch should contain exactly one file, but it has %d" , len (files ))
142- }
143- if len (files [0 ].TextFragments ) != 1 {
144- t .Fatalf ("patch should contain exactly one fragment, but it has %d" , len (files [0 ].TextFragments ))
145- }
146-
147- applier := NewApplier (bytes .NewReader (src ))
148-
149- var dst bytes.Buffer
150- err = applier .ApplyTextFragment (& dst , files [0 ].TextFragments [0 ])
151- if test .Err != nil {
152- checkApplyError (t , test .Err , err )
153- return
154- }
155- if err != nil {
156- t .Fatalf ("unexpected error applying fragment: %v" , err )
157- }
158-
159- if ! bytes .Equal (out , dst .Bytes ()) {
160- t .Errorf ("incorrect result after apply\n expected:\n %s\n actual:\n %s" , out , dst .Bytes ())
161- }
131+ test .run (t , func (w io.Writer , applier * Applier , file * File ) error {
132+ if len (file .TextFragments ) != 1 {
133+ t .Fatalf ("patch should contain exactly one fragment, but it has %d" , len (file .TextFragments ))
134+ }
135+ return applier .ApplyTextFragment (w , file .TextFragments [0 ])
136+ })
162137 })
163138 }
164139}
165140
166- func TestBinaryFragmentApply (t * testing.T ) {
167- tests := map [string ]struct {
168- Files applyFiles
169- Err interface {}
170- }{
141+ func TestApplyBinaryFragment (t * testing.T ) {
142+ tests := map [string ]applyTest {
171143 "literalCreate" : {Files : getApplyFiles ("bin_fragment_literal_create" )},
172144 "literalModify" : {Files : getApplyFiles ("bin_fragment_literal_modify" )},
173145 "deltaModify" : {Files : getApplyFiles ("bin_fragment_delta_modify" )},
@@ -205,41 +177,73 @@ func TestBinaryFragmentApply(t *testing.T) {
205177
206178 for name , test := range tests {
207179 t .Run (name , func (t * testing.T ) {
208- src , patch , out := test .Files .Load (t )
180+ test .run (t , func (w io.Writer , applier * Applier , file * File ) error {
181+ return applier .ApplyBinaryFragment (w , file .BinaryFragment )
182+ })
183+ })
184+ }
185+ }
186+
187+ func TestApplyFile (t * testing.T ) {
188+ tests := map [string ]applyTest {
189+ "textModify" : {Files : getApplyFiles ("text_file_modify" )},
190+ "binaryModify" : {Files : getApplyFiles ("bin_file_modify" )},
191+ "modeChange" : {Files : getApplyFiles ("file_mode_change" )},
192+ }
193+
194+ for name , test := range tests {
195+ t .Run (name , func (t * testing.T ) {
196+ test .run (t , func (w io.Writer , applier * Applier , file * File ) error {
197+ return applier .ApplyFile (w , file )
198+ })
199+ })
200+ }
201+ }
202+
203+ type applyTest struct {
204+ Files applyFiles
205+ Err interface {}
206+ }
207+
208+ func (at applyTest ) run (t * testing.T , apply func (io.Writer , * Applier , * File ) error ) {
209+ src , patch , out := at .Files .Load (t )
209210
210- files , _ , err := Parse (bytes .NewReader (patch ))
211- if err != nil {
212- t .Fatalf ("failed to parse patch file: %v" , err )
213- }
214- if len (files ) != 1 {
215- t .Fatalf ("patch should contain exactly one file, but it has %d" , len (files ))
216- }
211+ files , _ , err := Parse (bytes .NewReader (patch ))
212+ if err != nil {
213+ t .Fatalf ("failed to parse patch file: %v" , err )
214+ }
215+ if len (files ) != 1 {
216+ t .Fatalf ("patch should contain exactly one file, but it has %d" , len (files ))
217+ }
217218
218- applier := NewApplier (bytes .NewReader (src ))
219+ applier := NewApplier (bytes .NewReader (src ))
219220
220- var dst bytes.Buffer
221- err = applier .ApplyBinaryFragment (& dst , files [0 ].BinaryFragment )
222- if test .Err != nil {
223- checkApplyError (t , test .Err , err )
224- return
225- }
226- if err != nil {
227- t .Fatalf ("unexpected error applying fragment: %v" , err )
228- }
221+ var dst bytes.Buffer
222+ err = apply (& dst , applier , files [0 ])
223+ if at .Err != nil {
224+ at .assertError (t , err )
225+ return
226+ }
227+ if err != nil {
228+ var aerr * ApplyError
229+ if errors .As (err , & aerr ) {
230+ t .Fatalf ("unexpected error applying: at %d: fragment %d at %d: %v" , aerr .Line , aerr .Fragment , aerr .FragmentLine , err )
231+ } else {
232+ t .Fatalf ("unexpected error applying: %v" , err )
233+ }
234+ }
229235
230- if ! bytes .Equal (out , dst .Bytes ()) {
231- t .Errorf ("incorrect result after apply\n expected:\n %x\n actual:\n %x" , out , dst .Bytes ())
232- }
233- })
236+ if ! bytes .Equal (out , dst .Bytes ()) {
237+ t .Errorf ("incorrect result after apply\n expected:\n %x\n actual:\n %x" , out , dst .Bytes ())
234238 }
235239}
236240
237- func checkApplyError ( t * testing.T , terr interface {} , err error ) {
241+ func ( at applyTest ) assertError ( t * testing.T , err error ) {
238242 if err == nil {
239243 t .Fatalf ("expected error applying fragment, but got nil" )
240244 }
241245
242- switch terr := terr .(type ) {
246+ switch terr := at . Err .(type ) {
243247 case string :
244248 if ! strings .Contains (err .Error (), terr ) {
245249 t .Fatalf ("incorrect apply error: %q does not contain %q" , err .Error (), terr )
@@ -249,7 +253,7 @@ func checkApplyError(t *testing.T, terr interface{}, err error) {
249253 t .Fatalf ("incorrect apply error: expected: %T (%v), actual: %T (%v)" , terr , terr , err , err )
250254 }
251255 default :
252- t .Fatalf ("unsupported error type: %T" , terr )
256+ t .Fatalf ("unsupported expected error type: %T" , terr )
253257 }
254258}
255259
0 commit comments