initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / type_support.go
1 /*
2 Gomega matchers
3
4 This package implements the Gomega matchers and does not typically need to be imported.
5 See the docs for Gomega for documentation on the matchers
6
7 http://onsi.github.io/gomega/
8 */
9 package matchers
10
11 import (
12         "fmt"
13         "reflect"
14 )
15
16 type omegaMatcher interface {
17         Match(actual interface{}) (success bool, err error)
18         FailureMessage(actual interface{}) (message string)
19         NegatedFailureMessage(actual interface{}) (message string)
20 }
21
22 func isBool(a interface{}) bool {
23         return reflect.TypeOf(a).Kind() == reflect.Bool
24 }
25
26 func isNumber(a interface{}) bool {
27         if a == nil {
28                 return false
29         }
30         kind := reflect.TypeOf(a).Kind()
31         return reflect.Int <= kind && kind <= reflect.Float64
32 }
33
34 func isInteger(a interface{}) bool {
35         kind := reflect.TypeOf(a).Kind()
36         return reflect.Int <= kind && kind <= reflect.Int64
37 }
38
39 func isUnsignedInteger(a interface{}) bool {
40         kind := reflect.TypeOf(a).Kind()
41         return reflect.Uint <= kind && kind <= reflect.Uint64
42 }
43
44 func isFloat(a interface{}) bool {
45         kind := reflect.TypeOf(a).Kind()
46         return reflect.Float32 <= kind && kind <= reflect.Float64
47 }
48
49 func toInteger(a interface{}) int64 {
50         if isInteger(a) {
51                 return reflect.ValueOf(a).Int()
52         } else if isUnsignedInteger(a) {
53                 return int64(reflect.ValueOf(a).Uint())
54         } else if isFloat(a) {
55                 return int64(reflect.ValueOf(a).Float())
56         } else {
57                 panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
58         }
59 }
60
61 func toUnsignedInteger(a interface{}) uint64 {
62         if isInteger(a) {
63                 return uint64(reflect.ValueOf(a).Int())
64         } else if isUnsignedInteger(a) {
65                 return reflect.ValueOf(a).Uint()
66         } else if isFloat(a) {
67                 return uint64(reflect.ValueOf(a).Float())
68         } else {
69                 panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
70         }
71 }
72
73 func toFloat(a interface{}) float64 {
74         if isInteger(a) {
75                 return float64(reflect.ValueOf(a).Int())
76         } else if isUnsignedInteger(a) {
77                 return float64(reflect.ValueOf(a).Uint())
78         } else if isFloat(a) {
79                 return reflect.ValueOf(a).Float()
80         } else {
81                 panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
82         }
83 }
84
85 func isError(a interface{}) bool {
86         _, ok := a.(error)
87         return ok
88 }
89
90 func isChan(a interface{}) bool {
91         if isNil(a) {
92                 return false
93         }
94         return reflect.TypeOf(a).Kind() == reflect.Chan
95 }
96
97 func isMap(a interface{}) bool {
98         if a == nil {
99                 return false
100         }
101         return reflect.TypeOf(a).Kind() == reflect.Map
102 }
103
104 func isArrayOrSlice(a interface{}) bool {
105         if a == nil {
106                 return false
107         }
108         switch reflect.TypeOf(a).Kind() {
109         case reflect.Array, reflect.Slice:
110                 return true
111         default:
112                 return false
113         }
114 }
115
116 func isString(a interface{}) bool {
117         if a == nil {
118                 return false
119         }
120         return reflect.TypeOf(a).Kind() == reflect.String
121 }
122
123 func toString(a interface{}) (string, bool) {
124         aString, isString := a.(string)
125         if isString {
126                 return aString, true
127         }
128
129         aBytes, isBytes := a.([]byte)
130         if isBytes {
131                 return string(aBytes), true
132         }
133
134         aStringer, isStringer := a.(fmt.Stringer)
135         if isStringer {
136                 return aStringer.String(), true
137         }
138
139         return "", false
140 }
141
142 func lengthOf(a interface{}) (int, bool) {
143         if a == nil {
144                 return 0, false
145         }
146         switch reflect.TypeOf(a).Kind() {
147         case reflect.Map, reflect.Array, reflect.String, reflect.Chan, reflect.Slice:
148                 return reflect.ValueOf(a).Len(), true
149         default:
150                 return 0, false
151         }
152 }
153 func capOf(a interface{}) (int, bool) {
154         if a == nil {
155                 return 0, false
156         }
157         switch reflect.TypeOf(a).Kind() {
158         case reflect.Array, reflect.Chan, reflect.Slice:
159                 return reflect.ValueOf(a).Cap(), true
160         default:
161                 return 0, false
162         }
163 }
164
165 func isNil(a interface{}) bool {
166         if a == nil {
167                 return true
168         }
169
170         switch reflect.TypeOf(a).Kind() {
171         case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
172                 return reflect.ValueOf(a).IsNil()
173         }
174
175         return false
176 }