-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tests with custom regexp checker
- Loading branch information
Showing
2 changed files
with
60 additions
and
6 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
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,46 @@ | ||
package defaultresolver | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
type argNames []string | ||
|
||
func (a argNames) ArgNames() []string { | ||
return a | ||
} | ||
|
||
var MatchesRegexp qt.Checker = ®expChecker{ | ||
argNames: []string{"got value", "regexp"}, | ||
} | ||
|
||
type regexpChecker struct { | ||
argNames | ||
} | ||
|
||
// match checks that the given error message matches the given pattern. | ||
func match(got string, pattern *regexp.Regexp, msg string, note func(key string, value interface{})) error { | ||
if pattern.MatchString(got) { | ||
return nil | ||
} | ||
|
||
return errors.New(msg) | ||
} | ||
|
||
func (c *regexpChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) error { | ||
switch pattern := args[0].(type) { | ||
case *regexp.Regexp: | ||
switch v := got.(type) { | ||
case string: | ||
return match(v, pattern, "value does not match regexp", note) | ||
case fmt.Stringer: | ||
return match(v.String(), pattern, "value.String() does not match regexp", note) | ||
} | ||
return qt.BadCheckf("value is not a string or a fmt.Stringer") | ||
} | ||
return qt.BadCheckf("pattern is not a *regexp.Regexp") | ||
} |