Skip to content

Commit

Permalink
Merge pull request #7 from Storytel/regexp-matcher
Browse files Browse the repository at this point in the history
Add Regexp Matcher
  • Loading branch information
noseglid authored Sep 10, 2020
2 parents 0bf61ee + f3e1a60 commit ec80d44
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ func TestSomething(t *testing.T) {

# Matchers


<details>
<summary><strong>RegexpMatcher</strong> - <em>Matcher which accepts a string to be interpreted as a <a href="https://golang.org/pkg/regexp/">Go Regexp</a>.</em></summary>

Regexpmatcher returns a matcher which will match if the supplied string, interpreted
as a regexp, matches the input string.

This is useful when you don't know exactly what the string might be in a run.
Maybe it's a timestamp or something else outside of your control.

```go
func TestRegexpMatcher(t *testing.T) {
m := matchers.Regexp("^[^@]+@.+$")
m.Matches("[email protected]") // true
m.Matches("daisy") // false
}
```
</details>

<details>
<summary><strong>RecordMatcher</strong> - <em>Proxy matcher which captures the argument for further inspection.</em></summary>
Wraps another matcher and records the value of the argument it's called with.
Expand Down Expand Up @@ -105,7 +124,7 @@ func TestAsyncBlockMatcher(t *testing.T) {

// This blocks until `Matches` is actually called
<-m.Channel()
assert.True(didMatch)
assert.True(didMatch)
}
```
</details>
Expand Down
29 changes: 29 additions & 0 deletions regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package matchers

import (
"fmt"
"regexp"
)

type regexpMatcher struct {
pattern *regexp.Regexp
}

func Regexp(pattern string) *regexpMatcher {
return &regexpMatcher{
pattern: regexp.MustCompile(pattern),
}
}

func (m *regexpMatcher) String() string {
return fmt.Sprintf("matches pattern /%v/", m.pattern)
}

func (m *regexpMatcher) Matches(x interface{}) bool {
s, ok := x.(string)
if !ok {
return false
}

return m.pattern.MatchString(s)
}
59 changes: 59 additions & 0 deletions regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package matchers_test

import (
"fmt"
"testing"

matchers "github.com/Storytel/gomock-matchers"
"github.com/stretchr/testify/assert"
)

func TestRegexpMatcher(t *testing.T) {

testCases := []struct {
pattern string
value interface{}
matches bool
}{
{"^something .*", "something good", true},
{"^something .*", "that's something good", false},
{"^[0-9][1-5]a?$", "42a", true},
{".*", 12, false},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test #%d", i), func(t *testing.T) {
assert := assert.New(t)
m := matchers.Regexp(testCase.pattern)
assert.Equal(testCase.matches, m.Matches(testCase.value))
})
}
}

func TestRegexpMatcherCompileError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("invalid regexp did not panic")
}
}()
_ = matchers.Regexp("^(abc$")
}

func TestRegexpMatcherString(t *testing.T) {
testCases := []struct {
pattern string
str string
}{
{"^[a-z]$", "matches pattern /^[a-z]$/"},
{"asdf", "matches pattern /asdf/"},
{"something[[:space:]]", "matches pattern /something[[:space:]]/"},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test #%d", i), func(t *testing.T) {
assert := assert.New(t)
m := matchers.Regexp(testCase.pattern)
assert.Equal(testCase.str, m.String())
})
}
}

0 comments on commit ec80d44

Please sign in to comment.