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

Function TestDiffDelta

diffmatchpatch/diff_test.go:1111–1198  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1109}
1110
1111func TestDiffDelta(t *testing.T) {
1112 type TestCase struct {
1113 Name string
1114
1115 Text string
1116 Delta string
1117
1118 ErrorMessagePrefix string
1119 }
1120
1121 dmp := New()
1122
1123 for i, tc := range []TestCase{
1124 {"Delta shorter than text", "jumps over the lazyx", "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", "Delta length (19) is different from source text length (20)"},
1125 {"Delta longer than text", "umps over the lazy", "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", "Delta length (19) is different from source text length (18)"},
1126 {"Invalid URL escaping", "", "+%c3%xy", "invalid URL escape \"%xy\""},
1127 {"Invalid UTF-8 sequence", "", "+%c3xy", "invalid UTF-8 token: \"\\xc3xy\""},
1128 {"Invalid diff operation", "", "a", "Invalid diff operation in DiffFromDelta: a"},
1129 {"Invalid diff syntax", "", "-", "strconv.ParseInt: parsing \"\": invalid syntax"},
1130 {"Negative number in delta", "", "--1", "Negative number in DiffFromDelta: -1"},
1131 {"Empty case", "", "", ""},
1132 } {
1133 diffs, err := dmp.DiffFromDelta(tc.Text, tc.Delta)
1134 msg := fmt.Sprintf("Test case #%d, %s", i, tc.Name)
1135 if tc.ErrorMessagePrefix == "" {
1136 assert.Nil(t, err, msg)
1137 assert.Nil(t, diffs, msg)
1138 } else {
1139 e := err.Error()
1140 if strings.HasPrefix(e, tc.ErrorMessagePrefix) {
1141 e = tc.ErrorMessagePrefix
1142 }
1143 assert.Nil(t, diffs, msg)
1144 assert.Equal(t, tc.ErrorMessagePrefix, e, msg)
1145 }
1146 }
1147
1148 // Convert a diff into delta string.
1149 diffs := []Diff{
1150 Diff{DiffEqual, "jump"},
1151 Diff{DiffDelete, "s"},
1152 Diff{DiffInsert, "ed"},
1153 Diff{DiffEqual, " over "},
1154 Diff{DiffDelete, "the"},
1155 Diff{DiffInsert, "a"},
1156 Diff{DiffEqual, " lazy"},
1157 Diff{DiffInsert, "old dog"},
1158 }
1159 text1 := dmp.DiffText1(diffs)
1160 assert.Equal(t, "jumps over the lazy", text1)
1161
1162 delta := dmp.DiffToDelta(diffs)
1163 assert.Equal(t, "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta)
1164
1165 // Convert delta string into a diff.
1166 deltaDiffs, err := dmp.DiffFromDelta(text1, delta)
1167 assert.Equal(t, diffs, deltaDiffs)
1168

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
DiffFromDeltaMethod · 0.80
DiffText1Method · 0.80
DiffToDeltaMethod · 0.80

Tested by

no test coverage detected