diff --git a/assert/assertions.go b/assert/assertions.go index fbf03f4..0a01629 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -334,7 +334,7 @@ func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", object), msgAndArgs...) } -var zeros = []interface{}{ +var numericZeros = []interface{}{ int(0), int8(0), int16(0), @@ -360,7 +360,7 @@ func isEmpty(object interface{}) bool { return true } - for _, v := range zeros { + for _, v := range numericZeros { if object == v { return true } @@ -893,3 +893,19 @@ func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interf return !match } + +// Zero asserts that i is the zero value for its type and returns the truth. +func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if i != nil && !reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should be zero, but was %v", i), msgAndArgs...) + } + return true +} + +// NotZero asserts that i is not the zero value for its type and returns the truth. +func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if i == nil || reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should not be zero, but was %v", i), msgAndArgs...) + } + return true +} diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 36c671e..ce4ecae 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -4,11 +4,79 @@ import ( "errors" "io" "math" + "reflect" "regexp" "testing" "time" ) +var ( + i interface{} + zeros = []interface{}{ + false, + byte(0), + complex64(0), + complex128(0), + float32(0), + float64(0), + int(0), + int8(0), + int16(0), + int32(0), + int64(0), + rune(0), + uint(0), + uint8(0), + uint16(0), + uint32(0), + uint64(0), + uintptr(0), + "", + [0]interface{}{}, + []interface{}(nil), + struct{ x int }{}, + (*interface{})(nil), + (func())(nil), + nil, + interface{}(nil), + map[interface{}]interface{}(nil), + (chan interface{})(nil), + (<-chan interface{})(nil), + (chan<- interface{})(nil), + } + nonZeros = []interface{}{ + true, + byte(1), + complex64(1), + complex128(1), + float32(1), + float64(1), + int(1), + int8(1), + int16(1), + int32(1), + int64(1), + rune(1), + uint(1), + uint8(1), + uint16(1), + uint32(1), + uint64(1), + uintptr(1), + "s", + [1]interface{}{1}, + []interface{}{}, + struct{ x int }{1}, + (*interface{})(&i), + (func())(func() {}), + interface{}(1), + map[interface{}]interface{}{}, + (chan interface{})(make(chan interface{})), + (<-chan interface{})(make(chan interface{})), + (chan<- interface{})(make(chan interface{})), + } +) + // AssertionTesterInterface defines an interface to be used for testing assertion methods type AssertionTesterInterface interface { TestMethod() @@ -811,3 +879,27 @@ func TestCallerInfoWithAutogeneratedFunctions(t *testing.T) { testAutogeneratedFunction() }) } + +func TestZero(t *testing.T) { + mockT := new(testing.T) + + for _, test := range zeros { + True(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) + } + + for _, test := range nonZeros { + False(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) + } +} + +func TestNotZero(t *testing.T) { + mockT := new(testing.T) + + for _, test := range zeros { + False(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) + } + + for _, test := range nonZeros { + True(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) + } +} diff --git a/assert/forward_assertions.go b/assert/forward_assertions.go index d8d3f53..dc14771 100644 --- a/assert/forward_assertions.go +++ b/assert/forward_assertions.go @@ -263,3 +263,13 @@ func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...inter func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { return NotRegexp(a.t, rx, str, msgAndArgs...) } + +// Zero asserts that i is the zero value for its type and returns the truth. +func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { + return Zero(a.t, i, msgAndArgs...) +} + +// NotZero asserts that i is not the zero value for its type and returns the truth. +func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool { + return NotZero(a.t, i, msgAndArgs...) +} diff --git a/assert/forward_assertions_test.go b/assert/forward_assertions_test.go index 3df3f39..280d0ab 100644 --- a/assert/forward_assertions_test.go +++ b/assert/forward_assertions_test.go @@ -509,3 +509,29 @@ func TestRegexpWrapper(t *testing.T) { True(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str)) } } + +func TestZeroWrapper(t *testing.T) { + assert := New(t) + mockAssert := New(new(testing.T)) + + for _, test := range zeros { + assert.True(mockAssert.Zero(test), "Zero should return true for %v", test) + } + + for _, test := range nonZeros { + assert.False(mockAssert.Zero(test), "Zero should return false for %v", test) + } +} + +func TestNotZeroWrapper(t *testing.T) { + assert := New(t) + mockAssert := New(new(testing.T)) + + for _, test := range zeros { + assert.False(mockAssert.NotZero(test), "Zero should return true for %v", test) + } + + for _, test := range nonZeros { + assert.True(mockAssert.NotZero(test), "Zero should return false for %v", test) + } +} diff --git a/require/forward_requirements.go b/require/forward_requirements.go index 069d419..d58512f 100644 --- a/require/forward_requirements.go +++ b/require/forward_requirements.go @@ -209,3 +209,13 @@ func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...inter func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) { NotRegexp(a.t, rx, str, msgAndArgs...) } + +// Zero asserts that i is the zero value for its type and returns the truth. +func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) { + Zero(a.t, i, msgAndArgs...) +} + +// NotZero asserts that i is not the zero value for its type and returns the truth. +func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) { + NotZero(a.t, i, msgAndArgs...) +} diff --git a/require/forward_requirements_test.go b/require/forward_requirements_test.go index 02be291..404962d 100644 --- a/require/forward_requirements_test.go +++ b/require/forward_requirements_test.go @@ -258,3 +258,27 @@ func TestInDeltaWrapper(t *testing.T) { t.Error("Check should fail") } } + +func TestZeroWrapper(t *testing.T) { + require := New(t) + require.Zero(0) + + mockT := new(MockT) + mockRequire := New(mockT) + mockRequire.Zero(1) + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotZeroWrapper(t *testing.T) { + require := New(t) + require.NotZero(1) + + mockT := new(MockT) + mockRequire := New(mockT) + mockRequire.NotZero(0) + if !mockT.Failed { + t.Error("Check should fail") + } +} diff --git a/require/requirements.go b/require/requirements.go index 122a3f3..cfc1887 100644 --- a/require/requirements.go +++ b/require/requirements.go @@ -269,3 +269,17 @@ func EqualError(t TestingT, theError error, errString string, msgAndArgs ...inte t.FailNow() } } + +// Zero asserts that i is the zero value for its type and returns the truth. +func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) { + if !assert.Zero(t, i, msgAndArgs...) { + t.FailNow() + } +} + +// NotZero asserts that i is not the zero value for its type and returns the truth. +func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) { + if !assert.NotZero(t, i, msgAndArgs...) { + t.FailNow() + } +} diff --git a/require/requirements_test.go b/require/requirements_test.go index 9131b2f..2c9d351 100644 --- a/require/requirements_test.go +++ b/require/requirements_test.go @@ -264,3 +264,25 @@ func TestInDelta(t *testing.T) { t.Error("Check should fail") } } + +func TestZero(t *testing.T) { + + Zero(t, "") + + mockT := new(MockT) + Zero(mockT, "x") + if !mockT.Failed { + t.Error("Check should fail") + } +} + +func TestNotZero(t *testing.T) { + + NotZero(t, "x") + + mockT := new(MockT) + NotZero(mockT, "") + if !mockT.Failed { + t.Error("Check should fail") + } +}