Skip to content

Commit

Permalink
Merge pull request #137 from pajlada/feat/checker-accept-regexp
Browse files Browse the repository at this point in the history
Allow Matches and ErrorMatches matches to use an already-compiled regexp.Regexp
  • Loading branch information
frankban committed Apr 11, 2023
2 parents a619a7e + 08f7639 commit 1af932f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ the provided regular expression.
For instance:

c.Assert(err, qt.ErrorMatches, `bad wolf .*`)
c.Assert(err, qt.ErrorMatches, regexp.MustCompile("bad wolf .*"))


### HasLen
Expand Down Expand Up @@ -301,6 +302,7 @@ For instance:

c.Assert("these are the voyages", qt.Matches, `these are .*`)
c.Assert(net.ParseIP("1.2.3.4"), qt.Matches, `1.*`)
c.Assert("line 1\nline 2", qt.Matches, regexp.MustCompile(`line \d\nline \d`))


### Not
Expand All @@ -321,6 +323,7 @@ the provided regular expression.
For instance:

c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, `bad wolf .*`)
c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, regexp.MustCompile(`bad wolf .*`))


### Satisfies
Expand Down
9 changes: 9 additions & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ var ContentEquals = CmpEquals(cmpopts.SortSlices(func(x, y interface{}) bool {
//
// c.Assert("these are the voyages", qt.Matches, "these are .*")
// c.Assert(net.ParseIP("1.2.3.4"), qt.Matches, "1.*")
// c.Assert("my multi-line\nnumber", qt.Matches, regexp.MustCompile(`my multi-line\n(string|number)`))
var Matches Checker = &matchesChecker{
argNames: []string{"got value", "regexp"},
}
Expand Down Expand Up @@ -210,6 +211,7 @@ func checkFirstArgIsError(got interface{}, note func(key string, value interface
// For instance:
//
// c.Assert(err, qt.ErrorMatches, "bad wolf .*")
// c.Assert(err, qt.ErrorMatches, regexp.MustCompile("bad wolf .*"))
var ErrorMatches Checker = &errorMatchesChecker{
argNames: []string{"got error", "regexp"},
}
Expand All @@ -235,6 +237,7 @@ func (c *errorMatchesChecker) Check(got interface{}, args []interface{}, note fu
// For instance:
//
// c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, "bad wolf .*")
// c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, regexp.MustCompile(`bad wolf .*`))
var PanicMatches Checker = &panicMatchesChecker{
argNames: []string{"function", "regexp"},
}
Expand Down Expand Up @@ -766,6 +769,12 @@ func (a argNames) ArgNames() []string {

// match checks that the given error message matches the given pattern.
func match(got string, pattern interface{}, msg string, note func(key string, value interface{})) error {
if actualRegex, ok := pattern.(*regexp.Regexp); ok {
if actualRegex.MatchString(got) {
return nil
}
return errors.New(msg)
}
regex, ok := pattern.(string)
if !ok {
note("regexp", pattern)
Expand Down
128 changes: 128 additions & 0 deletions checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -937,6 +938,45 @@ got value:
regexp:
"these are the .*"
`,
}, {
about: "Matches: match with pre-compiled regexp",
checker: qt.Matches,
got: bytes.NewBufferString("resistance is futile"),
args: []interface{}{regexp.MustCompile("resistance is (futile|useful)")},
expectedNegateFailure: `
error:
unexpected success
got value:
s"resistance is futile"
regexp:
s"resistance is (futile|useful)"
`,
}, {
about: "Matches: mismatch with pre-compiled regexp",
checker: qt.Matches,
got: bytes.NewBufferString("resistance is cool"),
args: []interface{}{regexp.MustCompile("resistance is (futile|useful)")},
expectedCheckFailure: `
error:
value.String() does not match regexp
got value:
s"resistance is cool"
regexp:
s"resistance is (futile|useful)"
`,
}, {
about: "Matches: match with pre-compiled multi-line regexp",
checker: qt.Matches,
got: bytes.NewBufferString("line 1\nline 2"),
args: []interface{}{regexp.MustCompile(`line \d\nline \d`)},
expectedNegateFailure: `
error:
unexpected success
got value:
s"line 1\nline 2"
regexp:
s"line \\d\\nline \\d"
`,
}, {
about: "Matches: match with stringer",
checker: qt.Matches,
Expand Down Expand Up @@ -1271,6 +1311,49 @@ got args:
want args:
regexp
`,
}, {
about: "ErrorMatches: match with pre-compiled regexp",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{regexp.MustCompile("bad (wolf|dog)")},
expectedNegateFailure: `
error:
unexpected success
got error:
bad wolf
file:line
regexp:
s"bad (wolf|dog)"
`,
}, {
about: "ErrorMatches: match with pre-compiled multi-line regexp",
checker: qt.ErrorMatches,
got: errBadWolfMultiLine,
args: []interface{}{regexp.MustCompile(`bad (wolf|dog)\nfaulty (logic|statement)`)},
expectedNegateFailure: `
error:
unexpected success
got error:
bad wolf
faulty logic
file:line
regexp:
s"bad (wolf|dog)\\nfaulty (logic|statement)"
`,
}, {
about: "ErrorMatches: mismatch with pre-compiled regexp",
checker: qt.ErrorMatches,
got: errBadWolf,
args: []interface{}{regexp.MustCompile("good (wolf|dog)")},
expectedCheckFailure: `
error:
error does not match regexp
got error:
bad wolf
file:line
regexp:
s"good (wolf|dog)"
`,
}, {
about: "PanicMatches: perfect match",
checker: qt.PanicMatches,
Expand Down Expand Up @@ -1388,6 +1471,51 @@ panic value:
regexp:
nil
`,
}, {
about: "PanicMatches: match with pre-compiled regexp",
checker: qt.PanicMatches,
got: func() { panic("error: bad wolf") },
args: []interface{}{regexp.MustCompile("error: bad (wolf|dog)")},
expectedNegateFailure: `
error:
unexpected success
panic value:
"error: bad wolf"
function:
func() {...}
regexp:
s"error: bad (wolf|dog)"
`,
}, {
about: "PanicMatches: match with pre-compiled multi-line regexp",
checker: qt.PanicMatches,
got: func() { panic("error: bad wolf\nfaulty logic") },
args: []interface{}{regexp.MustCompile(`error: bad (wolf|dog)\nfaulty (logic|statement)`)},
expectedNegateFailure: `
error:
unexpected success
panic value:
"error: bad wolf\nfaulty logic"
function:
func() {...}
regexp:
s"error: bad (wolf|dog)\\nfaulty (logic|statement)"
`,
}, {
about: "PanicMatches: mismatch with pre-compiled regexp",
checker: qt.PanicMatches,
got: func() { panic("error: bad wolf") },
args: []interface{}{regexp.MustCompile("good (wolf|dog)")},
expectedCheckFailure: `
error:
panic value does not match regexp
panic value:
"error: bad wolf"
function:
func() {...}
regexp:
s"good (wolf|dog)"
`,
}, {
about: "PanicMatches: not a function",
checker: qt.PanicMatches,
Expand Down
5 changes: 5 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ var errBadWolf = &errTest{
formatted: true,
}

var errBadWolfMultiLine = &errTest{
msg: "bad wolf\nfaulty logic",
formatted: true,
}

// errTest is an error type used in tests.
type errTest struct {
msg string
Expand Down

0 comments on commit 1af932f

Please sign in to comment.