-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.go
79 lines (74 loc) · 1.8 KB
/
bytes.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package assert
import (
"bytes"
"fmt"
"testing"
)
// BytesEqual asserts that b1 and b2 are equal.
// It uses [bytes.Equal] to compare the two byte slices.
//
//nolint:thelper // It's called below.
func BytesEqual(tb testing.TB, b1, b2 []byte, opts ...Option) bool {
ok := bytes.Equal(b1, b2)
if !ok {
tb.Helper()
Fail(
tb,
"bytes_equal",
fmt.Sprintf("not equal:\nb1 = %s\nb2 = %s", ValueStringer(b1), ValueStringer(b2)),
opts...,
)
}
return ok
}
// BytesNotEqual asserts that b1 and b2 are not equal.
// It uses [bytes.Equal] to compare the two byte slices.
//
//nolint:thelper // It's called below.
func BytesNotEqual(tb testing.TB, b1, b2 []byte, opts ...Option) bool {
ok := !bytes.Equal(b1, b2)
if !ok {
tb.Helper()
Fail(
tb,
"bytes_not_equal",
fmt.Sprintf("equal:\nb1 = %s\nb2 = %s", ValueStringer(b1), ValueStringer(b2)),
opts...,
)
}
return ok
}
// BytesContains asserts that b contains subslice.
// It uses [bytes.Contains] to check if subslice is contained in b.
//
//nolint:thelper // It's called below.
func BytesContains(tb testing.TB, b, subslice []byte, opts ...Option) bool {
ok := bytes.Contains(b, subslice)
if !ok {
tb.Helper()
Fail(
tb,
"bytes_contains",
fmt.Sprintf("not contains:\nb = %s\nsubslice = %s", ValueStringer(b), ValueStringer(subslice)),
opts...,
)
}
return ok
}
// BytesNotContains asserts that b does not contain subslice.
// It uses [bytes.Contains] to check if subslice is contained in b.
//
//nolint:thelper // It's called below.
func BytesNotContains(tb testing.TB, b, subslice []byte, opts ...Option) bool {
ok := !bytes.Contains(b, subslice)
if !ok {
tb.Helper()
Fail(
tb,
"bytes_not_contains",
fmt.Sprintf("contains:\nb = %s\nsubslice = %s", ValueStringer(b), ValueStringer(subslice)),
opts...,
)
}
return ok
}