4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
9 var _ = Describe("BeNumerically", func() {
10 Context("when passed a number", func() {
11 It("should support ==", func() {
12 Ω(uint32(5)).Should(BeNumerically("==", 5))
13 Ω(float64(5.0)).Should(BeNumerically("==", 5))
14 Ω(int8(5)).Should(BeNumerically("==", 5))
17 It("should not have false positives", func() {
18 Ω(5.1).ShouldNot(BeNumerically("==", 5))
19 Ω(5).ShouldNot(BeNumerically("==", 5.1))
22 It("should support >", func() {
23 Ω(uint32(5)).Should(BeNumerically(">", 4))
24 Ω(float64(5.0)).Should(BeNumerically(">", 4.9))
25 Ω(int8(5)).Should(BeNumerically(">", 4))
27 Ω(uint32(5)).ShouldNot(BeNumerically(">", 5))
28 Ω(float64(5.0)).ShouldNot(BeNumerically(">", 5.0))
29 Ω(int8(5)).ShouldNot(BeNumerically(">", 5))
32 It("should support <", func() {
33 Ω(uint32(5)).Should(BeNumerically("<", 6))
34 Ω(float64(5.0)).Should(BeNumerically("<", 5.1))
35 Ω(int8(5)).Should(BeNumerically("<", 6))
37 Ω(uint32(5)).ShouldNot(BeNumerically("<", 5))
38 Ω(float64(5.0)).ShouldNot(BeNumerically("<", 5.0))
39 Ω(int8(5)).ShouldNot(BeNumerically("<", 5))
42 It("should support >=", func() {
43 Ω(uint32(5)).Should(BeNumerically(">=", 4))
44 Ω(float64(5.0)).Should(BeNumerically(">=", 4.9))
45 Ω(int8(5)).Should(BeNumerically(">=", 4))
47 Ω(uint32(5)).Should(BeNumerically(">=", 5))
48 Ω(float64(5.0)).Should(BeNumerically(">=", 5.0))
49 Ω(int8(5)).Should(BeNumerically(">=", 5))
51 Ω(uint32(5)).ShouldNot(BeNumerically(">=", 6))
52 Ω(float64(5.0)).ShouldNot(BeNumerically(">=", 5.1))
53 Ω(int8(5)).ShouldNot(BeNumerically(">=", 6))
56 It("should support <=", func() {
57 Ω(uint32(5)).Should(BeNumerically("<=", 6))
58 Ω(float64(5.0)).Should(BeNumerically("<=", 5.1))
59 Ω(int8(5)).Should(BeNumerically("<=", 6))
61 Ω(uint32(5)).Should(BeNumerically("<=", 5))
62 Ω(float64(5.0)).Should(BeNumerically("<=", 5.0))
63 Ω(int8(5)).Should(BeNumerically("<=", 5))
65 Ω(uint32(5)).ShouldNot(BeNumerically("<=", 4))
66 Ω(float64(5.0)).ShouldNot(BeNumerically("<=", 4.9))
67 Ω(int8(5)).Should(BeNumerically("<=", 5))
70 Context("when passed ~", func() {
71 Context("when passed a float", func() {
72 Context("and there is no precision parameter", func() {
73 It("should default to 1e-8", func() {
74 Ω(5.00000001).Should(BeNumerically("~", 5.00000002))
75 Ω(5.00000001).ShouldNot(BeNumerically("~", 5.0000001))
79 Context("and there is a precision parameter", func() {
80 It("should use the precision parameter", func() {
81 Ω(5.1).Should(BeNumerically("~", 5.19, 0.1))
82 Ω(5.1).Should(BeNumerically("~", 5.01, 0.1))
83 Ω(5.1).ShouldNot(BeNumerically("~", 5.22, 0.1))
84 Ω(5.1).ShouldNot(BeNumerically("~", 4.98, 0.1))
89 Context("when passed an int/uint", func() {
90 Context("and there is no precision parameter", func() {
91 It("should just do strict equality", func() {
92 Ω(5).Should(BeNumerically("~", 5))
93 Ω(5).ShouldNot(BeNumerically("~", 6))
94 Ω(uint(5)).ShouldNot(BeNumerically("~", 6))
98 Context("and there is a precision parameter", func() {
99 It("should use precision paramter", func() {
100 Ω(5).Should(BeNumerically("~", 6, 2))
101 Ω(5).ShouldNot(BeNumerically("~", 8, 2))
102 Ω(uint(5)).Should(BeNumerically("~", 6, 1))
109 Context("when passed a non-number", func() {
110 It("should error", func() {
111 success, err := (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{5}}).Match("foo")
112 Ω(success).Should(BeFalse())
113 Ω(err).Should(HaveOccurred())
115 success, err = (&BeNumericallyMatcher{Comparator: "=="}).Match(5)
116 Ω(success).Should(BeFalse())
117 Ω(err).Should(HaveOccurred())
119 success, err = (&BeNumericallyMatcher{Comparator: "~", CompareTo: []interface{}{3.0, "foo"}}).Match(5.0)
120 Ω(success).Should(BeFalse())
121 Ω(err).Should(HaveOccurred())
123 success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match(5)
124 Ω(success).Should(BeFalse())
125 Ω(err).Should(HaveOccurred())
127 success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match("foo")
128 Ω(success).Should(BeFalse())
129 Ω(err).Should(HaveOccurred())
131 success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{nil}}).Match(0)
132 Ω(success).Should(BeFalse())
133 Ω(err).Should(HaveOccurred())
135 success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{0}}).Match(nil)
136 Ω(success).Should(BeFalse())
137 Ω(err).Should(HaveOccurred())
141 Context("when passed an unsupported comparator", func() {
142 It("should error", func() {
143 success, err := (&BeNumericallyMatcher{Comparator: "!=", CompareTo: []interface{}{5}}).Match(4)
144 Ω(success).Should(BeFalse())
145 Ω(err).Should(HaveOccurred())