Skip to content

Commit 84e56c1

Browse files
authored
Merge pull request #6 from vcaesar/dev
add Error() assert support and IsType() is nil return
2 parents 6220b2b + 41a2b9d commit 84e56c1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

assert.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ func (at *Assertions) NotNil(actual interface{}, args ...interface{}) bool {
7676
return NotEqual(at.t, nil, actual, info, call, cinfo)
7777
}
7878

79+
// Error asserts that not equal error.
80+
//
81+
// at.Error(t *testing.T, err)
82+
//
83+
func (at *Assertions) Error(actual interface{}, args ...interface{}) bool {
84+
info, call, cinfo := callAdd(Type, args...)
85+
return IsType(at.t, "*errors.errorString", actual, info, call, cinfo)
86+
}
87+
7988
// Empty asserts that empty and objects are equal.
8089
func (at *Assertions) Empty(actual interface{}, args ...interface{}) bool {
8190
info, call, cinfo := typeCall("", actual, args...)

tt.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ func TypeOf(expect, actual interface{}) bool {
8585

8686
// IsType asserts that two objects type are equal
8787
func IsType(t TestingT, expect string, actual interface{}, args ...interface{}) bool {
88+
if actual == nil && expect == "nil" {
89+
return true
90+
}
91+
if actual == nil {
92+
return false
93+
}
94+
8895
s := reflect.TypeOf(actual).String()
8996

9097
info, call, cinfo := typeCall(expect, s, args...)
@@ -236,6 +243,12 @@ func NotNil(t TestingT, actual interface{}, args ...interface{}) bool {
236243
return NotEqual(t, nil, actual, info, call, cinfo)
237244
}
238245

246+
// Error asserts that equal error.
247+
func Error(t TestingT, actual interface{}, args ...interface{}) bool {
248+
info, call, cinfo := callAdd(Type, args...)
249+
return IsType(t, "*errors.errorString", actual, info, call, cinfo)
250+
}
251+
239252
// Empty asserts that empty and objects are equal.
240253
func Empty(t TestingT, actual interface{}, args ...interface{}) bool {
241254
info, call, cinfo := typeCall("", actual, args...)

tt_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tt
22

33
import (
4+
"errors"
45
"fmt"
56
"testing"
67

@@ -33,6 +34,8 @@ func TestTT(t *testing.T) {
3334
Equal(t, 2, add.Add(1, 1))
3435

3536
Nil(t, nil)
37+
Error(t, errors.New("error new"))
38+
IsType(t, "nil", nil)
3639
Empty(t, "")
3740
Zero(t, 0)
3841
Bool(t, 1 == 1)
@@ -121,6 +124,8 @@ func TestArgs(t *testing.T) {
121124
at.NotEqual(3, add.Add(1, 1), "", 5)
122125

123126
at.Nil(nil, "", 4)
127+
at.NotNil(1, "", "tt_test.go:127")
128+
at.Error(errors.New("error new"), "", "tt_test.go:128")
124129
at.Empty("", "", 4)
125130
at.Bool(1 == 1, "", 4)
126131
at.True(1 == 1, "", 4)

0 commit comments

Comments
 (0)