Skip to content
Merged
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
12 changes: 6 additions & 6 deletions be_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,8 +55,8 @@ func Example() {
// got: <nil>
// got errors.Is(<nil>, 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
Expand Down
33 changes: 11 additions & 22 deletions in.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 12 additions & 1 deletion match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down