Skip to content

Commit ef98919

Browse files
committed
feat: Add string regexp matcher
1 parent 727f08d commit ef98919

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

string-regexp-matcher.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package extra
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
"github.com/golang/mock/gomock"
8+
)
9+
10+
type stringRegexp struct {
11+
reg *regexp.Regexp
12+
}
13+
14+
func (s *stringRegexp) String() string {
15+
return fmt.Sprintf("input matching regexp %s", s.reg.String())
16+
}
17+
18+
func (s *stringRegexp) Matches(x interface{}) bool {
19+
// Try to cast input as string
20+
st, ok := x.(string)
21+
// Check if it is a string
22+
if !ok {
23+
return false
24+
}
25+
26+
return s.reg.Match([]byte(st))
27+
}
28+
29+
// Will return a new string regexp matcher
30+
func StringRegexpMatcher(regexSt string) gomock.Matcher {
31+
return &stringRegexp{
32+
reg: regexp.MustCompile(regexSt),
33+
}
34+
}

string-regexp-matcher_test.go

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package extra
2+
3+
import (
4+
"reflect"
5+
"regexp"
6+
"testing"
7+
)
8+
9+
func Test_stringRegexp_String(t *testing.T) {
10+
type fields struct {
11+
reg *regexp.Regexp
12+
}
13+
tests := []struct {
14+
name string
15+
fields fields
16+
want string
17+
}{
18+
{
19+
name: "should display regex",
20+
fields: fields{
21+
reg: regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`),
22+
},
23+
want: `input matching regexp ^[a-z]+\[[0-9]+\]$`,
24+
},
25+
}
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
s := &stringRegexp{
29+
reg: tt.fields.reg,
30+
}
31+
if got := s.String(); got != tt.want {
32+
t.Errorf("stringRegexp.String() = %v, want %v", got, tt.want)
33+
}
34+
})
35+
}
36+
}
37+
38+
func Test_stringRegexp_Matches(t *testing.T) {
39+
starStrFunc := func(s string) *string { return &s }
40+
type fields struct {
41+
reg *regexp.Regexp
42+
}
43+
type args struct {
44+
x interface{}
45+
}
46+
tests := []struct {
47+
name string
48+
fields fields
49+
args args
50+
want bool
51+
}{
52+
{
53+
name: "not a string",
54+
args: args{
55+
x: 1,
56+
},
57+
want: false,
58+
},
59+
{
60+
name: "not a string 2",
61+
args: args{
62+
x: starStrFunc("fake"),
63+
},
64+
want: false,
65+
},
66+
{
67+
name: "not matching regexp",
68+
fields: fields{
69+
reg: regexp.MustCompile("^a$"),
70+
},
71+
args: args{
72+
x: "0",
73+
},
74+
},
75+
{
76+
name: "matching regexp",
77+
fields: fields{
78+
reg: regexp.MustCompile("^a$"),
79+
},
80+
args: args{
81+
x: "a",
82+
},
83+
want: true,
84+
},
85+
}
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
s := &stringRegexp{
89+
reg: tt.fields.reg,
90+
}
91+
if got := s.Matches(tt.args.x); got != tt.want {
92+
t.Errorf("stringRegexp.Matches() = %v, want %v", got, tt.want)
93+
}
94+
})
95+
}
96+
}
97+
98+
func TestStringRegexpMatcher(t *testing.T) {
99+
type args struct {
100+
regexSt string
101+
}
102+
tests := []struct {
103+
name string
104+
args args
105+
want *stringRegexp
106+
}{
107+
{
108+
name: "constructor",
109+
args: args{
110+
regexSt: "^a$",
111+
},
112+
want: &stringRegexp{
113+
reg: regexp.MustCompile("^a$"),
114+
},
115+
},
116+
}
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
if got := StringRegexpMatcher(tt.args.regexSt); !reflect.DeepEqual(got, tt.want) {
120+
t.Errorf("StringRegexpMatcher() = %v, want %v", got, tt.want)
121+
}
122+
})
123+
}
124+
}

0 commit comments

Comments
 (0)