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

Merge branch 'master' into wip-config

Conflicts:
	lib/pry.rb
This commit is contained in:
Robert Gleeson 2014-01-20 08:43:36 +01:00
commit 5177913c91
20 changed files with 1291 additions and 1386 deletions

View file

@ -37,8 +37,9 @@ class Pry
# Read the class name off of the singleton class to provide a default
# inspect.
eig = class << obj; self; end
klass = Pry::Method.safe_send(eig, :ancestors).first
singleton = class << obj; self; end
ancestors = Pry::Method.safe_send(singleton, :ancestors)
klass = ancestors.reject { |k| k == singleton }.first
obj_id = obj.__id__.to_s(16) rescue 0
str = "#<#{klass}:0x#{obj_id}>"

View file

@ -6,8 +6,6 @@ class Pry
group 'Context'
description 'Recursively search for a method within a Class/Module or the current namespace.'
command_options :shellwords => false
command_options :requires_gem => 'ruby18_source_location' if mri_18?
banner <<-'BANNER'
Usage: find-method [-n|-c] METHOD [NAMESPACE]
@ -26,10 +24,6 @@ class Pry
find-method -c 'output.puts' Pry
BANNER
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opti)
opti.on :n, :name, "Search for a method by name"
opti.on :c, :content, "Search for a method based on content in Regex form"

View file

@ -3,11 +3,6 @@ class Pry
extend Pry::Helpers::BaseHelpers
command_options :shellwords => false, :interpolate => false
command_options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors", :as => :count

View file

@ -99,37 +99,33 @@ class Object
end
end
if defined?(BasicObject)
class BasicObject
# Return a binding object for the receiver.
#
# The `self` of the binding is set to the current object, and it contains no
# local variables.
#
# The default definee (http://yugui.jp/articles/846) is set such that new
# methods defined will be added to the singleton class of the BasicObject.
#
# @return [Binding]
def __binding__
# BasicObjects don't have respond_to?, so we just define the method
# every time. As they also don't have `.freeze`, this call won't
# fail as it can for normal Objects.
(class << self; self; end).class_eval <<-EOF, __FILE__, __LINE__ + 1
# Get a binding with 'self' set to self, and no locals.
#
# The default definee is determined by the context in which the
# definition is eval'd.
#
# Please don't call this method directly, see {__binding__}.
#
# @return [Binding]
def __pry__
# In ruby-1.8.7 ::Kernel.binding sets self to Kernel in the returned binding.
# Luckily ruby-1.8.7 doesn't have BasicObject, so this is safe.
::Kernel.binding
end
EOF
self.__pry__
end
class BasicObject
# Return a binding object for the receiver.
#
# The `self` of the binding is set to the current object, and it contains no
# local variables.
#
# The default definee (http://yugui.jp/articles/846) is set such that new
# methods defined will be added to the singleton class of the BasicObject.
#
# @return [Binding]
def __binding__
# BasicObjects don't have respond_to?, so we just define the method
# every time. As they also don't have `.freeze`, this call won't
# fail as it can for normal Objects.
(class << self; self; end).class_eval <<-EOF, __FILE__, __LINE__ + 1
# Get a binding with 'self' set to self, and no locals.
#
# The default definee is determined by the context in which the
# definition is eval'd.
#
# Please don't call this method directly, see {__binding__}.
#
# @return [Binding]
def __pry__
::Kernel.binding
end
EOF
self.__pry__
end
end

View file

@ -90,10 +90,6 @@ class Pry
RbConfig::CONFIG['ruby_install_name'] == 'ruby'
end
def mri_18?
mri? && RUBY_VERSION =~ /1.8/
end
def mri_19?
mri? && RUBY_VERSION =~ /1.9/
end

View file

@ -146,18 +146,14 @@ class Pry
def exec_hook(event_name, *args, &block)
@hooks[event_name] ||= []
# silence warnings to get rid of 1.8's "warning: multiple values
# for a block parameter" warnings
Pry::Helpers::BaseHelpers.silence_warnings do
@hooks[event_name].map do |hook_name, callable|
begin
callable.call(*args, &block)
rescue RescuableException => e
errors << e
e
end
end.last
end
@hooks[event_name].map do |hook_name, callable|
begin
callable.call(*args, &block)
rescue RescuableException => e
errors << e
e
end
end.last
end
# Return the number of hook functions registered for the `event_name` event.

View file

@ -405,8 +405,6 @@ class Pry
end
# @return [Array<String>] All known aliases for the method.
# @note On Ruby 1.8 this method always returns an empty Array for methods
# implemented in C.
def aliases
owner = @method.owner
# Avoid using `to_sym` on {Method#name}, which returns a `String`, because

View file

@ -44,7 +44,8 @@ class Pry::Terminal
require 'io/console'
$stdout.winsize if $stdout.tty? and $stdout.respond_to?(:winsize)
rescue LoadError
# They're probably on 1.8 without the io-console gem. We'll keep trying.
# They probably don't have the io/console stdlib or the io-console gem.
# We'll keep trying.
end
def screen_size_according_to_env

View file

@ -47,10 +47,6 @@ module PryTestHelpers
end
end
def mri18_and_no_real_source_location?
Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
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(ext='.rb')

View file

@ -66,28 +66,12 @@ class Pry
end
# Returns an array of the names of the constants accessible in the wrapped
# module. This provides a consistent interface between 1.8 and 1.9 and also
# avoids the problem of accidentally calling the singleton method
# `Module.constants`.
# module. This avoids the problem of accidentally calling the singleton
# method `Module.constants`.
# @param [Boolean] inherit Include the names of constants from included
# modules?
def constants(inherit = true)
method = Module.instance_method(:constants).bind(@wrapped)
# If we're on 1.8, we have to manually remove ancestors' constants. If
# we're on 1.9, though, it's better to use the built-in `inherit` param,
# since it doesn't do things like incorrectly remove Pry::Config.
if method.arity == 0
consts = method.call
if !inherit
ancestors_ = Pry::Method.safe_send(@wrapped, :ancestors)
consts -= (ancestors_ - [@wrapped]).map(&:constants).flatten
end
else
consts = method.call(inherit)
end
consts
Module.instance_method(:constants).bind(@wrapped).call(inherit)
end
# The prefix that would appear before methods defined on this class.
@ -259,7 +243,7 @@ class Pry
# @return [Enumerator, Array] on JRuby 1.9 and higher returns Array, on
# other rubies returns Enumerator
def candidates
enum = generator.new do |y|
enum = Enumerator.new do |y|
(0...number_of_candidates).each do |num|
y.yield candidate(num)
end
@ -292,18 +276,6 @@ class Pry
private
# Ruby 1.8 doesn't support `Enumerator` (it's called Generator instead)
#
# @return [Object] Return the appropriate generator class.
def generator
@generator ||= if defined?(Enumerator)
Enumerator
else
require 'generator'
Generator
end
end
# @return [Pry::WrappedModule::Candidate] The candidate with the
# highest rank, that is the 'monkey patch' of this module with the
# highest number of methods, which contains a source code line that

View file

@ -664,10 +664,8 @@ describe "Pry::Command" do
pry_eval('my---test').should =~ /my-testmy-test/
end
if !mri18_and_no_real_source_location?
it "shows the source of the process method" do
pry_eval('show-source my-test').should =~ /output.puts command_name/
end
it "shows the source of the process method" do
pry_eval('show-source my-test').should =~ /output.puts command_name/
end
describe "command options hash" do

View file

@ -1,70 +1,63 @@
require 'helper'
# we turn off the test for MRI 1.8 because our source_location hack
# for C methods actually runs the methods - and since it runs ALL
# methods (in an attempt to find a match) it runs 'exit' and aborts
# the test, causing a failure. We should fix this in the future by
# blacklisting certain methods for 1.8 MRI (such as exit, fork, and so on)
unless Pry::Helpers::BaseHelpers.mri_18?
MyKlass = Class.new do
def hello
"timothy"
MyKlass = Class.new do
def hello
"timothy"
end
def goodbye
"jenny"
end
def tea_tim?
"timothy"
end
def tea_time?
"polly"
end
end
describe "find-method" do
describe "find matching methods by name regex (-n option)" do
it "should find a method by regex" do
pry_eval("find-method hell MyKlass").should =~
/MyKlass.*?hello/m
end
def goodbye
"jenny"
end
def tea_tim?
"timothy"
end
def tea_time?
"polly"
it "should NOT match a method that does not match the regex" do
pry_eval("find-method hell MyKlass").should.not =~
/MyKlass.*?goodbye/m
end
end
describe "find-method" do
describe "find matching methods by name regex (-n option)" do
it "should find a method by regex" do
pry_eval("find-method hell MyKlass").should =~
/MyKlass.*?hello/m
end
it "should NOT match a method that does not match the regex" do
pry_eval("find-method hell MyKlass").should.not =~
/MyKlass.*?goodbye/m
end
end
describe "find matching methods by content regex (-c option)" do
it "should find a method by regex" do
pry_eval("find-method -c timothy MyKlass").should =~
/MyKlass.*?hello/m
end
it "should NOT match a method that does not match the regex" do
pry_eval("find-method timothy MyKlass").should.not =~
/MyKlass.*?goodbye/m
end
end
it "should work with badly behaved constants" do
MyKlass::X = Object.new
def (MyKlass::X).hash
raise "mooo"
end
describe "find matching methods by content regex (-c option)" do
it "should find a method by regex" do
pry_eval("find-method -c timothy MyKlass").should =~
/MyKlass.*?hello/m
end
it "should escape regexes correctly" do
good = /tea_time\?/
bad = /tea_tim\?/
pry_eval('find-method tea_time? MyKlass').should =~ good
pry_eval('find-method tea_time? MyKlass').should =~ good
pry_eval('find-method tea_time\? MyKlass').should.not =~ bad
pry_eval('find-method tea_time\? MyKlass').should =~ good
it "should NOT match a method that does not match the regex" do
pry_eval("find-method timothy MyKlass").should.not =~
/MyKlass.*?goodbye/m
end
end
Object.remove_const(:MyKlass)
it "should work with badly behaved constants" do
MyKlass::X = Object.new
def (MyKlass::X).hash
raise "mooo"
end
pry_eval("find-method -c timothy MyKlass").should =~
/MyKlass.*?hello/m
end
it "should escape regexes correctly" do
good = /tea_time\?/
bad = /tea_tim\?/
pry_eval('find-method tea_time? MyKlass').should =~ good
pry_eval('find-method tea_time? MyKlass').should =~ good
pry_eval('find-method tea_time\? MyKlass').should.not =~ bad
pry_eval('find-method tea_time\? MyKlass').should =~ good
end
end
Object.remove_const(:MyKlass)

View file

@ -1,7 +1,6 @@
require 'helper'
describe "gem-list" do
# fixing bug for 1.8 compat
it 'should not raise when invoked' do
proc {
pry_eval(self, 'gem-list')

View file

@ -34,18 +34,16 @@ describe "ls" do
end
end
if defined?(BasicObject)
describe "BasicObject" do
it "should work on BasicObject" do
pry_eval("ls BasicObject.new").should =~ /BasicObject#methods:.*__send__/m
end
describe "BasicObject" do
it "should work on BasicObject" do
pry_eval("ls BasicObject.new").should =~ /BasicObject#methods:.*__send__/m
end
it "should work on subclasses of BasicObject" do
pry_eval(
"class LessBasic < BasicObject; def jaroussky; 5; end; end",
"ls LessBasic.new"
).should =~ /LessBasic#methods:.*jaroussky/m
end
it "should work on subclasses of BasicObject" do
pry_eval(
"class LessBasic < BasicObject; def jaroussky; 5; end; end",
"ls LessBasic.new"
).should =~ /LessBasic#methods:.*jaroussky/m
end
end

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -41,16 +41,14 @@ describe "whereami" do
Object.remove_const(:Cor)
end
if defined?(BasicObject)
it 'should work in BasicObjects' do
cor = Class.new(BasicObject) do
def blimey!
::Kernel.binding # omnom
end
end.new.blimey!
it 'should work in BasicObjects' do
cor = Class.new(BasicObject) do
def blimey!
::Kernel.binding # omnom
end
end.new.blimey!
pry_eval(cor, 'whereami').should =~ /::Kernel.binding [#] omnom/
end
pry_eval(cor, 'whereami').should =~ /::Kernel.binding [#] omnom/
end
it 'should show description and correct code when __LINE__ and __FILE__ are outside @method.source_location' do

View file

@ -144,7 +144,8 @@ describe Pry::Method do
m.name.should == "gag"
end
if defined?(BasicObject) && !Pry::Helpers::BaseHelpers.rbx? # rubinius issue 1921
# Temporarily disabled to work around rubinius/rubinius#2871.
unless Pry::Helpers::BaseHelpers.rbx?
it "should find the right method from a BasicObject" do
a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end }
@ -433,8 +434,8 @@ describe Pry::Method do
it "should include the Pry::Method.instance_resolution_order of Class after the singleton classes" do
Pry::Method.resolution_order(LS::Top).should ==
[singleton_class(LS::Top), singleton_class(Object), (defined? BasicObject) && singleton_class(BasicObject)].compact +
Pry::Method.instance_resolution_order(Class)
[singleton_class(LS::Top), singleton_class(Object), singleton_class(BasicObject),
*Pry::Method.instance_resolution_order(Class)]
end
end
end
@ -496,15 +497,11 @@ describe Pry::Method do
end
end
unless Pry::Helpers::BaseHelpers.mri_18?
# Ruby 1.8 doesn't support this feature.
it 'should be able to find aliases for methods implemented in C' do
meth = Pry::Method(Hash.new.method(:key?))
aliases = Set.new(meth.aliases)
it 'should be able to find aliases for methods implemented in C' do
meth = Pry::Method(Hash.new.method(:key?))
aliases = Set.new(meth.aliases)
aliases.should == Set.new(["include?", "member?", "has_key?"])
end
aliases.should == Set.new(["include?", "member?", "has_key?"])
end
end
end

View file

@ -5,15 +5,11 @@ describe Pry do
@str_output = StringIO.new
end
if RUBY_VERSION =~ /1.9/
describe "Exotic object support" do
# regression test for exotic object support
it "Should not error when return value is a BasicObject instance" do
ReplTester.start do
input('BasicObject.new').should =~ /^=> #<BasicObject:/
end
describe "Exotic object support" do
# regression test for exotic object support
it "Should not error when return value is a BasicObject instance" do
ReplTester.start do
input('BasicObject.new').should =~ /^=> #<BasicObject:/
end
end
end
@ -89,11 +85,9 @@ describe Pry do
}.should.raise(NameError)
end
if defined?(BasicObject)
it 'should be able to operate inside the BasicObject class' do
pry_eval(BasicObject, ":foo", "Pad.obj = _")
Pad.obj.should == :foo
end
it 'should be able to operate inside the BasicObject class' do
pry_eval(BasicObject, ":foo", "Pad.obj = _")
Pad.obj.should == :foo
end
it 'should set an ivar on an object' do
@ -325,9 +319,8 @@ describe Pry do
end
it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
# should include float in here, but test fails for some reason
# on 1.8.7, no idea why!
[:test, 0, true, false, nil].each do |val|
[:test, 0, true, false, nil,
(0.0 unless Pry::Helpers::BaseHelpers.jruby?)].each do |val|
pry_eval(val, "def hello; end");
val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
end

View file

@ -15,11 +15,9 @@ describe "Pry.run_command" do
out.string.should =~ /hokey_pokey/
end
if !PryTestHelpers.mri18_and_no_real_source_location?
# This is a regression test as 0.9.11 broke this behaviour
it 'can perform a show-source' do
Pry.run_command "show-source drum", :context => @context, :output => out = StringIO.new
out.string.should =~ /roken is dodelijk/
end
# This is a regression test as 0.9.11 broke this behaviour
it 'can perform a show-source' do
Pry.run_command "show-source drum", :context => @context, :output => out = StringIO.new
out.string.should =~ /roken is dodelijk/
end
end