...

Source file src/github.com/onsi/gomega/matchers/be_numerically_matcher_test.go

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  	. "github.com/onsi/gomega/matchers"
     7  )
     8  
     9  var _ = Describe("BeNumerically", func() {
    10  	When("passed a number", func() {
    11  		It("should support ==", func() {
    12  			Expect(uint32(5)).Should(BeNumerically("==", 5))
    13  			Expect(float64(5.0)).Should(BeNumerically("==", 5))
    14  			Expect(int8(5)).Should(BeNumerically("==", 5))
    15  		})
    16  
    17  		It("should not have false positives", func() {
    18  			Expect(5.1).ShouldNot(BeNumerically("==", 5))
    19  			Expect(5).ShouldNot(BeNumerically("==", 5.1))
    20  		})
    21  
    22  		It("should support >", func() {
    23  			Expect(uint32(5)).Should(BeNumerically(">", 4))
    24  			Expect(float64(5.0)).Should(BeNumerically(">", 4.9))
    25  			Expect(int8(5)).Should(BeNumerically(">", 4))
    26  
    27  			Expect(uint32(5)).ShouldNot(BeNumerically(">", 5))
    28  			Expect(float64(5.0)).ShouldNot(BeNumerically(">", 5.0))
    29  			Expect(int8(5)).ShouldNot(BeNumerically(">", 5))
    30  		})
    31  
    32  		It("should support <", func() {
    33  			Expect(uint32(5)).Should(BeNumerically("<", 6))
    34  			Expect(float64(5.0)).Should(BeNumerically("<", 5.1))
    35  			Expect(int8(5)).Should(BeNumerically("<", 6))
    36  
    37  			Expect(uint32(5)).ShouldNot(BeNumerically("<", 5))
    38  			Expect(float64(5.0)).ShouldNot(BeNumerically("<", 5.0))
    39  			Expect(int8(5)).ShouldNot(BeNumerically("<", 5))
    40  		})
    41  
    42  		It("should support >=", func() {
    43  			Expect(uint32(5)).Should(BeNumerically(">=", 4))
    44  			Expect(float64(5.0)).Should(BeNumerically(">=", 4.9))
    45  			Expect(int8(5)).Should(BeNumerically(">=", 4))
    46  
    47  			Expect(uint32(5)).Should(BeNumerically(">=", 5))
    48  			Expect(float64(5.0)).Should(BeNumerically(">=", 5.0))
    49  			Expect(int8(5)).Should(BeNumerically(">=", 5))
    50  
    51  			Expect(uint32(5)).ShouldNot(BeNumerically(">=", 6))
    52  			Expect(float64(5.0)).ShouldNot(BeNumerically(">=", 5.1))
    53  			Expect(int8(5)).ShouldNot(BeNumerically(">=", 6))
    54  		})
    55  
    56  		It("should support <=", func() {
    57  			Expect(uint32(5)).Should(BeNumerically("<=", 6))
    58  			Expect(float64(5.0)).Should(BeNumerically("<=", 5.1))
    59  			Expect(int8(5)).Should(BeNumerically("<=", 6))
    60  
    61  			Expect(uint32(5)).Should(BeNumerically("<=", 5))
    62  			Expect(float64(5.0)).Should(BeNumerically("<=", 5.0))
    63  			Expect(int8(5)).Should(BeNumerically("<=", 5))
    64  
    65  			Expect(uint32(5)).ShouldNot(BeNumerically("<=", 4))
    66  			Expect(float64(5.0)).ShouldNot(BeNumerically("<=", 4.9))
    67  			Expect(int8(5)).Should(BeNumerically("<=", 5))
    68  		})
    69  
    70  		When("passed ~", func() {
    71  			When("passed a float", func() {
    72  				Context("and there is no precision parameter", func() {
    73  					It("should default to 1e-8", func() {
    74  						Expect(5.00000001).Should(BeNumerically("~", 5.00000002))
    75  						Expect(5.00000001).ShouldNot(BeNumerically("~", 5.0000001))
    76  					})
    77  
    78  					It("should show failure message", func() {
    79  						actual := BeNumerically("~", 4.98).FailureMessage(123)
    80  						expected := "Expected\n    <int>: 123\nto be ~\n    <float64>: 4.98"
    81  						Expect(actual).To(Equal(expected))
    82  					})
    83  
    84  					It("should show negated failure message", func() {
    85  						actual := BeNumerically("~", 4.98).NegatedFailureMessage(123)
    86  						expected := "Expected\n    <int>: 123\nnot to be ~\n    <float64>: 4.98"
    87  						Expect(actual).To(Equal(expected))
    88  					})
    89  				})
    90  
    91  				Context("and there is a precision parameter", func() {
    92  					It("should use the precision parameter", func() {
    93  						Expect(5.1).Should(BeNumerically("~", 5.19, 0.1))
    94  						Expect(5.1).Should(BeNumerically("~", 5.01, 0.1))
    95  						Expect(5.1).ShouldNot(BeNumerically("~", 5.22, 0.1))
    96  						Expect(5.1).ShouldNot(BeNumerically("~", 4.98, 0.1))
    97  					})
    98  
    99  					It("should show precision in failure message", func() {
   100  						actual := BeNumerically("~", 4.98, 0.1).FailureMessage(123)
   101  						expected := "Expected\n    <int>: 123\nto be within 0.1 of ~\n    <float64>: 4.98"
   102  						Expect(actual).To(Equal(expected))
   103  					})
   104  
   105  					It("should show precision in negated failure message", func() {
   106  						actual := BeNumerically("~", 4.98, 0.1).NegatedFailureMessage(123)
   107  						expected := "Expected\n    <int>: 123\nnot to be within 0.1 of ~\n    <float64>: 4.98"
   108  						Expect(actual).To(Equal(expected))
   109  					})
   110  				})
   111  			})
   112  
   113  			When("passed an int/uint", func() {
   114  				Context("and there is no precision parameter", func() {
   115  					It("should just do strict equality", func() {
   116  						Expect(5).Should(BeNumerically("~", 5))
   117  						Expect(5).ShouldNot(BeNumerically("~", 6))
   118  						Expect(uint(5)).ShouldNot(BeNumerically("~", 6))
   119  					})
   120  				})
   121  
   122  				Context("and there is a precision parameter", func() {
   123  					It("should use precision paramter", func() {
   124  						Expect(5).Should(BeNumerically("~", 6, 2))
   125  						Expect(5).ShouldNot(BeNumerically("~", 8, 2))
   126  						Expect(uint(5)).Should(BeNumerically("~", 6, 1))
   127  					})
   128  				})
   129  			})
   130  		})
   131  	})
   132  
   133  	When("passed a non-number", func() {
   134  		It("should error", func() {
   135  			success, err := (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{5}}).Match("foo")
   136  			Expect(success).Should(BeFalse())
   137  			Expect(err).Should(HaveOccurred())
   138  
   139  			success, err = (&BeNumericallyMatcher{Comparator: "=="}).Match(5)
   140  			Expect(success).Should(BeFalse())
   141  			Expect(err).Should(HaveOccurred())
   142  
   143  			success, err = (&BeNumericallyMatcher{Comparator: "~", CompareTo: []interface{}{3.0, "foo"}}).Match(5.0)
   144  			Expect(success).Should(BeFalse())
   145  			Expect(err).Should(HaveOccurred())
   146  			Expect(err.Error()).Should(ContainSubstring("foo"))
   147  
   148  			success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match(5)
   149  			Expect(success).Should(BeFalse())
   150  			Expect(err).Should(HaveOccurred())
   151  
   152  			success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match("foo")
   153  			Expect(success).Should(BeFalse())
   154  			Expect(err).Should(HaveOccurred())
   155  
   156  			success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{nil}}).Match(0)
   157  			Expect(success).Should(BeFalse())
   158  			Expect(err).Should(HaveOccurred())
   159  
   160  			success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{0}}).Match(nil)
   161  			Expect(success).Should(BeFalse())
   162  			Expect(err).Should(HaveOccurred())
   163  		})
   164  	})
   165  
   166  	When("passed an unsupported comparator", func() {
   167  		It("should error", func() {
   168  			success, err := (&BeNumericallyMatcher{Comparator: "!=", CompareTo: []interface{}{5}}).Match(4)
   169  			Expect(success).Should(BeFalse())
   170  			Expect(err).Should(HaveOccurred())
   171  		})
   172  	})
   173  })
   174  

View as plain text