forked from viant/assertly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
48 lines (43 loc) · 1.41 KB
/
assert.go
File metadata and controls
48 lines (43 loc) · 1.41 KB
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
package assertly
import (
"fmt"
"testing"
)
//AssertValues validates expected against actual data structure
func AssertValues(t *testing.T, expected, actual interface{}, arguments ...interface{}) bool {
validation, err := Assert(expected, actual, NewDataPath("/"))
if err != nil {
return handlerValidationError(t, err, arguments...)
}
if validation.FailedCount != 0 {
return handlerValidationFailures(t, validation, arguments...)
}
return true
}
//AssertValuesWithContext validates expected against actual data structure with context
func AssertValuesWithContext(context *Context, t *testing.T, expected, actual interface{}, arguments ...interface{}) bool {
validation, err := AssertWithContext(expected, actual, NewDataPath("/"), context)
if err != nil {
return handlerValidationError(t, err, arguments...)
}
if validation.FailedCount != 0 {
return handlerValidationFailures(t, validation, arguments...)
}
return true
}
func handlerValidationError(t *testing.T, err error, arguments ...interface{}) bool {
if len(arguments) > 0 {
handleFailure(t, fmt.Sprint(arguments...))
}
handleFailure(t, err)
return false
}
func handlerValidationFailures(t *testing.T, validation *Validation, arguments ...interface{}) bool {
if len(arguments) > 0 {
handleFailure(t, arguments...)
}
for _, failure := range validation.Failures {
handleFailure(t, fmt.Sprintf("%v: %v", failure.Path, failure.Message))
}
return false
}