@@ -172,5 +172,99 @@ describe "expectations" do
172172    it " pass if raises MyError" do 
173173      expect_raises(Exception , " Ops" raise  Exception .new(" Ops" 
174174    end 
175+ 
176+     describe " failure message format" do 
177+       context " given string to compare with message" do 
178+         it " contains expected exception, actual exception and backtrace" do 
179+           expect_raises(Exception , " digits should be non-negative" do 
180+             raise  IndexError .new(" Index out of bounds" 
181+           end 
182+         rescue  e : Spec ::AssertionFailed 
183+           e.message.as(String ).should contain(<<-MESSAGE )
184+             Expected Exception with message containing: "digits should be non-negative" 
185+                  got IndexError with message: "Index out of bounds" 
186+             Backtrace: 
187+               # spec/std/spec/expectations_spec.cr:#{ __LINE__  -  7 }  
188+             MESSAGE 
189+         else 
190+           raise  " nothing is raised" 
191+         end 
192+ 
193+         it " contains expected class, actual exception and backtrace when expected class does not match actual class" do 
194+           expect_raises(ArgumentError , " digits should be non-negative" do 
195+             raise  IndexError .new(" Index out of bounds" 
196+           end 
197+         rescue  e : Spec ::AssertionFailed 
198+           e.message.as(String ).should contain(<<-MESSAGE )
199+             Expected ArgumentError 
200+                  got IndexError with message: "Index out of bounds" 
201+             Backtrace: 
202+               # spec/std/spec/expectations_spec.cr:#{ __LINE__  -  7 }  
203+             MESSAGE 
204+         else 
205+           raise  " nothing is raised" 
206+         end 
207+ 
208+         it " escapes expected and actual messages in the same way" do 
209+           expect_raises(Exception , %q( a\tb\nc) do 
210+             raise  %q( a\tb\nc) 
211+           end 
212+         rescue  e : Spec ::AssertionFailed 
213+           e.message.as(String ).should contain(" Expected Exception with message containing: #{ %q( a\tb\nc) } " 
214+           e.message.as(String ).should contain(" got Exception with message: #{ %q( a\tb\nc) } " 
215+         else 
216+           raise  " nothing is raised" 
217+         end 
218+       end 
219+ 
220+       context " given regex to match a message" do 
221+         it " contains expected exception, actual exception and backtrace" do 
222+           expect_raises(Exception , /digits should be non-negative/ ) do 
223+             raise  IndexError .new(" Index out of bounds" 
224+           end 
225+         rescue  e : Spec ::AssertionFailed 
226+           e.message.as(String ).should contain(<<-MESSAGE )
227+             Expected Exception with message matching: /digits should be non-negative/ 
228+                  got IndexError with message: "Index out of bounds" 
229+             Backtrace: 
230+               # spec/std/spec/expectations_spec.cr:#{ __LINE__  -  7 }  
231+             MESSAGE 
232+         else 
233+           raise  " nothing is raised" 
234+         end 
235+ 
236+         it " contains expected class, actual exception and backtrace when expected class does not match actual class" do 
237+           expect_raises(ArgumentError , /digits should be non-negative/ ) do 
238+             raise  IndexError .new(" Index out of bounds" 
239+           end 
240+         rescue  e : Spec ::AssertionFailed 
241+           e.message.as(String ).should contain(<<-MESSAGE )
242+             Expected ArgumentError 
243+                  got IndexError with message: "Index out of bounds" 
244+             Backtrace: 
245+               # spec/std/spec/expectations_spec.cr:#{ __LINE__  -  7 }  
246+             MESSAGE 
247+         else 
248+           raise  " nothing is raised" 
249+         end 
250+       end 
251+ 
252+       context " given nil to allow any message" do 
253+         it " contains expected class, actual exception and backtrace when expected class does not match actual class" do 
254+           expect_raises(ArgumentError , nil ) do 
255+             raise  IndexError .new(" Index out of bounds" 
256+           end 
257+         rescue  e : Spec ::AssertionFailed 
258+           e.message.as(String ).should contain(<<-MESSAGE )
259+             Expected ArgumentError 
260+                  got IndexError with message: "Index out of bounds" 
261+             Backtrace: 
262+               # spec/std/spec/expectations_spec.cr:#{ __LINE__  -  7 }  
263+             MESSAGE 
264+         else 
265+           raise  " nothing is raised" 
266+         end 
267+       end 
268+     end 
175269  end 
176270end 
0 commit comments