-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion.go
181 lines (158 loc) · 6.94 KB
/
assertion.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
177
178
179
180
181
package venom
import (
"fmt"
"strings"
"github.com/fsamin/go-dump"
"github.com/mitchellh/mapstructure"
"github.com/smartystreets/assertions"
)
type testingT struct {
ErrorS []string
}
func (t *testingT) Error(args ...interface{}) {
for _, a := range args {
switch v := a.(type) {
case string:
t.ErrorS = append(t.ErrorS, v)
default:
t.ErrorS = append(t.ErrorS, fmt.Sprintf("%s", v))
}
}
}
// applyChecks apply checks on result, return true if all assertions are OK, false otherwise
func applyChecks(executorResult *ExecutorResult, step TestStep, defaultAssertions *StepAssertions, l Logger) (bool, []Failure, []Failure, string, string) {
isOK, errors, failures, systemout, systemerr := applyAssertions(*executorResult, step, defaultAssertions, l)
if !isOK {
return isOK, errors, failures, systemout, systemerr
}
isOKExtract, errorsExtract, failuresExtract := applyExtracts(executorResult, step, l)
errors = append(errors, errorsExtract...)
failures = append(failures, failuresExtract...)
return isOKExtract, errors, failures, systemout, systemerr
}
func applyAssertions(executorResult ExecutorResult, step TestStep, defaultAssertions *StepAssertions, l Logger) (bool, []Failure, []Failure, string, string) {
var sa StepAssertions
var errors []Failure
var failures []Failure
var systemerr, systemout string
if err := mapstructure.Decode(step, &sa); err != nil {
return false, []Failure{{Value: fmt.Sprintf("error decoding assertions: %s", err)}}, failures, systemout, systemerr
}
if len(sa.Assertions) == 0 && defaultAssertions != nil {
sa = *defaultAssertions
}
isOK := true
for _, assertion := range sa.Assertions {
errs, fails := check(assertion, executorResult, l)
if errs != nil {
errors = append(errors, *errs)
isOK = false
}
if fails != nil {
failures = append(failures, *fails)
isOK = false
}
}
if _, ok := executorResult["result.systemerr"]; ok {
systemerr = executorResult["result.systemerr"]
}
if _, ok := executorResult["result.systemout"]; ok {
systemout = executorResult["result.systemout"]
}
return isOK, errors, failures, systemout, systemerr
}
func check(assertion string, executorResult ExecutorResult, l Logger) (*Failure, *Failure) {
assert := strings.Split(assertion, " ")
if len(assert) < 2 {
return &Failure{Value: fmt.Sprintf("invalid assertion '%s' len:'%d'", assertion, len(assert))}, nil
}
actual, ok := executorResult[assert[0]]
if !ok {
if assert[1] == "ShouldNotExist" {
return nil, nil
}
return &Failure{Value: fmt.Sprintf("key '%s' does not exist in result of executor: %+v", assert[0], executorResult)}, nil
} else if assert[1] == "ShouldNotExist" {
return &Failure{Value: fmt.Sprintf("key '%s' should not exist in result of executor. Value: %+v", assert[0], actual)}, nil
}
f, ok := assertMap[assert[1]]
if !ok {
return &Failure{Value: fmt.Sprintf("Method not found '%s'", assert[1])}, nil
}
args := make([]interface{}, len(assert[2:]))
for i, v := range assert[2:] { // convert []string to []interface for assertions.func()...
args[i] = v
}
out := f(actual, args...)
if out != "" {
prefix := "assertion: " + assertion
sdump, _ := dump.Sdump(executorResult)
return nil, &Failure{Value: prefix + "\n" + out + "\n" + sdump}
}
return nil, nil
}
// assertMap contains list of assertions func
var assertMap = map[string]func(actual interface{}, expected ...interface{}) string{
// "ShouldNotExist" see func check
"ShouldEqual": assertions.ShouldEqual,
"ShouldNotEqual": assertions.ShouldNotEqual,
"ShouldAlmostEqual": assertions.ShouldAlmostEqual,
"ShouldNotAlmostEqual": assertions.ShouldNotAlmostEqual,
"ShouldResemble": assertions.ShouldResemble,
"ShouldNotResemble": assertions.ShouldNotResemble,
"ShouldPointTo": assertions.ShouldPointTo,
"ShouldNotPointTo": assertions.ShouldNotPointTo,
"ShouldBeNil": assertions.ShouldBeNil,
"ShouldNotBeNil": assertions.ShouldNotBeNil,
"ShouldBeTrue": assertions.ShouldBeTrue,
"ShouldBeFalse": assertions.ShouldBeFalse,
"ShouldBeZeroValue": assertions.ShouldBeZeroValue,
"ShouldBeGreaterThan": assertions.ShouldBeGreaterThan,
"ShouldBeGreaterThanOrEqualTo": assertions.ShouldBeGreaterThanOrEqualTo,
"ShouldBeLessThan": assertions.ShouldBeLessThan,
"ShouldBeLessThanOrEqualTo": assertions.ShouldBeLessThanOrEqualTo,
"ShouldBeBetween": assertions.ShouldBeBetween,
"ShouldNotBeBetween": assertions.ShouldNotBeBetween,
"ShouldBeBetweenOrEqual": assertions.ShouldBeBetweenOrEqual,
"ShouldNotBeBetweenOrEqual": assertions.ShouldNotBeBetweenOrEqual,
"ShouldContain": assertions.ShouldContain,
"ShouldNotContain": assertions.ShouldNotContain,
"ShouldContainKey": assertions.ShouldContainKey,
"ShouldNotContainKey": assertions.ShouldNotContainKey,
"ShouldBeIn": assertions.ShouldBeIn,
"ShouldNotBeIn": assertions.ShouldNotBeIn,
"ShouldBeEmpty": assertions.ShouldBeEmpty,
"ShouldNotBeEmpty": assertions.ShouldNotBeEmpty,
"ShouldHaveLength": assertions.ShouldHaveLength,
"ShouldStartWith": assertions.ShouldStartWith,
"ShouldNotStartWith": assertions.ShouldNotStartWith,
"ShouldEndWith": assertions.ShouldEndWith,
"ShouldNotEndWith": assertions.ShouldNotEndWith,
"ShouldBeBlank": assertions.ShouldBeBlank,
"ShouldNotBeBlank": assertions.ShouldNotBeBlank,
"ShouldContainSubstring": ShouldContainSubstring,
"ShouldNotContainSubstring": assertions.ShouldNotContainSubstring,
"ShouldEqualWithout": assertions.ShouldEqualWithout,
"ShouldEqualTrimSpace": assertions.ShouldEqualTrimSpace,
"ShouldHappenBefore": assertions.ShouldHappenBefore,
"ShouldHappenOnOrBefore": assertions.ShouldHappenOnOrBefore,
"ShouldHappenAfter": assertions.ShouldHappenAfter,
"ShouldHappenOnOrAfter": assertions.ShouldHappenOnOrAfter,
"ShouldHappenBetween": assertions.ShouldHappenBetween,
"ShouldHappenOnOrBetween": assertions.ShouldHappenOnOrBetween,
"ShouldNotHappenOnOrBetween": assertions.ShouldNotHappenOnOrBetween,
"ShouldHappenWithin": assertions.ShouldHappenWithin,
"ShouldNotHappenWithin": assertions.ShouldNotHappenWithin,
"ShouldBeChronological": assertions.ShouldBeChronological,
}
// ShouldContainSubstring receives exactly more than 2 string parameters and ensures that the first contains the second as a substring.
func ShouldContainSubstring(actual interface{}, expected ...interface{}) string {
if len(expected) == 1 {
return assertions.ShouldContainSubstring(actual, expected...)
}
var arg string
for _, e := range expected {
arg += fmt.Sprintf("%v ", e)
}
return assertions.ShouldContainSubstring(actual, strings.TrimSpace(arg))
}