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
|
package matchers_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeTemporally", func() {
var t0, t1, t2 time.Time
BeforeEach(func() {
t0 = time.Now()
t1 = t0.Add(time.Second)
t2 = t0.Add(-time.Second)
})
Context("When comparing times", func() {
It("should support ==", func() {
Expect(t0).Should(BeTemporally("==", t0))
Expect(t1).ShouldNot(BeTemporally("==", t0))
Expect(t0).ShouldNot(BeTemporally("==", t1))
Expect(t0).ShouldNot(BeTemporally("==", time.Time{}))
})
It("should support >", func() {
Expect(t0).Should(BeTemporally(">", t2))
Expect(t0).ShouldNot(BeTemporally(">", t0))
Expect(t2).ShouldNot(BeTemporally(">", t0))
})
It("should support <", func() {
Expect(t0).Should(BeTemporally("<", t1))
Expect(t0).ShouldNot(BeTemporally("<", t0))
Expect(t1).ShouldNot(BeTemporally("<", t0))
})
It("should support >=", func() {
Expect(t0).Should(BeTemporally(">=", t2))
Expect(t0).Should(BeTemporally(">=", t0))
Expect(t0).ShouldNot(BeTemporally(">=", t1))
})
It("should support <=", func() {
Expect(t0).Should(BeTemporally("<=", t1))
Expect(t0).Should(BeTemporally("<=", t0))
Expect(t0).ShouldNot(BeTemporally("<=", t2))
})
Context("when passed ~", func() {
Context("and there is no precision parameter", func() {
BeforeEach(func() {
t1 = t0.Add(time.Millisecond / 2)
t2 = t0.Add(-2 * time.Millisecond)
})
It("should approximate", func() {
Expect(t0).Should(BeTemporally("~", t0))
Expect(t0).Should(BeTemporally("~", t1))
Expect(t0).ShouldNot(BeTemporally("~", t2))
})
})
Context("and there is a precision parameter", func() {
BeforeEach(func() {
t2 = t0.Add(3 * time.Second)
})
It("should use precision paramter", func() {
d := 2 * time.Second
Expect(t0).Should(BeTemporally("~", t0, d))
Expect(t0).Should(BeTemporally("~", t1, d))
Expect(t0).ShouldNot(BeTemporally("~", t2, d))
})
})
})
})
Context("when passed a non-time", func() {
It("should error", func() {
success, err := (&BeTemporallyMatcher{Comparator: "==", CompareTo: t0}).Match("foo")
Expect(success).Should(BeFalse())
Expect(err).Should(HaveOccurred())
success, err = (&BeTemporallyMatcher{Comparator: "=="}).Match(nil)
Expect(success).Should(BeFalse())
Expect(err).Should(HaveOccurred())
})
})
Context("when passed an unsupported comparator", func() {
It("should error", func() {
success, err := (&BeTemporallyMatcher{Comparator: "!=", CompareTo: t0}).Match(t2)
Expect(success).Should(BeFalse())
Expect(err).Should(HaveOccurred())
})
})
})
|