forked from petergtz/pegomock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
120 lines (94 loc) · 2.66 KB
/
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
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
package pegomock
import (
"fmt"
"reflect"
"sync"
"github.com/petergtz/pegomock/v4/internal/verify"
)
type EqMatcher struct {
Value Param
actual Param
sync.Mutex
}
func (matcher *EqMatcher) Matches(param Param) bool {
matcher.Lock()
defer matcher.Unlock()
matcher.actual = param
return reflect.DeepEqual(matcher.Value, param)
}
func (matcher *EqMatcher) FailureMessage() string {
return fmt.Sprintf("Expected: %v; but got: %v", matcher.Value, matcher.actual)
}
func (matcher *EqMatcher) String() string {
return fmt.Sprintf("Eq(%#v)", matcher.Value)
}
type NotEqMatcher struct {
Value Param
}
func (matcher *NotEqMatcher) Matches(param Param) bool {
return !reflect.DeepEqual(matcher.Value, param)
}
func (matcher *NotEqMatcher) FailureMessage() string {
return fmt.Sprintf("Expected: not %v; but got: %v", matcher.Value, matcher.Value)
}
func (matcher *NotEqMatcher) String() string {
return fmt.Sprintf("NotEq(%#v)", matcher.Value)
}
type AnyMatcher struct {
Type reflect.Type
actual reflect.Type
sync.Mutex
}
func NewAnyMatcher(typ reflect.Type) *AnyMatcher {
verify.Argument(typ != nil, "Must provide a non-nil type")
return &AnyMatcher{Type: typ}
}
func (matcher *AnyMatcher) Matches(param Param) bool {
matcher.Lock()
defer matcher.Unlock()
matcher.actual = reflect.TypeOf(param)
if matcher.actual == nil {
switch matcher.Type.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return true
default:
return false
}
}
return matcher.actual.AssignableTo(matcher.Type)
}
func (matcher *AnyMatcher) FailureMessage() string {
return fmt.Sprintf("Expected: %v; but got: %v", matcher.Type, matcher.actual)
}
func (matcher *AnyMatcher) String() string {
return fmt.Sprintf("Any(%v)", matcher.Type)
}
type AtLeastIntMatcher struct {
Value int
actual int
}
func (matcher *AtLeastIntMatcher) Matches(param Param) bool {
matcher.actual = param.(int)
return param.(int) >= matcher.Value
}
func (matcher *AtLeastIntMatcher) FailureMessage() string {
return fmt.Sprintf("Expected: at least %v; but got: %v", matcher.Value, matcher.actual)
}
func (matcher *AtLeastIntMatcher) String() string {
return fmt.Sprintf("AtLeast(%v)", matcher.Value)
}
type AtMostIntMatcher struct {
Value int
actual int
}
func (matcher *AtMostIntMatcher) Matches(param Param) bool {
matcher.actual = param.(int)
return param.(int) <= matcher.Value
}
func (matcher *AtMostIntMatcher) FailureMessage() string {
return fmt.Sprintf("Expected: at most %v; but got: %v", matcher.Value, matcher.actual)
}
func (matcher *AtMostIntMatcher) String() string {
return fmt.Sprintf("AtMost(%v)", matcher.Value)
}