added support for string type
[govpp.git] / vendor / github.com / onsi / gomega / matchers / be_numerically_matcher_test.go
1 package matchers_test
2
3 import (
4         . "github.com/onsi/ginkgo"
5         . "github.com/onsi/gomega"
6         . "github.com/onsi/gomega/matchers"
7 )
8
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))
15                 })
16
17                 It("should not have false positives", func() {
18                         Ω(5.1).ShouldNot(BeNumerically("==", 5))
19                         Ω(5).ShouldNot(BeNumerically("==", 5.1))
20                 })
21
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))
26
27                         Ω(uint32(5)).ShouldNot(BeNumerically(">", 5))
28                         Ω(float64(5.0)).ShouldNot(BeNumerically(">", 5.0))
29                         Ω(int8(5)).ShouldNot(BeNumerically(">", 5))
30                 })
31
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))
36
37                         Ω(uint32(5)).ShouldNot(BeNumerically("<", 5))
38                         Ω(float64(5.0)).ShouldNot(BeNumerically("<", 5.0))
39                         Ω(int8(5)).ShouldNot(BeNumerically("<", 5))
40                 })
41
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))
46
47                         Ω(uint32(5)).Should(BeNumerically(">=", 5))
48                         Ω(float64(5.0)).Should(BeNumerically(">=", 5.0))
49                         Ω(int8(5)).Should(BeNumerically(">=", 5))
50
51                         Ω(uint32(5)).ShouldNot(BeNumerically(">=", 6))
52                         Ω(float64(5.0)).ShouldNot(BeNumerically(">=", 5.1))
53                         Ω(int8(5)).ShouldNot(BeNumerically(">=", 6))
54                 })
55
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))
60
61                         Ω(uint32(5)).Should(BeNumerically("<=", 5))
62                         Ω(float64(5.0)).Should(BeNumerically("<=", 5.0))
63                         Ω(int8(5)).Should(BeNumerically("<=", 5))
64
65                         Ω(uint32(5)).ShouldNot(BeNumerically("<=", 4))
66                         Ω(float64(5.0)).ShouldNot(BeNumerically("<=", 4.9))
67                         Ω(int8(5)).Should(BeNumerically("<=", 5))
68                 })
69
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))
76                                         })
77                                 })
78
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))
85                                         })
86                                 })
87                         })
88
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))
95                                         })
96                                 })
97
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))
103                                         })
104                                 })
105                         })
106                 })
107         })
108
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())
114
115                         success, err = (&BeNumericallyMatcher{Comparator: "=="}).Match(5)
116                         Ω(success).Should(BeFalse())
117                         Ω(err).Should(HaveOccurred())
118
119                         success, err = (&BeNumericallyMatcher{Comparator: "~", CompareTo: []interface{}{3.0, "foo"}}).Match(5.0)
120                         Ω(success).Should(BeFalse())
121                         Ω(err).Should(HaveOccurred())
122
123                         success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match(5)
124                         Ω(success).Should(BeFalse())
125                         Ω(err).Should(HaveOccurred())
126
127                         success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match("foo")
128                         Ω(success).Should(BeFalse())
129                         Ω(err).Should(HaveOccurred())
130
131                         success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{nil}}).Match(0)
132                         Ω(success).Should(BeFalse())
133                         Ω(err).Should(HaveOccurred())
134
135                         success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{0}}).Match(nil)
136                         Ω(success).Should(BeFalse())
137                         Ω(err).Should(HaveOccurred())
138                 })
139         })
140
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())
146                 })
147         })
148 })