Skip to content

Commit 4c80099

Browse files
committed
feat: add regular expression assertions.
1 parent cc55697 commit 4c80099

File tree

4 files changed

+381
-0
lines changed

4 files changed

+381
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func TestExample(t *testing.T) {
105105

106106
- [`DeepEqual`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.DeepEqual) and [`NotDeepEqual`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotDeepEqual): assert the deep equality or inequality.
107107
- [`Equal`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Equal) and [`NotEqual`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotEqual): assert the equality or inequality.
108+
- [`Match`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Match) and [`NotMatch`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotMatch): assert whether the string matches the regular expression pattern or not.
109+
- [`MatchString`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.MatchString) and [`NotMatchString`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotMatchString): compile the regular expression pattern and assert whether the string matches the pattern or not.
108110
- [`Nil`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Nil) and [`NotNil`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotNil): assert the value is nil or not.
109111
- [`Panic`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Panic) and [`NotPanic`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotPanic): assert the function will panic or not.
110112
- [`True`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.True) and [`NotTrue`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotTrue): assert the truthy of the value.

builtin.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assert
22

33
import (
4+
"regexp"
45
"testing"
56
)
67

@@ -68,6 +69,81 @@ func NotEqualNow(t *testing.T, actual, expect any, message ...string) error {
6869
return tryNotEqual(t, true, actual, expect, message...)
6970
}
7071

72+
// Match tests whether the string matches the regular expression or not.
73+
func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
74+
t.Helper()
75+
76+
return tryMatchRegexp(t, false, val, pattern, message...)
77+
}
78+
79+
// MatchNow tests whether the string matches the regular expression or not, and it will terminate
80+
// the execution if it does not match.
81+
func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
82+
t.Helper()
83+
84+
return tryMatchRegexp(t, true, val, pattern, message...)
85+
}
86+
87+
// MatchString will compile the pattern and test whether the string matches the regular expression
88+
// or not. It will panic if the pattern is not a valid regular expression.
89+
func MatchString(t *testing.T, val, pattern string, message ...string) error {
90+
t.Helper()
91+
92+
regPattern := regexp.MustCompile(pattern)
93+
94+
return tryMatchRegexp(t, false, val, regPattern, message...)
95+
}
96+
97+
// MatchStringNow will compile the pattern and test whether the string matches the regular
98+
// expression or not. It will terminate the execution if it does not match, and it will panic if
99+
// the pattern is not a valid regular expression.
100+
func MatchStringNow(t *testing.T, val, pattern string, message ...string) error {
101+
t.Helper()
102+
103+
regPattern := regexp.MustCompile(pattern)
104+
105+
return tryMatchRegexp(t, true, val, regPattern, message...)
106+
}
107+
108+
// NotMatch tests whether the string matches the regular expression or not, and it set the result
109+
// to fail if the string matches the pattern.
110+
func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
111+
t.Helper()
112+
113+
return tryNotMatchRegexp(t, false, val, pattern, message...)
114+
}
115+
116+
// NotMatchNow tests whether the string matches the regular expression or not, and it will
117+
// terminate the execution if the string matches the pattern.
118+
func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error {
119+
t.Helper()
120+
121+
return tryNotMatchRegexp(t, true, val, pattern, message...)
122+
}
123+
124+
// MatchString will compile the pattern and test whether the string matches the regular expression
125+
// or not, and it set the result to fail if the string matches the pattern. It will also panic if
126+
// the pattern is not a valid regular expression.
127+
func NotMatchString(t *testing.T, val, pattern string, message ...string) error {
128+
t.Helper()
129+
130+
regPattern := regexp.MustCompile(pattern)
131+
132+
return tryNotMatchRegexp(t, false, val, regPattern, message...)
133+
}
134+
135+
// NotMatchStringNow will compile the pattern and test whether the string matches the regular
136+
// expression or not, and it set the result to fail if the string matches the pattern. It will
137+
// terminate the execution if the string matches the pattern, and it will panic if the pattern is
138+
// not a valid regular expression.
139+
func NotMatchStringNow(t *testing.T, val, pattern string, message ...string) error {
140+
t.Helper()
141+
142+
regPattern := regexp.MustCompile(pattern)
143+
144+
return tryNotMatchRegexp(t, true, val, regPattern, message...)
145+
}
146+
71147
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
72148
// always return false if the value is a bool, an integer, a floating number, a complex, or a
73149
// string.

regexp.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package assert
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
)
7+
8+
// Match tests whether the string matches the regular expression or not.
9+
func (a *Assertion) Match(val string, pattern *regexp.Regexp, message ...string) error {
10+
a.Helper()
11+
12+
return tryMatchRegexp(a.T, false, val, pattern, message...)
13+
}
14+
15+
// MatchNow tests whether the string matches the regular expression or not, and it will terminate
16+
// the execution if it does not match.
17+
func (a *Assertion) MatchNow(val string, pattern *regexp.Regexp, message ...string) error {
18+
a.Helper()
19+
20+
return tryMatchRegexp(a.T, true, val, pattern, message...)
21+
}
22+
23+
// MatchString will compile the pattern and test whether the string matches the regular expression
24+
// or not. It will panic if the pattern is not a valid regular expression.
25+
func (a *Assertion) MatchString(val, pattern string, message ...string) error {
26+
a.Helper()
27+
28+
regPattern := regexp.MustCompile(pattern)
29+
30+
return tryMatchRegexp(a.T, false, val, regPattern, message...)
31+
}
32+
33+
// MatchStringNow will compile the pattern and test whether the string matches the regular
34+
// expression or not. It will terminate the execution if it does not match, and it will panic if
35+
// the pattern is not a valid regular expression.
36+
func (a *Assertion) MatchStringNow(val, pattern string, message ...string) error {
37+
a.Helper()
38+
39+
regPattern := regexp.MustCompile(pattern)
40+
41+
return tryMatchRegexp(a.T, true, val, regPattern, message...)
42+
}
43+
44+
// NotMatch tests whether the string matches the regular expression or not, and it set the result
45+
// to fail if the string matches the pattern.
46+
func (a *Assertion) NotMatch(val string, pattern *regexp.Regexp, message ...string) error {
47+
a.Helper()
48+
49+
return tryNotMatchRegexp(a.T, false, val, pattern, message...)
50+
}
51+
52+
// NotMatchNow tests whether the string matches the regular expression or not, and it will
53+
// terminate the execution if the string matches the pattern.
54+
func (a *Assertion) NotMatchNow(val string, pattern *regexp.Regexp, message ...string) error {
55+
a.Helper()
56+
57+
return tryNotMatchRegexp(a.T, true, val, pattern, message...)
58+
}
59+
60+
// MatchString will compile the pattern and test whether the string matches the regular expression
61+
// or not, and it set the result to fail if the string matches the pattern. It will also panic if
62+
// the pattern is not a valid regular expression.
63+
func (a *Assertion) NotMatchString(val, pattern string, message ...string) error {
64+
a.Helper()
65+
66+
regPattern := regexp.MustCompile(pattern)
67+
68+
return tryNotMatchRegexp(a.T, false, val, regPattern, message...)
69+
}
70+
71+
// NotMatchStringNow will compile the pattern and test whether the string matches the regular
72+
// expression or not, and it set the result to fail if the string matches the pattern. It will
73+
// terminate the execution if the string matches the pattern, and it will panic if the pattern is
74+
// not a valid regular expression.
75+
func (a *Assertion) NotMatchStringNow(val, pattern string, message ...string) error {
76+
a.Helper()
77+
78+
regPattern := regexp.MustCompile(pattern)
79+
80+
return tryNotMatchRegexp(a.T, true, val, regPattern, message...)
81+
}
82+
83+
// tryMatchRegexp tries to test whether the string matches the regular expression pattern or not,
84+
// and it'll fail if the string does not match.
85+
func tryMatchRegexp(
86+
t *testing.T,
87+
failedNow bool,
88+
val string,
89+
pattern *regexp.Regexp,
90+
message ...string,
91+
) error {
92+
t.Helper()
93+
94+
if pattern.Match([]byte(val)) {
95+
return nil
96+
}
97+
98+
err := newAssertionError("The input did not match the regular expression", message...)
99+
failed(t, err, failedNow)
100+
return err
101+
}
102+
103+
// tryNotMatchRegexp tries to test whether the string matches the regular expression pattern or
104+
// not, and it'll fail if the string matches the pattern.
105+
func tryNotMatchRegexp(
106+
t *testing.T,
107+
failedNow bool,
108+
val string,
109+
pattern *regexp.Regexp,
110+
message ...string,
111+
) error {
112+
t.Helper()
113+
114+
if !pattern.Match([]byte(val)) {
115+
return nil
116+
}
117+
118+
err := newAssertionError("The input match the regular expression", message...)
119+
failed(t, err, failedNow)
120+
return err
121+
}

0 commit comments

Comments
 (0)