allow (class|instance)_eval based monkeypatch detection for show-source -a

This commit is contained in:
John Mair 2012-06-04 05:00:11 +12:00
parent b6b36c5e0f
commit b313a03c8f
3 changed files with 38 additions and 3 deletions

View File

@ -206,13 +206,14 @@ class Pry
return nil if !file.is_a?(String)
class_regex1 = /#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/
class_regex2 = /(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/
class_regexes = [/#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/,
/(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/,
/(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/]
host_file_lines = lines_for_file(file)
search_lines = host_file_lines[0..(line - 2)]
idx = search_lines.rindex { |v| class_regex1 =~ v || class_regex2 =~ v }
idx = search_lines.rindex { |v| class_regexes.any? { |r| r =~ v } }
[file, idx + 1]
rescue Pry::RescuableException

View File

@ -46,6 +46,17 @@ class TestClassForShowSource
end
end
class TestClassForShowSourceClassEval
def alpha
end
end
class TestClassForShowSourceInstanceEval
def alpha
end
end
# in case the tests call reset_defaults, ensure we reset them to
# amended (test friendly) values
class << Pry

View File

@ -289,6 +289,9 @@ if !mri18_and_no_real_source_location?
end
end
# note that pry assumes a class is only monkey-patched at most
# ONCE per file, so will not find multiple monkeypatches in the
# SAME file.
describe "show-source -a" do
it 'should show the source for all monkeypatches defined in different files' do
class TestClassForShowSource
@ -300,6 +303,26 @@ if !mri18_and_no_real_source_location?
result.should =~ /def alpha/
result.should =~ /def beta/
end
it 'should show the source for a class_eval-based monkeypatch' do
TestClassForShowSourceClassEval.class_eval do
def class_eval_method
end
end
result = mock_pry("show-source TestClassForShowSourceClassEval -a")
result.should =~ /def class_eval_method/
end
it 'should show the source for an instance_eval-based monkeypatch' do
TestClassForShowSourceInstanceEval.instance_eval do
def instance_eval_method
end
end
result = mock_pry("show-source TestClassForShowSourceInstanceEval -a")
result.should =~ /def instance_eval_method/
end
end
end
end