lib/rspec/power_assert.rb in rspec-power_assert-0.2.0 vs lib/rspec/power_assert.rb in rspec-power_assert-0.3.0
- old
+ new
@@ -44,11 +44,12 @@
def is_asserted_by(&blk)
result, msg = ::PowerAssert.start(blk, assertion_method: __callee__) do |tp|
[tp.yield, tp.message_proc.call]
end
- handle_result_and_message(result, msg, __callee__)
+ location = blk.source_location.join(":")
+ handle_result_and_message(result, msg, __callee__, location)
end
private
def evaluate_example(method_name, &blk)
@@ -57,24 +58,34 @@
result, msg = ::PowerAssert.rspec_start(pr, assertion_method: method_name) do |tp|
[tp.yield, tp.message_proc.call]
end
- handle_result_and_message(result, msg, method_name)
+ location = blk.source_location.join(":")
+ handle_result_and_message(result, msg, method_name, location)
end
- def handle_result_and_message(result, msg, method_name)
+ def handle_result_and_message(result, msg, method_name, location)
if result
RSpec::Matchers.last_matcher = DummyAssertionMatcher.new(msg, method_name.to_s)
if RSpec::Matchers.respond_to?(:last_should=)
RSpec::Matchers.last_should = :should # for RSpec 2
else
RSpec::Matchers.last_expectation_handler = DummyExpectationHandler # for RSpec 3
end
else
- raise RSpec::Expectations::ExpectationNotMetError, msg
+ ex = RSpec::Expectations::ExpectationNotMetError.new(msg)
+
+ if defined?(RSpec::Support) && RSpec::Support.respond_to?(:notify_failure)
+ # for RSpec 3.3+
+ RSpec::Support.notify_failure(ex)
+ else
+ # for RSpec 2, 3.0, 3.1, 3.2
+ ex.set_backtrace(location)
+ raise ex
+ end
end
end
# for generating description
class DummyExpectationHandler
@@ -133,12 +144,14 @@
def self.assertion_method_alias(name)
alias_method name.to_sym, :it_is_asserted_by
end
def it_is_asserted_by(description = nil, &blk)
- file, lineno = blk.source_location
- cmd = description ? "it(description)" : "specify"
- eval %{#{cmd} do evaluate_example("#{__callee__}", &blk) end}, binding, file, lineno
+ file, _lineno = blk.source_location
+ backtrace = caller.drop_while {|l| l !~ /#{Regexp.escape(file)}/}
+ it description, caller: backtrace do
+ evaluate_example(__callee__, &blk)
+ end
end
end
end
RSpec.configure do |config|