Skip to content

Commit

Permalink
Merge 7c976b2 into 590a19e
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Aug 23, 2013
2 parents 590a19e + 7c976b2 commit 070360b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 140 deletions.
81 changes: 0 additions & 81 deletions benchmarks/caller.rb

This file was deleted.

55 changes: 55 additions & 0 deletions lib/rspec/caller_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module RSpec
# Consistent implementation for "cleaning" the caller method to strip out
# non-rspec lines. This enables errors to be reported at the call site in
# the code using the library, which is far more useful than the particular
# internal method that raised an error.
class CallerFilter

# This list is an unfortunate dependency on other RSpec core libraries.
# It would be nice if this was not needed.
RSPEC_LIBS = %w[
core
mocks
expectations
matchers
rails
]

LIB_REGEX = %r{/lib/rspec/(#{RSPEC_LIBS.join('|')})(\.rb|/)}

if RUBY_VERSION >= '2.0.0'
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)
raise "No non-lib lines in stack" unless stack

line = stack.find { |l| l !~ LIB_REGEX }

i += increment
increment *= 2 # The choice of two here is arbitrary.
end

line
end
else
# Earlier rubies do not support the two argument form of `caller`. This
# fallback is logically the same, but slower.
def self.first_non_rspec_line
caller.find { |line| line !~ LIB_REGEX }
end
end
end
end
58 changes: 0 additions & 58 deletions lib/rspec/mocks/caller_filter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rspec/mocks/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# supports wrapping rspec's mocking functionality without invading every
# object in the system.

require 'rspec/mocks/caller_filter'
require 'rspec/caller_filter'
require 'rspec/mocks/deprecation'
require 'rspec/mocks/instance_method_stasher'
require 'rspec/mocks/method_double'
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/mocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@
expect(RSpec::Mocks.configuration).to be(RSpec::Mocks.configuration)
end
end

def file_contents_for(lib, filename)
# http://rubular.com/r/HYpUMftlG2
path = $LOAD_PATH.find { |p| p.match(/\/rspec-#{lib}(-[a-f0-9]+)?\/lib/) }
file = File.join(path, filename)
File.read(file)
end

it 'has an up-to-date rspec/caller_filter file' do
mocks = file_contents_for("mocks", "rspec/caller_filter.rb")
core = file_contents_for("core", "rspec/caller_filter.rb")

expect(mocks).to eq(core)
end
end

0 comments on commit 070360b

Please sign in to comment.