class PowerAssert::Context

Constants

Value

Attributes

message_proc[R]

Public Class Methods

new(base_caller_length) click to toggle source
# File lib/power_assert/context.rb, line 12
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|
    method_id_set ||= @parser.method_id_set
    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, nil]
      end
    end
  end
  @message_proc = -> {
    raise RuntimeError, 'call #yield or #enable at first' unless fired?
    @message ||= build_assertion_message(@parser.line, @parser.idents, @parser.binding, return_values).freeze
  }
end

Public Instance Methods

message() click to toggle source
# File lib/power_assert/context.rb, line 44
def message
  @message_proc.()
end