Skip to content

Commit a9a0cd4

Browse files
committed
refactor: add failed handler and try functions.
1 parent eb9b4b3 commit a9a0cd4

File tree

6 files changed

+148
-73
lines changed

6 files changed

+148
-73
lines changed

assertion.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,6 @@ func New(t *testing.T) *Assertion {
2121
return a
2222
}
2323

24-
// #########################
25-
// ## Assertion Functions ##
26-
// #########################
27-
28-
// DeepEqual tests deeply equality between actual and expect parameters.
29-
func (a *Assertion) DeepEqual(actual, expect any, message ...string) error {
30-
return DeepEqual(a.t, actual, expect, message...)
31-
}
32-
33-
// NotDeepEqual tests deeply inequality between actual and expected parameters.
34-
func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
35-
return NotDeepEqual(a.t, actual, expect, message...)
36-
}
37-
38-
// Nil 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-
func (a *Assertion) Nil(val any, message ...string) error {
42-
return Nil(a.t, val, message...)
43-
}
44-
45-
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
46-
// always return true if the value is a bool, an integer, a floating number, a complex, or a
47-
// string.
48-
func (a *Assertion) NotNil(val any, message ...string) error {
49-
return NotNil(a.t, val, message...)
50-
}
51-
52-
// Panic expects the function fn to panic.
53-
func (a *Assertion) Panic(fn func(), message ...string) (err error) {
54-
return Panic(a.t, fn, message...)
55-
}
56-
57-
// NotPanic asserts that the function fn does not panic.
58-
func (a *Assertion) NotPanic(fn func(), message ...string) (err error) {
59-
return NotPanic(a.t, fn, message...)
60-
}
61-
6224
// ##########################
6325
// ## Delegation Functions ##
6426
// ##########################

builtin.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package assert
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// DeepEqual tests deeply equality between actual and expect parameters.
8+
func DeepEqual(t *testing.T, actual, expect any, message ...string) error {
9+
return tryDeepEqual(t, false, actual, expect, message...)
10+
}
11+
12+
// NotDeepEqual tests deeply inequality between actual and expected parameters.
13+
func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error {
14+
return tryNotDeepEqual(t, false, actual, expect, message...)
15+
}
16+
17+
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
18+
// always return false if the value is a bool, an integer, a floating number, a complex, or a
19+
// string.
20+
func Nil(t *testing.T, val any, message ...string) error {
21+
return tryNil(t, false, val, message...)
22+
}
23+
24+
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
25+
// always return true if the value is a bool, an integer, a floating number, a complex, or a
26+
// string.
27+
func NotNil(t *testing.T, val any, message ...string) error {
28+
return tryNotNil(t, false, val, message...)
29+
}
30+
31+
// Panic expects the function fn to panic.
32+
func Panic(t *testing.T, fn func(), message ...string) error {
33+
return tryPanic(t, false, fn, message...)
34+
}
35+
36+
// NotPanic asserts that the function fn does not panic.
37+
func NotPanic(t *testing.T, fn func(), message ...string) error {
38+
return tryNotPanic(t, false, fn, message...)
39+
}

equal.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,75 @@ import (
77
)
88

99
// DeepEqual tests deeply equality between actual and expect parameters.
10-
func DeepEqual(t *testing.T, actual, expect any, message ...string) error {
10+
func (a *Assertion) DeepEqual(actual, expect any, message ...string) error {
11+
return tryDeepEqual(a.t, false, actual, expect, message...)
12+
}
13+
14+
// NotDeepEqual tests deeply inequality between actual and expected parameters.
15+
func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error {
16+
return tryNotDeepEqual(a.t, false, actual, expect, message...)
17+
}
18+
19+
// tryDeepEqual try to testing the deeply equality between actual and expect values, and it'll
20+
// fail if the values are not deeply equal.
21+
func tryDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
1122
if reflect.DeepEqual(actual, expect) {
1223
return nil
1324
}
1425

1526
err := newAssertionError(fmt.Sprintf("%v == %v", actual, expect), message...)
16-
17-
t.Error(err)
27+
failed(t, err, failedNow)
1828

1929
return err
2030
}
2131

22-
// NotDeepEqual tests deeply inequality between actual and expected parameters.
23-
func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error {
32+
// tryNotDeepEqual try to testing the deeply inequality between actual and expect values, and it'll
33+
// fail if the values are deeply equal.
34+
func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
2435
if !reflect.DeepEqual(actual, expect) {
2536
return nil
2637
}
2738

2839
err := newAssertionError(fmt.Sprintf("%v != %v", actual, expect), message...)
29-
30-
t.Error(err)
40+
failed(t, err, failedNow)
3141

3242
return err
3343
}
3444

3545
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
3646
// always return false if the value is a bool, an integer, a floating number, a complex, or a
3747
// string.
38-
func Nil(t *testing.T, val any, message ...string) error {
48+
func (a *Assertion) Nil(val any, message ...string) error {
49+
return tryNil(a.t, false, val, message...)
50+
}
51+
52+
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
53+
// always return true if the value is a bool, an integer, a floating number, a complex, or a
54+
// string.
55+
func (a *Assertion) NotNil(val any, message ...string) error {
56+
return tryNotNil(a.t, false, val, message...)
57+
}
58+
59+
// tryNil try to testing a value is nil or not, and it'll fail the value is nil.
60+
func tryNil(t *testing.T, failedNow bool, val any, message ...string) error {
3961
if isNil(val) {
4062
return nil
4163
}
4264

4365
err := newAssertionError(fmt.Sprintf("expect nil, got %v", val), message...)
44-
45-
t.Error(err)
66+
failed(t, err, failedNow)
4667

4768
return err
4869
}
4970

50-
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
51-
// always return true if the value is a bool, an integer, a floating number, a complex, or a
52-
// string.
53-
func NotNil(t *testing.T, val any, message ...string) error {
71+
// tryNotNil try to testing a value is nil or not, and it'll fail the value is not nil.
72+
func tryNotNil(t *testing.T, failedNow bool, val any, message ...string) error {
5473
if !isNil(val) {
5574
return nil
5675
}
5776

5877
err := newAssertionError("expect not nil, got nil", message...)
59-
60-
t.Error(err)
78+
failed(t, err, failedNow)
6179

6280
return err
6381
}

panic.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,38 @@ import (
66
)
77

88
// Panic expects the function fn to panic.
9-
func Panic(t *testing.T, fn func(), message ...string) (err error) {
10-
defer func() {
11-
if e := recover(); e == nil {
12-
err = newAssertionError("mssing expected panic", message...)
13-
}
14-
}()
15-
16-
fn()
17-
18-
return
9+
func (a *Assertion) Panic(fn func(), message ...string) error {
10+
return tryPanic(a.t, false, fn, message...)
1911
}
2012

2113
// NotPanic asserts that the function fn does not panic.
22-
func NotPanic(t *testing.T, fn func(), message ...string) (err error) {
23-
defer func() {
24-
if e := recover(); e != nil {
25-
err = newAssertionError(fmt.Sprintf("got unwanted error: %v", e), message...)
26-
}
27-
}()
14+
func (a *Assertion) NotPanic(fn func(), message ...string) error {
15+
return tryNotPanic(a.t, false, fn, message...)
16+
}
17+
18+
// tryPanic executes the function fn, and try to catching the panic error. It expect the function
19+
// fn to panic, and returns error if fn does not panic.
20+
func tryPanic(t *testing.T, failedNow bool, fn func(), message ...string) error {
21+
e := isPanic(fn)
22+
if e != nil {
23+
return nil
24+
}
25+
26+
err := newAssertionError("missing expected panic", message...)
27+
failed(t, err, failedNow)
28+
29+
return err
30+
}
2831

29-
fn()
32+
// tryNotPanic executes the function fn, and try to catching the panic error. It expect the
33+
// function fn does not to panic, and returns error if panic.
34+
func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...string) error {
35+
e := isPanic(fn)
36+
if e == nil {
37+
return nil
38+
}
3039

31-
return
40+
err := newAssertionError(fmt.Sprintf("got unwanted error: %v", e), message...)
41+
failed(t, err, failedNow)
42+
return err
3243
}

util.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
package assert
22

3-
import "reflect"
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
// failed handles the assertion error with the specific testing.T or the assertion's t. It will set
9+
// marks the function has failed if the err is not nil. It'll also stops the execution if failedNow
10+
// set to true.
11+
func failed(t *testing.T, err error, failedNow bool) {
12+
if err == nil {
13+
return
14+
}
15+
16+
if failedNow {
17+
t.Fatal(err)
18+
} else {
19+
t.Error(err)
20+
}
21+
}
22+
23+
// ################################
24+
// ## Assertion Helper Functions ##
25+
// ################################
426

527
// isNil checks whether a value is nil or not. It'll always return false if the value is not a
628
// channel, a function, a map, a point, an unsafe point, an interface, or a slice.
@@ -19,3 +41,17 @@ func isNil(val any) bool {
1941
return false
2042
}
2143
}
44+
45+
// isPanic executes the function, and tries to catching and returns the return value from
46+
// recover().
47+
func isPanic(fn func()) (err any) {
48+
defer func() {
49+
if e := recover(); e != nil {
50+
err = e
51+
}
52+
}()
53+
54+
fn()
55+
56+
return
57+
}

util_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ func TestIsNil(t *testing.T) {
1212
assert.DeepEqual(isNil(testAssert), true)
1313
assert.DeepEqual(isNil(assert), false)
1414
}
15+
16+
func TestIsPanic(t *testing.T) {
17+
Nil(t, isPanic(func() {
18+
// no panic
19+
}))
20+
NotNil(t, isPanic(func() {
21+
panic("unexpected panic")
22+
}))
23+
}

0 commit comments

Comments
 (0)