-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
expect_bytes.go
111 lines (97 loc) · 2.84 KB
/
expect_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package hit
import (
"bytes"
"github.com/Eun/go-hit/internal/minitest"
)
// IExpectBytes provides assertions for the byte slice type.
type IExpectBytes interface {
// Equal expects the body to be equal to the specified value.
//
// Usage:
// Expect().Body().Bytes().Equal([]byte("Hello World"))
Equal(value []byte) IStep
// NotEqual expects the body to be not equal to the specified value
//
// Usage:
// Expect().Body().Bytes().NotEqual([]byte("Hello World"))
NotEqual(value []byte) IStep
// Contains expects the body to contain the specified value.
//
// Usage:
// Expect().Body().Bytes().Contains([]byte("Hello World"))
Contains(value []byte) IStep
// NotContains expects the body to not contain the specified value.
//
// Usage:
// Expect().Body().Bytes().NotContains([]byte("Hello World"))
NotContains(value []byte) IStep
// Len provides assertions to the body size.
//
// Usage:
// Expect().Body().Bytes().Len().Equal(10)
Len() IExpectInt
}
type expectBytesValueCallback func(hit Hit) []byte
type expectBytes struct {
cleanPath callPath
valueCallback expectBytesValueCallback
}
func newExpectBytes(cleanPath callPath, valueCallback expectBytesValueCallback) IExpectBytes {
return &expectBytes{
cleanPath: cleanPath,
valueCallback: valueCallback,
}
}
func (v *expectBytes) Equal(value []byte) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("Equal", []interface{}{value}),
Exec: func(hit *hitImpl) error {
return minitest.Equal(v.valueCallback(hit), value)
},
}
}
func (v *expectBytes) NotEqual(value []byte) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("NotEqual", []interface{}{value}),
Exec: func(hit *hitImpl) error {
return minitest.NotEqual(v.valueCallback(hit), value)
},
}
}
func (v *expectBytes) Contains(value []byte) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("Contains", []interface{}{value}),
Exec: func(hit *hitImpl) error {
buf := v.valueCallback(hit)
if !bytes.Contains(buf, value) {
return minitest.Errorf(`%s does not contain %s`, minitest.PrintValue(buf), minitest.PrintValue(value))
}
return nil
},
}
}
func (v *expectBytes) NotContains(value []byte) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: ExpectStep,
CallPath: v.cleanPath.Push("NotContains", []interface{}{value}),
Exec: func(hit *hitImpl) error {
buf := v.valueCallback(hit)
if bytes.Contains(buf, value) {
return minitest.Errorf(`%s should not contain %s`, minitest.PrintValue(buf), minitest.PrintValue(value))
}
return nil
},
}
}
func (v *expectBytes) Len() IExpectInt {
return newExpectInt(v.cleanPath.Push("Len", nil), func(hit Hit) int {
return len(v.valueCallback(hit))
})
}