-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamcrest_is.go
43 lines (36 loc) · 1004 Bytes
/
hamcrest_is.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
package assert
import (
"bytes"
"reflect"
)
type isMatcher struct {
expectedValue interface{}
message string
}
// Is checks if the value eq with the expected value
func Is(expectedValue interface{}) *isMatcher {
matcher := new(isMatcher)
matcher.expectedValue = expectedValue
return matcher
}
func (m *isMatcher) Message(message string) Matcher {
m.message = message
return m
}
func (m *isMatcher) Matches(value interface{}) bool {
if sameType(value, m.expectedValue) {
if isByteArray(value) {
return bytes.Equal(value.([]byte), m.expectedValue.([]byte))
}
return m.expectedValue == value
}
return false
}
func (m *isMatcher) DescribeMismatch(value interface{}) error {
if sameType(value, m.expectedValue) {
return buildError("expected <%v> but got <%v>", m.message, m.expectedValue, value)
}
expectedType := reflect.TypeOf(m.expectedValue)
valueType := reflect.TypeOf(value)
return buildError("expected type %v but got %v", m.message, expectedType, valueType)
}