forked from petergtz/pegomock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
94 lines (75 loc) · 2.17 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
package pegomock
import (
"fmt"
"reflect"
"github.com/petergtz/pegomock/internal/verify"
)
type EqMatcher struct {
Value Param
actual Param
}
func (matcher *EqMatcher) Matches(param Param) bool {
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 AnyMatcher struct {
Type reflect.Type
actual reflect.Type
}
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.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)
}