...

Source file src/github.com/onsi/gomega/matchers/or_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("OrMatcher", func() {
    10  	It("works with positive cases", func() {
    11  		Expect(input).To(Or(true1))
    12  		Expect(input).To(Or(true1, true2))
    13  		Expect(input).To(Or(true1, false1))
    14  		Expect(input).To(Or(false1, true2))
    15  		Expect(input).To(Or(true1, true2, true3))
    16  		Expect(input).To(Or(true1, true2, false3))
    17  		Expect(input).To(Or(true1, false2, true3))
    18  		Expect(input).To(Or(false1, true2, true3))
    19  		Expect(input).To(Or(true1, false2, false3))
    20  		Expect(input).To(Or(false1, false2, true3))
    21  
    22  		// use alias
    23  		Expect(input).To(SatisfyAny(false1, false2, true3))
    24  	})
    25  
    26  	It("stops on errors", func() {
    27  		failuresMessages := InterceptGomegaFailures(func() {
    28  			Expect(input).To(Or(Panic(), true1))
    29  		})
    30  		Expect(failuresMessages).To(Equal([]string{"PanicMatcher expects a function.  Got:\n    <string>: hi"}))
    31  	})
    32  
    33  	It("works with negative cases", func() {
    34  		Expect(input).ToNot(Or())
    35  		Expect(input).ToNot(Or(false1))
    36  		Expect(input).ToNot(Or(false1, false2))
    37  		Expect(input).ToNot(Or(false1, false2, false3))
    38  	})
    39  
    40  	Context("failure messages", func() {
    41  		When("match fails", func() {
    42  			It("gives a descriptive message", func() {
    43  				verifyFailureMessage(Or(false1, false2), input,
    44  					"To satisfy at least one of these matchers: [%!s(*matchers.HaveLenMatcher=&{1}) %!s(*matchers.EqualMatcher=&{hip})]")
    45  			})
    46  		})
    47  
    48  		When("match succeeds, but expected it to fail", func() {
    49  			It("gives a descriptive message", func() {
    50  				verifyFailureMessage(Not(Or(true1, true2)), input, `not to have length 2`)
    51  			})
    52  		})
    53  	})
    54  
    55  	Context("MatchMayChangeInTheFuture", func() {
    56  		Context("Match returned false", func() {
    57  			It("returns true if any of the matchers could change", func() {
    58  				// 3 matchers, all return false, and all could change
    59  				m := Or(BeNil(), Equal("hip"), HaveLen(1))
    60  				Expect(m.Match("hi")).To(BeFalse())
    61  				Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // all 3 of these matchers default to 'true'
    62  			})
    63  			It("returns false if none of the matchers could change", func() {
    64  				// empty Or() has the property of never matching, and never can change since there are no sub-matchers that could change
    65  				m := Or()
    66  				Expect(m.Match("anything")).To(BeFalse())
    67  				Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
    68  
    69  				// Or() with 3 sub-matchers that return false, and can't change
    70  				m = Or(Or(), Or(), Or())
    71  				Expect(m.Match("hi")).To(BeFalse())
    72  				Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // the 3 empty Or()'s won't change
    73  			})
    74  		})
    75  		Context("Match returned true", func() {
    76  			Context("returns value of the successful matcher", func() {
    77  				It("false if successful matcher not going to change", func() {
    78  					// 3 matchers: 1st returns false, 2nd returns true and is not going to change, 3rd is never called
    79  					m := Or(BeNil(), And(), Equal(1))
    80  					Expect(m.Match("hi")).To(BeTrue())
    81  					Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse())
    82  				})
    83  				It("true if successful matcher indicates it might change", func() {
    84  					// 3 matchers: 1st returns false, 2nd returns true and "might" change, 3rd is never called
    85  					m := Or(Not(BeNil()), Equal("hi"), Equal(1))
    86  					Expect(m.Match("hi")).To(BeTrue())
    87  					Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // Equal("hi") indicates it might change
    88  				})
    89  			})
    90  		})
    91  	})
    92  })
    93  

View as plain text