| 1 |
package expect |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
Equals comparitorType = iota |
| 10 |
NotEqual |
| 11 |
LessThan |
| 12 |
LessThanOrEqual |
| 13 |
GreaterThan |
| 14 |
GreaterThanOrEqual |
| 15 |
) |
| 16 |
|
| 17 |
type comparitorType int |
| 18 |
type comparitor func(k reflect.Kind, a, b interface{}) bool |
| 19 |
type resolver func(a, b interface{}) bool |
| 20 |
|
| 21 |
var ( |
| 22 |
Comparitors = map[reflect.Kind]map[comparitorType]resolver{ |
| 23 |
reflect.Int64: map[comparitorType]resolver{ |
| 24 |
LessThan: func(a, b interface{}) bool { return a.(int64) < b.(int64) }, |
| 25 |
}, |
| 26 |
reflect.Uint64: map[comparitorType]resolver{ |
| 27 |
LessThan: func(a, b interface{}) bool { return a.(uint64) < b.(uint64) }, |
| 28 |
}, |
| 29 |
reflect.Float32: map[comparitorType]resolver{ |
| 30 |
LessThan: func(a, b interface{}) bool { return a.(float32) < b.(float32) }, |
| 31 |
}, |
| 32 |
reflect.Float64: map[comparitorType]resolver{ |
| 33 |
LessThan: func(a, b interface{}) bool { return a.(float64) < b.(float64) }, |
| 34 |
}, |
| 35 |
} |
| 36 |
) |
| 37 |
|
| 38 |
func EqualsComparitor(k reflect.Kind, a, b interface{}) bool { |
| 39 |
return reflect.DeepEqual(a, b) |
| 40 |
} |
| 41 |
|
| 42 |
func NotEqualsComparitor(k reflect.Kind, a, b interface{}) bool { |
| 43 |
return !EqualsComparitor(k, a, b) |
| 44 |
} |
| 45 |
|
| 46 |
func LessThanComparitor(k reflect.Kind, a, b interface{}) bool { |
| 47 |
if t, ok := a.(time.Time); ok { |
| 48 |
return t.Before(b.(time.Time)) |
| 49 |
} |
| 50 |
return Comparitors[k][LessThan](a, b) |
| 51 |
} |
| 52 |
|
| 53 |
func LessThanOrEqualToComparitor(k reflect.Kind, a, b interface{}) bool { |
| 54 |
if t, ok := a.(time.Time); ok { |
| 55 |
bt := b.(time.Time) |
| 56 |
return t.Before(bt) || t == bt |
| 57 |
} |
| 58 |
return EqualsComparitor(k, a, b) || Comparitors[k][LessThan](a, b) |
| 59 |
} |
| 60 |
|
| 61 |
func GreaterThanComparitor(k reflect.Kind, a, b interface{}) bool { |
| 62 |
if t, ok := a.(time.Time); ok { |
| 63 |
return t.After(b.(time.Time)) |
| 64 |
} |
| 65 |
return !EqualsComparitor(k, a, b) && !Comparitors[k][LessThan](a, b) |
| 66 |
} |
| 67 |
|
| 68 |
func GreaterOrEqualToComparitor(k reflect.Kind, a, b interface{}) bool { |
| 69 |
if t, ok := a.(time.Time); ok { |
| 70 |
bt := b.(time.Time) |
| 71 |
return t.After(bt) || t == bt |
| 72 |
} |
| 73 |
return !Comparitors[k][LessThan](a, b) |
| 74 |
} |