Skip to content

Commit

Permalink
feat: Add string regexp matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Sep 6, 2020
1 parent 727f08d commit ef98919
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
34 changes: 34 additions & 0 deletions string-regexp-matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package extra

import (
"fmt"
"regexp"

"github.com/golang/mock/gomock"
)

type stringRegexp struct {
reg *regexp.Regexp
}

func (s *stringRegexp) String() string {
return fmt.Sprintf("input matching regexp %s", s.reg.String())
}

func (s *stringRegexp) Matches(x interface{}) bool {
// Try to cast input as string
st, ok := x.(string)
// Check if it is a string
if !ok {
return false
}

return s.reg.Match([]byte(st))
}

// Will return a new string regexp matcher
func StringRegexpMatcher(regexSt string) gomock.Matcher {
return &stringRegexp{
reg: regexp.MustCompile(regexSt),
}
}
124 changes: 124 additions & 0 deletions string-regexp-matcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package extra

import (
"reflect"
"regexp"
"testing"
)

func Test_stringRegexp_String(t *testing.T) {
type fields struct {
reg *regexp.Regexp
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "should display regex",
fields: fields{
reg: regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`),
},
want: `input matching regexp ^[a-z]+\[[0-9]+\]$`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &stringRegexp{
reg: tt.fields.reg,
}
if got := s.String(); got != tt.want {
t.Errorf("stringRegexp.String() = %v, want %v", got, tt.want)
}
})
}
}

func Test_stringRegexp_Matches(t *testing.T) {
starStrFunc := func(s string) *string { return &s }
type fields struct {
reg *regexp.Regexp
}
type args struct {
x interface{}
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "not a string",
args: args{
x: 1,
},
want: false,
},
{
name: "not a string 2",
args: args{
x: starStrFunc("fake"),
},
want: false,
},
{
name: "not matching regexp",
fields: fields{
reg: regexp.MustCompile("^a$"),
},
args: args{
x: "0",
},
},
{
name: "matching regexp",
fields: fields{
reg: regexp.MustCompile("^a$"),
},
args: args{
x: "a",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &stringRegexp{
reg: tt.fields.reg,
}
if got := s.Matches(tt.args.x); got != tt.want {
t.Errorf("stringRegexp.Matches() = %v, want %v", got, tt.want)
}
})
}
}

func TestStringRegexpMatcher(t *testing.T) {
type args struct {
regexSt string
}
tests := []struct {
name string
args args
want *stringRegexp
}{
{
name: "constructor",
args: args{
regexSt: "^a$",
},
want: &stringRegexp{
reg: regexp.MustCompile("^a$"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringRegexpMatcher(tt.args.regexSt); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringRegexpMatcher() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ef98919

Please sign in to comment.