-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
727f08d
commit ef98919
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |