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

Function TestDiffDelta

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

Source from the content-addressed store, hash-verified

1085}
1086
1087func TestDiffDelta(t *testing.T) {
1088 type TestCase struct {
1089 Name string
1090
1091 Text string
1092 Delta string
1093
1094 ErrorMessagePrefix string
1095 }
1096
1097 dmp := New()
1098
1099 for i, tc := range []TestCase{
1100 {"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)"},
1101 {"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)"},
1102 {"Invalid URL escaping", "", "+%c3%xy", "invalid URL escape \"%xy\""},
1103 {"Invalid UTF-8 sequence", "", "+%c3xy", "invalid UTF-8 token: \"\\xc3xy\""},
1104 {"Invalid diff operation", "", "a", "Invalid diff operation in DiffFromDelta: a"},
1105 {"Invalid diff syntax", "", "-", "strconv.ParseInt: parsing \"\": invalid syntax"},
1106 {"Negative number in delta", "", "--1", "Negative number in DiffFromDelta: -1"},
1107 {"Empty case", "", "", ""},
1108 } {
1109 diffs, err := dmp.DiffFromDelta(tc.Text, tc.Delta)
1110 msg := fmt.Sprintf("Test case #%d, %s", i, tc.Name)
1111 if tc.ErrorMessagePrefix == "" {
1112 assert.Nil(t, err, msg)
1113 assert.Nil(t, diffs, msg)
1114 } else {
1115 e := err.Error()
1116 if strings.HasPrefix(e, tc.ErrorMessagePrefix) {
1117 e = tc.ErrorMessagePrefix
1118 }
1119 assert.Nil(t, diffs, msg)
1120 assert.Equal(t, tc.ErrorMessagePrefix, e, msg)
1121 }
1122 }
1123
1124 // Convert a diff into delta string.
1125 diffs := []Diff{
1126 Diff{DiffEqual, "jump"},
1127 Diff{DiffDelete, "s"},
1128 Diff{DiffInsert, "ed"},
1129 Diff{DiffEqual, " over "},
1130 Diff{DiffDelete, "the"},
1131 Diff{DiffInsert, "a"},
1132 Diff{DiffEqual, " lazy"},
1133 Diff{DiffInsert, "old dog"},
1134 }
1135 text1 := dmp.DiffText1(diffs)
1136 assert.Equal(t, "jumps over the lazy", text1)
1137
1138 delta := dmp.DiffToDelta(diffs)
1139 assert.Equal(t, "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta)
1140
1141 // Convert delta string into a diff.
1142 deltaDiffs, err := dmp.DiffFromDelta(text1, delta)
1143 assert.Equal(t, diffs, deltaDiffs)
1144

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…