Skip to content

Commit c45ff43

Browse files
committed
feat(testutil): assert added zero-value assertion functionality Zero, NotZero
- 实现了 Zero 函数用于断言变量是否为零值 - 实现了 NotZero 函数用于断言变量不为零值
1 parent 9a273ec commit c45ff43

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

testutil/assert/asserts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ func NotEmpty(t TestingT, give any, fmtAndArgs ...any) bool {
7878
return nEmpty
7979
}
8080

81+
// Zero asserts that the give should be zero value
82+
func Zero(t TestingT, give any, fmtAndArgs ...any) bool {
83+
t.Helper()
84+
85+
if give != nil && !reflect.DeepEqual(give, reflect.Zero(reflect.TypeOf(give)).Interface()) {
86+
return Fail(t, fmt.Sprintf("Should be zero value, but was: %v", give), fmtAndArgs...)
87+
}
88+
return true
89+
}
90+
91+
// NotZero asserts that the give should not be zero value
92+
func NotZero(t TestingT, give any, fmtAndArgs ...any) bool {
93+
t.Helper()
94+
if give == nil || reflect.DeepEqual(give, reflect.Zero(reflect.TypeOf(give)).Interface()) {
95+
return Fail(t, fmt.Sprintf("Should not be zero, but was: %v", give), fmtAndArgs...)
96+
}
97+
return true
98+
}
99+
81100
// PanicRunFunc define
82101
type PanicRunFunc func()
83102

testutil/assert/asserts_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func TestCommon_success(t *testing.T) {
7373
// EqFloat/InDelta
7474
assert.EqFloat(t, 1.1, 1.2, 0.1)
7575

76+
// zero
77+
assert.Zero(t, 0)
78+
assert.NotZero(t, 1)
79+
7680
// kind
7781
assert.IsType(t, 1, 1)
7882
assert.IsKind(t, reflect.Int, 1)
@@ -135,10 +139,16 @@ func TestCommon_fail(t *testing.T) {
135139
// empty
136140
assert.Empty(tc, "abc")
137141
assert.StrContains(t, tc.ResetGet(), "Should be empty, but was:")
138-
139142
assert.NotEmpty(tc, "")
140143
assert.StrContains(t, tc.ResetGet(), "Should not be empty, but was:")
141144

145+
// zero
146+
assert.Zero(tc, 10)
147+
assert.StrContains(t, tc.ResetGet(), "Should be zero value, but was")
148+
149+
assert.NotZero(tc, 0)
150+
assert.StrContains(t, tc.ResetGet(), "Should not be zero, but was")
151+
142152
// error
143153
assert.Error(tc, nil)
144154
assert.StrContains(t, tc.ResetGet(), "An error is expected but got nil.")

0 commit comments

Comments
 (0)