Skip to content

Commit

Permalink
Ensure that equality on unicode chars works.
Browse files Browse the repository at this point in the history
Unicode characters were broken as it would only compare the first byte of
the character due to the strange way that string indexing works in
golang. This converts that to a working rune iteration which is far
safer and catches errors.

A regression test was added in order to test for this case in the
future.
  • Loading branch information
liquidgecka committed Nov 18, 2016
1 parent ca1ec5a commit 2eb52e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,22 @@ func (t *T) deepEqual(
// and assert to a string will panic.
hstr := have.String()
wstr := want.String()
if len(hstr) != len(wstr) {
if hstr == wstr {
// Cheap equality test passed, no need to continue.
break
}
hrunes := []rune(hstr)
wrunes := []rune(wstr)
if len(hrunes) != len(wrunes) {
return []string{
fmt.Sprintf("%s: len(have) %d != len(want) %d.",
desc, len(hstr), len(wstr)),
desc, len(hrunes), len(wrunes)),
fmt.Sprintf(" have: %#v", hstr),
fmt.Sprintf(" want: %#v", wstr),
}
}
for i := range hstr {
if hstr[i] != wstr[i] {
for i, r := range hrunes {
if r != wrunes[i] {
return []string{
fmt.Sprintf("%s: difference at index %d.", desc, i),
fmt.Sprintf(" have: %#v", hstr),
Expand Down Expand Up @@ -446,6 +452,5 @@ func (t *T) deepEqual(
}
}

// This shouldn't ever be reached.
return diffs
}
5 changes: 5 additions & 0 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ func TestT_EqualAndNotEqual(t *testing.T) {
[]interface{}{"", d1, d2, d3})
}

// Unicode strings + Regressions
runTest(
[]interface{}{"└"},
[]interface{}{"╰"})

// Boolean (no need to test with 100 values.)
runTest(
[]interface{}{true},
Expand Down

0 comments on commit 2eb52e2

Please sign in to comment.