Skip to content

Commit

Permalink
Fixes a bug in Equal()
Browse files Browse the repository at this point in the history
This fixes a bug in theEqual and NotEqual functions in how it deals with
integers in private structures. Before it could panic where not it
handles the properly.

This also adds a function for checking that a returned error contains
a given string message in its Error() output.
  • Loading branch information
liquidgecka committed Oct 1, 2014
1 parent 2e2f1a3 commit a1e267a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
67 changes: 67 additions & 0 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,76 @@ func (t *T) deepEqual(
}
}

case reflect.Bool:
haveBool := have.Bool()
wantBool := want.Bool()
if haveBool != wantBool {
return []string{
fmt.Sprintf("%s: not equal.", desc),
fmt.Sprintf(" have: bool(%s)\n", haveBool),
fmt.Sprintf(" want: bool(%s)\n", wantBool),
}
}

case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
// Basic integer types.
haveInt := have.Int()
wantInt := want.Int()
if haveInt != wantInt {
return []string{
fmt.Sprintf("%s: not equal", desc),
fmt.Sprintf(" have: %s(%i)\n", have.Type(), haveInt),
fmt.Sprintf(" want: %s(%i)\n", want.Type(), wantInt),
}
}

case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
// Basic unsigned integer types.
haveUint := have.Uint()
wantUint := want.Uint()
if haveUint != wantUint {
return []string{
fmt.Sprintf("%s: not equal", desc),
fmt.Sprintf(" have: %s(%i)\n", have.Type(), haveUint),
fmt.Sprintf(" want: %s(%i)\n", want.Type(), wantUint),
}
}

case reflect.Float32:
fallthrough
case reflect.Float64:
// Float types.
haveFloat := have.Float()
wantFloat := want.Float()
if haveFloat != wantFloat {
return []string{
fmt.Sprintf("%s: not equal", desc),
fmt.Sprintf(" have: %s(%f)\n", have.Type(), haveFloat),
fmt.Sprintf(" want: %s(%f)\n", want.Type(), wantFloat),
}
}


default:
// All other cases are primitive and therefor reflect.DeepEqual
// actually handles them very well.
fmt.Printf("%s %s %s\n", desc, have.Type(), want.Type())
if !reflect.DeepEqual(want.Interface(), have.Interface()) {
return []string{
fmt.Sprintf("%s: not equal.", desc),
Expand Down
17 changes: 16 additions & 1 deletion expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,20 @@ func (t *T) ExpectSuccess(err error, desc ...string) {
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
}
t.Fatalf("%sUnexpected error encountered: %#v", prefix, err)
t.Fatalf("%sUnexpected error encountered: %#v (%s)",
prefix, err, err.Error())
}

// Fails if the error message does not contain the given string.
func (t *T) ExpectErrorMessage(err error, msg string, desc ...string) {
prefix := ""
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
}
if err == nil {
t.Fatalf("%sExpected error was not returned.", prefix)
} else if !strings.Contains(err.Error(), msg) {
t.Fatalf("%sError message didn't contain the expected message:\n"+
"Error message=%s\nExpected string=%s", prefix, err.Error(), msg)
}
}
29 changes: 29 additions & 0 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,32 @@ func TestT_ExpectSuccess(t *testing.T) {
t.Fatalf("The prefix was not prepended to the message: '''%s'''", msg)
}
}

func TestT_ExpectErrorMessage(t *testing.T) {
t.Parallel()
m, T := testSetup()

// Capture the error message.
msg := ""
m.funcFatal = func(args ...interface{}) {
msg = fmt.Sprint(args...)
}
m.CheckFail(t, func() {
T.ExpectErrorMessage(nil, "test")
})
m.CheckFail(t, func() {
T.ExpectErrorMessage(fmt.Errorf("XXX"), "test")
})
m.CheckFail(t, func() {
T.ExpectErrorMessage(fmt.Errorf("ERROR"), "test", "prefix")
})
if msg == "" {
t.Fatalf("No error message was reported.")
} else if !strings.HasPrefix(msg, "prefix: ") {
t.Fatalf("The prefix was not prepended to the message: '''%s'''", msg)
}

m.CheckPass(t, func() {
T.ExpectErrorMessage(fmt.Errorf("XXX"), "XXX")
})
}

0 comments on commit a1e267a

Please sign in to comment.