5 "github.com/onsi/gomega/format"
9 type BeTemporallyMatcher struct {
12 Threshold []time.Duration
15 func (matcher *BeTemporallyMatcher) FailureMessage(actual interface{}) (message string) {
16 return format.Message(actual, fmt.Sprintf("to be %s", matcher.Comparator), matcher.CompareTo)
19 func (matcher *BeTemporallyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
20 return format.Message(actual, fmt.Sprintf("not to be %s", matcher.Comparator), matcher.CompareTo)
23 func (matcher *BeTemporallyMatcher) Match(actual interface{}) (bool, error) {
24 // predicate to test for time.Time type
25 isTime := func(t interface{}) bool {
26 _, ok := t.(time.Time)
31 return false, fmt.Errorf("Expected a time.Time. Got:\n%s", format.Object(actual, 1))
34 switch matcher.Comparator {
35 case "==", "~", ">", ">=", "<", "<=":
37 return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
40 var threshold = time.Millisecond
41 if len(matcher.Threshold) == 1 {
42 threshold = matcher.Threshold[0]
45 return matcher.matchTimes(actual.(time.Time), matcher.CompareTo, threshold), nil
48 func (matcher *BeTemporallyMatcher) matchTimes(actual, compareTo time.Time, threshold time.Duration) (success bool) {
49 switch matcher.Comparator {
51 return actual.Equal(compareTo)
53 diff := actual.Sub(compareTo)
54 return -threshold <= diff && diff <= threshold
56 return actual.After(compareTo)
58 return !actual.Before(compareTo)
60 return actual.Before(compareTo)
62 return !actual.After(compareTo)