-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathbe_numerically_matcher.go
68 lines (60 loc) · 1.49 KB
/
be_numerically_matcher.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
package matchers
import (
"encoding/json"
"fmt"
"github.com/onsi/gomega/matchers"
)
type BeNumericallyMatcher struct {
fakeOmegaMatcher
Comparator string
CompareTo []interface{}
}
func BeNumerically(comparator string, compareTo ...interface{}) GossMatcher {
return &BeNumericallyMatcher{
Comparator: comparator,
CompareTo: compareTo,
}
}
func (m *BeNumericallyMatcher) Match(actual interface{}) (success bool, err error) {
comparator, err := strToSymbol(m.Comparator)
if err != nil {
return false, err
}
matcher := &matchers.BeNumericallyMatcher{
Comparator: comparator,
CompareTo: m.CompareTo,
}
return matcher.Match(actual)
}
func (m *BeNumericallyMatcher) FailureResult(actual interface{}) MatcherResult {
return MatcherResult{
Actual: actual,
Message: fmt.Sprintf("to be numerically %s", m.Comparator),
Expected: m.CompareTo[0],
}
}
func (m *BeNumericallyMatcher) NegatedFailureResult(actual interface{}) MatcherResult {
return MatcherResult{
Actual: actual,
Message: fmt.Sprintf("not to be numerically %s", m.Comparator),
Expected: m.CompareTo[0],
}
}
func (m *BeNumericallyMatcher) MarshalJSON() ([]byte, error) {
j := make(map[string]interface{})
j[m.Comparator] = m.CompareTo[0]
return json.Marshal(j)
}
func strToSymbol(s string) (string, error) {
comparator, ok := map[string]string{
"gt": ">",
"ge": ">=",
"lt": "<",
"le": "<=",
"eq": "==",
}[s]
if !ok {
return "", fmt.Errorf("Unknown comparator: %s", s)
}
return comparator, nil
}