MCPcopy Create free account
hub / github.com/sergi/go-diff / TestPatchApply

Function TestPatchApply

diffmatchpatch/patch_test.go:271–345  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

269}
270
271func TestPatchApply(t *testing.T) {
272 type TestCase struct {
273 Name string
274
275 Text1 string
276 Text2 string
277 TextBase string
278
279 Expected string
280 ExpectedApplies []bool
281 }
282
283 dmp := New()
284 dmp.MatchDistance = 1000
285 dmp.MatchThreshold = 0.5
286 dmp.PatchDeleteThreshold = 0.5
287
288 for i, tc := range []TestCase{
289 {"Null case", "", "", "Hello world.", "Hello world.", []bool{}},
290 {"Exact match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", []bool{true, true}},
291 {"Partial match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "The quick red rabbit jumps over the tired tiger.", "That quick red rabbit jumped over a tired tiger.", []bool{true, true}},
292 {"Failed match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "I am the very model of a modern major general.", "I am the very model of a modern major general.", []bool{false, false}},
293 {"Big delete, small Diff", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y", "xabcy", []bool{true, true}},
294 {"Big delete, big Diff 1", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabc12345678901234567890---------------++++++++++---------------12345678901234567890y", []bool{false, true}},
295 } {
296 patches := dmp.PatchMake(tc.Text1, tc.Text2)
297
298 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
299 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
300 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
301 }
302
303 dmp.PatchDeleteThreshold = 0.6
304
305 for i, tc := range []TestCase{
306 {"Big delete, big Diff 2", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabcy", []bool{true, true}},
307 } {
308 patches := dmp.PatchMake(tc.Text1, tc.Text2)
309
310 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
311 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
312 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
313 }
314
315 dmp.MatchDistance = 0
316 dmp.MatchThreshold = 0.0
317 dmp.PatchDeleteThreshold = 0.5
318
319 for i, tc := range []TestCase{
320 {"Compensate for failed patch", "abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890", []bool{false, true}},
321 } {
322 patches := dmp.PatchMake(tc.Text1, tc.Text2)
323
324 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
325 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
326 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
327 }
328

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
PatchMakeMethod · 0.80
PatchApplyMethod · 0.80

Tested by

no test coverage detected