Skip to content

Commit

Permalink
Fix tests with custo mregexp checker
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Apr 23, 2022
1 parent 87687e3 commit 04010d1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
25 changes: 19 additions & 6 deletions internal/resolvers/default/link_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ func TestLinkResolver(t *testing.T) {
inputReq: newLinkResolverRequest(t, ctx, "GET", ts.URL, nil),
inputLinkKey: ts.URL,
expected: resolver.Response{
Status: 200,
Link: ts.URL,
Tooltip: `.*`,
Status: 200,
Link: ts.URL,
Tooltip: `<div style="text-align: left;">
<b>/ title</b><hr>
<b>URL:</b> http://127\.0\.0\.1:[\d]{2,7}</div>`,
},
},
}
Expand All @@ -109,9 +114,17 @@ func TestLinkResolver(t *testing.T) {
unescapedTooltip, err := url.QueryUnescape(response.Tooltip)
c.Assert(err, qt.IsNil)

fmt.Println(unescapedTooltip)
fmt.Println(test.expected.Tooltip)
c.Assert("asd\nasd", qt.Matches, regexp.MustCompile(`.*`))
if test.expected.Tooltip != "" {
c.Assert(unescapedTooltip, MatchesRegexp, regexp.MustCompile(test.expected.Tooltip), qt.Commentf("%s does not match %s", unescapedTooltip, test.expected.Tooltip))
}
if test.expected.Message != "" {
c.Assert(response.Message, qt.Matches, test.expected.Message, qt.Commentf("%s does not match %s", response.Message, test.expected.Message))
}

c.Assert(pool.ExpectationsWereMet(), qt.IsNil)
})
}
})

c.Assert(pool.ExpectationsWereMet(), qt.IsNil)
})
Expand Down
46 changes: 46 additions & 0 deletions internal/resolvers/default/regexpchecker_test.go
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 = &regexpChecker{
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")
}

0 comments on commit 04010d1

Please sign in to comment.