lib/power_assert/context.rb in power_assert-1.2.1 vs lib/power_assert/context.rb in power_assert-2.0.0
- old
+ new
@@ -10,40 +10,35 @@
def initialize(base_caller_length)
@fired = false
@target_thread = Thread.current
method_id_set = nil
@return_values = []
- trace_alias_method = PowerAssert.configuration._trace_alias_method
@trace_return = TracePoint.new(:return, :c_return) do |tp|
- begin
- unless method_id_set
- next unless Thread.current == @target_thread
- method_id_set = @parser.method_id_set
+ unless method_id_set
+ next unless Thread.current == @target_thread
+ method_id_set = @parser.method_id_set
+ end
+ method_id = tp.callee_id
+ next if ! method_id_set[method_id]
+ next if tp.event == :c_return and
+ not (@parser.lineno == tp.lineno and @parser.path == tp.path)
+ locs = PowerAssert.app_caller_locations
+ diff = locs.length - base_caller_length
+ if (tp.event == :c_return && diff == 1 || tp.event == :return && diff <= 2) and Thread.current == @target_thread
+ idx = -(base_caller_length + 1)
+ if @parser.path == locs[idx].path and @parser.lineno == locs[idx].lineno
+ val = PowerAssert.configuration.lazy_inspection ?
+ tp.return_value :
+ InspectedValue.new(SafeInspectable.new(tp.return_value).inspect)
+ @return_values << Value[method_id.to_s, val, locs[idx].lineno, nil]
end
- method_id = SUPPORT_ALIAS_METHOD ? tp.callee_id :
- trace_alias_method && tp.event == :return ? tp.binding.eval('::Kernel.__callee__') :
- tp.method_id
- next if ! method_id_set[method_id]
- next if tp.event == :c_return and
- not (@parser.lineno == tp.lineno and @parser.path == tp.path)
- locs = PowerAssert.app_caller_locations
- diff = locs.length - base_caller_length
- if (tp.event == :c_return && diff == 1 || tp.event == :return && diff <= 2) and Thread.current == @target_thread
- idx = -(base_caller_length + 1)
- if @parser.path == locs[idx].path and @parser.lineno == locs[idx].lineno
- val = PowerAssert.configuration.lazy_inspection ?
- tp.return_value :
- InspectedValue.new(SafeInspectable.new(tp.return_value).inspect)
- @return_values << Value[method_id.to_s, val, locs[idx].lineno, nil]
- end
- end
- rescue Exception => e
- warn "power_assert: [BUG] Failed to trace: #{e.class}: #{e.message}"
- if e.respond_to?(:full_message)
- warn e.full_message.gsub(/^/, 'power_assert: ')
- end
end
+ rescue Exception => e
+ warn "power_assert: [BUG] Failed to trace: #{e.class}: #{e.message}"
+ if e.respond_to?(:full_message)
+ warn e.full_message.gsub(/^/, 'power_assert: ')
+ end
end
end
def message
raise 'call #yield or #enable at first' unless fired?
@@ -59,12 +54,12 @@
def fired?
@fired
end
def build_assertion_message(parser, return_values)
- if PowerAssert.configuration._colorize_message
- line = Pry::Code.new(parser.line).highlighted
+ if PowerAssert.configuration.colorize_message
+ line = IRB::Color.colorize_code(parser.line, ignore_error: true)
else
line = parser.line
end
path = detect_path(parser, return_values)
@@ -91,15 +86,15 @@
line[i] == "\t" ? "\t" : ' '
end
end.join
lines = []
lines << line.chomp
- lines << sprintf(fmt, vals.each_with_object({}) {|v, h| h[v.display_offset.to_s.to_sym] = '|' }).chomp
+ lines << sprintf(fmt, vals.each_with_object({}) {|v, h| h[:"#{v.display_offset}"] = '|' }).chomp
vals.each do |i|
- inspected_val = SafeInspectable.new(Formatter.new(i.value, i.display_offset)).inspect
+ inspected_val = SafeInspectable.new(Inspector.new(i.value, i.display_offset)).inspect
inspected_val.each_line do |l|
map_to = vals.each_with_object({}) do |j, h|
- h[j.display_offset.to_s.to_sym] = [l, '|', ' '][i.display_offset <=> j.display_offset]
+ h[:"#{j.display_offset}"] = [l, '|', ' '][i.display_offset <=> j.display_offset]
end
lines << encoding_safe_rstrip(sprintf(fmt, map_to))
end
end
lines.join("\n")