initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / and_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         "github.com/onsi/gomega/types"
8 )
9
10 // sample data
11 var (
12         // example input
13         input = "hi"
14         // some matchers that succeed against the input
15         true1 = HaveLen(2)
16         true2 = Equal("hi")
17         true3 = MatchRegexp("hi")
18         // some matchers that fail against the input.
19         false1 = HaveLen(1)
20         false2 = Equal("hip")
21         false3 = MatchRegexp("hope")
22 )
23
24 // verifyFailureMessage expects the matcher to fail with the given input, and verifies the failure message.
25 func verifyFailureMessage(m types.GomegaMatcher, input string, expectedFailureMsgFragment string) {
26         Expect(m.Match(input)).To(BeFalse())
27         Expect(m.FailureMessage(input)).To(Equal(
28                 "Expected\n    <string>: " + input + "\n" + expectedFailureMsgFragment))
29 }
30
31 var _ = Describe("AndMatcher", func() {
32         It("works with positive cases", func() {
33                 Expect(input).To(And())
34                 Expect(input).To(And(true1))
35                 Expect(input).To(And(true1, true2))
36                 Expect(input).To(And(true1, true2, true3))
37
38                 // use alias
39                 Expect(input).To(SatisfyAll(true1, true2, true3))
40         })
41
42         It("works with negative cases", func() {
43                 Expect(input).ToNot(And(false1, false2))
44                 Expect(input).ToNot(And(true1, true2, false3))
45                 Expect(input).ToNot(And(true1, false2, false3))
46                 Expect(input).ToNot(And(false1, true1, true2))
47         })
48
49         Context("failure messages", func() {
50                 Context("when match fails", func() {
51                         It("gives a descriptive message", func() {
52                                 verifyFailureMessage(And(false1, true1), input, "to have length 1")
53                                 verifyFailureMessage(And(true1, false2), input, "to equal\n    <string>: hip")
54                                 verifyFailureMessage(And(true1, true2, false3), input, "to match regular expression\n    <string>: hope")
55                         })
56                 })
57
58                 Context("when match succeeds, but expected it to fail", func() {
59                         It("gives a descriptive message", func() {
60                                 verifyFailureMessage(Not(And(true1, true2)), input,
61                                         `To not satisfy all of these matchers: [%!s(*matchers.HaveLenMatcher=&{2}) %!s(*matchers.EqualMatcher=&{hi})]`)
62                         })
63                 })
64         })
65
66         Context("MatchMayChangeInTheFuture", func() {
67                 Context("Match returned false", func() {
68                         Context("returns value of the failed matcher", func() {
69                                 It("false if failed matcher not going to change", func() {
70                                         // 3 matchers: 1st returns true, 2nd returns false and is not going to change, 3rd is never called
71                                         m := And(Not(BeNil()), Or(), Equal(1))
72                                         Expect(m.Match("hi")).To(BeFalse())
73                                         Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // empty Or() indicates not going to change
74                                 })
75                                 It("true if failed matcher indicates it might change", func() {
76                                         // 3 matchers: 1st returns true, 2nd returns false and "might" change, 3rd is never called
77                                         m := And(Not(BeNil()), Equal(5), Equal(1))
78                                         Expect(m.Match("hi")).To(BeFalse())
79                                         Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // Equal(5) indicates it might change
80                                 })
81                         })
82                 })
83                 Context("Match returned true", func() {
84                         It("returns true if any of the matchers could change", func() {
85                                 // 3 matchers, all return true, and all could change
86                                 m := And(Not(BeNil()), Equal("hi"), HaveLen(2))
87                                 Expect(m.Match("hi")).To(BeTrue())
88                                 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // all 3 of these matchers default to 'true'
89                         })
90                         It("returns false if none of the matchers could change", func() {
91                                 // empty And() has the property of always matching, and never can change since there are no sub-matchers that could change
92                                 m := And()
93                                 Expect(m.Match("anything")).To(BeTrue())
94                                 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
95
96                                 // And() with 3 sub-matchers that return true, and can't change
97                                 m = And(And(), And(), And())
98                                 Expect(m.Match("hi")).To(BeTrue())
99                                 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // the 3 empty And()'s won't change
100                         })
101                 })
102         })
103 })