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

Function TestPatchFromText

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

Source from the content-addressed store, hash-verified

51}
52
53func TestPatchFromText(t *testing.T) {
54 type TestCase struct {
55 Patch string
56
57 ErrorMessagePrefix string
58 }
59
60 dmp := New()
61
62 for i, tc := range []TestCase{
63 {"", ""},
64 {"@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n %0Alaz\n", ""},
65 {"@@ -1 +1 @@\n-a\n+b\n", ""},
66 {"@@ -1,3 +0,0 @@\n-abc\n", ""},
67 {"@@ -0,0 +1,3 @@\n+abc\n", ""},
68 {"@@ _0,0 +0,0 @@\n+abc\n", "Invalid patch string: @@ _0,0 +0,0 @@"},
69 {"Bad\nPatch\n", "Invalid patch string"},
70 {"@@ -1 +1 @@\n-%zz\n", "invalid URL escape"},
71 {"@@ -99999999999999999999,1 +1,1 @@\n+abc\n", "Invalid patch string"},
72 {"@@ -1,99999999999999999999 +1,1 @@\n+abc\n", "Invalid patch string"},
73 {"@@ -9223372036854775807,1 +1,1 @@\n+abc\n", "Invalid patch string"},
74 {"@@ -2147483648,1 +1,1 @@\n+abc\n", "Invalid patch string"},
75 {"@@ -2147483647,2147483647 +2147483647,2147483647 @@\n+abc\n", ""},
76 } {
77 patches, err := dmp.PatchFromText(tc.Patch)
78 if tc.ErrorMessagePrefix == "" {
79 assert.Nil(t, err)
80
81 if tc.Patch == "" {
82 assert.Equal(t, []Patch{}, patches, fmt.Sprintf("Test case #%d, %#v", i, tc))
83 } else {
84 assert.Equal(t, tc.Patch, patches[0].String(), fmt.Sprintf("Test case #%d, %#v", i, tc))
85 }
86 } else {
87 e := err.Error()
88 if strings.HasPrefix(e, tc.ErrorMessagePrefix) {
89 e = tc.ErrorMessagePrefix
90 }
91 assert.Equal(t, tc.ErrorMessagePrefix, e)
92 }
93 }
94
95 diffs := []Diff{
96 {DiffDelete, "`1234567890-=[]\\;',./"},
97 {DiffInsert, "~!@#$%^&*()_+{}|:\"<>?"},
98 }
99
100 patches, err := dmp.PatchFromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n")
101 assert.Len(t, patches, 1)
102 assert.Equal(t, diffs,
103 patches[0].diffs,
104 )
105 assert.Nil(t, err)
106}
107
108func TestPatchToText(t *testing.T) {
109 type TestCase struct {

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
PatchFromTextMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected