-
Notifications
You must be signed in to change notification settings - Fork 2
/
result.go
124 lines (107 loc) · 3.57 KB
/
result.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
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package result
import (
"errors"
"fmt"
gdterrors "github.com/gdt-dev/gdt/errors"
)
// Result is returned from a `Evaluable.Eval` execution. It serves two
// purposes:
//
// 1) to return an error, if any, from the Eval execution. This error will
// *always* be a `gdterrors.RuntimeError`. Failed assertions are not errors.
// 2) to pass back information about the Run that can be injected into the
// context's `PriorRun` cache. Some plugins, e.g. the gdt-http plugin, use
// cached data from a previous run in order to construct current Run fixtures.
// In the case of the gdt=http plugin, the previous `nethttp.Response` is
// returned in the Result and the `Scenario.Run` method injects that
// information into the context that is supplied to the next Spec's `Run`.
type Result struct {
// err is any error that was returned from the Evaluable's execution. This
// is guaranteed to be a `gdterrors.RuntimeError`.
err error
// failures is the collection of error messages from assertion failures
// that occurred during Eval(). These are *not* `gdterrors.RuntimeError`.
failures []error
// data is a map, keyed by plugin name, of data about the spec run. Plugins
// can place anything they want in here and grab it from the context with
// the `gdtcontext.PriorRunData()` function. Plugins are responsible for
// clearing and setting any used prior run data.
data map[string]interface{}
}
// HasRuntimeError returns true if the Eval() returned a runtime error, false
// otherwise.
func (r *Result) HasRuntimeError() bool {
return r.err != nil
}
// RuntimeError returns the runtime error
func (r *Result) RuntimeError() error {
return r.err
}
// HasData returns true if any of the run data has been set, false otherwise.
func (r *Result) HasData() bool {
return r.data != nil
}
// Data returns the raw run data saved in the result
func (r *Result) Data() map[string]interface{} {
return r.data
}
// Failed returns true if any assertion failed during Eval(), false otherwise.
func (r *Result) Failed() bool {
return len(r.failures) > 0
}
// Failures returns the collection of assertion failures that occurred during
// Eval().
func (r *Result) Failures() []error {
return r.failures
}
// SetData sets a value in the result's run data cache.
func (r *Result) SetData(
key string,
val interface{},
) {
if r.data == nil {
r.data = map[string]interface{}{}
}
r.data[key] = val
}
// SetFailures sets the result's collection of assertion failures.
func (r *Result) SetFailures(failures ...error) {
r.failures = failures
}
type ResultModifier func(*Result)
// WithRuntimeError modifies the Result with the supplied error
func WithRuntimeError(err error) ResultModifier {
if !errors.Is(err, gdterrors.RuntimeError) {
msg := fmt.Sprintf("expected %s to be a gdterrors.RuntimeError", err)
// panic here because a plugin author incorrectly implemented their
// plugin Spec's Eval() method...
panic(msg)
}
return func(r *Result) {
r.err = err
}
}
// WithData modifies the Result with the supplied run data key and value
func WithData(key string, val interface{}) ResultModifier {
return func(r *Result) {
r.SetData(key, val)
}
}
// WithFailures modifies the Result the supplied collection of assertion
// failures
func WithFailures(failures ...error) ResultModifier {
return func(r *Result) {
r.SetFailures(failures...)
}
}
// New returns a new Result
func New(mods ...ResultModifier) *Result {
r := &Result{}
for _, mod := range mods {
mod(r)
}
return r
}