diff --git a/string-regexp-matcher.go b/string-regexp-matcher.go new file mode 100644 index 0000000..7c10a54 --- /dev/null +++ b/string-regexp-matcher.go @@ -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), + } +} diff --git a/string-regexp-matcher_test.go b/string-regexp-matcher_test.go new file mode 100644 index 0000000..6673f81 --- /dev/null +++ b/string-regexp-matcher_test.go @@ -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) + } + }) + } +}