@@ -267,17 +267,8 @@ module Spec
267
267
end
268
268
269
269
# Creates an `Expectation` that passes if actual is of type *type* (`is_a?`).
270
- macro be_a (type )
271
- Spec ::BeAExpectation ({{type }}).new
272
- end
273
-
274
- # Runs the block and passes if it raises an exception of type *klass*.
275
- #
276
- # It returns the rescued exception.
277
- macro expect_raises (klass )
278
- expect_raises({{klass}}, nil ) do
279
- {{yield }}
280
- end
270
+ def be_a (type : T .class) forall T
271
+ Spec ::BeAExpectation (T ).new
281
272
end
282
273
283
274
# Runs the block and passes if it raises an exception of type *klass* and the error message matches.
@@ -286,43 +277,37 @@ module Spec
286
277
# If *message* is a regular expression, it is used to match the error message.
287
278
#
288
279
# It returns the rescued exception.
289
- macro expect_raises (klass , message , file = __FILE__ , line = __LINE__ )
290
- % failed = false
291
- begin
292
- {{yield }}
293
- % failed = true
294
- fail " Expected {{klass.id}} but nothing was raised" , {{file}}, {{line}}
295
- rescue % ex : {{klass.id}}
296
- # We usually bubble Spec::AssertaionFailed, unless this is the expected exception
297
- if % ex.class == Spec ::AssertionFailed && {{klass}} != Spec ::AssertionFailed
298
- raise % ex
299
- end
280
+ def expect_raises (klass : T .class, message = nil , file = __FILE__ , line = __LINE__ ) forall T
281
+ yield
282
+ rescue ex : T
283
+ # We usually bubble Spec::AssertaionFailed, unless this is the expected exception
284
+ if ex.is_a?(Spec ::AssertionFailed ) && klass != Spec ::AssertionFailed
285
+ raise ex
286
+ end
300
287
301
- % msg = {{message}}
302
- % ex_to_s = % ex.to_s
303
- case % msg
304
- when Regex
305
- unless (% ex_to_s =~ % msg)
306
- backtrace = % ex.backtrace.map { |f | " # #{ f } " }.join " \n "
307
- fail " Expected {{klass.id}} with message matching #{ % msg.inspect } , got #<#{ % ex.class } : #{ % ex_to_s } > with backtrace:\n #{ backtrace } " , {{file}}, {{line}}
308
- end
309
- when String
310
- unless % ex_to_s.includes?(% msg)
311
- backtrace = % ex.backtrace.map { |f | " # #{ f } " }.join " \n "
312
- fail " Expected {{klass.id}} with #{ % msg.inspect } , got #<#{ % ex.class } : #{ % ex_to_s } > with backtrace:\n #{ backtrace } " , {{file}}, {{line}}
313
- end
288
+ ex_to_s = ex.to_s
289
+ case message
290
+ when Regex
291
+ unless (ex_to_s =~ message)
292
+ backtrace = ex.backtrace.join(" \n " ) { |f | " # #{ f } " }
293
+ fail " Expected #{ klass } with message matching #{ message.inspect } , " \
294
+ " got #<#{ ex.class } : #{ ex_to_s } > with backtrace:\n #{ backtrace } " , file, line
314
295
end
315
-
316
- % ex
317
- rescue % ex
318
- if % failed
319
- raise % ex
320
- else
321
- % ex_to_s = % ex.to_s
322
- backtrace = % ex.backtrace.map { |f | " # #{ f } " }.join " \n "
323
- fail " Expected {{klass.id}}, got #<#{ % ex.class } : #{ % ex_to_s } > with backtrace:\n #{ backtrace } " , {{file}}, {{line}}
296
+ when String
297
+ unless ex_to_s.includes?(message)
298
+ backtrace = ex.backtrace.join(" \n " ) { |f | " # #{ f } " }
299
+ fail " Expected #{ klass } with #{ message.inspect } , got #<#{ ex.class } : " \
300
+ " #{ ex_to_s } > with backtrace:\n #{ backtrace } " , file, line
324
301
end
325
302
end
303
+
304
+ ex
305
+ rescue ex
306
+ backtrace = ex.backtrace.join(" \n " ) { |f | " # #{ f } " }
307
+ fail " Expected #{ klass } , got #<#{ ex.class } : #{ ex.to_s } > with backtrace:\n " \
308
+ " #{ backtrace } " , file, line
309
+ else
310
+ fail " Expected #{ klass } but nothing was raised" , file, line
326
311
end
327
312
end
328
313
0 commit comments