-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathassertion.go
274 lines (248 loc) · 9.09 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package venom
import (
"fmt"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
"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))
}
}
}
type assertionsApplied struct {
ok bool
errors []Failure
failures []Failure
systemout string
systemerr string
}
// applyChecks apply checks on result, return true if all assertions are OK, false otherwise
func applyChecks(executorResult *ExecutorResult, tc TestCase, stepNumber int, step TestStep, defaultAssertions *StepAssertions) assertionsApplied {
res := applyAssertions(*executorResult, tc, stepNumber, step, defaultAssertions)
if !res.ok {
return res
}
resExtract := applyExtracts(executorResult, step)
res.errors = append(res.errors, resExtract.errors...)
res.failures = append(res.failures, resExtract.failures...)
res.ok = resExtract.ok
return res
}
func applyAssertions(executorResult ExecutorResult, tc TestCase, stepNumber int, step TestStep, defaultAssertions *StepAssertions) assertionsApplied {
var sa StepAssertions
var errors []Failure
var failures []Failure
var systemerr, systemout string
if err := mapstructure.Decode(step, &sa); err != nil {
return assertionsApplied{
false,
[]Failure{{Value: RemoveNotPrintableChar(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(tc, stepNumber, assertion, executorResult)
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 = fmt.Sprintf("%v", executorResult["result.systemerr"])
}
if _, ok := executorResult["result.systemout"]; ok {
systemout = fmt.Sprintf("%v", executorResult["result.systemout"])
}
return assertionsApplied{isOK, errors, failures, systemout, systemerr}
}
func check(tc TestCase, stepNumber int, assertion string, executorResult ExecutorResult) (*Failure, *Failure) {
assert := splitAssertion(assertion)
if len(assert) < 2 {
return &Failure{Value: RemoveNotPrintableChar(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: RemoveNotPrintableChar(fmt.Sprintf("key '%s' does not exist in result of executor: %+v", assert[0], executorResult))}, nil
} else if assert[1] == "ShouldNotExist" {
return &Failure{Value: RemoveNotPrintableChar(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: RemoveNotPrintableChar(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], _ = stringToType(v, actual)
}
out := f(actual, args...)
if out != "" {
var prefix string
if stepNumber >= 0 {
prefix = fmt.Sprintf("testcase %s / step n°%d / assertion: %s", tc.Name, stepNumber, assertion)
} else {
// venom used as lib
prefix = fmt.Sprintf("assertion: %s", assertion)
}
return nil, &Failure{Value: RemoveNotPrintableChar(prefix + "\n" + out + "\n"), Result: executorResult}
}
return nil, nil
}
// splitAssertion splits the assertion string a, with support
// for quoted arguments.
func splitAssertion(a string) []string {
lastQuote := rune(0)
f := func(c rune) bool {
switch {
case c == lastQuote:
lastQuote = rune(0)
return false
case lastQuote != rune(0):
return false
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
default:
return unicode.IsSpace(c)
}
}
m := strings.FieldsFunc(a, f)
for i, e := range m {
first, _ := utf8.DecodeRuneInString(e)
last, _ := utf8.DecodeLastRuneInString(e)
if unicode.In(first, unicode.Quotation_Mark) && first == last {
m[i] = string([]rune(e)[1 : utf8.RuneCountInString(e)-1])
}
}
return m
}
// 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))
}
func stringToType(val string, valType interface{}) (interface{}, error) {
switch valType.(type) {
case bool:
return strconv.ParseBool(val)
case string:
return val, nil
case int:
return strconv.Atoi(val)
case int8:
return strconv.ParseInt(val, 10, 8)
case int16:
return strconv.ParseInt(val, 10, 16)
case int32:
return strconv.ParseInt(val, 10, 32)
case int64:
return strconv.ParseInt(val, 10, 64)
case uint:
newVal, err := strconv.Atoi(val)
return uint(newVal), err
case uint8:
return strconv.ParseUint(val, 10, 8)
case uint16:
return strconv.ParseUint(val, 10, 16)
case uint32:
return strconv.ParseUint(val, 10, 32)
case uint64:
strconv.ParseUint(val, 10, 64)
case float32:
iVal, err := strconv.ParseFloat(val, 32)
return float32(iVal), err
case float64:
iVal, err := strconv.ParseFloat(val, 64)
return float64(iVal), err
case time.Time:
return time.Parse(time.RFC3339, val)
case time.Duration:
return time.ParseDuration(val)
}
return val, nil
}