Skip to content

Commit ffda890

Browse files
asteriteRX14
authored andcommittedJan 27, 2018
Spec: implement be_a and expect_raises without macros (#5646)
* Spec: implement `be_a` and `expect_raises` without macros * Simplify `expect_raises` code by adding an `else` clause * Remove redundant `begin` in `expect_raises` * More refactors in `expect_raises`
1 parent 0f9af00 commit ffda890

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed
 

Diff for: ‎spec/std/socket_spec.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe Socket::Addrinfo do
9191
end
9292

9393
it "eventually raises returned error" do
94-
expect_raises(Socket::Error) do |addrinfo|
94+
expect_raises(Socket::Error) do
9595
Socket::Addrinfo.resolve("localhost", 80, type: Socket::Type::DGRAM) do |addrinfo|
9696
Socket::Error.new("please fail")
9797
end

Diff for: ‎src/spec/expectations.cr

+29-44
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,8 @@ module Spec
267267
end
268268

269269
# 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
281272
end
282273

283274
# Runs the block and passes if it raises an exception of type *klass* and the error message matches.
@@ -286,43 +277,37 @@ module Spec
286277
# If *message* is a regular expression, it is used to match the error message.
287278
#
288279
# 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
300287

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
314295
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
324301
end
325302
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
326311
end
327312
end
328313

0 commit comments

Comments
 (0)
Please sign in to comment.