1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

update commands and tests to use Pry::Method

This commit is contained in:
Ryan Fitzgerald 2011-09-19 01:18:57 -07:00
parent 343487fc2c
commit 7c43bcfe58
14 changed files with 192 additions and 459 deletions

View file

@ -174,6 +174,7 @@ end
require "pry/version"
require "pry/rbx_method"
require "pry/rbx_path"
require "pry/method"
require "pry/history_array"
require "pry/helpers"

View file

@ -28,17 +28,17 @@ class Pry
end
command "reload-method", "Reload the source file that contains the specified method" do |meth_name|
if (meth = get_method_object(meth_name, target, {})).nil?
if (method = Pry::Method.from_str(meth_name, target)).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
if is_a_c_method?(meth)
if method.source_type == :c
output.puts "Error: Can't reload a C method."
elsif is_a_dynamically_defined_method?(meth)
elsif method.dynamically_defined?
output.puts "Error: Can't reload an eval method."
else
file_name = meth.source_location.first
file_name = method.source_file
load file_name
output.puts "Reloaded #{file_name}."
end

View file

@ -136,8 +136,11 @@ class Pry
i_num = 5
end
meth_name = meth_name_from_binding(target)
meth_name = "N/A" if !meth_name
if (method = Pry::Method.from_binding(target))
meth_name = method.name
else
meth_name = "N/A"
end
if file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e"
output.puts "Cannot find local context. Did you use `binding.pry` ?"

View file

@ -32,20 +32,20 @@ class Pry
args = [nil] if args.empty?
args.each do |method_name|
meth_name = method_name
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{meth_name}. Type `show-doc --help` for help"
if (method = Pry::Method.from_str(method_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{method_name}. Type `show-doc --help` for help"
next
end
doc, code_type = doc_and_code_type_for(meth)
next if !doc
next unless method.doc
set_file_and_dir_locals(method.source_file)
next output.puts("No documentation found.") if doc.empty?
doc = process_comment_markup(doc, code_type)
output.puts make_header(meth, code_type, doc)
output.puts "#{text.bold("visibility: ")} #{method_visibility(meth).to_s}"
output.puts "#{text.bold("signature: ")} #{signature_for(meth)}"
next output.puts("No documentation found.") if method.doc.empty?
doc = process_comment_markup(method.doc, method.source_type)
output.puts make_header(method, doc)
output.puts "#{text.bold("visibility: ")} #{method.visibility}"
output.puts "#{text.bold("signature: ")} #{method.signature}"
output.puts
render_output(opts.flood?, false, doc)
doc
@ -77,19 +77,19 @@ class Pry
next if opts.help?
meth_name = args.shift
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
if (method = Pry::Method.from_str(meth_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{meth_name}. Type `stat --help` for help"
next
end
if !is_a_c_method?(meth) && !is_a_dynamically_defined_method?(meth)
set_file_and_dir_locals(path_line_for(meth).first)
if method.source_type != :c and !method.dynamically_defined?
set_file_and_dir_locals(method.source_file)
end
output.puts "Method Information:"
output.puts "--"
output.puts "Name: " + meth_name
output.puts "Owner: " + (meth.owner.to_s ? meth.owner.to_s : "Unknown")
output.puts "Owner: " + (method.owner ? method.owner.to_s : "Unknown")
output.puts "Visibility: " + method_visibility(meth).to_s
output.puts "Type: " + (meth.is_a?(Method) ? "Bound" : "Unbound")
output.puts "Arity: " + meth.arity.to_s
@ -125,18 +125,21 @@ class Pry
# This needs to be extracted into its own method as it's shared
# by show-method and show-doc and stat commands
meth_name = args.shift
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
if (method = Pry::Method.from_str(meth_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{meth_name}. Type `gist-method --help` for help"
next
end
type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
if !opts.doc?
content, code_type = code_and_code_type_for(meth)
content = method.source
code_type = method.source_type
else
content, code_type = doc_and_code_type_for(meth)
raw_content = method.doc
orig_code_type = method.source_type
text.no_color do
content = process_comment_markup(content, code_type)
content = process_comment_markup(content, orig_code_type)
end
code_type = :plain
end
@ -146,45 +149,9 @@ class Pry
opts.p?)
output.puts "Gist created at #{link}"
set_file_and_dir_locals(method.source_file)
end
helpers do
# paraphrased from awesome_print gem
def signature_for(method)
if method.respond_to?(:parameters)
args = method.parameters.inject([]) do |arr, (type, name)|
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
arr << case type
when :req then name.to_s
when :opt, :rest then "*#{name}"
when :block then "&#{name}"
else '?'
end
end
else
args = (1..method.arity.abs).map { |i| "arg#{i}" }
args[-1] = "*#{args[-1]}" if method.arity < 0
end
"#{method.name}(#{args.join(', ')})"
end
def method_visibility(meth)
if meth.owner.public_instance_methods.include? meth.name
:public
elsif meth.owner.protected_instance_methods.include? meth.name
:protected
elsif meth.owner.private_instance_methods.include? meth.name
:private
else
:none
end
end
end
end
end
end

View file

@ -77,17 +77,17 @@ class Pry
if opts.m?
meth_name = opts[:m]
if (meth = get_method_object(meth_name, target, {})).nil?
if (method = Pry::Method.from_str(meth_name, target)).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
code, _ = code_and_code_type_for(meth)
next if !code
next if !method.source
set_file_and_dir_locals(method.source_file)
range = opts.l? ? one_index_range_or_number(opts[:l]) : (0..-1)
range = (0..-2) if opts.o?
eval_string << Array(code.each_line.to_a[range]).join
eval_string << Array(method.source.each_line.to_a[range]).join
elsif opts.f?
file_name = File.expand_path(opts[:f])
next output.puts "No such file: #{opts[:f]}" if !File.exists?(file_name)

View file

@ -28,33 +28,34 @@ class Pry
output.puts opt
end
end
next if opts.help?
opts[:instance] = opts['instance-methods'] if opts.m?
args = [nil] if args.empty?
args.each do |method_name|
meth_name = method_name
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{meth_name}. Type `show-method --help` for help"
if method_name.nil?
method = Pry::Method.from_binding(target)
elsif (method = Pry::Method.from_str(method_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{method_name}. Type `show-method --help` for help"
next
end
next if !method.source
set_file_and_dir_locals(method.source_file)
code, code_type = code_and_code_type_for(meth)
next if !code
output.puts make_header(meth, code_type, code)
output.puts make_header(method)
if Pry.color
code = CodeRay.scan(code, code_type).term
code = CodeRay.scan(method.source, method.source_type).term
else
code = method.source
end
start_line = false
if opts.l?
start_line = meth.source_location ? meth.source_location.last : 1
if opts.b?
start_line = 1
elsif opts.l?
start_line = method.source_line || 1
end
start_line = opts.b? ? 1 : start_line
render_output(opts.flood?, start_line, code)
code
end
@ -89,23 +90,25 @@ class Pry
end
if find_command(command_name)
block = find_command(command_name).block
block = Pry::Method.new(find_command(command_name).block)
code, _ = code_and_code_type_for(block)
next if !code
next unless block.source
set_file_and_dir_locals(block.source_file)
output.puts make_header(block, :ruby, code)
output.puts make_header(block)
if Pry.color
code = CodeRay.scan(code, :ruby).term
code = CodeRay.scan(block.source, :ruby).term
else
code = block.source
end
start_line = false
if opts.l?
start_line = block.source_location ? block.source_location.last : 1
start_line = block.source_line || 1
end
render_output(opts.flood?, opts.l? ? block.source_location.last : false, code)
render_output(opts.flood?, opts.l? ? block.source_line : false, code)
code
else
output.puts "No such command: #{command_name}."
@ -168,8 +171,8 @@ class Pry
bt_index = opts[:ex].to_i
ex_file, ex_line = ex.bt_source_location_for(bt_index)
if ex_file && is_core_rbx_path?(ex_file)
file_name = rbx_convert_path_to_full(ex_file)
if ex_file && RbxPath.is_core_path?(ex_file)
file_name = RbxPath.convert_path_to_full(ex_file)
else
file_name = ex_file
end
@ -221,7 +224,6 @@ class Pry
output.puts opt
end
end
next if opts.help?
if !Pry.config.editor
@ -231,20 +233,18 @@ class Pry
end
meth_name = args.shift
meth_name, target, type = get_method_attributes(meth_name, target, opts.to_hash(true))
meth = get_method_object_from_target(meth_name, target, type)
if meth.nil?
if (method = Pry::Method.from_str(meth_name, target, opts.to_hash(true))).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
if opts.p? || is_a_dynamically_defined_method?(meth)
code, _ = code_and_code_type_for(meth)
if opts.p? || method.dynamically_defined?
lines = method.source.lines.to_a
set_file_and_dir_locals(method.source_file)
lines = code.lines.to_a
if lines[0] =~ /^def [^( \n]+/
lines[0] = "def #{meth_name}#{$'}"
lines[0] = "def #{method.name}#{$'}"
else
next output.puts "Error: Pry can only patch methods created with the `def` keyword."
end
@ -253,15 +253,16 @@ class Pry
f.puts lines.join
f.flush
invoke_editor(f.path, 0)
Pry.new(:input => StringIO.new(File.read(f.path))).rep(meth.owner)
Pry.new(:input => StringIO.new(File.read(f.path))).rep(method.owner)
end
next
end
if is_a_c_method?(meth)
if method.source_type == :c
output.puts "Error: Can't edit a C method."
else
file, line = path_line_for(meth)
file, line = method.source_file, method.source_line
set_file_and_dir_locals(file)
invoke_editor(file, opts["no-jump"] ? 0 : line)

View file

@ -61,8 +61,8 @@ class Pry
start_line = (ex_line - 1) - window_size
start_line = start_line < 0 ? 0 : start_line
end_line = (ex_line - 1) + window_size
if ex_file && is_core_rbx_path?(ex_file)
file_name = rbx_convert_path_to_full(ex_file)
if ex_file && RbxPath.is_core_path?(ex_file)
file_name = RbxPath.convert_path_to_full(ex_file)
else
file_name = ex_file
end

View file

@ -4,12 +4,12 @@ class Pry
Experimental = Pry::CommandSet.new do
command "reload-method", "Reload the source specifically for a method", :requires_gem => "method_reload" do |meth_name|
if (meth = get_method_object(meth_name, target, {})).nil?
if (method = Pry::Method.from_str(meth_name, target)).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
meth.reload
method.reload
end
end
end

View file

@ -5,15 +5,6 @@ class Pry
module_function
def meth_name_from_binding(b)
meth_name = b.eval('__method__')
if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name)
nil
else
meth_name
end
end
# if start_line is not false then add line numbers starting with start_line
def render_output(should_flood, start_line, text, color=:blue)
if start_line
@ -27,18 +18,6 @@ class Pry
end
end
def is_a_dynamically_defined_method?(meth)
file, _ = meth.source_location
!!(file =~ /(\(.*\))|<.*>/)
end
def check_for_dynamically_defined_method(meth)
file, _ = meth.source_location
if file =~ /(\(.*\))|<.*>/ && file != Pry.eval_path
raise "Cannot retrieve source for dynamically defined method."
end
end
# Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file
def temp_file
@ -48,225 +27,16 @@ class Pry
file.close
end
########### RBX HELPERS #############
def is_core_rbx_path?(path)
rbx? &&
path.start_with?("kernel")
end
def rbx_core?(meth)
meth.source_location &&
is_core_rbx_path?(meth.source_location.first)
end
def rvm_ruby?(path)
!!(path =~ /\.rvm/)
end
def rbx_core_code_for(meth)
rbx_core_code_or_doc_for(meth, :code)
end
def rbx_core_doc_for(meth)
rbx_core_code_or_doc_for(meth, :doc)
end
def rbx_core_code_or_doc_for(meth, code_or_doc)
path_line = rbx_core_path_line_for(meth)
case code_or_doc
when :code
MethodSource.source_helper(path_line)
when :doc
MethodSource.comment_helper(path_line)
end
end
def rbx_convert_path_to_full(path)
if rvm_ruby?(Rubinius::BIN_PATH)
rbx_rvm_convert_path_to_full(path)
else
rbx_std_convert_path_to_full(path)
end
end
def rbx_rvm_convert_path_to_full(path)
ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last
source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name)
file_name = File.join(source_path, path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
end
def rbx_std_convert_path_to_full(path)
file_name = File.join(Rubinius::BIN_PATH, "..", path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
end
def rbx_core_path_line_for(meth)
if rvm_ruby?(Rubinius::BIN_PATH)
rvm_rbx_core_path_line_for(meth)
else
std_rbx_core_path_line_for(meth)
end
end
def std_rbx_core_path_line_for(meth)
file_name = rbx_std_convert_path_to_full(meth.source_location.first)
start_line = meth.source_location.last
[file_name, start_line]
end
def rvm_rbx_core_path_line_for(meth)
file_name = rbx_rvm_convert_path_to_full(meth.source_location.first)
start_line = meth.source_location.last
[file_name, start_line]
end
######### END RBX HELPERS ###############
def code_and_code_type_for(meth)
case code_type = code_type_for(meth)
when nil
return nil
when :c
code = Pry::MethodInfo.info_for(meth).source
code = strip_comments_from_c_code(code)
when :ruby
if meth.source_location.first == Pry.eval_path
start_line = meth.source_location.last
# FIXME this line below needs to be refactored, WAY too
# much of a hack. We pass nothing to prompt because if
# prompt uses #inspect (or #pretty_inspect) on the context
# it can hang the session if the object being inspected on
# is enormous see: https://github.com/pry/pry/issues/245
p = Pry.new(:input => StringIO.new(Pry.line_buffer[start_line..-1].join), :prompt => proc {""}, :hooks => {}).r(target)
code = strip_leading_whitespace(p)
else
if rbx_core?(meth)
code = strip_leading_whitespace(rbx_core_code_for(meth))
else
code = strip_leading_whitespace(meth.source)
end
end
set_file_and_dir_locals(path_line_for(meth).first)
end
[code, code_type]
end
def doc_and_code_type_for(meth)
case code_type = code_type_for(meth)
when nil
return nil
when :c
doc = Pry::MethodInfo.info_for(meth).docstring
when :ruby
if rbx_core?(meth)
doc = strip_leading_hash_and_whitespace_from_ruby_comments(rbx_core_doc_for(meth))
else
doc = strip_leading_hash_and_whitespace_from_ruby_comments(meth.comment)
end
set_file_and_dir_locals(path_line_for(meth).first)
end
[doc, code_type]
end
def get_method_object(meth_name, target=nil, options={})
get_method_object_from_target(*get_method_attributes(meth_name, target, options)) rescue nil
end
def get_method_attributes(meth_name, target=nil, options={})
if meth_name
if meth_name =~ /(\S+)\#(\S+)\Z/
context, meth_name = $1, $2
target = Pry.binding_for(target.eval(context))
type = :instance
elsif meth_name =~ /(\S+)\.(\S+)\Z/
context, meth_name = $1, $2
target = Pry.binding_for(target.eval(context))
type = :singleton
elsif options["instance_methods"]
type = :instance
elsif options[:methods]
type = :singleton
else
type = nil
end
else
meth_name = meth_name_from_binding(target)
type = nil
end
[meth_name, target, type]
end
def get_method_object_from_target(meth_name, target, type=nil)
case type
when :instance
target.eval("instance_method(:#{meth_name})") rescue nil
when :singleton
target.eval("method(:#{meth_name})") rescue nil
else
get_method_object_from_target(meth_name, target, :instance) ||
get_method_object_from_target(meth_name, target, :singleton)
end
end
def path_line_for(meth)
if rbx_core?(meth)
rbx_core_path_line_for(meth)
else
meth.source_location
end
end
def make_header(meth, code_type, content)
def make_header(meth, content=meth.source)
code_type = meth.source_type
num_lines = "Number of lines: #{Pry::Helpers::Text.bold(content.each_line.count.to_s)}"
case code_type
when :ruby
file, line = path_line_for(meth)
"\n#{Pry::Helpers::Text.bold('From:')} #{file} @ line #{line}:\n#{num_lines}\n\n"
else
when :c
file = Pry::MethodInfo.info_for(meth).file
"\n#{Pry::Helpers::Text.bold('From:')} #{file} in Ruby Core (C Method):\n#{num_lines}\n\n"
end
end
def is_a_c_method?(meth)
meth.source_location.nil?
end
def should_use_pry_doc?(meth)
Pry.config.has_pry_doc && is_a_c_method?(meth)
end
def code_type_for(meth)
# only C methods
if should_use_pry_doc?(meth)
info = Pry::MethodInfo.info_for(meth)
if info && info.source
code_type = :c
else
output.puts "Cannot find C method: #{meth.name}"
code_type = nil
end
else
if is_a_c_method?(meth)
output.puts "Cannot locate this method: #{meth.name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
code_type = nil
else
check_for_dynamically_defined_method(meth)
code_type = :ruby
end
end
code_type
end
def file_map
{
[".c", ".h"] => :c,
@ -357,24 +127,6 @@ class Pry
process_yardoc process_rdoc(comment, code_type)
end
# strip leading whitespace but preserve indentation
def strip_leading_whitespace(text)
return text if text.empty?
leading_spaces = text.lines.first[/^(\s+)/, 1]
text.gsub(/^#{leading_spaces}/, '')
end
def strip_leading_hash_and_whitespace_from_ruby_comments(comment)
comment = comment.dup
comment.gsub!(/\A\#+?$/, '')
comment.gsub!(/^\s*#/, '')
strip_leading_whitespace(comment)
end
def strip_comments_from_c_code(code)
code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
end
def invoke_editor(file, line)
if Pry.config.editor.respond_to?(:call)
editor_invocation = Pry.config.editor.call(file, line)

View file

@ -11,11 +11,12 @@ class Pry
end
def self.from_str(str, target=TOPLEVEL_BINDING, options={})
str = str.to_s
if str =~ /(\S+)\#(\S+)\Z/
if str.nil?
from_binding(target)
elsif str.to_s =~ /(\S+)\#(\S+)\Z/
context, meth_name = $1, $2
from_module(target.eval(context), meth_name)
elsif str =~ /(\S+)\.(\S+)\Z/
elsif str.to_s =~ /(\S+)\.(\S+)\Z/
context, meth_name = $1, $2
from_obj(target.eval(context), meth_name)
elsif options[:instance]

View file

@ -2,7 +2,7 @@ class Pry
module RbxMethod
private
def core?
source_file and is_core_path?(source_file)
source_file and RbxPath.is_core_path?(source_file)
end
def core_code
@ -14,37 +14,7 @@ class Pry
end
def core_path_line
[convert_path_to_full(source_file), source_line]
end
def is_core_path?(path)
path.start_with?("kernel")
end
def rvm_ruby?(path)
!!(path =~ /\.rvm/)
end
def convert_path_to_full(path)
if rvm_ruby?(Rubinius::BIN_PATH)
rvm_convert_path_to_full(path)
else
std_convert_path_to_full(path)
end
end
def rvm_convert_path_to_full(path)
ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last
source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name)
file_name = File.join(source_path, path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
end
def std_convert_path_to_full(path)
file_name = File.join(Rubinius::BIN_PATH, "..", path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
[RbxPath.convert_path_to_full(source_file), source_line]
end
end
end

34
lib/pry/rbx_path.rb Normal file
View file

@ -0,0 +1,34 @@
class Pry
module RbxPath
module_function
def is_core_path?(path)
path.start_with?("kernel")
end
def convert_path_to_full(path)
if rvm_ruby?(Rubinius::BIN_PATH)
rvm_convert_path_to_full(path)
else
std_convert_path_to_full(path)
end
end
def rvm_ruby?(path)
!!(path =~ /\.rvm/)
end
def rvm_convert_path_to_full(path)
ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last
source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name)
file_name = File.join(source_path, path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
end
def std_convert_path_to_full(path)
file_name = File.join(Rubinius::BIN_PATH, "..", path)
raise "Cannot find rbx core source" if !File.exists?(file_name)
file_name
end
end
end

View file

@ -5,73 +5,5 @@ describe Pry::Helpers::CommandHelpers do
@helper = Pry::Helpers::CommandHelpers
end
describe "get_method_object" do
it 'should look up instance methods if no methods available and no options provided' do
klass = Class.new { def hello; end }
meth = @helper.get_method_object(:hello, Pry.binding_for(klass), {})
meth.should == klass.instance_method(:hello)
end
it 'should look up methods if no instance methods available and no options provided' do
klass = Class.new { def self.hello; end }
meth = @helper.get_method_object(:hello, Pry.binding_for(klass), {})
meth.should == klass.method(:hello)
end
it 'should look up instance methods first even if methods available and no options provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = @helper.get_method_object(:hello, Pry.binding_for(klass), {})
meth.should == klass.instance_method(:hello)
end
it 'should look up instance methods if "instance-methods" option provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = @helper.get_method_object(:hello, Pry.binding_for(klass), {"instance-methods" => true})
meth.should == klass.instance_method(:hello)
end
it 'should look up methods if :methods option provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = @helper.get_method_object(:hello, Pry.binding_for(klass), {:methods => true})
meth.should == klass.method(:hello)
end
it 'should look up instance methods using the Class#method syntax' do
klass = Class.new { def hello; end; def self.hello; end }
meth = @helper.get_method_object("klass#hello", Pry.binding_for(binding), {})
meth.should == klass.instance_method(:hello)
end
it 'should look up methods using the object.method syntax' do
klass = Class.new { def hello; end; def self.hello; end }
meth = @helper.get_method_object("klass.hello", Pry.binding_for(binding), {})
meth.should == klass.method(:hello)
end
it 'should NOT look up instance methods using the Class#method syntax if no instance methods defined' do
klass = Class.new { def self.hello; end }
meth = @helper.get_method_object("klass#hello", Pry.binding_for(binding), {})
meth.should == nil
end
it 'should NOT look up methods using the object.method syntax if no methods defined' do
klass = Class.new { def hello; end }
meth = @helper.get_method_object("klass.hello", Pry.binding_for(binding), {})
meth.should == nil
end
it 'should look up methods using klass.new.method syntax' do
klass = Class.new { def hello; :hello; end }
meth = @helper.get_method_object("klass.new.hello", Pry.binding_for(binding), {})
meth.name.to_sym.should == :hello
end
it 'should look up instance methods using klass.meth#method syntax' do
klass = Class.new { def self.meth; Class.new; end }
meth = @helper.get_method_object("klass.meth#initialize", Pry.binding_for(binding), {})
meth.name.to_sym.should == :initialize
end
end
# FIXME: currently no tests
end

72
test/test_method.rb Normal file
View file

@ -0,0 +1,72 @@
require 'helper'
describe Pry::Method do
describe ".from_str" do
it 'should look up instance methods if no methods available and no options provided' do
klass = Class.new { def hello; end }
meth = Pry::Method.from_str(:hello, Pry.binding_for(klass))
meth.should == klass.instance_method(:hello)
end
it 'should look up methods if no instance methods available and no options provided' do
klass = Class.new { def self.hello; end }
meth = Pry::Method.from_str(:hello, Pry.binding_for(klass))
meth.should == klass.method(:hello)
end
it 'should look up instance methods first even if methods available and no options provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = Pry::Method.from_str(:hello, Pry.binding_for(klass))
meth.should == klass.instance_method(:hello)
end
it 'should look up instance methods if "instance-methods" option provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = Pry::Method.from_str(:hello, Pry.binding_for(klass), {"instance-methods" => true})
meth.should == klass.instance_method(:hello)
end
it 'should look up methods if :methods option provided' do
klass = Class.new { def hello; end; def self.hello; end }
meth = Pry::Method.from_str(:hello, Pry.binding_for(klass), {:methods => true})
meth.should == klass.method(:hello)
end
it 'should look up instance methods using the Class#method syntax' do
klass = Class.new { def hello; end; def self.hello; end }
meth = Pry::Method.from_str("klass#hello", Pry.binding_for(binding))
meth.should == klass.instance_method(:hello)
end
it 'should look up methods using the object.method syntax' do
klass = Class.new { def hello; end; def self.hello; end }
meth = Pry::Method.from_str("klass.hello", Pry.binding_for(binding))
meth.should == klass.method(:hello)
end
it 'should NOT look up instance methods using the Class#method syntax if no instance methods defined' do
klass = Class.new { def self.hello; end }
meth = Pry::Method.from_str("klass#hello", Pry.binding_for(binding))
meth.should == nil
end
it 'should NOT look up methods using the object.method syntax if no methods defined' do
klass = Class.new { def hello; end }
meth = Pry::Method.from_str("klass.hello", Pry.binding_for(binding))
meth.should == nil
end
it 'should look up methods using klass.new.method syntax' do
klass = Class.new { def hello; :hello; end }
meth = Pry::Method.from_str("klass.new.hello", Pry.binding_for(binding))
meth.name.to_sym.should == :hello
end
it 'should look up instance methods using klass.meth#method syntax' do
klass = Class.new { def self.meth; Class.new; end }
meth = Pry::Method.from_str("klass.meth#initialize", Pry.binding_for(binding))
meth.name.to_sym.should == :initialize
end
end
end