Skip to content

Commit ff01f62

Browse files
authored
[type: refactor] assert enhance. (#108)
[type:feat] add assert
1 parent 972a1fe commit ff01f62

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

assert/assert.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package assert
1919

2020
import (
2121
"fmt"
22+
"path/filepath"
2223
"reflect"
24+
"regexp"
2325
"runtime"
2426
"testing"
2527
)
@@ -80,6 +82,77 @@ func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) {
8082
assert(t, result, fn, 1)
8183
}
8284

85+
// Panic asserts that function fn() panics.
86+
// It assumes that recover() either returns a string or
87+
// an error and fails if the message does not match
88+
// the regular expression in 'matches'.
89+
// @param t tet
90+
// @param fn function
91+
// @param matches matcher
92+
func Panic(t *testing.T, fn func(), matches string) {
93+
if x := doesPanic(2, fn, matches); x != "" {
94+
fmt.Println(x)
95+
t.Fail()
96+
}
97+
}
98+
99+
// doesPanic do panic
100+
// @param skip judge skip
101+
// @param fn func
102+
// @param expr got
103+
// @return err error
104+
func doesPanic(skip int, fn func(), expr string) (err string) {
105+
defer func() {
106+
r := recover()
107+
if r == nil {
108+
err = fail(skip, "did not panic")
109+
return
110+
}
111+
var v string
112+
switch r.(type) {
113+
case error:
114+
v = r.(error).Error()
115+
case string:
116+
v = r.(string)
117+
}
118+
err = matches(skip, v, expr)
119+
}()
120+
fn()
121+
return ""
122+
}
123+
124+
// Matches asserts that a value matches a given regular expression.
125+
// @param t test
126+
// @param value value
127+
// @param expr got
128+
func Matches(t *testing.T, value, expr string) {
129+
if x := matches(2, value, expr); x != "" {
130+
fmt.Println(x)
131+
t.Fail()
132+
}
133+
}
134+
135+
// matches matches got and expr value
136+
// @param skip judge skip
137+
// @param value value
138+
// @param expr got
139+
// @return string result
140+
func matches(skip int, value, expr string) string {
141+
ok, err := regexp.MatchString(expr, value)
142+
if err != nil {
143+
return fail(skip, "invalid pattern %q. %s", expr, err)
144+
}
145+
if !ok {
146+
return fail(skip, "got %s which does not match %s", value, expr)
147+
}
148+
return ""
149+
}
150+
151+
func fail(skip int, format string, args ...interface{}) string {
152+
_, file, line, _ := runtime.Caller(skip)
153+
return fmt.Sprintf("\t%s:%d: %s\n", filepath.Base(file), line, fmt.Sprintf(format, args...))
154+
}
155+
83156
// assert assertion
84157
// @param t test
85158
// @param result result

assert/assert_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,39 @@ func TestIsTrue(t *testing.T) {
6767
func TestIsFalse(t *testing.T) {
6868
IsFalse(t, reflect.DeepEqual("foo", "bar"), "msg!")
6969
}
70+
71+
func TestPanicPanics(t *testing.T) {
72+
if got, want := doesPanic(2, func() { panic("foo") }, ""), ""; got != want {
73+
t.Fatalf("got %q want %q", got, want)
74+
}
75+
}
76+
77+
func TestPanicPanicsAndMatches(t *testing.T) {
78+
if got, want := doesPanic(2, func() { panic("foo") }, "foo"), ""; got != want {
79+
t.Fatalf("got %q want %q", got, want)
80+
}
81+
}
82+
83+
func TestPanicPanicsAndDoesNotMatch(t *testing.T) {
84+
if got, want := doesPanic(2, func() { panic("foo") }, "bar"), "\tassert.go:118: got foo which does not match bar\n"; got != want {
85+
t.Fatalf("got %q want %q", got, want)
86+
}
87+
}
88+
89+
func TestPanicPanicsAndDoesNotPanic(t *testing.T) {
90+
if got, want := doesPanic(2, func() {}, "bar"), "\tassert.go:121: did not panic\n"; got != want {
91+
t.Fatalf("got %q want %q", got, want)
92+
}
93+
}
94+
95+
func TestMatchesMatches(t *testing.T) {
96+
if got, want := matches(2, "aaa", "a"), ""; got != want {
97+
t.Fatalf("got %q want %q", got, want)
98+
}
99+
}
100+
101+
func TestMatchesDoesNotMatch(t *testing.T) {
102+
if got, want := matches(2, "aaa", "b"), "\tassert_test.go:102: got aaa which does not match b\n"; got != want {
103+
t.Fatalf("got %q want %q", got, want)
104+
}
105+
}

0 commit comments

Comments
 (0)