Skip to content

Commit e539d85

Browse files
committed
refector: add function test.
1 parent 9bda2cf commit e539d85

File tree

6 files changed

+132
-108
lines changed

6 files changed

+132
-108
lines changed

builtin.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func NotEqualNow(t *testing.T, actual, expect any, message ...string) error {
122122
func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
123123
t.Helper()
124124

125-
return tryMatchRegexp(t, false, val, pattern, message...)
125+
return tryMatchRegexp(t, false, val, pattern, "", message...)
126126
}
127127

128128
// MatchNow tests whether the string matches the regular expression or not, and it will terminate
@@ -135,7 +135,7 @@ func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...string)
135135
func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
136136
t.Helper()
137137

138-
return tryMatchRegexp(t, true, val, pattern, message...)
138+
return tryMatchRegexp(t, true, val, pattern, "", message...)
139139
}
140140

141141
// MatchString will compile the pattern and test whether the string matches the regular expression
@@ -146,9 +146,7 @@ func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...strin
146146
func MatchString(t *testing.T, val, pattern string, message ...string) error {
147147
t.Helper()
148148

149-
regPattern := regexp.MustCompile(pattern)
150-
151-
return tryMatchRegexp(t, false, val, regPattern, message...)
149+
return tryMatchRegexp(t, false, val, nil, pattern, message...)
152150
}
153151

154152
// MatchStringNow will compile the pattern and test whether the string matches the regular
@@ -161,9 +159,7 @@ func MatchString(t *testing.T, val, pattern string, message ...string) error {
161159
func MatchStringNow(t *testing.T, val, pattern string, message ...string) error {
162160
t.Helper()
163161

164-
regPattern := regexp.MustCompile(pattern)
165-
166-
return tryMatchRegexp(t, true, val, regPattern, message...)
162+
return tryMatchRegexp(t, true, val, nil, pattern, message...)
167163
}
168164

169165
// NotMatch tests whether the string matches the regular expression or not, and it set the result
@@ -175,7 +171,7 @@ func MatchStringNow(t *testing.T, val, pattern string, message ...string) error
175171
func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
176172
t.Helper()
177173

178-
return tryNotMatchRegexp(t, false, val, pattern, message...)
174+
return tryNotMatchRegexp(t, false, val, pattern, "", message...)
179175
}
180176

181177
// NotMatchNow tests whether the string matches the regular expression or not, and it will
@@ -188,7 +184,7 @@ func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...strin
188184
func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
189185
t.Helper()
190186

191-
return tryNotMatchRegexp(t, true, val, pattern, message...)
187+
return tryNotMatchRegexp(t, true, val, pattern, "", message...)
192188
}
193189

194190
// MatchString will compile the pattern and test whether the string matches the regular expression
@@ -200,9 +196,7 @@ func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...st
200196
func NotMatchString(t *testing.T, val, pattern string, message ...string) error {
201197
t.Helper()
202198

203-
regPattern := regexp.MustCompile(pattern)
204-
205-
return tryNotMatchRegexp(t, false, val, regPattern, message...)
199+
return tryNotMatchRegexp(t, false, val, nil, pattern, message...)
206200
}
207201

208202
// NotMatchStringNow will compile the pattern and test whether the string matches the regular
@@ -216,9 +210,7 @@ func NotMatchString(t *testing.T, val, pattern string, message ...string) error
216210
func NotMatchStringNow(t *testing.T, val, pattern string, message ...string) error {
217211
t.Helper()
218212

219-
regPattern := regexp.MustCompile(pattern)
220-
221-
return tryNotMatchRegexp(t, true, val, regPattern, message...)
213+
return tryNotMatchRegexp(t, true, val, nil, pattern, message...)
222214
}
223215

224216
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will

equal.go

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,27 @@ func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...string) error
6363
func tryDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
6464
t.Helper()
6565

66-
if reflect.DeepEqual(actual, expect) {
67-
return nil
68-
}
69-
70-
err := newAssertionError(fmt.Sprintf("%v == %v", actual, expect), message...)
71-
failed(t, err, failedNow)
72-
73-
return err
66+
return test(
67+
t,
68+
func() bool { return reflect.DeepEqual(actual, expect) },
69+
failedNow,
70+
fmt.Sprintf(defaultErrMessageEqual, actual, expect),
71+
message...,
72+
)
7473
}
7574

7675
// tryNotDeepEqual try to testing the deeply inequality between actual and expect values, and it'll
7776
// fail if the values are deeply equal.
7877
func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
7978
t.Helper()
8079

81-
if !reflect.DeepEqual(actual, expect) {
82-
return nil
83-
}
84-
85-
err := newAssertionError(fmt.Sprintf("%v != %v", actual, expect), message...)
86-
failed(t, err, failedNow)
87-
88-
return err
80+
return test(
81+
t,
82+
func() bool { return !reflect.DeepEqual(actual, expect) },
83+
failedNow,
84+
fmt.Sprintf(defaultErrMessageNotEqual, actual, expect),
85+
message...,
86+
)
8987
}
9088

9189
// Equal tests the equality between actual and expect parameters. It'll set the result to fail if
@@ -150,29 +148,27 @@ func (a *Assertion) NotEqualNow(actual, expect any, message ...string) error {
150148
func tryEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
151149
t.Helper()
152150

153-
if isEqual(actual, expect) {
154-
return nil
155-
}
156-
157-
err := newAssertionError(fmt.Sprintf("%v == %v", actual, expect), message...)
158-
failed(t, err, failedNow)
159-
160-
return err
151+
return test(
152+
t,
153+
func() bool { return isEqual(actual, expect) },
154+
failedNow,
155+
fmt.Sprintf(defaultErrMessageEqual, actual, expect),
156+
message...,
157+
)
161158
}
162159

163160
// tryNotEqual try to testing the inequality between actual and expect values, and it'll fail if
164161
// the values are equal.
165162
func tryNotEqual(t *testing.T, failedNow bool, actual, expect any, message ...string) error {
166163
t.Helper()
167164

168-
if !isEqual(actual, expect) {
169-
return nil
170-
}
171-
172-
err := newAssertionError(fmt.Sprintf("%v != %v", actual, expect), message...)
173-
failed(t, err, failedNow)
174-
175-
return err
165+
return test(
166+
t,
167+
func() bool { return !isEqual(actual, expect) },
168+
failedNow,
169+
fmt.Sprintf(defaultErrMessageNotEqual, actual, expect),
170+
message...,
171+
)
176172
}
177173

178174
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
@@ -245,28 +241,26 @@ func (a *Assertion) NotNilNow(val any, message ...string) error {
245241
func tryNil(t *testing.T, failedNow bool, val any, message ...string) error {
246242
t.Helper()
247243

248-
if isNil(val) {
249-
return nil
250-
}
251-
252-
err := newAssertionError(fmt.Sprintf("expect nil, got %v", val), message...)
253-
failed(t, err, failedNow)
254-
255-
return err
244+
return test(
245+
t,
246+
func() bool { return isNil(val) },
247+
failedNow,
248+
fmt.Sprintf(defaultErrMessageNil, val),
249+
message...,
250+
)
256251
}
257252

258253
// tryNotNil try to testing a value is nil or not, and it'll fail the value is not nil.
259254
func tryNotNil(t *testing.T, failedNow bool, val any, message ...string) error {
260255
t.Helper()
261256

262-
if !isNil(val) {
263-
return nil
264-
}
265-
266-
err := newAssertionError("expect not nil, got nil", message...)
267-
failed(t, err, failedNow)
268-
269-
return err
257+
return test(
258+
t,
259+
func() bool { return !isNil(val) },
260+
failedNow,
261+
defaultErrMessageNotNil,
262+
message...,
263+
)
270264
}
271265

272266
// True tests whether a value is truthy or not. It'll set the result to fail if the value is a
@@ -337,26 +331,28 @@ func (a *Assertion) NotTrueNow(val any, message ...string) error {
337331
func tryTrue(t *testing.T, failedNow bool, val any, message ...string) error {
338332
t.Helper()
339333

340-
if isTrue(val) {
341-
return nil
342-
}
343-
344-
err := newAssertionError("the expression evaluated to a falsy value")
345-
failed(t, err, failedNow)
346-
347-
return err
334+
return test(
335+
t,
336+
func() bool {
337+
return isTrue(val)
338+
},
339+
failedNow,
340+
defaultErrMessageTrue,
341+
message...,
342+
)
348343
}
349344

350345
// tryNotTrue try to testing a value is truthy or falsy, and it'll fail the value is truthy.
351346
func tryNotTrue(t *testing.T, failedNow bool, val any, message ...string) error {
352347
t.Helper()
353348

354-
if !isTrue(val) {
355-
return nil
356-
}
357-
358-
err := newAssertionError("the expression evaluated to a truthy value")
359-
failed(t, err, failedNow)
360-
361-
return err
349+
return test(
350+
t,
351+
func() bool {
352+
return !isTrue(val)
353+
},
354+
failedNow,
355+
defaultErrMessageNotTrue,
356+
message...,
357+
)
362358
}

error.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package assert
22

3+
const (
4+
defaultErrMessageEqual string = "%v == %v"
5+
defaultErrMessageNotEqual string = "%v != %v"
6+
defaultErrMessageMatch string = "the input did not match the regular expression"
7+
defaultErrMessageNotMatch string = "the input match the regular expression"
8+
defaultErrMessageNil string = "expect nil, got %v"
9+
defaultErrMessageNotNil string = "expect not nil, got nil"
10+
defaultErrMessagePanic string = "missing expected panic"
11+
defaultErrMessageNotPanic string = "got unwanted error: %v"
12+
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
13+
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
14+
)
15+
316
// AssertionError indicates the failure of an assertion.
417
type AssertionError struct {
518
message string

panic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func tryPanic(t *testing.T, failedNow bool, fn func(), message ...string) error
8181
return nil
8282
}
8383

84-
err := newAssertionError("missing expected panic", message...)
84+
err := newAssertionError(defaultErrMessagePanic, message...)
8585
failed(t, err, failedNow)
8686

8787
return err
@@ -97,7 +97,7 @@ func tryNotPanic(t *testing.T, failedNow bool, fn func(), message ...string) err
9797
return nil
9898
}
9999

100-
err := newAssertionError(fmt.Sprintf("got unwanted error: %v", e), message...)
100+
err := newAssertionError(fmt.Sprintf(defaultErrMessageNotPanic, e), message...)
101101
failed(t, err, failedNow)
102102
return err
103103
}

0 commit comments

Comments
 (0)