github.com/onsi/gomega@v1.32.0/internal/assertion_test.go (about) 1 package internal_test 2 3 import ( 4 "errors" 5 "reflect" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("Making Synchronous Assertions", func() { 12 var SHOULD_MATCH = true 13 var SHOULD_NOT_MATCH = false 14 var IT_PASSES = true 15 var IT_FAILS = false 16 17 Extras := func(extras ...interface{}) []interface{} { 18 return extras 19 } 20 21 OptionalDescription := func(optionalDescription ...interface{}) []interface{} { 22 return optionalDescription 23 } 24 25 DescribeTable( 26 "the various cases", 27 func(actual interface{}, extras []interface{}, optionalDescription []interface{}, isPositiveAssertion bool, expectedFailureMessage string, expectedReturnValue bool) { 28 if isPositiveAssertion { 29 ig := NewInstrumentedGomega() 30 returnValue := ig.G.Expect(actual, extras...).To(SpecMatch(), optionalDescription...) 31 Expect(returnValue).To(Equal(expectedReturnValue)) 32 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 33 if expectedFailureMessage != "" { 34 Expect(ig.FailureSkip).To(Equal([]int{2})) 35 } 36 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).To")) 37 38 ig = NewInstrumentedGomega() 39 returnValue = ig.G.ExpectWithOffset(3, actual, extras...).To(SpecMatch(), optionalDescription...) 40 Expect(returnValue).To(Equal(expectedReturnValue)) 41 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 42 if expectedFailureMessage != "" { 43 Expect(ig.FailureSkip).To(Equal([]int{5})) 44 } 45 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).To")) 46 47 ig = NewInstrumentedGomega() 48 returnValue = ig.G.Ω(actual, extras...).Should(SpecMatch(), optionalDescription...) 49 Expect(returnValue).To(Equal(expectedReturnValue)) 50 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 51 if expectedFailureMessage != "" { 52 Expect(ig.FailureSkip).To(Equal([]int{2})) 53 } 54 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).Should")) 55 } else { 56 ig := NewInstrumentedGomega() 57 returnValue := ig.G.Expect(actual, extras...).ToNot(SpecMatch(), optionalDescription...) 58 Expect(returnValue).To(Equal(expectedReturnValue)) 59 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 60 if expectedFailureMessage != "" { 61 Expect(ig.FailureSkip).To(Equal([]int{2})) 62 } 63 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).ToNot")) 64 65 ig = NewInstrumentedGomega() 66 returnValue = ig.G.Expect(actual, extras...).NotTo(SpecMatch(), optionalDescription...) 67 Expect(returnValue).To(Equal(expectedReturnValue)) 68 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 69 if expectedFailureMessage != "" { 70 Expect(ig.FailureSkip).To(Equal([]int{2})) 71 } 72 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).NotTo")) 73 74 ig = NewInstrumentedGomega() 75 returnValue = ig.G.ExpectWithOffset(3, actual, extras...).NotTo(SpecMatch(), optionalDescription...) 76 Expect(returnValue).To(Equal(expectedReturnValue)) 77 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 78 if expectedFailureMessage != "" { 79 Expect(ig.FailureSkip).To(Equal([]int{5})) 80 } 81 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).NotTo")) 82 83 ig = NewInstrumentedGomega() 84 returnValue = ig.G.Ω(actual, extras...).ShouldNot(SpecMatch(), optionalDescription...) 85 Expect(returnValue).To(Equal(expectedReturnValue)) 86 Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage)) 87 if expectedFailureMessage != "" { 88 Expect(ig.FailureSkip).To(Equal([]int{2})) 89 } 90 Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).ShouldNot")) 91 } 92 }, 93 Entry( 94 "when the matcher matches and a positive assertion is being made", 95 MATCH, Extras(), OptionalDescription(), 96 SHOULD_MATCH, "", IT_PASSES, 97 ), 98 Entry( 99 "when the matcher matches and a negative assertion is being made", 100 MATCH, Extras(), OptionalDescription(), 101 SHOULD_NOT_MATCH, "negative: match", IT_FAILS, 102 ), 103 Entry( 104 "when the matcher does not match and a positive assertion is being made", 105 NO_MATCH, Extras(), OptionalDescription(), 106 SHOULD_MATCH, "positive: no match", IT_FAILS, 107 ), 108 Entry( 109 "when the matcher does not match and a negative assertion is being made", 110 NO_MATCH, Extras(), OptionalDescription(), 111 SHOULD_NOT_MATCH, "", IT_PASSES, 112 ), 113 Entry( 114 "when the matcher returns an error and a positive assertion is being made", 115 ERR_MATCH, Extras(), OptionalDescription(), 116 SHOULD_MATCH, "spec matcher error", IT_FAILS, 117 ), 118 Entry( 119 "when the matcher returns an error and a negative assertion is being made", 120 ERR_MATCH, Extras(), OptionalDescription(), 121 SHOULD_NOT_MATCH, "spec matcher error", IT_FAILS, 122 ), 123 Entry( 124 "when a failure occurs and there is a single optional description", 125 NO_MATCH, Extras(), OptionalDescription("a description"), 126 SHOULD_MATCH, "a description\npositive: no match", IT_FAILS, 127 ), 128 Entry( 129 "when a failure occurs and there are multiple optional descriptions", 130 NO_MATCH, Extras(), OptionalDescription("a description of [%d]", 3), 131 SHOULD_MATCH, "a description of [3]\npositive: no match", IT_FAILS, 132 ), 133 Entry( 134 "when a failure occurs and the optional description is a function", 135 NO_MATCH, Extras(), OptionalDescription(func() string { return "a description" }), 136 SHOULD_MATCH, "a description\npositive: no match", IT_FAILS, 137 ), 138 Entry( 139 "when the matcher matches and zero-valued extra parameters are included, it passes", 140 MATCH, Extras(0, "", struct{ Foo string }{}, nil), OptionalDescription(), 141 SHOULD_MATCH, "", IT_PASSES, 142 ), 143 Entry( 144 "when the matcher matches but a non-zero-valued extra parameter is included, it fails", 145 MATCH, Extras(1, "bam", struct{ Foo string }{Foo: "foo"}, nil), OptionalDescription(), 146 SHOULD_MATCH, "Unexpected non-nil/non-zero argument at index 1:\n\t<int>: 1", IT_FAILS, 147 ), 148 Entry( 149 "when the matcher matches but an error is included, it fails", 150 MATCH, Extras(0, "", errors.New("welp!")), OptionalDescription(), 151 SHOULD_MATCH, "Unexpected error: welp!", IT_FAILS, 152 ), 153 ) 154 155 var SHOULD_OCCUR = true 156 var SHOULD_NOT_OCCUR = false 157 158 DescribeTable("error expectations", 159 func(a, b int, e error, isPositiveAssertion bool, expectedFailureMessage string, expectedReturnValue bool) { 160 abe := func(a, b int, e error) (int, int, error) { 161 return a, b, e 162 } 163 ig := NewInstrumentedGomega() 164 var returnValue bool 165 if isPositiveAssertion { 166 returnValue = ig.G.Expect(abe(a, b, e)).Error().To(HaveOccurred()) 167 } else { 168 returnValue = ig.G.Expect(abe(a, b, e)).Error().NotTo(HaveOccurred()) 169 } 170 Expect(returnValue).To(Equal(expectedReturnValue)) 171 Expect(ig.FailureMessage).To(Equal(expectedFailureMessage)) 172 if expectedFailureMessage != "" { 173 Expect(ig.FailureSkip).To(Equal([]int{2})) 174 } 175 }, 176 Entry( 177 "when non-zero results without error", 178 1, 2, nil, 179 SHOULD_NOT_OCCUR, "", IT_PASSES, 180 ), 181 Entry( 182 "when non-zero results with error", 183 1, 2, errors.New("D'oh!"), 184 SHOULD_NOT_OCCUR, "Unexpected non-nil/non-zero argument at index 0:\n\t<int>: 1", IT_FAILS, 185 ), 186 Entry( 187 "when non-zero results without error", 188 0, 0, errors.New("D'oh!"), 189 SHOULD_OCCUR, "", IT_PASSES, 190 ), 191 Entry( 192 "when non-zero results with error", 193 1, 2, errors.New("D'oh!"), 194 SHOULD_OCCUR, "Unexpected non-nil/non-zero argument at index 0:\n\t<int>: 1", IT_FAILS, 195 ), 196 ) 197 198 When("vetting optional description parameters", func() { 199 It("panics when Gomega matcher is at the beginning of optional description parameters", func() { 200 ig := NewInstrumentedGomega() 201 for _, expectator := range []string{ 202 "To", "NotTo", "ToNot", 203 "Should", "ShouldNot", 204 } { 205 Expect(func() { 206 expect := ig.G.Expect(42) // sic! 207 meth := reflect.ValueOf(expect).MethodByName(expectator) 208 Expect(meth.IsValid()).To(BeTrue()) 209 meth.Call([]reflect.Value{ 210 reflect.ValueOf(HaveLen(1)), 211 reflect.ValueOf(ContainElement(42)), 212 }) 213 }).To(PanicWith(MatchRegexp("Assertion has a GomegaMatcher as the first element of optionalDescription"))) 214 } 215 }) 216 217 It("accepts Gomega matchers in optional description parameters after the first", func() { 218 Expect(func() { 219 ig := NewInstrumentedGomega() 220 ig.G.Expect(42).To(HaveLen(1), "foo", ContainElement(42)) 221 }).NotTo(Panic()) 222 }) 223 224 }) 225 226 })