diff --git a/be_example_test.go b/be_example_test.go index d9d365c..a6e4f08 100644 --- a/be_example_test.go +++ b/be_example_test.go @@ -35,10 +35,10 @@ func Example() { type mytype string var mystring mytype = "hello, world" - be.Match(t, `world`, mystring) // good - be.Match(t, `World`, mystring) // bad - be.Match(t, `^\W*$`, []byte("\a\b\x00\r\t")) // good - be.Match(t, `^\W*$`, []byte("\a\bo\r\t")) // bad + be.Match(t, `world`, mystring) // good + be.Match(t, `World`, mystring) // bad + be.Match(t, `^\W*$`, []byte("\a\b\x00\r\t")) // good + be.NoMatch(t, `^\W*$`, []byte("\a\b\x00\r\t")) // bad seq := strings.FieldsSeq("1 2 3 4") be.EqualLength(t, 4, seq) // good @@ -55,8 +55,8 @@ func Example() { // got: // got errors.Is(, permission denied) == false // got errors.As((O_o), **fs.PathError) == false - // /World/ !~ "hello, world" - // /^\W*$/ !~ "\a\bo\r\t" + // missing match: /World/ !~ "hello, world" + // unexpected match: /^\W*$/ =~ "\a\b\x00\r\t" // want len(seq) == 1; got at least 2 // want len(seq) >= 5; got 4 // want len(seq) >= 4; got 3 diff --git a/in.go b/in.go index da4e236..e55da6a 100644 --- a/in.go +++ b/in.go @@ -1,35 +1,24 @@ package be import ( - "bytes" - "reflect" - "strings" + "regexp" "testing" ) // In calls t.Fatalf if needle is not contained in the string or []byte haystack. +// +// Deprecated: Use Match(t, regexp.QuoteMeta(needle), haystack). +// +//go:fix inline func In[byteseq ~string | ~[]byte](t testing.TB, needle string, haystack byteseq) { - t.Helper() - if !in(needle, haystack) { - t.Fatalf("%q not in %q", needle, haystack) - } + Match(t, regexp.QuoteMeta(needle), haystack) } // NotIn calls t.Fatalf if needle is contained in the string or []byte haystack. +// +// Deprecated: Use NoMatch(t, regexp.QuoteMeta(needle), haystack). +// +//go:fix inline func NotIn[byteseq ~string | ~[]byte](t testing.TB, needle string, haystack byteseq) { - t.Helper() - if in(needle, haystack) { - t.Fatalf("%q in %q", needle, haystack) - } -} - -func in[byteseq ~string | ~[]byte](needle string, haystack byteseq) bool { - rv := reflect.ValueOf(haystack) - switch rv.Kind() { - case reflect.String: - return strings.Contains(rv.String(), needle) - case reflect.Slice: - return bytes.Contains(rv.Bytes(), []byte(needle)) - } - panic("unreachable") + NoMatch(t, regexp.QuoteMeta(needle), haystack) } diff --git a/match.go b/match.go index f4229b3..ad98855 100644 --- a/match.go +++ b/match.go @@ -13,7 +13,18 @@ func Match[byteseq ~string | ~[]byte](t testing.TB, pattern string, got byteseq) t.Helper() reg := regexp.MustCompile(pattern) if !match(reg, got) { - t.Fatalf("/%s/ !~ %q", pattern, got) + t.Fatalf("missing match: /%s/ !~ %q", pattern, got) + } +} + +// NoMatch calls t.Fatalf if got matches the [regexp] pattern. +// +// The pattern must compile. +func NoMatch[byteseq ~string | ~[]byte](t testing.TB, pattern string, got byteseq) { + t.Helper() + reg := regexp.MustCompile(pattern) + if match(reg, got) { + t.Fatalf("unexpected match: /%s/ =~ %q", pattern, got) } }