Class: RSpec::CallerFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/expectations/caller_filter.rb

Constant Summary

RSPEC_LIBS =
%w[
core
mocks
expectations
matchers
rails
ADDITIONAL_TOP_LEVEL_FILES =
%w[ autorun ]
LIB_REGEX =
%r{/lib/rspec/(#{(RSPEC_LIBS + ADDITIONAL_TOP_LEVEL_FILES).join('|')})(\.rb|/)}

Class Method Summary (collapse)

Class Method Details

+ (Object) first_non_rspec_line

Earlier rubies do not support the two argument form of caller. This fallback is logically the same, but slower.

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/expectations/caller_filter.rb', line 53
def self.first_non_rspec_line
  # `caller` is an expensive method that scales linearly with the size of
  # the stack. The performance hit for fetching it in chunks is small,
  # and since the target line is probably near the top of the stack, the
  # overall improvement of a chunked search like this is significant.
  #
  # See benchmarks/caller.rb for measurements.

  # Initial value here is mostly arbitrary, but is chosen to give good
  # performance on the common case of creating a double.
  increment = 5
  i         = 1
  line      = nil
  while !line
    stack = caller(i, increment)
    return nil unless stack
    line = stack.find { |l| l !~ LIB_REGEX }
    i         += increment
    increment *= 2 # The choice of two here is arbitrary.
  end
  line
end