From ae8bb5fd16cec8e583fd4d561bd9779b8fb5628a Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Wed, 6 Dec 2017 15:58:52 -0800 Subject: [PATCH] add Nil and NotNil to test utils --- util_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/util_test.go b/util_test.go index 69af7e9e3..56d870f69 100644 --- a/util_test.go +++ b/util_test.go @@ -65,3 +65,65 @@ func Equals(tb testing.TB, exp, act interface{}) { 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() + } +}