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 "github.com/onsi/gomega/types"
8 )
9
10
11 var (
12
13 input = "hi"
14
15 true1 = HaveLen(2)
16 true2 = Equal("hi")
17 true3 = MatchRegexp("hi")
18
19 false1 = HaveLen(1)
20 false2 = Equal("hip")
21 false3 = MatchRegexp("hope")
22 )
23
24
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
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 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 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
71 m := And(Not(BeNil()), Or(), Equal(1))
72 Expect(m.Match("hi")).To(BeFalse())
73 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse())
74 })
75 It("true if failed matcher indicates it might change", func() {
76
77 m := And(Not(BeNil()), Equal(5), Equal(1))
78 Expect(m.Match("hi")).To(BeFalse())
79 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue())
80 })
81 })
82 })
83 Context("Match returned true", func() {
84 It("returns true if any of the matchers could change", func() {
85
86 m := And(Not(BeNil()), Equal("hi"), HaveLen(2))
87 Expect(m.Match("hi")).To(BeTrue())
88 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue())
89 })
90 It("returns false if none of the matchers could change", func() {
91
92 m := And()
93 Expect(m.Match("anything")).To(BeTrue())
94 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
95
96
97 m = And(And(), And(), And())
98 Expect(m.Match("hi")).To(BeTrue())
99 Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse())
100 })
101 })
102 })
103 })
104
View as plain text