-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathin.go
36 lines (32 loc) · 885 Bytes
/
in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package be
import (
"bytes"
"reflect"
"strings"
"testing"
)
// In calls t.Fatalf if needle is not contained in the string or []byte haystack.
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)
}
}
// NotIn calls t.Fatalf if needle is contained in the string or []byte haystack.
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))
default:
panic("unreachable")
}
}