Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec: implement be_a and expect_raises without macros #5646

Merged
merged 4 commits into from
Jan 27, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove redundant begin in expect_raises
  • Loading branch information
asterite committed Jan 26, 2018
commit ee45b88c239283666f6723b7024ad45571a18de6
52 changes: 25 additions & 27 deletions src/spec/expectations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,36 +278,34 @@ module Spec
#
# It returns the rescued exception.
def expect_raises(klass : T.class, message = nil, file = __FILE__, line = __LINE__) forall T
begin
yield
rescue ex : T
# We usually bubble Spec::AssertaionFailed, unless this is the expected exception
if ex.is_a?(Spec::AssertionFailed) && klass != Spec::AssertionFailed
raise ex
end
yield
rescue ex : T
# We usually bubble Spec::AssertaionFailed, unless this is the expected exception
if ex.is_a?(Spec::AssertionFailed) && klass != Spec::AssertionFailed
raise ex
end

ex_to_s = ex.to_s
case message
when Regex
unless (ex_to_s =~ message)
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass} with message matching #{message.inspect}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end
when String
unless ex_to_s.includes?(message)
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass} with #{message.inspect}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end
ex_to_s = ex.to_s
case message
when Regex
unless (ex_to_s =~ message)
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass} with message matching #{message.inspect}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end
when String
unless ex_to_s.includes?(message)
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass} with #{message.inspect}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end

ex
rescue ex
ex_to_s = ex.to_s
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
else
fail "Expected #{klass} but nothing was raised", file, line
end

ex
rescue ex
ex_to_s = ex.to_s
backtrace = ex.backtrace.map { |f| " # #{f}" }.join "\n"
fail "Expected #{klass}, got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
else
fail "Expected #{klass} but nothing was raised", file, line
end
end

Expand Down