Skip to content

Commit

Permalink
Refactored error checking tests. Fixes sergi#71
Browse files Browse the repository at this point in the history
Code-review changes, update to an incorrectly worded error message
  • Loading branch information
maksimov committed Apr 9, 2017
1 parent 83532ca commit 43ee123
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
13 changes: 9 additions & 4 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) {
i := 0
runes := []rune(text1)

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

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

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

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

return diffs, nil
Expand Down
81 changes: 34 additions & 47 deletions diffmatchpatch/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,42 @@ func TestDiffText(t *testing.T) {
}

func TestDiffDelta(t *testing.T) {
type TestCase struct {
Name string

Text string
Delta string

ErrorMessagePrefix string
}

dmp := New()

for i, tc := range []TestCase{
{"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)"},
{"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)"},
{"Invalid URL escaping", "", "+%c3%xy", "invalid URL escape \"%xy\""},
{"Invalid UTF-8 sequence", "", "+%c3xy", "invalid UTF-8 token: \"\\xc3xy\""},
{"Invalid diff operation", "", "a", "Invalid diff operation in DiffFromDelta: a"},
{"Invalid diff syntax", "", "-", "strconv.ParseInt: parsing \"\": invalid syntax"},
{"Negative number in delta", "", "--1", "Negative number in DiffFromDelta: -1"},
{"Empty case", "", "", ""},
} {
diffs, err := dmp.DiffFromDelta(tc.Text, tc.Delta)
msg := fmt.Sprintf("Test case #%d, %s", i, tc.Name)
if tc.ErrorMessagePrefix == "" {
assert.Nil(t, err, msg)
assert.Nil(t, diffs, msg)
} else {
e := err.Error()
if strings.HasPrefix(e, tc.ErrorMessagePrefix) {
e = tc.ErrorMessagePrefix
}
assert.Nil(t, diffs, msg)
assert.Equal(t, tc.ErrorMessagePrefix, e, msg)
}
}

// Convert a diff into delta string.
diffs := []Diff{
Diff{DiffEqual, "jump"},
Expand All @@ -1021,30 +1055,6 @@ func TestDiffDelta(t *testing.T) {
deltaDiffs, err := dmp.DiffFromDelta(text1, delta)
assert.Equal(t, diffs, deltaDiffs)

// Generates error (19 < 20).
_, err = dmp.DiffFromDelta(text1+"x", delta)
if err == nil {
t.Fatal("Too long.")
}

// Generates error (19 > 18).
_, err = dmp.DiffFromDelta(text1[1:], delta)
if err == nil {
t.Fatal("Too short.")
}

// Generates error (%xy invalid URL escape).
_, err = dmp.DiffFromDelta("", "+%c3%xy")
if err == nil {
assert.Fail(t, "expected Invalid URL escape.")
}

// Generates error (invalid utf8).
_, err = dmp.DiffFromDelta("", "+%c3xy")
if err == nil {
assert.Fail(t, "expected Invalid utf8.")
}

// Test deltas with special characters.
diffs = []Diff{
Diff{DiffEqual, "\u0680 \x00 \t %"},
Expand Down Expand Up @@ -1074,29 +1084,6 @@ func TestDiffDelta(t *testing.T) {
deltaDiffs, err = dmp.DiffFromDelta("", delta)
assert.Equal(t, diffs, deltaDiffs)
assert.Nil(t, err)

// Test blank tokens.
_, err = dmp.DiffFromDelta("", "")
assert.Nil(t, err)

// Test invalid diff operation "a"
_, err = dmp.DiffFromDelta("", "a")
if err == nil {
assert.Fail(t, "expected Invalid diff operation.")
}

// Test non-numeric parameter
_, err = dmp.DiffFromDelta("", "-")
if err == nil {
assert.Fail(t, "expected Invalid syntax.")
}

// Test negative parameter
_, err = dmp.DiffFromDelta("", "--1")
if err == nil {
assert.Fail(t, "expected Negative number.")
}

}

func TestDiffXIndex(t *testing.T) {
Expand Down

0 comments on commit 43ee123

Please sign in to comment.