Skip to content

Commit 77f9d90

Browse files
authored
Merge pull request sergi#72 from maksimov/master
Refactored error checking tests. Fixes sergi#71
2 parents 24e2351 + ad02bdd commit 77f9d90

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

diffmatchpatch/diff.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,7 @@ func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
12851285
// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
12861286
func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) {
12871287
i := 0
1288+
runes := []rune(text1)
12881289

12891290
for _, token := range strings.Split(delta, "\t") {
12901291
if len(token) == 0 {
@@ -1316,9 +1317,13 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
13161317
return nil, errors.New("Negative number in DiffFromDelta: " + param)
13171318
}
13181319

1319-
// Remember that string slicing is by byte - we want by rune here.
1320-
text := string([]rune(text1)[i : i+int(n)])
13211320
i += int(n)
1321+
// Break out if we are out of bounds, go1.6 can't handle this very well
1322+
if i > len(runes) {
1323+
break
1324+
}
1325+
// Remember that string slicing is by byte - we want by rune here.
1326+
text := string(runes[i-int(n) : i])
13221327

13231328
if op == '=' {
13241329
diffs = append(diffs, Diff{DiffEqual, text})
@@ -1331,8 +1336,8 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
13311336
}
13321337
}
13331338

1334-
if i != len([]rune(text1)) {
1335-
return nil, fmt.Errorf("Delta length (%v) smaller than source text length (%v)", i, len(text1))
1339+
if i != len(runes) {
1340+
return nil, fmt.Errorf("Delta length (%v) is different from source text length (%v)", i, len(text1))
13361341
}
13371342

13381343
return diffs, nil

diffmatchpatch/diff_test.go

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,42 @@ func TestDiffText(t *testing.T) {
998998
}
999999

10001000
func TestDiffDelta(t *testing.T) {
1001+
type TestCase struct {
1002+
Name string
1003+
1004+
Text string
1005+
Delta string
1006+
1007+
ErrorMessagePrefix string
1008+
}
1009+
10011010
dmp := New()
10021011

1012+
for i, tc := range []TestCase{
1013+
{"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)"},
1014+
{"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)"},
1015+
{"Invalid URL escaping", "", "+%c3%xy", "invalid URL escape \"%xy\""},
1016+
{"Invalid UTF-8 sequence", "", "+%c3xy", "invalid UTF-8 token: \"\\xc3xy\""},
1017+
{"Invalid diff operation", "", "a", "Invalid diff operation in DiffFromDelta: a"},
1018+
{"Invalid diff syntax", "", "-", "strconv.ParseInt: parsing \"\": invalid syntax"},
1019+
{"Negative number in delta", "", "--1", "Negative number in DiffFromDelta: -1"},
1020+
{"Empty case", "", "", ""},
1021+
} {
1022+
diffs, err := dmp.DiffFromDelta(tc.Text, tc.Delta)
1023+
msg := fmt.Sprintf("Test case #%d, %s", i, tc.Name)
1024+
if tc.ErrorMessagePrefix == "" {
1025+
assert.Nil(t, err, msg)
1026+
assert.Nil(t, diffs, msg)
1027+
} else {
1028+
e := err.Error()
1029+
if strings.HasPrefix(e, tc.ErrorMessagePrefix) {
1030+
e = tc.ErrorMessagePrefix
1031+
}
1032+
assert.Nil(t, diffs, msg)
1033+
assert.Equal(t, tc.ErrorMessagePrefix, e, msg)
1034+
}
1035+
}
1036+
10031037
// Convert a diff into delta string.
10041038
diffs := []Diff{
10051039
Diff{DiffEqual, "jump"},
@@ -1021,30 +1055,6 @@ func TestDiffDelta(t *testing.T) {
10211055
deltaDiffs, err := dmp.DiffFromDelta(text1, delta)
10221056
assert.Equal(t, diffs, deltaDiffs)
10231057

1024-
// Generates error (19 < 20).
1025-
_, err = dmp.DiffFromDelta(text1+"x", delta)
1026-
if err == nil {
1027-
t.Fatal("Too long.")
1028-
}
1029-
1030-
// Generates error (19 > 18).
1031-
_, err = dmp.DiffFromDelta(text1[1:], delta)
1032-
if err == nil {
1033-
t.Fatal("Too short.")
1034-
}
1035-
1036-
// Generates error (%xy invalid URL escape).
1037-
_, err = dmp.DiffFromDelta("", "+%c3%xy")
1038-
if err == nil {
1039-
assert.Fail(t, "expected Invalid URL escape.")
1040-
}
1041-
1042-
// Generates error (invalid utf8).
1043-
_, err = dmp.DiffFromDelta("", "+%c3xy")
1044-
if err == nil {
1045-
assert.Fail(t, "expected Invalid utf8.")
1046-
}
1047-
10481058
// Test deltas with special characters.
10491059
diffs = []Diff{
10501060
Diff{DiffEqual, "\u0680 \x00 \t %"},
@@ -1074,29 +1084,6 @@ func TestDiffDelta(t *testing.T) {
10741084
deltaDiffs, err = dmp.DiffFromDelta("", delta)
10751085
assert.Equal(t, diffs, deltaDiffs)
10761086
assert.Nil(t, err)
1077-
1078-
// Test blank tokens.
1079-
_, err = dmp.DiffFromDelta("", "")
1080-
assert.Nil(t, err)
1081-
1082-
// Test invalid diff operation "a"
1083-
_, err = dmp.DiffFromDelta("", "a")
1084-
if err == nil {
1085-
assert.Fail(t, "expected Invalid diff operation.")
1086-
}
1087-
1088-
// Test non-numeric parameter
1089-
_, err = dmp.DiffFromDelta("", "-")
1090-
if err == nil {
1091-
assert.Fail(t, "expected Invalid syntax.")
1092-
}
1093-
1094-
// Test negative parameter
1095-
_, err = dmp.DiffFromDelta("", "--1")
1096-
if err == nil {
1097-
assert.Fail(t, "expected Negative number.")
1098-
}
1099-
11001087
}
11011088

11021089
func TestDiffXIndex(t *testing.T) {

0 commit comments

Comments
 (0)