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

Function TestPatchApply

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

Source from the content-addressed store, hash-verified

263}
264
265func TestPatchApply(t *testing.T) {
266 type TestCase struct {
267 Name string
268
269 Text1 string
270 Text2 string
271 TextBase string
272
273 Expected string
274 ExpectedApplies []bool
275 }
276
277 dmp := New()
278 dmp.MatchDistance = 1000
279 dmp.MatchThreshold = 0.5
280 dmp.PatchDeleteThreshold = 0.5
281
282 for i, tc := range []TestCase{
283 {"Null case", "", "", "Hello world.", "Hello world.", []bool{}},
284 {"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}},
285 {"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}},
286 {"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}},
287 {"Big delete, small Diff", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y", "xabcy", []bool{true, true}},
288 {"Big delete, big Diff 1", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabc12345678901234567890---------------++++++++++---------------12345678901234567890y", []bool{false, true}},
289 } {
290 patches := dmp.PatchMake(tc.Text1, tc.Text2)
291
292 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
293 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
294 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
295 }
296
297 dmp.PatchDeleteThreshold = 0.6
298
299 for i, tc := range []TestCase{
300 {"Big delete, big Diff 2", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabcy", []bool{true, true}},
301 } {
302 patches := dmp.PatchMake(tc.Text1, tc.Text2)
303
304 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
305 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
306 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
307 }
308
309 dmp.MatchDistance = 0
310 dmp.MatchThreshold = 0.0
311 dmp.PatchDeleteThreshold = 0.5
312
313 for i, tc := range []TestCase{
314 {"Compensate for failed patch", "abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890", []bool{false, true}},
315 } {
316 patches := dmp.PatchMake(tc.Text1, tc.Text2)
317
318 actual, actualApplies := dmp.PatchApply(patches, tc.TextBase)
319 assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
320 assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
321 }
322

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
PatchMakeMethod · 0.80
PatchApplyMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…