github.com/searKing/golang/go@v1.2.74/testing/testing.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package testing 6 7 import ( 8 "fmt" 9 10 "github.com/searKing/golang/go/util/object" 11 ) 12 13 // Nil asserts that the specified object is nil. 14 func Nil(v interface{}, a ...interface{}) (bool, string) { 15 return object.IsNil(v), fmt.Sprintf(fmt.Sprintf("Expected nil, but got: %#v; ", v), a...) 16 } 17 18 func Nilf(v interface{}, format string, a ...interface{}) (bool, string) { 19 return object.IsNil(v), fmt.Sprintf(fmt.Sprintf("Expected nil, but got: %#v; %s", v, format), a...) 20 } 21 22 // NonNil asserts that the specified object is none nil. 23 func NonNil(v interface{}, a ...interface{}) (bool, string) { 24 return !object.IsNil(v), fmt.Sprintf(fmt.Sprintf("Expected non-nil, but got: %#v; ", v), a...) 25 } 26 27 func NonNilf(v interface{}, format string, a ...interface{}) (bool, string) { 28 return !object.IsNil(v), fmt.Sprintf(fmt.Sprintf("Expected non-nil, but got: %#v; %s", v, format), a...) 29 } 30 31 // Zero asserts that the specified object is zero. I.e. nil, "", false, 0 or either 32 // a slice or a channel with len == 0. 33 func Zero(v interface{}, a ...interface{}) (bool, string) { 34 return object.IsZero(v), fmt.Sprintf(fmt.Sprintf("Expected zero value, but got: %#v; ", v), a...) 35 } 36 37 func Zerof(v interface{}, format string, a ...interface{}) (bool, string) { 38 return object.IsZero(v), fmt.Sprintf(fmt.Sprintf("Expected zero value, but got: %#v; %s", v, format), a...) 39 } 40 41 // NonZero asserts that the specified object is none zero. 42 func NonZero(v interface{}, a ...interface{}) (bool, string) { 43 return !object.IsZero(v), fmt.Sprintf(fmt.Sprintf("Expected non-zero value, but got: %#v; ", v), a...) 44 } 45 46 func NonZerof(v interface{}, format string, a ...interface{}) (bool, string) { 47 return !object.IsZero(v), fmt.Sprintf(fmt.Sprintf("Expected non-zero value, but got: %#v; %s", v, format), a...) 48 } 49 50 // Error asserts that a function returned an error (i.e. not `nil`). 51 func Error(v interface{}, a ...interface{}) (bool, string) { 52 return NonNil(v, a...) 53 } 54 55 func Errorf(v interface{}, format string, a ...interface{}) (bool, string) { 56 return NonNilf(v, format, a...) 57 } 58 59 // NonError asserts that a function returned a none error (i.e. `nil`). 60 func NonError(v interface{}, a ...interface{}) (bool, string) { 61 return Nil(v, a...) 62 } 63 64 func NonErrorf(v interface{}, format string, a ...interface{}) (bool, string) { 65 return Nilf(v, format, a...) 66 } 67 68 // EqualError asserts that a function returned an error (i.e. not `nil`) 69 // and that it is equal to the provided error. 70 func EqualError(actual error, expected error, a ...interface{}) (bool, string) { 71 if actual == nil && expected == nil { 72 return true, "" 73 } 74 if actual != nil && expected != nil { 75 return actual.Error() == expected.Error(), fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 76 "expected: %q\n"+ 77 "actual : %q", expected, actual), a...) 78 } 79 return false, fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 80 "expected: %q\n"+ 81 "actual : %q", expected, actual), a...) 82 } 83 84 func EqualErrorf(actual error, expected error, format string, a ...interface{}) (bool, string) { 85 if actual == nil && expected == nil { 86 return true, "" 87 } 88 if actual != nil && expected != nil { 89 return actual.Error() == expected.Error(), fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 90 "expected: %q\n"+ 91 "actual : %q: %s", expected, actual, format), a...) 92 } 93 94 return false, fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 95 "expected: %q\n"+ 96 "actual : %q: %s", expected, actual, format), a...) 97 }