-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
clear.go
134 lines (120 loc) · 3.81 KB
/
clear.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
// +build !generate
package hit
import (
"fmt"
"strings"
"golang.org/x/xerrors"
)
// IClear provides a clear functionality to remove previous steps from running.
type IClear interface {
// Send removes all previous steps chained to Send()
// e.g. Send().Body().String("Hello World") or Send().Headers("Content-Type", "application/json").
//
// Usage:
// // will remove all previous steps chained to Send() e.g. Send().Body().String("Hello World") or
// // Send().Headers("Content-Type", "application/json")
// Clear().Send()
//
// // will remove all previous steps chained to Send().Body() e.g. Send().Body().String("Hello World") or
// // Send().Body().Int(127)
// Clear().Send().Body()
//
// // will remove all previous Send().Body().String() steps e.g. Send().Body().String("Hello World") or
// // Send().Body().String("Hello Earth")
// Clear().Send().Body().String()
//
// // will remove all previous Send().Body().String("Hello World") steps
// Clear().Send().Body().String("Hello World")
//
// Example:
// MustDo(
// Post("https://example.com"),
// Send().Body().String("Hello Earth"),
// Clear().Send(),
// Send().Body().String("Hello World"),
// )
Send() IClearSend
// Expect removes all previous steps chained to Expect()
// e.g. Expect().Body().String().Equal("Hello World") or Expect().Headers("Content-Type").Equal("application/json").
//
// Usage:
// // will remove all previous steps chained to Expect() e.g. Expect().Body().String().Equal("Hello World")
// // or Expect().Status().Equal(200)
// Clear().Expect()
//
// // will remove all previous steps chained to Expect().Body().String() e.g.
// // Expect().Body().String().Equal("Hello World") or Expect().Body().String().Contains("Hello")
// Clear().Expect().Body().String()
//
// // will remove all previous Expect().Body().String().Equal() steps e.g.
// // Expect().Body().String().Equal("Hello World") or Expect().Body().String().Contains("Hello")
// Clear().Expect().Body().String().Equal()
//
// // will remove all previous Expect().Body().String().Equal("Hello World") steps
// Clear().Expect().Body().String().Equal("Hello World")
//
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Body().String().Equal("Hello Earth"),
// Clear().Expect(),
// Expect().Body().String().Equal("Hello World"),
// )
Expect() IClearExpect
}
type clear struct {
cp callPath
}
func newClear(cp callPath) IClear {
return &clear{
cp: cp,
}
}
func (clr *clear) Send() IClearSend {
return newClearSend(clr.cp.Push("Send", nil))
}
func (clr *clear) Expect() IClearExpect {
return newClearExpect(clr.cp.Push("Expect", nil))
}
func removeSteps(hit Hit, path callPath) error {
path = path[1:] // remove Clean()
var stepsToRemove []IStep
steps := hit.Steps()
availableSteps := make([]IStep, 0, len(steps))
for _, step := range steps {
if step == hit.CurrentStep() {
break
}
p := step.callPath()
if p == nil {
continue
}
availableSteps = append(availableSteps, step)
if p.Contains(path) {
stepsToRemove = append(stepsToRemove, step)
}
}
if len(stepsToRemove) == 0 {
var sb strings.Builder
fmt.Fprintf(&sb, "unable to find a step with %s\n", path.CallString(true))
if len(availableSteps) > 0 {
fmt.Fprintf(&sb, "got these steps:\n")
for _, step := range availableSteps {
fmt.Fprintf(&sb, "\t%s\n", step.callPath().CallString(true))
}
}
return xerrors.New(sb.String())
}
hit.RemoveSteps(stepsToRemove...)
return nil
}
func removeStep(cleanPath callPath) *hitStep {
return &hitStep{
Trace: ett.Prepare(),
When: cleanStep,
CallPath: cleanPath,
Exec: func(hit *hitImpl) error {
return removeSteps(hit, cleanPath)
},
}
}