forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear_expect_body_json.go
176 lines (156 loc) · 6.44 KB
/
clear_expect_body_json.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package hit
import (
"github.com/Eun/go-hit/errortrace"
"github.com/Eun/go-hit/internal"
"golang.org/x/xerrors"
)
// IClearExpectBody provides a clear functionality to remove previous steps from running in the Expect().Body().JSON() scope
type IClearExpectBodyJSON interface {
IStep
// Equal removes all previous Expect().Body().JSON().Equal() steps.
//
// If you specify an argument it will only remove the Expect().Body().Equal() steps matching that argument.
//
// Usage:
// Clear().Expect().Body().JSON().Equal() // will remove all Expect().Body().JSON().Equal() steps
// Clear().Expect().Body().JSON().Equal("Name") // will remove all Expect().Body().JSON().Equal("Name", ...) steps
// Clear().Expect().Body().JSON().Equal("Name", "Joe") // will remove all Expect().Body().JSON().Equal("Name", "Joe") steps
//
// Example:
// MustDo(
// Post("https://example.com"),
// Expect().Body().JSON().Equal("Name", "Joe"),
// Expect().Body().JSON().Equal("Id", 10),
// Clear().Expect().Body().JSON().Equal("Name"),
// Clear().Expect().Body().JSON().Equal("Id", 10),
// Expect().Body().JSON().Equal("Name", "Alice"),
// )
Equal(value ...interface{}) IStep
// NotEqual removes all previous Expect().Body().JSON().NotEqual() steps.
//
// If you specify an argument it will only remove the Expect().Body().NotEqual() steps matching that argument.
//
// Usage:
// Clear().Expect().Body().JSON().NotEqual() // will remove all Expect().Body().JSON().NotEqual() steps
// Clear().Expect().Body().JSON().NotEqual("Name") // will remove all Expect().Body().JSON().NotEqual("Name") steps
// Clear().Expect().Body().JSON().NotEqual("Name", "Joe") // will remove all Expect().Body().JSON().NotEqual("Name", "Joe") steps
//
// Example:
// Do(
// Post("https://example.com"),
// Expect().Body().JSON().NotEqual("Name", "Joe"),
// Expect().Body().JSON().NotEqual("Id", 10),
// Clear().Expect().Body().JSON().NotEqual("Name"),
// Clear().Expect().Body().JSON().NotEqual("Id", 10),
// Expect().Body().JSON().NotEqual("Name", "Alice"),
// )
NotEqual(value ...interface{}) IStep
// Contains removes all previous Expect().Body().JSON().Contains() steps.
//
// If you specify an argument it will only remove the Expect().Body().Contains() steps matching that argument.
//
// Usage:
// Clear().Expect().Body().JSON().Contains() // will remove all Expect().Body().JSON().Contains() steps
// Clear().Expect().Body().JSON().Contains("Name") // will remove all Expect().Body().JSON().Contains("Name") steps
// Clear().Expect().Body().JSON().Contains("Name", "Joe") // will remove all Expect().Body().JSON().Contains("Name", "Joe") steps
//
// Example:
// Do(
// Post("https://example.com"),
// Expect().Body().JSON().Contains("Name", "Joe"),
// Expect().Body().JSON().Contains("Id", 10),
// Clear().Expect().Body().JSON().Contains("Name"),
// Clear().Expect().Body().JSON().Contains("Id", 10),
// Expect().Body().JSON().Contains("Name", "Alice"),
// )
Contains(value ...interface{}) IStep
// NotContains removes all previous Expect().Body().JSON().NotContains() steps.
//
// If you specify an argument it will only remove the Expect().Body().NotContains() steps matching that argument.
//
// Usage:
// Clear().Expect().Body().JSON().NotContains() // will remove all Expect().Body().JSON().NotContains() steps
// Clear().Expect().Body().JSON().NotContains("Name") // will remove all Expect().Body().JSON().NotContains("Name") steps
// Clear().Expect().Body().JSON().NotContains("Name", "Joe") // will remove all Expect().Body().JSON().NotContains("Name", "Joe") steps
//
// Example:
// Do(
// Post("https://example.com"),
// Expect().Body().JSON().NotContains("Name", "Joe"),
// Expect().Body().JSON().NotContains("Id", 10),
// Clear().Expect().Body().JSON().NotContains("Name"),
// Clear().Expect().Body().JSON().NotContains("Id", 10),
// Expect().Body().JSON().NotContains("Name", "Alice"),
// )
NotContains(value ...interface{}) IStep
}
type clearExpectBodyJSON struct {
clearExpectBody IClearExpectBody
cleanPath clearPath
trace *errortrace.ErrorTrace
}
func newClearExpectBodyJSON(body IClearExpectBody, cleanPath clearPath, params []interface{}) IClearExpectBodyJSON {
if _, ok := internal.GetLastArgument(params); ok {
// this runs if we called Clear().Expect().Body().JSON(something)
return &finalClearExpectBodyJSON{
removeStep(cleanPath),
"only usable with Clear().Expect().Body().JSON() not with Clear().Expect().Body().JSON(value)",
}
}
return &clearExpectBodyJSON{
clearExpectBody: body,
cleanPath: cleanPath,
trace: ett.Prepare(),
}
}
func (jsn *clearExpectBodyJSON) when() StepTime {
return CleanStep
}
func (jsn *clearExpectBodyJSON) exec(hit Hit) error {
// this runs if we called Clear().Expect().Body().JSON()
if err := removeSteps(hit, jsn.clearPath()); err != nil {
return jsn.trace.Format(hit.Description(), err.Error())
}
return nil
}
func (jsn *clearExpectBodyJSON) clearPath() clearPath {
return jsn.cleanPath
}
func (jsn *clearExpectBodyJSON) Equal(value ...interface{}) IStep {
return removeStep(jsn.clearPath().Push("Equal", value))
}
func (jsn *clearExpectBodyJSON) NotEqual(value ...interface{}) IStep {
return removeStep(jsn.clearPath().Push("NotEqual", value))
}
func (jsn *clearExpectBodyJSON) Contains(value ...interface{}) IStep {
return removeStep(jsn.clearPath().Push("Contains", value))
}
func (jsn *clearExpectBodyJSON) NotContains(value ...interface{}) IStep {
return removeStep(jsn.clearPath().Push("NotContains", value))
}
type finalClearExpectBodyJSON struct {
IStep
message string
}
func (jsn *finalClearExpectBodyJSON) fail() IStep {
return &hitStep{
Trace: ett.Prepare(),
When: CleanStep,
ClearPath: nil,
Exec: func(hit Hit) error {
return xerrors.New(jsn.message)
},
}
}
func (jsn *finalClearExpectBodyJSON) Equal(...interface{}) IStep {
return jsn.fail()
}
func (jsn *finalClearExpectBodyJSON) NotEqual(...interface{}) IStep {
return jsn.fail()
}
func (jsn *finalClearExpectBodyJSON) Contains(...interface{}) IStep {
return jsn.fail()
}
func (jsn *finalClearExpectBodyJSON) NotContains(...interface{}) IStep {
return jsn.fail()
}