Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Regex() matcher #114

Merged
merged 4 commits into from Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions gomock/matchers.go
Expand Up @@ -17,6 +17,7 @@ package gomock
import (
"fmt"
"reflect"
"regexp"
"strings"
)

Expand Down Expand Up @@ -168,6 +169,33 @@ func (n notMatcher) String() string {
return "not(" + n.m.String() + ")"
}

type regexMatcher struct {
regex *regexp.Regexp
compileErr error
}

func (m regexMatcher) Matches(x any) bool {
if m.regex == nil {
return false
}

switch t := x.(type) {
case string:
return m.regex.MatchString(t)
case []byte:
return m.regex.Match(t)
default:
return false
}
}

func (m regexMatcher) String() string {
if m.compileErr != nil {
return m.compileErr.Error()
}
merrett010 marked this conversation as resolved.
Show resolved Hide resolved
return "matching regex " + m.regex.String()
merrett010 marked this conversation as resolved.
Show resolved Hide resolved
}

type assignableToTypeOfMatcher struct {
targetType reflect.Type
}
Expand Down Expand Up @@ -382,6 +410,22 @@ func Not(x any) Matcher {
return notMatcher{Eq(x)}
}

// Regex checks whether parameter matches the associated regex.
//
// Example usage:
//
// Regex("[0-9]{2}:[0-9]{2}").Matches("23:02") // returns true
// Regex("[0-9]{2}:[0-9]{2}").Matches([]byte{50,51,58,48,50}) // returns true
merrett010 marked this conversation as resolved.
Show resolved Hide resolved
// Regex("[0-9]{2}:[0-9]{2}").Matches("hello world") // returns false
// Regex("[0-9]{2}").Matches(21) // returns false as it's not a valid type
func Regex(regexStr string) Matcher {
compiledRegexp, err := regexp.Compile(regexStr)
if err == nil {
return regexMatcher{regex: compiledRegexp}
}
return regexMatcher{regex: nil, compileErr: err}
merrett010 marked this conversation as resolved.
Show resolved Hide resolved
}

// AssignableToTypeOf is a Matcher that matches if the parameter to the mock
// function is assignable to the type of the parameter to this function.
//
Expand Down
54 changes: 54 additions & 0 deletions gomock/matchers_test.go
Expand Up @@ -47,6 +47,7 @@ func TestMatchers(t *testing.T) {
[]e{nil, (error)(nil), (chan bool)(nil), (*int)(nil)},
[]e{"", 0, make(chan bool), errors.New("err"), new(int)}},
{"test Not", gomock.Not(gomock.Eq(4)), []e{3, "blah", nil, int64(4)}, []e{4}},
{"test Regex", gomock.Regex("[0-9]{2}:[0-9]{2}"), []e{"23:02", "[23:02]: Hello world", []byte("23:02")}, []e{4, "23-02", "hello world", true, []byte("23-02")}},
{"test All", gomock.All(gomock.Any(), gomock.Eq(4)), []e{4}, []e{3, "blah", nil, int64(4)}},
{"test Len", gomock.Len(2),
[]e{[]int{1, 2}, "ab", map[string]int{"a": 0, "b": 1}, [2]string{"a", "b"}},
Expand Down Expand Up @@ -92,6 +93,59 @@ func TestNotMatcher(t *testing.T) {
}
}

// A more thorough test of regexMatcher
func TestRegexMatcher(t *testing.T) {
tests := []struct {
name string
regex string
input any
wantMatch bool
wantStringResponse string
}{
{
name: "match for whole num regex with start and end position matching",
regex: "^\\d+$",
input: "2302",
wantMatch: true,
wantStringResponse: "matching regex ^\\d+$",
},
{
name: "match for valid regex with start and end position matching on longer string",
regex: "^[0-9]{2}:[0-9]{2}$",
input: "[23:02]: Hello world",
wantMatch: false,
wantStringResponse: "matching regex ^[0-9]{2}:[0-9]{2}$",
},
{
name: "match for valid regex with struct as bytes",
regex: `^{"id":[0-9]{2},"name":"world"}$`,
input: []byte{123, 34, 105, 100, 34, 58, 49, 50, 44, 34, 110, 97, 109, 101, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125},
merrett010 marked this conversation as resolved.
Show resolved Hide resolved
wantMatch: true,
wantStringResponse: `matching regex ^{"id":[0-9]{2},"name":"world"}$`,
},
{
name: "match for invalid regex",
regex: `^[0-9]\\?{2}:[0-9]{2}$`,
input: "23:02",
wantMatch: false,
wantStringResponse: "error parsing regexp: invalid nested repetition operator: `?{2}`",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matcher := gomock.Regex(tt.regex)

if got := matcher.Matches(tt.input); got != tt.wantMatch {
t.Errorf("got = %v, wantMatch = %v", got, tt.wantMatch)
}
if gotStr := matcher.String(); gotStr != tt.wantStringResponse {
t.Errorf("got string = %v, want string = %v", gotStr, tt.wantStringResponse)
}
})
}
}

type Dog struct {
Breed, Name string
}
Expand Down