add Nil and NotNil to test utils

pull/5805/head
Callum Styan 7 years ago
parent 31c420088b
commit ae8bb5fd16

@ -65,3 +65,65 @@ func Equals(tb testing.TB, exp, act interface{}) {
tb.FailNow() tb.FailNow()
} }
} }
// from stretchr/testify
// Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
// Please consider promoting this project if you find it useful.
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// isNil checks if a specified object is nil or not, without Failing.
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
// modified from stretchr/testify
// Nil asserts that the specified object is nil.
//
// assert.Nil(t, err)
//
// Returns whether the assertion was successful (true) or not (false).
func Nil(tb testing.TB, object interface{}, msg string) {
if !isNil(object) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v to be nil\n\n\tmsg: %#v\033[39m\n\n", filepath.Base(file), line, object, msg)
tb.FailNow()
}
}
func NotNil(tb testing.TB, object interface{}, msg string) {
if isNil(object) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v to not be nil\n\n\tmsg: %#v\033[39m\n\n", filepath.Base(file), line, object, msg)
tb.FailNow()
}
}

Loading…
Cancel
Save