Skip to content

Commit 7bb623b

Browse files
committed
feat: mark helper functions.
1 parent a538eaa commit 7bb623b

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

builtin.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,41 @@ import (
77
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
88
// fail if they are not deeply equal, and it doesn't stop the execution.
99
func DeepEqual(t *testing.T, actual, expect any, message ...string) error {
10+
t.Helper()
11+
1012
return tryDeepEqual(t, false, actual, expect, message...)
1113
}
1214

1315
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
1416
// execution if they are not deeply equal.
1517
func DeepEqualNow(t *testing.T, actual, expect any, message ...string) error {
18+
t.Helper()
19+
1620
return tryDeepEqual(t, true, actual, expect, message...)
1721
}
1822

1923
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
2024
// result to fail if they are deeply equal, but it doesn't stop the execution.
2125
func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error {
26+
t.Helper()
27+
2228
return tryNotDeepEqual(t, false, actual, expect, message...)
2329
}
2430

2531
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
2632
// the execution if they are deeply equal.
2733
func NotDeepEqualNow(t *testing.T, actual, expect any, message ...string) error {
34+
t.Helper()
35+
2836
return tryNotDeepEqual(t, true, actual, expect, message...)
2937
}
3038

3139
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
3240
// always return false if the value is a bool, an integer, a floating number, a complex, or a
3341
// string.
3442
func Nil(t *testing.T, val any, message ...string) error {
43+
t.Helper()
44+
3545
return tryNil(t, false, val, message...)
3646
}
3747

@@ -41,13 +51,17 @@ func Nil(t *testing.T, val any, message ...string) error {
4151
//
4252
// This function will set the result to fail, and stop the execution if the value is not nil.
4353
func NilNow(t *testing.T, val any, message ...string) error {
54+
t.Helper()
55+
4456
return tryNil(t, true, val, message...)
4557
}
4658

4759
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
4860
// always return true if the value is a bool, an integer, a floating number, a complex, or a
4961
// string.
5062
func NotNil(t *testing.T, val any, message ...string) error {
63+
t.Helper()
64+
5165
return tryNotNil(t, false, val, message...)
5266
}
5367

@@ -57,29 +71,39 @@ func NotNil(t *testing.T, val any, message ...string) error {
5771
//
5872
// This function will set the result to fail, and stop the execution if the value is nil.
5973
func NotNilNow(t *testing.T, val any, message ...string) error {
74+
t.Helper()
75+
6076
return tryNotNil(t, true, val, message...)
6177
}
6278

6379
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
6480
// panic.
6581
func Panic(t *testing.T, fn func(), message ...string) error {
82+
t.Helper()
83+
6684
return tryPanic(t, false, fn, message...)
6785
}
6886

6987
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
7088
// panic, and stop the execution.
7189
func PanicNow(t *testing.T, fn func(), message ...string) error {
90+
t.Helper()
91+
7292
return tryPanic(t, true, fn, message...)
7393
}
7494

7595
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
7696
// function panic.
7797
func NotPanic(t *testing.T, fn func(), message ...string) error {
98+
t.Helper()
99+
78100
return tryNotPanic(t, false, fn, message...)
79101
}
80102

81103
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
82104
// function panic, and it also stops the execution.
83105
func NotPanicNow(t *testing.T, fn func(), message ...string) error {
106+
t.Helper()
107+
84108
return tryNotPanic(t, false, fn, message...)
85109
}

equal.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,40 @@ import (
99
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
1010
// fail if they are not deeply equal, and it doesn't stop the execution.
1111
func (a *Assertion) DeepEqual(actual, expect any, message ...string) error {
12+
a.Helper()
13+
1214
return tryDeepEqual(a.T, false, actual, expect, message...)
1315
}
1416

1517
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
1618
// execution if they are not deeply equal.
1719
func (a *Assertion) DeepEqualNow(actual, expect any, message ...string) error {
20+
a.Helper()
21+
1822
return tryDeepEqual(a.T, true, actual, expect, message...)
1923
}
2024

2125
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
2226
// result to fail if they are deeply equal, but it doesn't stop the execution.
2327
func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
28+
a.Helper()
29+
2430
return tryNotDeepEqual(a.T, false, actual, expect, message...)
2531
}
2632

2733
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
2834
// the execution if they are deeply equal.
2935
func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...string) error {
36+
a.Helper()
37+
3038
return tryNotDeepEqual(a.T, true, actual, expect, message...)
3139
}
3240

3341
// tryDeepEqual try to testing the deeply equality between actual and expect values, and it'll
3442
// fail if the values are not deeply equal.
3543
func tryDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
44+
t.Helper()
45+
3646
if reflect.DeepEqual(actual, expect) {
3747
return nil
3848
}
@@ -46,6 +56,8 @@ func tryDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...s
4656
// tryNotDeepEqual try to testing the deeply inequality between actual and expect values, and it'll
4757
// fail if the values are deeply equal.
4858
func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
59+
t.Helper()
60+
4961
if !reflect.DeepEqual(actual, expect) {
5062
return nil
5163
}
@@ -60,6 +72,8 @@ func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message .
6072
// always return false if the value is a bool, an integer, a floating number, a complex, or a
6173
// string.
6274
func (a *Assertion) Nil(val any, message ...string) error {
75+
a.Helper()
76+
6377
return tryNil(a.T, false, val, message...)
6478
}
6579

@@ -69,13 +83,17 @@ func (a *Assertion) Nil(val any, message ...string) error {
6983
//
7084
// This function will set the result to fail, and stop the execution if the value is not nil.
7185
func (a *Assertion) NilNow(val any, message ...string) error {
86+
a.Helper()
87+
7288
return tryNil(a.T, true, val, message...)
7389
}
7490

7591
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
7692
// always return true if the value is a bool, an integer, a floating number, a complex, or a
7793
// string.
7894
func (a *Assertion) NotNil(val any, message ...string) error {
95+
a.Helper()
96+
7997
return tryNotNil(a.T, false, val, message...)
8098
}
8199

@@ -85,11 +103,15 @@ func (a *Assertion) NotNil(val any, message ...string) error {
85103
//
86104
// This function will set the result to fail, and stop the execution if the value is nil.
87105
func (a *Assertion) NotNilNow(val any, message ...string) error {
106+
a.Helper()
107+
88108
return tryNotNil(a.T, true, val, message...)
89109
}
90110

91111
// tryNil try to testing a value is nil or not, and it'll fail the value is nil.
92112
func tryNil(t *testing.T, failedNow bool, val any, message ...string) error {
113+
t.Helper()
114+
93115
if isNil(val) {
94116
return nil
95117
}
@@ -102,6 +124,8 @@ func tryNil(t *testing.T, failedNow bool, val any, message ...string) error {
102124

103125
// tryNotNil try to testing a value is nil or not, and it'll fail the value is not nil.
104126
func tryNotNil(t *testing.T, failedNow bool, val any, message ...string) error {
127+
t.Helper()
128+
105129
if !isNil(val) {
106130
return nil
107131
}

panic.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,40 @@ import (
88
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
99
// panic.
1010
func (a *Assertion) Panic(fn func(), message ...string) error {
11+
a.Helper()
12+
1113
return tryPanic(a.T, false, fn, message...)
1214
}
1315

1416
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
1517
// panic, and stop the execution.
1618
func (a *Assertion) PanicNow(fn func(), message ...string) error {
19+
a.Helper()
20+
1721
return tryPanic(a.T, true, fn, message...)
1822
}
1923

2024
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
2125
// function panic.
2226
func (a *Assertion) NotPanic(fn func(), message ...string) error {
27+
a.Helper()
28+
2329
return tryNotPanic(a.T, false, fn, message...)
2430
}
2531

2632
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
2733
// function panic, and it also stops the execution.
2834
func (a *Assertion) NotPanicNow(fn func(), message ...string) error {
35+
a.Helper()
36+
2937
return tryNotPanic(a.T, false, fn, message...)
3038
}
3139

3240
// tryPanic executes the function fn, and try to catching the panic error. It expect the function
3341
// fn to panic, and returns error if fn does not panic.
3442
func tryPanic(t *testing.T, failedNow bool, fn func(), message ...string) error {
43+
t.Helper()
44+
3545
e := isPanic(fn)
3646
if e != nil {
3747
return nil
@@ -46,6 +56,8 @@ func tryPanic(t *testing.T, failedNow bool, fn func(), message ...string) error
4656
// tryNotPanic executes the function fn, and try to catching the panic error. It expect the
4757
// function fn does not to panic, and returns error if panic.
4858
func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...string) error {
59+
t.Helper()
60+
4961
e := isPanic(fn)
5062
if e == nil {
5163
return nil

util.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import (
99
// marks the function has failed if the err is not nil. It'll also stops the execution if failedNow
1010
// set to true.
1111
func failed(t *testing.T, err error, failedNow bool) {
12+
t.Helper()
13+
1214
if err == nil {
1315
return
1416
}
1517

18+
t.Error(err)
19+
1620
if failedNow {
17-
t.Fatal(err)
18-
} else {
19-
t.Error(err)
21+
t.FailNow()
2022
}
2123
}
2224

0 commit comments

Comments
 (0)