File: assert.go

package info (click to toggle)
golang-github-viant-assertly 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download | duplicates (2)
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
}