assertion_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package assertion_test
  2. import (
  3. "errors"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. . "github.com/onsi/gomega/internal/assertion"
  7. "github.com/onsi/gomega/internal/fakematcher"
  8. )
  9. var _ = Describe("Assertion", func() {
  10. var (
  11. a *Assertion
  12. failureMessage string
  13. failureCallerSkip int
  14. matcher *fakematcher.FakeMatcher
  15. )
  16. input := "The thing I'm testing"
  17. var fakeFailHandler = func(message string, callerSkip ...int) {
  18. failureMessage = message
  19. if len(callerSkip) == 1 {
  20. failureCallerSkip = callerSkip[0]
  21. }
  22. }
  23. BeforeEach(func() {
  24. matcher = &fakematcher.FakeMatcher{}
  25. failureMessage = ""
  26. failureCallerSkip = 0
  27. a = New(input, fakeFailHandler, 1)
  28. })
  29. Context("when called", func() {
  30. It("should pass the provided input value to the matcher", func() {
  31. a.Should(matcher)
  32. Ω(matcher.ReceivedActual).Should(Equal(input))
  33. matcher.ReceivedActual = ""
  34. a.ShouldNot(matcher)
  35. Ω(matcher.ReceivedActual).Should(Equal(input))
  36. matcher.ReceivedActual = ""
  37. a.To(matcher)
  38. Ω(matcher.ReceivedActual).Should(Equal(input))
  39. matcher.ReceivedActual = ""
  40. a.ToNot(matcher)
  41. Ω(matcher.ReceivedActual).Should(Equal(input))
  42. matcher.ReceivedActual = ""
  43. a.NotTo(matcher)
  44. Ω(matcher.ReceivedActual).Should(Equal(input))
  45. })
  46. })
  47. Context("when the matcher succeeds", func() {
  48. BeforeEach(func() {
  49. matcher.MatchesToReturn = true
  50. matcher.ErrToReturn = nil
  51. })
  52. Context("and a positive assertion is being made", func() {
  53. It("should not call the failure callback", func() {
  54. a.Should(matcher)
  55. Ω(failureMessage).Should(Equal(""))
  56. })
  57. It("should be true", func() {
  58. Ω(a.Should(matcher)).Should(BeTrue())
  59. })
  60. })
  61. Context("and a negative assertion is being made", func() {
  62. It("should call the failure callback", func() {
  63. a.ShouldNot(matcher)
  64. Ω(failureMessage).Should(Equal("negative: The thing I'm testing"))
  65. Ω(failureCallerSkip).Should(Equal(3))
  66. })
  67. It("should be false", func() {
  68. Ω(a.ShouldNot(matcher)).Should(BeFalse())
  69. })
  70. })
  71. })
  72. Context("when the matcher fails", func() {
  73. BeforeEach(func() {
  74. matcher.MatchesToReturn = false
  75. matcher.ErrToReturn = nil
  76. })
  77. Context("and a positive assertion is being made", func() {
  78. It("should call the failure callback", func() {
  79. a.Should(matcher)
  80. Ω(failureMessage).Should(Equal("positive: The thing I'm testing"))
  81. Ω(failureCallerSkip).Should(Equal(3))
  82. })
  83. It("should be false", func() {
  84. Ω(a.Should(matcher)).Should(BeFalse())
  85. })
  86. })
  87. Context("and a negative assertion is being made", func() {
  88. It("should not call the failure callback", func() {
  89. a.ShouldNot(matcher)
  90. Ω(failureMessage).Should(Equal(""))
  91. })
  92. It("should be true", func() {
  93. Ω(a.ShouldNot(matcher)).Should(BeTrue())
  94. })
  95. })
  96. })
  97. Context("When reporting a failure", func() {
  98. BeforeEach(func() {
  99. matcher.MatchesToReturn = false
  100. matcher.ErrToReturn = nil
  101. })
  102. Context("and there is an optional description", func() {
  103. It("should append the description to the failure message", func() {
  104. a.Should(matcher, "A description")
  105. Ω(failureMessage).Should(Equal("A description\npositive: The thing I'm testing"))
  106. Ω(failureCallerSkip).Should(Equal(3))
  107. })
  108. })
  109. Context("and there are multiple arguments to the optional description", func() {
  110. It("should append the formatted description to the failure message", func() {
  111. a.Should(matcher, "A description of [%d]", 3)
  112. Ω(failureMessage).Should(Equal("A description of [3]\npositive: The thing I'm testing"))
  113. Ω(failureCallerSkip).Should(Equal(3))
  114. })
  115. })
  116. })
  117. Context("When the matcher returns an error", func() {
  118. BeforeEach(func() {
  119. matcher.ErrToReturn = errors.New("Kaboom!")
  120. })
  121. Context("and a positive assertion is being made", func() {
  122. It("should call the failure callback", func() {
  123. matcher.MatchesToReturn = true
  124. a.Should(matcher)
  125. Ω(failureMessage).Should(Equal("Kaboom!"))
  126. Ω(failureCallerSkip).Should(Equal(3))
  127. })
  128. })
  129. Context("and a negative assertion is being made", func() {
  130. It("should call the failure callback", func() {
  131. matcher.MatchesToReturn = false
  132. a.ShouldNot(matcher)
  133. Ω(failureMessage).Should(Equal("Kaboom!"))
  134. Ω(failureCallerSkip).Should(Equal(3))
  135. })
  136. })
  137. It("should always be false", func() {
  138. Ω(a.Should(matcher)).Should(BeFalse())
  139. Ω(a.ShouldNot(matcher)).Should(BeFalse())
  140. })
  141. })
  142. Context("when there are extra parameters", func() {
  143. It("(a simple example)", func() {
  144. Ω(func() (string, int, error) {
  145. return "foo", 0, nil
  146. }()).Should(Equal("foo"))
  147. })
  148. Context("when the parameters are all nil or zero", func() {
  149. It("should invoke the matcher", func() {
  150. matcher.MatchesToReturn = true
  151. matcher.ErrToReturn = nil
  152. var typedNil []string
  153. a = New(input, fakeFailHandler, 1, 0, nil, typedNil)
  154. result := a.Should(matcher)
  155. Ω(result).Should(BeTrue())
  156. Ω(matcher.ReceivedActual).Should(Equal(input))
  157. Ω(failureMessage).Should(BeZero())
  158. })
  159. })
  160. Context("when any of the parameters are not nil or zero", func() {
  161. It("should call the failure callback", func() {
  162. matcher.MatchesToReturn = false
  163. matcher.ErrToReturn = nil
  164. a = New(input, fakeFailHandler, 1, errors.New("foo"))
  165. result := a.Should(matcher)
  166. Ω(result).Should(BeFalse())
  167. Ω(matcher.ReceivedActual).Should(BeZero(), "The matcher doesn't even get called")
  168. Ω(failureMessage).Should(ContainSubstring("foo"))
  169. failureMessage = ""
  170. a = New(input, fakeFailHandler, 1, nil, 1)
  171. result = a.ShouldNot(matcher)
  172. Ω(result).Should(BeFalse())
  173. Ω(failureMessage).Should(ContainSubstring("1"))
  174. failureMessage = ""
  175. a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
  176. result = a.To(matcher)
  177. Ω(result).Should(BeFalse())
  178. Ω(failureMessage).Should(ContainSubstring("foo"))
  179. failureMessage = ""
  180. a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
  181. result = a.ToNot(matcher)
  182. Ω(result).Should(BeFalse())
  183. Ω(failureMessage).Should(ContainSubstring("foo"))
  184. failureMessage = ""
  185. a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
  186. result = a.NotTo(matcher)
  187. Ω(result).Should(BeFalse())
  188. Ω(failureMessage).Should(ContainSubstring("foo"))
  189. Ω(failureCallerSkip).Should(Equal(3))
  190. })
  191. })
  192. })
  193. Context("Making an assertion without a registered fail handler", func() {
  194. It("should panic", func() {
  195. defer func() {
  196. e := recover()
  197. RegisterFailHandler(Fail)
  198. if e == nil {
  199. Fail("expected a panic to have occurred")
  200. }
  201. }()
  202. RegisterFailHandler(nil)
  203. Ω(true).Should(BeTrue())
  204. })
  205. })
  206. })