forked from lyokato/goidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
131 lines (109 loc) · 2.31 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
121
122
123
124
125
126
127
128
129
130
131
package test_helper
import (
"fmt"
"reflect"
"regexp"
)
type (
Matcher interface {
RequireAbsent() bool
Match(v interface{}) bool
WantValue() string
}
AbsentMatcher struct {
}
Int64Matcher struct {
value int64
}
Int64RangeMatcher struct {
from int64
to int64
}
StrMatcher struct {
value string
}
RegexMatcher struct {
origin string
value *regexp.Regexp
}
)
func NewAbsentMatcher() *AbsentMatcher {
return &AbsentMatcher{}
}
func (m *AbsentMatcher) Match(v interface{}) bool {
return false
}
func (m *AbsentMatcher) WantValue() string {
return "nil"
}
func (m *AbsentMatcher) RequireAbsent() bool {
return true
}
func NewInt64Matcher(v int64) *Int64Matcher {
return &Int64Matcher{v}
}
func NewInt64RangeMatcher(from, to int64) *Int64RangeMatcher {
return &Int64RangeMatcher{from, to}
}
func (m *Int64Matcher) Match(v interface{}) bool {
value := reflect.ValueOf(v)
if value.Kind() != reflect.Float64 {
return false
}
int_value := int64(v.(float64))
return m.value == int_value
}
func (m *Int64Matcher) WantValue() string {
return fmt.Sprintf("%d", m.value)
}
func (m *Int64Matcher) RequireAbsent() bool {
return false
}
func (m *Int64RangeMatcher) RequireAbsent() bool {
return false
}
func (m *Int64RangeMatcher) Match(v interface{}) bool {
value := reflect.ValueOf(v)
if value.Kind() != reflect.Float64 {
return false
}
int_value := int64(v.(float64))
return m.from <= int_value && m.to >= int_value
}
func (m *Int64RangeMatcher) WantValue() string {
return fmt.Sprintf("%d ~ %d", m.from, m.to)
}
func NewStrMatcher(v string) *StrMatcher {
return &StrMatcher{v}
}
func (m *StrMatcher) Match(v interface{}) bool {
value := reflect.ValueOf(v)
if value.Kind() != reflect.String {
return false
}
str, _ := v.(string)
return m.value == str
}
func (m *StrMatcher) WantValue() string {
return m.value
}
func (m *StrMatcher) RequireAbsent() bool {
return false
}
func NewRegexMatcher(v string) *RegexMatcher {
return &RegexMatcher{v, regexp.MustCompile(v)}
}
func (m *RegexMatcher) RequireAbsent() bool {
return false
}
func (m *RegexMatcher) Match(v interface{}) bool {
value := reflect.ValueOf(v)
if value.Kind() != reflect.String {
return false
}
str, _ := v.(string)
return m.value.MatchString(str)
}
func (m *RegexMatcher) WantValue() string {
return m.origin
}