Skip to content

Commit

Permalink
enable support for string subtype regexp matching
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed May 19, 2024
1 parent b6383c1 commit 3c446da
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
16 changes: 8 additions & 8 deletions assert/Asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,17 @@ searching:
}
}

// Match will match an expression against a given value.
// Match will fail for both receiving an invalid expression
// MatchRegexp will match an expression against a given value.
// MatchRegexp will fail for both receiving an invalid expression
// or having the value not matched by the expression.
// If the expression is invalid, test will fail early, regardless if Should or Must was used.
func (a Asserter) Match(v, expr string, msg ...Message) {
func (a Asserter) MatchRegexp(v, expr string, msg ...Message) {
a.TB.Helper()
if a.toRegexp(expr).MatchString(v) {
return
}
a.fn(fmterror.Message{
Method: "Match",
Method: "MatchRegexp",
Cause: "failed to match the expected expression",
Message: toMsg(msg),
Values: []fmterror.Value{
Expand All @@ -527,15 +527,15 @@ func (a Asserter) Match(v, expr string, msg ...Message) {
})
}

// NotMatch will check if an expression is not matching a given value.
// NotMatch will fail the test early for receiving an invalid expression.
func (a Asserter) NotMatch(v, expr string, msg ...Message) {
// NotMatchRegexp will check if an expression is not matching a given value.
// NotMatchRegexp will fail the test early for receiving an invalid expression.
func (a Asserter) NotMatchRegexp(v, expr string, msg ...Message) {
a.TB.Helper()
if !a.toRegexp(expr).MatchString(v) {
return
}
a.fn(fmterror.Message{
Method: "NotMatch",
Method: "NotMatchRegexp",
Cause: "value is matching the expression",
Message: toMsg(msg),
Values: []fmterror.Value{
Expand Down
15 changes: 8 additions & 7 deletions assert/Asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"go.llib.dev/testcase"
"io"
"math/big"
"net"
Expand All @@ -15,6 +14,8 @@ import (
"testing/iotest"
"time"

"go.llib.dev/testcase"

"go.llib.dev/testcase/internal/doubles"
"go.llib.dev/testcase/sandbox"

Expand Down Expand Up @@ -954,23 +955,23 @@ func TestAsserter_Match(t *testing.T) {
t.Run("rgx is incorrect", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).Match("val", `[a-z`)
asserter(dtb).MatchRegexp("val", `[a-z`)
})
assert.True(t, dtb.IsFailed)
assert.False(t, out.OK)
})
t.Run("when value doesn't match the expression", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).Match("42", `[a-z]+`)
asserter(dtb).MatchRegexp("42", `[a-z]+`)
})
assert.True(t, dtb.IsFailed)
assert.True(t, out.OK)
})
t.Run("when value match the expression", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).Match("42", `[0-9]+`)
asserter(dtb).MatchRegexp("42", `[0-9]+`)
})
assert.False(t, dtb.IsFailed)
assert.True(t, out.OK)
Expand All @@ -981,23 +982,23 @@ func TestAsserter_NotMatch(t *testing.T) {
t.Run("rgx is incorrect", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).NotMatch("val", `[a-z`)
asserter(dtb).NotMatchRegexp("val", `[a-z`)
})
assert.True(t, dtb.IsFailed)
assert.False(t, out.OK)
})
t.Run("when value doesn't match the expression", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).NotMatch("42", `[a-z]+`)
asserter(dtb).NotMatchRegexp("42", `[a-z]+`)
})
assert.False(t, dtb.IsFailed)
assert.True(t, out.OK)
})
t.Run("when value match the expression", func(t *testing.T) {
dtb := &doubles.TB{}
out := sandbox.Run(func() {
asserter(dtb).NotMatch("42", `[0-9]+`)
asserter(dtb).NotMatchRegexp("42", `[0-9]+`)
})
assert.True(t, dtb.IsFailed)
assert.True(t, out.OK)
Expand Down
23 changes: 12 additions & 11 deletions assert/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"go.llib.dev/testcase/random"
"math/rand"
"strings"
"testing"
"testing/iotest"
"time"

"go.llib.dev/testcase/random"

"go.llib.dev/testcase"
"go.llib.dev/testcase/assert"
)
Expand Down Expand Up @@ -733,28 +734,28 @@ func ExampleAsserter_OneOf() {

func ExampleMatch() {
var tb testing.TB
assert.Match(tb, "42", "[0-9]+")
assert.Match(tb, "forty-two", "[a-z]+")
assert.Match(tb, []byte("forty-two"), "[a-z]+")
assert.MatchRegexp(tb, "42", "[0-9]+")
assert.MatchRegexp(tb, "forty-two", "[a-z]+")
assert.MatchRegexp(tb, []byte("forty-two"), "[a-z]+")
}

func ExampleAsserter_Match() {
var tb testing.TB
assert.Must(tb).Match("42", "[0-9]+")
assert.Must(tb).Match("forty-two", "[a-z]+")
assert.Must(tb).MatchRegexp("42", "[0-9]+")
assert.Must(tb).MatchRegexp("forty-two", "[a-z]+")
}

func ExampleNotMatch() {
var tb testing.TB
assert.NotMatch(tb, "42", "^[a-z]+")
assert.NotMatch(tb, "forty-two", "^[0-9]+")
assert.NotMatch(tb, []byte("forty-two"), "^[0-9]+")
assert.NotMatchRegexp(tb, "42", "^[a-z]+")
assert.NotMatchRegexp(tb, "forty-two", "^[0-9]+")
assert.NotMatchRegexp(tb, []byte("forty-two"), "^[0-9]+")
}

func ExampleAsserter_NotMatch() {
var tb testing.TB
assert.Must(tb).NotMatch("42", "^[a-z]+")
assert.Must(tb).NotMatch("forty-two", "^[0-9]+")
assert.Must(tb).NotMatchRegexp("42", "^[a-z]+")
assert.Must(tb).NotMatchRegexp("forty-two", "^[0-9]+")
}

func ExampleAsserter_Eventually() {
Expand Down
8 changes: 4 additions & 4 deletions assert/pkgfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ func NotWithin(tb testing.TB, timeout time.Duration, blk func(context.Context),
Must(tb).NotWithin(timeout, blk, msg...)
}

func Match[T string | []byte](tb testing.TB, v T, expr string, msg ...Message) {
func MatchRegexp[T ~string | []byte](tb testing.TB, v T, expr string, msg ...Message) {
tb.Helper()
Must(tb).Match(string(v), expr, msg...)
Must(tb).MatchRegexp(string(v), expr, msg...)
}

func NotMatch[T string | []byte](tb testing.TB, v T, expr string, msg ...Message) {
func NotMatchRegexp[T ~string | []byte](tb testing.TB, v T, expr string, msg ...Message) {
tb.Helper()
Must(tb).NotMatch(string(v), expr, msg...)
Must(tb).NotMatchRegexp(string(v), expr, msg...)
}

func Eventually[T time.Duration | int](tb testing.TB, durationOrCount T, blk func(it It)) {
Expand Down
38 changes: 28 additions & 10 deletions assert/pkgfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,47 +360,65 @@ func TestPublicFunctions(t *testing.T) {
Desc: ".Match - happy",
Failed: false,
Assert: func(tb testing.TB) {
assert.Match(tb, "42", "[0-9]+")
assert.Match(tb, "forty-two", "[a-z]+")
assert.Match(tb, []byte("forty-two"), "[a-z]+")
assert.MatchRegexp(tb, "42", "[0-9]+")
assert.MatchRegexp(tb, "forty-two", "[a-z]+")
assert.MatchRegexp(tb, []byte("forty-two"), "[a-z]+")
},
},
{
Desc: ".Match - happy - subtype",
Failed: false,
Assert: func(tb testing.TB) {
type S string
assert.MatchRegexp(tb, S("42"), "[0-9]+")
assert.MatchRegexp(tb, S("forty-two"), "[a-z]+")
},
},
{
Desc: ".Match - rainy value",
Failed: true,
Assert: func(tb testing.TB) {
assert.Match(tb, "42", "[a-z]+")
assert.MatchRegexp(tb, "42", "[a-z]+")
},
},
{
Desc: ".Match - rainy pattern",
Failed: true,
Assert: func(tb testing.TB) {
assert.Match(tb, "42", "[0-9")
assert.MatchRegexp(tb, "42", "[0-9")
},
},
// .NotMatch
{
Desc: ".NotMatch - happy",
Failed: false,
Assert: func(tb testing.TB) {
assert.NotMatch(tb, "forty-two", "^[0-9]+")
assert.NotMatch(tb, "42", "^[a-z]+")
assert.NotMatch(tb, []byte("forty-two"), "^[0-9]+")
assert.NotMatchRegexp(tb, "forty-two", "^[0-9]+")
assert.NotMatchRegexp(tb, "42", "^[a-z]+")
assert.NotMatchRegexp(tb, []byte("forty-two"), "^[0-9]+")
},
},
{
Desc: ".NotMatch - happy - subtype",
Failed: false,
Assert: func(tb testing.TB) {
type S string
assert.NotMatchRegexp(tb, S("forty-two"), "^[0-9]+")
assert.NotMatchRegexp(tb, S("42"), "^[a-z]+")
},
},
{
Desc: ".NotMatch - rainy value",
Failed: true,
Assert: func(tb testing.TB) {
assert.NotMatch(tb, "42", "[0-9]+")
assert.NotMatchRegexp(tb, "42", "[0-9]+")
},
},
{
Desc: ".NotMatch - rainy pattern",
Failed: true,
Assert: func(tb testing.TB) {
assert.NotMatch(tb, "forty-two", "[0-9")
assert.NotMatchRegexp(tb, "forty-two", "[0-9")
},
},
// .Eventually
Expand Down

0 comments on commit 3c446da

Please sign in to comment.