github.com/searKing/golang/go@v1.2.117/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 "reflect" 10 11 reflect_ "github.com/searKing/golang/go/reflect" 12 ) 13 14 // Nil asserts that the specified object is nil. 15 func Nil(v any, a ...any) (bool, string) { 16 return reflect_.IsNil(v), fmt.Sprintf(fmt.Sprintf("expected nil, got: %#v; ", v), a...) 17 } 18 19 func Nilf(v any, format string, a ...any) (bool, string) { 20 return reflect_.IsNil(v), fmt.Sprintf(fmt.Sprintf("expected nil, got: %#v; %s", v, format), a...) 21 } 22 23 // NonNil asserts that the specified object is none nil. 24 func NonNil(v any, a ...any) (bool, string) { 25 return !reflect_.IsNil(v), fmt.Sprintf(fmt.Sprintf("expected non-nil, got: %#v; ", v), a...) 26 } 27 28 func NonNilf(v any, format string, a ...any) (bool, string) { 29 return !reflect_.IsNil(v), fmt.Sprintf(fmt.Sprintf("expected non-nil, got: %#v; %s", v, format), a...) 30 } 31 32 // Zero asserts that the specified object is zero. I.e. nil, "", false, 0 or either 33 // a slice or a channel with len == 0. 34 func Zero(v any, a ...any) (bool, string) { 35 return reflect_.IsZeroValue(reflect.ValueOf(v)), fmt.Sprintf(fmt.Sprintf("expected zero value, got: %#v; ", v), a...) 36 } 37 38 func Zerof(v any, format string, a ...any) (bool, string) { 39 return reflect_.IsZeroValue(reflect.ValueOf(v)), fmt.Sprintf(fmt.Sprintf("expected zero value, got: %#v; %s", v, format), a...) 40 } 41 42 // NonZero asserts that the specified object is none zero. 43 func NonZero(v any, a ...any) (bool, string) { 44 return !reflect_.IsZeroValue(reflect.ValueOf(v)), fmt.Sprintf(fmt.Sprintf("expected non-zero value, got: %#v; ", v), a...) 45 } 46 47 func NonZerof(v any, format string, a ...any) (bool, string) { 48 return !reflect_.IsZeroValue(reflect.ValueOf(v)), fmt.Sprintf(fmt.Sprintf("expected non-zero value, got: %#v; %s", v, format), a...) 49 } 50 51 // Error asserts that a function returned an error (i.e. not `nil`). 52 func Error(v any, a ...any) (bool, string) { 53 return NonNil(v, a...) 54 } 55 56 func Errorf(v any, format string, a ...any) (bool, string) { 57 return NonNilf(v, format, a...) 58 } 59 60 // NonError asserts that a function returned a none error (i.e. `nil`). 61 func NonError(v any, a ...any) (bool, string) { 62 return Nil(v, a...) 63 } 64 65 func NonErrorf(v any, format string, a ...any) (bool, string) { 66 return Nilf(v, format, a...) 67 } 68 69 // EqualError asserts that a function returned an error (i.e. not `nil`) 70 // and that it is equal to the provided error. 71 func EqualError(actual error, expected error, a ...any) (bool, string) { 72 if actual == nil && expected == nil { 73 return true, "" 74 } 75 if actual != nil && expected != nil { 76 return actual.Error() == expected.Error(), fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 77 "expected: %q\n"+ 78 "actual : %q", expected, actual), a...) 79 } 80 return false, fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 81 "expected: %q\n"+ 82 "actual : %q", expected, actual), a...) 83 } 84 85 func EqualErrorf(actual error, expected error, format string, a ...any) (bool, string) { 86 if actual == nil && expected == nil { 87 return true, "" 88 } 89 if actual != nil && expected != nil { 90 return actual.Error() == expected.Error(), fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 91 "expected: %q\n"+ 92 "actual : %q: %s", expected, actual, format), a...) 93 } 94 95 return false, fmt.Sprintf(fmt.Sprintf("Error message not equal:\n"+ 96 "expected: %q\n"+ 97 "actual : %q: %s", expected, actual, format), a...) 98 }