-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertion.go
95 lines (80 loc) · 2.87 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
package assertion
import (
"errors"
"fmt"
)
const (
errMsgMissingArgs = `missing required arguments`
errMsgNotSameType = `%v and %v are not of the same type`
errMsgNot = `%v is not %v`
errMsgNotEqual = `%v is not equal %v`
errMsgNotValid = `%v is not a valid %v`
errMsgNotGreater = `%v is not greater than %v`
errMsgNotLower = `%v is not lower than %v`
errMsgNotGreaterEqual = `%v is not greater than or equal %v`
errMsgNotLowerEqual = `%v is not lower than or equal %v`
errMsgNotDifferent = `%v is not different %v`
errMsgNotBetween = `%v is not between %v and %v`
errMsgNotBetweenExclude = `%v is not between %v and %v both excluded`
errMsgNotStartsWith = `%v does not start with %v`
errMsgNotEndsWith = `%v does not end with %v`
errMsgNotContains = `%v does not contain %v`
errMsgNotHasKey = `%v has not the key %v`
)
// Assertion represents a data assertion process. It provides several methods
// to execute common assertions, and stores and gives access to the corresponding
// errors result of assertion failures.
//
// Every assertion method admits message arguments (msgArgs) on their signature
// to allow the customization of error messages. If this arguments are provided
// they will form the error message in case of failure of the corresponding method.
type Assertion struct {
errors []error
}
// New creates and returns a new Assertion
func New() Assertion {
return Assertion{errors: make([]error, 0)}
}
// HasErrors returns if current Assertion stores some error
func (a *Assertion) HasErrors() bool {
return len(a.errors) > 0
}
// CountErrors returns the number of current errors
func (a *Assertion) CountErrors() int {
return len(a.errors)
}
// ErrorAt returns the error at given index. Negative indexes will be considered
// as reverse order, that is indexes from the last error element
func (a *Assertion) ErrorAt(index int) error {
if index > len(a.errors)-1 {
return nil
}
if len(a.errors)+index < 0 {
return nil
}
if index >= 0 {
return a.errors[index]
}
return a.errors[len(a.errors)+index]
}
// addError adds an error to Assertion
func (a *Assertion) addError(err error) {
a.errors = append(a.errors, err)
}
// addErrorMsg adds the default error message to Assertion or a formatted error
// message if msgArgs are provided
func (a *Assertion) addErrorMsg(defaultMsg string, msgArgs ...interface{}) {
a.errors = append(a.errors, buildError(defaultMsg, msgArgs...))
}
// buildError returns an error with a default message or a message built from
// message arguments if msgArgs are provided
func buildError(defaultMsg string, msgArgs ...interface{}) error {
errMsg := defaultMsg
if len(msgArgs) == 1 {
errMsg = fmt.Sprintf("%+v", msgArgs[0])
}
if len(msgArgs) > 1 {
errMsg = fmt.Sprintf(msgArgs[0].(string), msgArgs[1:]...)
}
return errors.New(errMsg)
}