Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions matchers/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
ipAddress = `(\d{1,3}\.)+\d{1,3}`
ipv6Address = `(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\Z)|(\A(([0-9a-f]{1,4}:){1,7}|:):\Z)|(\A:(:[0-9a-f]{1,4}){1,7}\Z)|(\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|(\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|(\A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|(\A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)`
uuid = `[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`
timestamp = `^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$`
date = `^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))?)`
timestamp = `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`
date = `^\d{4}-\d{2}-\d{2}`
Copy link
Author

@thompsnm thompsnm Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that Go’s regexp engine does not support Perl-style negative lookaheads: (?!. When running the test suite against the existing regular expressions using the newly added regex validation, I got the following error:

=== RUN   TestMatcher_SugarMatchers
--- FAIL: TestMatcher_SugarMatchers (0.00s)
panic: Term: invalid regex "^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)": error parsing regexp: invalid or unsupported Perl syntax: `(?!` [recovered]
        panic: Term: invalid regex "^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)": error parsing regexp: invalid or unsupported Perl syntax: `(?!`

goroutine 38 [running]:
testing.tRunner.func1.2({0x104eb5980, 0xc00002b410})
        /usr/local/go/src/testing/testing.go:1734 +0x2bc
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.go:1737 +0x47c
panic({0x104eb5980?, 0xc00002b410?})
        /usr/local/go/src/runtime/panic.go:792 +0x124
github.com/pact-foundation/pact-go/v2/matchers.Term({0xc00000fcb6, 0xa}, {0x104e33f99, 0x96})
        /Users/nathan/Development/pact-go/matchers/matcher.go:118 +0x2a0
github.com/pact-foundation/pact-go/v2/matchers.Date()
        /Users/nathan/Development/pact-go/matchers/matcher.go:161 +0xe4
github.com/pact-foundation/pact-go/v2/matchers.TestMatcher_SugarMatchers(0xc00016d880)
        /Users/nathan/Development/pact-go/matchers/matcher_test.go:472 +0x8b4
testing.tRunner(0xc00016d880, 0x104f0f158)
        /usr/local/go/src/testing/testing.go:1792 +0x184
created by testing.(*T).Run in goroutine 1
        /usr/local/go/src/testing/testing.go:1851 +0x688
FAIL    github.com/pact-foundation/pact-go/v2/matchers  0.279s
FAIL
make: *** [test] Error 1

timeRegex = `^(T\d\d:\d\d(:\d\d)?(\.\d+)?(([+-]\d\d:\d\d)|Z)?)?$`
)

Expand Down Expand Up @@ -113,6 +113,13 @@ func Like(content interface{}) Matcher {
// Term specifies that the matching should generate a value
// and also match using a regular expression.
func Term(generate string, matcher string) Matcher {
re, err := regexp.Compile(matcher)
if err != nil {
panic(fmt.Sprintf("Term: invalid regex %q: %v", matcher, err))
}
if !re.MatchString(generate) {
panic(fmt.Sprintf("Term: example value %q does not match regex %q", generate, matcher))
}
return term{
Value: generate,
Regex: matcher,
Expand Down
13 changes: 13 additions & 0 deletions matchers/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMatcher_TermString(t *testing.T) {
Expand All @@ -35,6 +36,18 @@ func TestMatcher_TermGetValue(t *testing.T) {
}
}

func TestMatcher_TermInvalidRegex(t *testing.T) {
require.Panics(t, func() {
_ = Term("test", `(`)
}, "Term should panic when provided with an invalid regex")
}

func TestMatcher_TermMismatch(t *testing.T) {
require.Panics(t, func() {
_ = Term("notanumber", `\d+`)
}, "Term should panic when example value does not match regex")
}

func TestMatcher_LikeBasicString(t *testing.T) {
expected := formatJSON(`
{
Expand Down