From 3c446dac0032cc75db05491cedf39e0310e42327 Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Sun, 19 May 2024 22:19:24 +0200 Subject: [PATCH] enable support for string subtype regexp matching --- assert/Asserter.go | 16 ++++++++-------- assert/Asserter_test.go | 15 ++++++++------- assert/example_test.go | 23 ++++++++++++----------- assert/pkgfunc.go | 8 ++++---- assert/pkgfunc_test.go | 38 ++++++++++++++++++++++++++++---------- 5 files changed, 60 insertions(+), 40 deletions(-) diff --git a/assert/Asserter.go b/assert/Asserter.go index 27b2bda..5cb7522 100644 --- a/assert/Asserter.go +++ b/assert/Asserter.go @@ -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{ @@ -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{ diff --git a/assert/Asserter_test.go b/assert/Asserter_test.go index eb76ff5..9b741c9 100644 --- a/assert/Asserter_test.go +++ b/assert/Asserter_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "go.llib.dev/testcase" "io" "math/big" "net" @@ -15,6 +14,8 @@ import ( "testing/iotest" "time" + "go.llib.dev/testcase" + "go.llib.dev/testcase/internal/doubles" "go.llib.dev/testcase/sandbox" @@ -954,7 +955,7 @@ 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) @@ -962,7 +963,7 @@ func TestAsserter_Match(t *testing.T) { 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) @@ -970,7 +971,7 @@ func TestAsserter_Match(t *testing.T) { 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) @@ -981,7 +982,7 @@ 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) @@ -989,7 +990,7 @@ func TestAsserter_NotMatch(t *testing.T) { 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) @@ -997,7 +998,7 @@ func TestAsserter_NotMatch(t *testing.T) { 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) diff --git a/assert/example_test.go b/assert/example_test.go index fb08115..a219814 100644 --- a/assert/example_test.go +++ b/assert/example_test.go @@ -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" ) @@ -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() { diff --git a/assert/pkgfunc.go b/assert/pkgfunc.go index 68cdf3c..3653a29 100644 --- a/assert/pkgfunc.go +++ b/assert/pkgfunc.go @@ -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)) { diff --git a/assert/pkgfunc_test.go b/assert/pkgfunc_test.go index 75fe84f..3f19671 100644 --- a/assert/pkgfunc_test.go +++ b/assert/pkgfunc_test.go @@ -360,23 +360,32 @@ 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 @@ -384,23 +393,32 @@ func TestPublicFunctions(t *testing.T) { 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