Skip to content

Commit 52f2d09

Browse files
committed
feat: add XXXNow functions to stop execution if assert failed.
1 parent a9a0cd4 commit 52f2d09

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

builtin.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,82 @@ import (
44
"testing"
55
)
66

7-
// DeepEqual tests deeply equality between actual and expect parameters.
7+
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
8+
// fail if they are not deeply equal, and it doesn't stop the execution.
89
func DeepEqual(t *testing.T, actual, expect any, message ...string) error {
910
return tryDeepEqual(t, false, actual, expect, message...)
1011
}
1112

12-
// NotDeepEqual tests deeply inequality between actual and expected parameters.
13+
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
14+
// execution if they are not deeply equal.
15+
func DeepEqualNow(t *testing.T, actual, expect any, message ...string) error {
16+
return tryDeepEqual(t, true, actual, expect, message...)
17+
}
18+
19+
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
20+
// result to fail if they are deeply equal, but it doesn't stop the execution.
1321
func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error {
1422
return tryNotDeepEqual(t, false, actual, expect, message...)
1523
}
1624

25+
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
26+
// the execution if they are deeply equal.
27+
func NotDeepEqualNow(t *testing.T, actual, expect any, message ...string) error {
28+
return tryNotDeepEqual(t, true, actual, expect, message...)
29+
}
30+
1731
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
1832
// always return false if the value is a bool, an integer, a floating number, a complex, or a
1933
// string.
2034
func Nil(t *testing.T, val any, message ...string) error {
2135
return tryNil(t, false, val, message...)
2236
}
2337

38+
// NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will
39+
// always return false if the value is a bool, an integer, a floating number, a complex, or a
40+
// string.
41+
//
42+
// This function will set the result to fail, and stop the execution if the value is not nil.
43+
func NilNow(t *testing.T, val any, message ...string) error {
44+
return tryNil(t, true, val, message...)
45+
}
46+
2447
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
2548
// always return true if the value is a bool, an integer, a floating number, a complex, or a
2649
// string.
2750
func NotNil(t *testing.T, val any, message ...string) error {
2851
return tryNotNil(t, false, val, message...)
2952
}
3053

31-
// Panic expects the function fn to panic.
54+
// NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will
55+
// always return true if the value is a bool, an integer, a floating number, a complex, or a
56+
// string.
57+
//
58+
// This function will set the result to fail, and stop the execution if the value is nil.
59+
func NotNilNow(t *testing.T, val any, message ...string) error {
60+
return tryNotNil(t, true, val, message...)
61+
}
62+
63+
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
64+
// panic.
3265
func Panic(t *testing.T, fn func(), message ...string) error {
3366
return tryPanic(t, false, fn, message...)
3467
}
3568

36-
// NotPanic asserts that the function fn does not panic.
69+
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
70+
// panic, and stop the execution.
71+
func PanicNow(t *testing.T, fn func(), message ...string) error {
72+
return tryPanic(t, true, fn, message...)
73+
}
74+
75+
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
76+
// function panic.
3777
func NotPanic(t *testing.T, fn func(), message ...string) error {
3878
return tryNotPanic(t, false, fn, message...)
3979
}
80+
81+
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
82+
// function panic, and it also stops the execution.
83+
func NotPanicNow(t *testing.T, fn func(), message ...string) error {
84+
return tryNotPanic(t, false, fn, message...)
85+
}

equal.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@ import (
66
"testing"
77
)
88

9-
// DeepEqual tests deeply equality between actual and expect parameters.
9+
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
10+
// fail if they are not deeply equal, and it doesn't stop the execution.
1011
func (a *Assertion) DeepEqual(actual, expect any, message ...string) error {
1112
return tryDeepEqual(a.t, false, actual, expect, message...)
1213
}
1314

14-
// NotDeepEqual tests deeply inequality between actual and expected parameters.
15+
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
16+
// execution if they are not deeply equal.
17+
func (a *Assertion) DeepEqualNow(actual, expect any, message ...string) error {
18+
return tryDeepEqual(a.t, true, actual, expect, message...)
19+
}
20+
21+
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
22+
// result to fail if they are deeply equal, but it doesn't stop the execution.
1523
func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
1624
return tryNotDeepEqual(a.t, false, actual, expect, message...)
1725
}
1826

27+
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
28+
// the execution if they are deeply equal.
29+
func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...string) error {
30+
return tryNotDeepEqual(a.t, true, actual, expect, message...)
31+
}
32+
1933
// tryDeepEqual try to testing the deeply equality between actual and expect values, and it'll
2034
// fail if the values are not deeply equal.
2135
func tryDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
@@ -49,13 +63,31 @@ func (a *Assertion) Nil(val any, message ...string) error {
4963
return tryNil(a.t, false, val, message...)
5064
}
5165

66+
// NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will
67+
// always return false if the value is a bool, an integer, a floating number, a complex, or a
68+
// string.
69+
//
70+
// This function will set the result to fail, and stop the execution if the value is not nil.
71+
func (a *Assertion) NilNow(val any, message ...string) error {
72+
return tryNil(a.t, true, val, message...)
73+
}
74+
5275
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
5376
// always return true if the value is a bool, an integer, a floating number, a complex, or a
5477
// string.
5578
func (a *Assertion) NotNil(val any, message ...string) error {
5679
return tryNotNil(a.t, false, val, message...)
5780
}
5881

82+
// NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will
83+
// always return true if the value is a bool, an integer, a floating number, a complex, or a
84+
// string.
85+
//
86+
// This function will set the result to fail, and stop the execution if the value is nil.
87+
func (a *Assertion) NotNilNow(val any, message ...string) error {
88+
return tryNotNil(a.t, true, val, message...)
89+
}
90+
5991
// tryNil try to testing a value is nil or not, and it'll fail the value is nil.
6092
func tryNil(t *testing.T, failedNow bool, val any, message ...string) error {
6193
if isNil(val) {

panic.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@ import (
55
"testing"
66
)
77

8-
// Panic expects the function fn to panic.
8+
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
9+
// panic.
910
func (a *Assertion) Panic(fn func(), message ...string) error {
1011
return tryPanic(a.t, false, fn, message...)
1112
}
1213

13-
// NotPanic asserts that the function fn does not panic.
14+
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
15+
// panic, and stop the execution.
16+
func (a *Assertion) PanicNow(fn func(), message ...string) error {
17+
return tryPanic(a.t, true, fn, message...)
18+
}
19+
20+
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
21+
// function panic.
1422
func (a *Assertion) NotPanic(fn func(), message ...string) error {
1523
return tryNotPanic(a.t, false, fn, message...)
1624
}
1725

26+
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
27+
// function panic, and it also stops the execution.
28+
func (a *Assertion) NotPanicNow(fn func(), message ...string) error {
29+
return tryNotPanic(a.t, false, fn, message...)
30+
}
31+
1832
// tryPanic executes the function fn, and try to catching the panic error. It expect the function
1933
// fn to panic, and returns error if fn does not panic.
2034
func tryPanic(t *testing.T, failedNow bool, fn func(), message ...string) error {

0 commit comments

Comments
 (0)