Skip to content

Commit 4949508

Browse files
committed
feat: add Nil and NotNil.
1 parent fde57e3 commit 4949508

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

assertion.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
2525
return NotDeepEqual(a.t, actual, expect, message...)
2626
}
2727

28+
// Nil tests a value is nil or not, and it'll failed when the value is not nil.
29+
func (a *Assertion) Nil(val any, message ...string) error {
30+
return Nil(a.t, val, message...)
31+
}
32+
33+
// NotNil tests a value is nil or not, and it'll failed when the value is nil.
34+
func (a *Assertion) NotNil(val any, message ...string) error {
35+
return NotNil(a.t, val, message...)
36+
}
37+
2838
// Panic expects the function fn to panic.
2939
func (a *Assertion) Panic(fn func(), message ...string) (err error) {
3040
return Panic(a.t, fn, message...)

equal.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error {
3131

3232
return err
3333
}
34+
35+
// Nil tests a value is nil or not, and it'll failed when the value is not nil.
36+
func Nil(t *testing.T, val any, message ...string) error {
37+
if isNil(val) {
38+
return nil
39+
}
40+
41+
err := newAssertionError(fmt.Sprintf("expect nil, got %v", val), message...)
42+
43+
t.Error(err)
44+
45+
return err
46+
}
47+
48+
// NotNil tests a value is nil or not, and it'll failed when the value is nil.
49+
func NotNil(t *testing.T, val any, message ...string) error {
50+
if !isNil(val) {
51+
return nil
52+
}
53+
54+
err := newAssertionError("expect not nil, got nil", message...)
55+
56+
t.Error(err)
57+
58+
return err
59+
}

equal_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,31 @@ func testDeepEqualAndNotDeepEqual(t *testing.T, assertion *Assertion, v1, v2 any
4545
t.Errorf("NotEqual(%v, %v) = %v, want = nil", v1, v2, err)
4646
}
4747
}
48+
49+
func TestNilAndNotNil(t *testing.T) {
50+
mockT := new(testing.T)
51+
assert := New(mockT)
52+
53+
testNilAndNotNil(t, assert, 1, false)
54+
testNilAndNotNil(t, assert, "", false)
55+
testNilAndNotNil(t, assert, nil, true)
56+
var testAssert *Assertion
57+
testNilAndNotNil(t, assert, testAssert, true)
58+
testNilAndNotNil(t, assert, assert, false)
59+
}
60+
61+
func testNilAndNotNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
62+
err := assertion.Nil(v)
63+
if isNil && err != nil {
64+
t.Errorf("Nil(%v) = %v, want nil", v, err)
65+
} else if !isNil && err == nil {
66+
t.Errorf("Nil(%v) = nil, want error", v)
67+
}
68+
69+
err = assertion.NotNil(v)
70+
if isNil && err == nil {
71+
t.Errorf("Nil(%v) = nil, want error", v)
72+
} else if !isNil && err != nil {
73+
t.Errorf("Nil(%v) = %v, want nil", v, err)
74+
}
75+
}

0 commit comments

Comments
 (0)