-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertion.go
428 lines (390 loc) · 13 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package venom
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/fatih/color"
"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 getLastValidResultFromPath(path string, r ExecutorResult) (string, string) {
tokens := strings.Split(path, ".")
for i := len(tokens); i >= 0; i-- {
newPath := strings.Join(tokens[:i], ".")
if res, ok := r[newPath]; ok {
encodedData, err := json.MarshalIndent(res, "", " ")
if err != nil {
return RemoveNotPrintableChar(fmt.Sprintf("%+v", res)), RemoveNotPrintableChar(newPath)
}
return RemoveNotPrintableChar(string(encodedData)), RemoveNotPrintableChar(newPath)
}
}
encodedData, err := json.MarshalIndent(r, "", " ")
if err != nil {
return fmt.Sprintf("%+v", r), "."
}
return string(encodedData), "."
}
func check(tc TestCase, stepNumber int, assertion string, executorResult ExecutorResult) (*Failure, *Failure) {
lineNumberSuffix := ""
assert := splitAssertion(assertion)
if len(assert) < 2 {
if lineNumber, err := findLineNumber(tc.Classname, tc.Name, stepNumber, assertion); err == nil && lineNumber > 0 {
lineNumberSuffix = fmt.Sprintf(":%d", lineNumber)
}
return &Failure{
Value: color.YellowString(
"Failure in %q\nIn test case %q, at step %d\nInvalid assertion %q length should be greater than 2\n",
tc.Classname+lineNumberSuffix,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assertion),
),
}, nil
}
var executorResultKeys []string
for k := range executorResult {
executorResultKeys = append(executorResultKeys, k)
}
actual, ok := executorResult[assert[0]]
if !ok {
if assert[1] == "ShouldNotExist" {
return nil, nil
}
if lineNumber, err := findLineNumber(tc.Classname, tc.Name, stepNumber, assertion); err == nil && lineNumber > 0 {
lineNumberSuffix = fmt.Sprintf(":%d", lineNumber)
}
data, path := getLastValidResultFromPath(assert[0], executorResult)
return &Failure{
Value: fmt.Sprintf(
color.YellowString(
"Failure in %q\nIn test case %q, at step %d\nCould not access %q in assertion %q.\nThis is what we have at %q:\n",
tc.Classname+lineNumberSuffix,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assert[0]),
RemoveNotPrintableChar(assertion),
path,
) + data + "\n",
),
}, nil
} else if assert[1] == "ShouldNotExist" {
if lineNumber, err := findLineNumber(tc.Classname, tc.Name, stepNumber, assertion); err == nil && lineNumber > 0 {
lineNumberSuffix = fmt.Sprintf(":%d", lineNumber)
}
paths := strings.Split(assert[0], ".")
if len(paths) > 0 {
paths = paths[:len(paths)-1]
}
data, path := getLastValidResultFromPath(strings.Join(paths, "."), executorResult)
return &Failure{
Value: fmt.Sprintf(
color.YellowString(
"Failure in %q\nIn test case %q, at step %d\nIn assertion %q, key %q should not exist.\nThis is what we have at %q:\n",
tc.Classname+lineNumberSuffix,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assertion),
RemoveNotPrintableChar(assert[0]),
path,
) + data + "\n",
),
}, nil
}
f, ok := assertMap[assert[1]]
if !ok {
if lineNumber, err := findLineNumber(tc.Classname, tc.Name, stepNumber, assertion); err == nil && lineNumber > 0 {
lineNumberSuffix = fmt.Sprintf(":%d", lineNumber)
}
return &Failure{
Value: color.YellowString(
"Failure in %q\nIn test case %q, at step %d\nMethod %q in assertion %q is not supported\n",
tc.Classname+lineNumberSuffix,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assert[1]),
RemoveNotPrintableChar(assertion),
),
}, 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 != "" {
if lineNumber, err := findLineNumber(tc.Classname, tc.Name, stepNumber, assertion); err == nil && lineNumber > 0 {
lineNumberSuffix = fmt.Sprintf(":%d", lineNumber)
}
var prefix string
if stepNumber >= 0 {
prefix = color.YellowString(
"Failure in %q\nIn test case %q, at step %d\nAssertion %q failed",
tc.Classname+lineNumberSuffix,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assertion),
)
} else {
// venom used as lib
prefix = RemoveNotPrintableChar(fmt.Sprintf("assertion: %s", assertion))
}
return nil, &Failure{Value: 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:
return 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
}
func findLineNumber(filename, testcase string, stepNumber int, assertion string) (int, error) {
countLine := 0
file, err := os.Open(filename)
if err != nil {
return countLine, err
}
defer file.Close()
lineFound := false
testcaseFound := false
commentBlock := false
countStep := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
countLine++
line := strings.Trim(scanner.Text(), " ")
if strings.HasPrefix(line, "/*") {
commentBlock = true
continue
}
if strings.HasPrefix(line, "*/") {
commentBlock = false
continue
}
if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") || commentBlock {
continue
}
if !testcaseFound && strings.Contains(line, testcase) {
testcaseFound = true
continue
}
if testcaseFound && countStep <= stepNumber && strings.Contains(line, "type") {
countStep++
continue
}
if testcaseFound && countStep > stepNumber && strings.Contains(line, assertion) {
lineFound = true
break
}
}
if err := scanner.Err(); err != nil {
return countLine, err
}
if !lineFound {
return 0, nil
}
return countLine, nil
}