require 'helper' if !mri18_and_no_real_source_location? describe "show-source" do before do @str_output = StringIO.new @o = Object.new Object.const_set(:Test, Module.new) end after do Pad.clear end it "should output a method's source" do pry_eval('show-source sample_method').should =~ /def sample/ end it "should output help" do pry_eval('show-source -h').should =~ /Usage: show-source/ end it "should output a method's source with line numbers" do pry_eval('show-source -l sample_method').should =~ /\d+: def sample/ end it "should output a method's source with line numbers starting at 1" do pry_eval('show-source -b sample_method').should =~ /1: def sample/ end it "should output a method's source if inside method and no name given" do def @o.sample pry_eval(binding, 'show-source').should =~ /def @o.sample/ end @o.sample end it "should output a method's source inside method using the -l switch" do def @o.sample pry_eval(binding, 'show-source -l').should =~ /def @o.sample/ end @o.sample end it "should find methods even if there are spaces in the arguments" do def @o.foo(*bars) "Mr flibble" self end out = pry_eval(binding, "show-source @o.foo('bar', 'baz bam').foo") out.should =~ /Mr flibble/ end it "should find methods even if the object overrides method method" do c = Class.new{ def method; 98 end } pry_eval(binding, "show-source c.new.method").should =~ /98/ end it "should not show the source when a non-extant method is requested" do c = Class.new{ def method; 98; end } mock_pry(binding, "show-source c#wrongmethod").should =~ /undefined method/ end it "should find instance_methods if the class overrides instance_method" do c = Class.new{ def method; 98 end def self.instance_method; 789; end } pry_eval(binding, "show-source c#method").should =~ /98/ end it "should find instance methods with -M" do c = Class.new{ def moo; "ve over!"; end } pry_eval(binding, "cd c", "show-source -M moo").should =~ /ve over/ end it "should not find instance methods with -m" do c = Class.new{ def moo; "ve over!"; end } proc { pry_eval(binding, 'cd c', 'show-source -m moo') }.should.raise(Pry::CommandError).message.should =~ /could not be found/ end it "should find normal methods with -m" do c = Class.new{ def self.moo; "ve over!"; end } pry_eval(binding, 'cd c', 'show-source -m moo').should =~ /ve over/ end it "should not find normal methods with -M" do c = Class.new{ def self.moo; "ve over!"; end } proc { pry_eval(binding, 'cd c', 'show-source -M moo') }.should.raise(Pry::CommandError).message.should =~ /could not be found/ end it "should find normal methods with no -M or -m" do c = Class.new{ def self.moo; "ve over!"; end } pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ end it "should find instance methods with no -M or -m" do c = Class.new{ def moo; "ve over!"; end } pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ end it "should find super methods" do class Foo def foo(*bars) :super_wibble end end o = Foo.new Object.remove_const(:Foo) def o.foo(*bars) :wibble end pry_eval(binding, "show-source --super o.foo"). should =~ /:super_wibble/ end it "should raise a CommandError when super method doesn't exist" do def @o.foo(*bars); end proc { pry_eval(binding, "show-source --super @o.foo") }.should.raise(Pry::CommandError).message.should =~ /no super method/ end # dynamically defined method source retrieval is only supported in # 1.9 - where Method#source_location is native if RUBY_VERSION =~ /1.9/ it "should output the source of a method defined inside Pry" do out = pry_eval("def dyn_method\n:test\nend", 'show-source dyn_method') out.should =~ /def dyn_method/ Object.remove_method :dyn_method end it 'should output source for an instance method defined inside pry' do pry_tester.tap do |t| t.eval "class Test::A\n def yo\n end\nend" t.eval('show-source Test::A#yo').should =~ /def yo/ end end it 'should output source for a repl method defined using define_method' do pry_tester.tap do |t| t.eval "class Test::A\n define_method(:yup) {}\nend" t.eval('show-source Test::A#yup').should =~ /define_method\(:yup\)/ end end end describe "on sourcable objects" do if RUBY_VERSION =~ /1.9/ it "should output source defined inside pry" do pry_tester.tap do |t| t.eval "hello = proc { puts 'hello world!' }" t.eval("show-source hello").should =~ /proc { puts/ end end end it "should output source for procs/lambdas stored in variables" do hello = proc { puts 'hello world!' } pry_eval(binding, 'show-source hello').should =~ /proc { puts/ end it "should output source for procs/lambdas stored in constants" do HELLO = proc { puts 'hello world!' } pry_eval(binding, "show-source HELLO").should =~ /proc { puts/ Object.remove_const(:HELLO) end it "should output source for method objects" do def @o.hi; puts 'hi world'; end meth = @o.method(:hi) pry_eval(binding, "show-source meth").should =~ /puts 'hi world'/ end describe "on variables that shadow methods" do before do @t = pry_tester.eval unindent(<<-EOS) class ::TestHost def hello hello = proc { ' smile ' } pry_tester(binding) end end ::TestHost.new.hello EOS end after do Object.remove_const(:TestHost) end it "source of variable should take precedence over method that is being shadowed" do source = @t.eval('show-source hello') source.should.not =~ /def hello/ source.should =~ /proc { ' smile ' }/ end it "source of method being shadowed should take precedence over variable if given self.meth_name syntax" do @t.eval('show-source self.hello').should =~ /def hello/ end end end describe "on variable or constant" do before do class TestHost def hello "hi there" end end end after do Object.remove_const(:TestHost) end it "should output source of its class if variable doesn't respond to source_location" do test_host = TestHost.new pry_eval(binding, 'show-source test_host'). should =~ /class TestHost\n.*def hello/ end it "should output source of its class if constant doesn't respond to source_location" do TEST_HOST = TestHost.new pry_eval(binding, 'show-source TEST_HOST'). should =~ /class TestHost\n.*def hello/ Object.remove_const(:TEST_HOST) end end describe "on modules" do before do class ShowSourceTestSuperClass def alpha end end class ShowSourceTestClass "bar" do; end pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/ end end end end