@@ -47,6 +47,7 @@ func TestMatchers(t *testing.T) {
4747 []e {nil , (error )(nil ), (chan bool )(nil ), (* int )(nil )},
4848 []e {"" , 0 , make (chan bool ), errors .New ("err" ), new (int )}},
4949 {"test Not" , gomock .Not (gomock .Eq (4 )), []e {3 , "blah" , nil , int64 (4 )}, []e {4 }},
50+ {"test Regex" , gomock .Regex ("[0-9]{2}:[0-9]{2}" ), []e {"23:02" , "[23:02]: Hello world" }, []e {4 , "23-02" , "hello world" , true }},
5051 {"test All" , gomock .All (gomock .Any (), gomock .Eq (4 )), []e {4 }, []e {3 , "blah" , nil , int64 (4 )}},
5152 {"test Len" , gomock .Len (2 ),
5253 []e {[]int {1 , 2 }, "ab" , map [string ]int {"a" : 0 , "b" : 1 }, [2 ]string {"a" , "b" }},
@@ -92,6 +93,52 @@ func TestNotMatcher(t *testing.T) {
9293 }
9394}
9495
96+ // A more thorough test of regexMatcher
97+ func TestRegexMatcher (t * testing.T ) {
98+ tests := []struct {
99+ name string
100+ regex string
101+ input any
102+ wantMatch bool
103+ wantStringResponse string
104+ }{
105+ {
106+ name : "match for whole num regex with start and end position matching" ,
107+ regex : "^\\ d+$" ,
108+ input : "2302" ,
109+ wantMatch : true ,
110+ wantStringResponse : "matching regex ^\\ d+$" ,
111+ },
112+ {
113+ name : "match for valid regex with start and end position matching on longer string" ,
114+ regex : "^[0-9]{2}:[0-9]{2}$" ,
115+ input : "[23:02]: Hello world" ,
116+ wantMatch : false ,
117+ wantStringResponse : "matching regex ^[0-9]{2}:[0-9]{2}$" ,
118+ },
119+ {
120+ name : "match for invalid regex" ,
121+ regex : "^[0-9{2}:[0-9]{2}$" ,
122+ input : "23:02" ,
123+ wantMatch : false ,
124+ wantStringResponse : "matching regex ^[0-9{2}:[0-9]{2}$" ,
125+ },
126+ }
127+
128+ for _ , tt := range tests {
129+ t .Run (tt .name , func (t * testing.T ) {
130+ matcher := gomock .Regex (tt .regex )
131+
132+ if got := matcher .Matches (tt .input ); got != tt .wantMatch {
133+ t .Errorf ("got = %v, wantMatch = %v" , got , tt .wantMatch )
134+ }
135+ if gotStr := matcher .String (); gotStr != tt .wantStringResponse {
136+ t .Errorf ("got string = %v, want string = %v" , gotStr , tt .wantStringResponse )
137+ }
138+ })
139+ }
140+ }
141+
95142type Dog struct {
96143 Breed , Name string
97144}
0 commit comments