2014-03-14 00:31:24 -04:00
|
|
|
require_relative '../helper'
|
2012-09-15 17:50:15 -04:00
|
|
|
|
|
|
|
describe "whereami" do
|
|
|
|
it 'should work with methods that have been undefined' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
Cor.send :undef_method, :blimey!
|
|
|
|
Pad.binding = binding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Cor.new.blimey!
|
|
|
|
|
|
|
|
# using [.] so the regex doesn't match itself
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval(Pad.binding, 'whereami')).to match(/self[.]blimey!/)
|
2012-09-15 17:50:15 -04:00
|
|
|
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should work in objects with no method methods' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami').should =~ /Cor[#]blimey!/
|
|
|
|
end
|
|
|
|
|
|
|
|
def method; "moo"; end
|
|
|
|
end
|
|
|
|
Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should properly set _file_, _line_ and _dir_' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami', '_file_').
|
|
|
|
should == File.expand_path(__FILE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
2015-03-17 17:46:45 -04:00
|
|
|
if RUBY_VERSION > "2.0.0"
|
|
|
|
it 'should work with prepended methods' do
|
|
|
|
module Cor2
|
|
|
|
def blimey!
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class Cor
|
|
|
|
prepend Cor2
|
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami').should =~ /Cor2[#]blimey!/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Cor.new.blimey!
|
|
|
|
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
Object.remove_const(:Cor2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-24 01:58:26 -04:00
|
|
|
it 'should work in BasicObjects' do
|
|
|
|
cor = Class.new(BasicObject) do
|
|
|
|
def blimey!
|
|
|
|
::Kernel.binding # omnom
|
|
|
|
end
|
|
|
|
end.new.blimey!
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval(cor, 'whereami')).to match(/::Kernel.binding [#] omnom/)
|
2012-09-23 23:54:35 -04:00
|
|
|
end
|
|
|
|
|
2012-09-15 17:50:15 -04:00
|
|
|
it 'should show description and correct code when __LINE__ and __FILE__ are outside @method.source_location' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
2012-11-18 04:33:32 -05:00
|
|
|
eval <<-END, binding, "spec/fixtures/example.erb", 1
|
2012-09-15 17:50:15 -04:00
|
|
|
pry_eval(binding, 'whereami')
|
|
|
|
END
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Cor.instance_method(:blimey!).source).to match(/pry_eval/)
|
|
|
|
expect(Cor.new.blimey!).to match(/Cor#blimey!.*Look at me/m)
|
2012-09-15 17:50:15 -04:00
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should show description and correct code when @method.source_location would raise an error' do
|
|
|
|
class Cor
|
2012-11-18 04:33:32 -05:00
|
|
|
eval <<-END, binding, "spec/fixtures/example.erb", 1
|
2012-09-15 17:50:15 -04:00
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami')
|
|
|
|
end
|
2015-01-23 04:30:41 -05:00
|
|
|
END
|
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
expect { Cor.instance_method(:blimey!).source }.to raise_error MethodSource::SourceNotFoundError
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Cor.new.blimey!).to match(/Cor#blimey!.*Look at me/m)
|
2015-01-23 04:30:41 -05:00
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Now that we use stagger_output (paging output) we no longer get
|
|
|
|
# the "From: " line, as we output everything in one go (not separate output.puts)
|
|
|
|
# and so the user just gets a single `Error: Cannot open
|
|
|
|
# "not.found.file.erb" for reading.`
|
|
|
|
# which is good enough IMO. Unfortunately we can't test for it
|
|
|
|
# though, as we don't hook stdout.
|
|
|
|
#
|
|
|
|
# it 'should display a description and error if reading the file goes wrong' do
|
|
|
|
# class Cor
|
|
|
|
# def blimey!
|
|
|
|
# eval <<-END, binding, "not.found.file.erb", 7
|
|
|
|
# Pad.tester = pry_tester(binding)
|
|
|
|
# Pad.tester.eval('whereami')
|
|
|
|
# END
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
|
|
|
|
|
|
|
|
# Pad.tester.last_output.should =~
|
|
|
|
# /From: not.found.file.erb @ line 7 Cor#blimey!:/
|
|
|
|
# Object.remove_const(:Cor)
|
|
|
|
# end
|
|
|
|
|
|
|
|
it 'should show code window (not just method source) if parameter passed to whereami' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami 3').should =~ /class Cor/
|
|
|
|
end
|
2013-02-16 20:38:16 -05:00
|
|
|
end
|
2015-01-23 04:30:41 -05:00
|
|
|
Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
it 'should show entire method when -m option used' do
|
|
|
|
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
|
|
|
old_cutoff, Pry::Command::Whereami.method_size_cutoff = Pry::Command::Whereami.method_size_cutoff, 1
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
@foo = 1
|
|
|
|
@bar = 2
|
|
|
|
pry_eval(binding, 'whereami -m')
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-23 04:30:41 -05:00
|
|
|
Pry::Command::Whereami.method_size_cutoff, Pry.config.default_window_size = old_cutoff, old_size
|
|
|
|
result = Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(result).to match(/def blimey/)
|
2015-01-23 04:30:41 -05:00
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
it 'should show entire file when -f option used' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
pry_eval(binding, 'whereami -f')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result = Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(result).to match(/show entire file when -f option used/)
|
2015-01-23 04:30:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "-c" do
|
|
|
|
it 'should show class when -c option used, and locate correct candidate' do
|
|
|
|
require 'fixtures/whereami_helper'
|
2013-02-16 20:38:16 -05:00
|
|
|
class Cor
|
|
|
|
def blimey!
|
2015-01-23 04:30:41 -05:00
|
|
|
pry_eval(binding, 'whereami -c')
|
2013-02-16 20:38:16 -05:00
|
|
|
end
|
2013-02-02 12:02:05 -05:00
|
|
|
end
|
2015-01-23 04:30:41 -05:00
|
|
|
out = Cor.new.blimey!
|
2013-02-16 20:38:16 -05:00
|
|
|
Object.remove_const(:Cor)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out).to match(/class Cor/)
|
|
|
|
expect(out).to match(/blimey/)
|
2013-02-02 12:02:05 -05:00
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
it 'should show class when -c option used, and locate correct superclass' do
|
2013-02-16 20:38:16 -05:00
|
|
|
class Cor
|
|
|
|
def blimey!
|
2015-01-23 04:30:41 -05:00
|
|
|
pry_eval(binding, 'whereami -c')
|
2013-02-16 20:38:16 -05:00
|
|
|
end
|
2013-02-02 12:02:05 -05:00
|
|
|
end
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
class Horse < Cor
|
|
|
|
def pig;end
|
2013-02-16 20:38:16 -05:00
|
|
|
end
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
out = Horse.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
Object.remove_const(:Horse)
|
2013-02-17 14:20:37 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out).to match(/class Cor/)
|
|
|
|
expect(out).to match(/blimey/)
|
2015-01-23 04:30:41 -05:00
|
|
|
end
|
2013-02-17 14:20:37 -05:00
|
|
|
|
2018-10-06 12:58:53 -04:00
|
|
|
it 'should show class when -c option used, and binding is outside a method' do
|
|
|
|
class Cor
|
|
|
|
def blimey;end
|
|
|
|
|
|
|
|
out = pry_eval(binding, 'whereami -c')
|
|
|
|
out.should =~ /class Cor/
|
|
|
|
out.should =~ /blimey/
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
2018-10-06 12:58:53 -04:00
|
|
|
Object.remove_const(:Cor)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
2015-01-23 04:30:41 -05:00
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2013-01-08 19:38:12 -05:00
|
|
|
it 'should not show line numbers or marker when -n switch is used' do
|
|
|
|
class Cor
|
|
|
|
def blimey!
|
|
|
|
out = pry_eval(binding, 'whereami -n')
|
2013-01-08 16:00:10 -05:00
|
|
|
out.should =~ /^\s*def/
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
out.should_not =~ /\=\>/
|
2013-01-08 19:38:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Cor.new.blimey!
|
|
|
|
Object.remove_const(:Cor)
|
|
|
|
end
|
|
|
|
|
2012-09-15 17:50:15 -04:00
|
|
|
it 'should use Pry.config.default_window_size for window size when outside a method context' do
|
|
|
|
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
2015-01-23 04:30:41 -05:00
|
|
|
_foo = :litella
|
|
|
|
_foo = :pig
|
2012-09-15 17:50:15 -04:00
|
|
|
out = pry_eval(binding, 'whereami')
|
2015-01-23 04:30:41 -05:00
|
|
|
_foo = :punk
|
|
|
|
_foo = :sanders
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out).not_to match(/:litella/)
|
|
|
|
expect(out).to match(/:pig/)
|
|
|
|
expect(out).to match(/:punk/)
|
|
|
|
expect(out).not_to match(/:sanders/)
|
2012-09-15 17:50:15 -04:00
|
|
|
|
|
|
|
Pry.config.default_window_size = old_size
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should work at the top level" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval(Pry.toplevel_binding, 'whereami')).to match(
|
2012-09-15 17:50:15 -04:00
|
|
|
/At the top level/
|
2015-03-10 16:49:29 -04:00
|
|
|
)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work inside a class" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval(Pry, 'whereami')).to match(/Inside Pry/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work inside an object" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(pry_eval(Object.new, 'whereami')).to match(/Inside #<Object/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|