assert.Equal(t,tc.Patch,actual,fmt.Sprintf("Test case #%d, %#v",i,tc))
}
}
funcTestPatchAddContext(t*testing.T){
typeTestCasestruct{
Namestring
Patchstring
Textstring
Expectedstring
}
dmp:=New()
dmp.PatchMargin=4
fori,tc:=range[]TestCase{
{"Simple case","@@ -21,4 +21,10 @@\n-jump\n+somersault\n","The quick brown fox jumps over the lazy dog.","@@ -17,12 +17,18 @@\n fox \n-jump\n+somersault\n s ov\n"},
{"Not enough trailing context","@@ -21,4 +21,10 @@\n-jump\n+somersault\n","The quick brown fox jumps.","@@ -17,10 +17,16 @@\n fox \n-jump\n+somersault\n s.\n"},
{"Not enough leading context","@@ -3 +3,2 @@\n-e\n+at\n","The quick brown fox jumps.","@@ -1,7 +1,8 @@\n Th\n-e\n+at\n qui\n"},
{"Ambiguity","@@ -3 +3,2 @@\n-e\n+at\n","The quick brown fox jumps. The quick brown fox crashes.","@@ -1,27 +1,28 @@\n Th\n-e\n+at\n quick brown fox jumps. \n"},
}{
patches,err:=dmp.PatchFromText(tc.Patch)
assert.Nil(t,err)
actual:=dmp.PatchAddContext(patches[0],tc.Text)
assert.Equal(t,tc.Expected,actual.String(),fmt.Sprintf("Test case #%d, %s",i,tc.Name))
}
}
funcTestPatchMakeAndPatchToText(t*testing.T){
typeTestCasestruct{
Namestring
Input1interface{}
Input2interface{}
Input3interface{}
Expectedstring
}
dmp:=New()
text1:="The quick brown fox jumps over the lazy dog."
text2:="That quick brown fox jumped over a lazy dog."
{"Long string with repeats",strings.Repeat("abcdef",100),strings.Repeat("abcdef",100)+"123",nil,"@@ -573,28 +573,31 @@\n cdefabcdefabcdefabcdefabcdef\n+123\n"},
{"Corner case of #31 fixed by #32","2016-09-01T03:07:14.807830741Z","2016-09-01T03:07:15.154800781Z",nil,"@@ -15,16 +15,16 @@\n 07:1\n+5.15\n 4\n-.\n 80\n+0\n 78\n-3074\n 1Z\n"},
assert.Equal(t,tc.Expected,actual,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
}
// Corner case of #28 wrong patch with timeout of 0
dmp.DiffTimeout=0
text1="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum libero vel."
text2="Lorem a ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum liberovel."
diffs:=dmp.DiffMain(text1,text2,true)
// Additional check that the diff texts are equal to the originals even if we are using DiffMain with checklines=true #29
assert.Equal(t,text1,dmp.DiffText1(diffs))
assert.Equal(t,text2,dmp.DiffText2(diffs))
patches:=dmp.PatchMake(text1,diffs)
actual:=dmp.PatchToText(patches)
assert.Equal(t,"@@ -1,14 +1,16 @@\n Lorem \n+a \n ipsum do\n@@ -148,13 +148,12 @@\n m libero\n- \n vel.\n",actual)
// Check that empty Patch array is returned for no parameter call
{"abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1","abcdefghij , h : 1 , t : 1 abcdefghij , h : 1 , t : 1 abcdefghij , h : 0 , t : 1","@@ -2,32 +2,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n@@ -29,32 +29,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n"},
}{
patches:=dmp.PatchMake(tc.Text1,tc.Text2)
patches=dmp.PatchSplitMax(patches)
actual:=dmp.PatchToText(patches)
assert.Equal(t,tc.Expected,actual,fmt.Sprintf("Test case #%d, %#v",i,tc))
{"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}},
{"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}},
{"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}},
{"Big delete, small Diff","x1234567890123456789012345678901234567890123456789012345678901234567890y","xabcy","x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y","xabcy",[]bool{true,true}},
{"Big delete, big Diff 1","x1234567890123456789012345678901234567890123456789012345678901234567890y","xabcy","x12345678901234567890---------------++++++++++---------------12345678901234567890y","xabc12345678901234567890---------------++++++++++---------------12345678901234567890y",[]bool{false,true}},
assert.Equal(t,tc.Expected,actual,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
assert.Equal(t,tc.ExpectedApplies,actualApplies,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
}
dmp.PatchDeleteThreshold=0.6
fori,tc:=range[]TestCase{
{"Big delete, big Diff 2","x1234567890123456789012345678901234567890123456789012345678901234567890y","xabcy","x12345678901234567890---------------++++++++++---------------12345678901234567890y","xabcy",[]bool{true,true}},
assert.Equal(t,tc.Expected,actual,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
assert.Equal(t,tc.ExpectedApplies,actualApplies,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
}
dmp.MatchDistance=0
dmp.MatchThreshold=0.0
dmp.PatchDeleteThreshold=0.5
fori,tc:=range[]TestCase{
{"Compensate for failed patch","abcdefghijklmnopqrstuvwxyz--------------------1234567890","abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890","ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890","ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890",[]bool{false,true}},
assert.Equal(t,tc.Expected,actual,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
assert.Equal(t,tc.ExpectedApplies,actualApplies,fmt.Sprintf("Test case #%d, %s",i,tc.Name))
}
dmp.MatchThreshold=0.5
dmp.MatchDistance=1000
fori,tc:=range[]TestCase{
{"No side effects","","test","","test",[]bool{true}},
{"No side effects with major delete","The quick brown fox jumps over the lazy dog.","Woof","The quick brown fox jumps over the lazy dog.","Woof",[]bool{true,true}},