-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
46 lines (40 loc) · 1.33 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package testutil
import (
"fmt"
"strings"
"github.com/starudream/go-lib/core/v2/utils/reflectutil"
)
func Nil(t T, value any, msgAndArgs ...any) bool {
t.Helper()
if reflectutil.IsNil(value) {
return true
}
return Fail(t, fmt.Sprintf("expected nil, but got [%T] %v", value, value), msgAndArgs...)
}
func NotNil(t T, value any, msgAndArgs ...any) bool {
t.Helper()
if !reflectutil.IsNil(value) {
return true
}
return Fail(t, fmt.Sprintf("expected not nil, but got [%T] %v", value, value), msgAndArgs...)
}
func Equal(t T, expected, actual any, msgAndArgs ...any) bool {
t.Helper()
if err := validateEqualArgs(expected, actual); err != nil {
return Fail(t, fmt.Sprintf("invalid operation: %#v == %#v (%v)", expected, actual, err), msgAndArgs...)
}
if !reflectutil.Equal(expected, actual) {
return Fail(t, fmt.Sprintf("should be equal to %T\n%s", expected, diff(expected, actual, strings.Repeat(" ", 2), "")), msgAndArgs...)
}
return true
}
func NotEqual(t T, expected, actual any, msgAndArgs ...any) bool {
t.Helper()
if err := validateEqualArgs(expected, actual); err != nil {
return Fail(t, fmt.Sprintf("invalid operation: %#v == %#v (%v)", expected, actual, err), msgAndArgs...)
}
if reflectutil.Equal(expected, actual) {
return Fail(t, fmt.Sprintf("should not be equal to %T", expected), msgAndArgs...)
}
return true
}