github.com/onsi/gomega@v1.32.0/matchers/be_temporally_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 "time" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/matchers" 9 ) 10 11 var _ = Describe("BeTemporally", func() { 12 13 var t0, t1, t2 time.Time 14 BeforeEach(func() { 15 t0 = time.Now() 16 t1 = t0.Add(time.Second) 17 t2 = t0.Add(-time.Second) 18 }) 19 20 Context("When comparing times", func() { 21 22 It("should support ==", func() { 23 Expect(t0).Should(BeTemporally("==", t0)) 24 Expect(t1).ShouldNot(BeTemporally("==", t0)) 25 Expect(t0).ShouldNot(BeTemporally("==", t1)) 26 Expect(t0).ShouldNot(BeTemporally("==", time.Time{})) 27 }) 28 29 It("should support >", func() { 30 Expect(t0).Should(BeTemporally(">", t2)) 31 Expect(t0).ShouldNot(BeTemporally(">", t0)) 32 Expect(t2).ShouldNot(BeTemporally(">", t0)) 33 }) 34 35 It("should support <", func() { 36 Expect(t0).Should(BeTemporally("<", t1)) 37 Expect(t0).ShouldNot(BeTemporally("<", t0)) 38 Expect(t1).ShouldNot(BeTemporally("<", t0)) 39 }) 40 41 It("should support >=", func() { 42 Expect(t0).Should(BeTemporally(">=", t2)) 43 Expect(t0).Should(BeTemporally(">=", t0)) 44 Expect(t0).ShouldNot(BeTemporally(">=", t1)) 45 }) 46 47 It("should support <=", func() { 48 Expect(t0).Should(BeTemporally("<=", t1)) 49 Expect(t0).Should(BeTemporally("<=", t0)) 50 Expect(t0).ShouldNot(BeTemporally("<=", t2)) 51 }) 52 53 When("passed ~", func() { 54 Context("and there is no precision parameter", func() { 55 BeforeEach(func() { 56 t1 = t0.Add(time.Millisecond / 2) 57 t2 = t0.Add(-2 * time.Millisecond) 58 }) 59 It("should approximate", func() { 60 Expect(t0).Should(BeTemporally("~", t0)) 61 Expect(t0).Should(BeTemporally("~", t1)) 62 Expect(t0).ShouldNot(BeTemporally("~", t2)) 63 }) 64 }) 65 66 Context("and there is a precision parameter", func() { 67 BeforeEach(func() { 68 t2 = t0.Add(3 * time.Second) 69 }) 70 It("should use precision paramter", func() { 71 d := 2 * time.Second 72 Expect(t0).Should(BeTemporally("~", t0, d)) 73 Expect(t0).Should(BeTemporally("~", t1, d)) 74 Expect(t0).ShouldNot(BeTemporally("~", t2, d)) 75 }) 76 }) 77 }) 78 }) 79 80 When("passed a non-time", func() { 81 It("should error", func() { 82 success, err := (&BeTemporallyMatcher{Comparator: "==", CompareTo: t0}).Match("foo") 83 Expect(success).Should(BeFalse()) 84 Expect(err).Should(HaveOccurred()) 85 86 success, err = (&BeTemporallyMatcher{Comparator: "=="}).Match(nil) 87 Expect(success).Should(BeFalse()) 88 Expect(err).Should(HaveOccurred()) 89 }) 90 }) 91 92 When("passed an unsupported comparator", func() { 93 It("should error", func() { 94 success, err := (&BeTemporallyMatcher{Comparator: "!=", CompareTo: t0}).Match(t2) 95 Expect(success).Should(BeFalse()) 96 Expect(err).Should(HaveOccurred()) 97 }) 98 }) 99 })