<commit_before>require 'helper'

if !mri18_and_no_real_source_location?
  describe "show-source" do
    before do
      @str_output = StringIO.new
      @o = Object.new
    end

    after do
      Pad.clear
    end

    it 'should output a method\'s source' do
      redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /def sample/
    end

    it 'should output help' do
      mock_pry('show-source -h').should =~ /Usage: show-source/
    end

    it 'should output a method\'s source with line numbers' do
      redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /\d+: def sample/
    end

    it 'should output a method\'s source with line numbers starting at 1' do
      redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /1: def sample/
    end

    it 'should output a method\'s source if inside method without needing to use method name' do
      Pad.str_output = @str_output

      def @o.sample
        redirect_pry_io(InputTester.new("show-source", "exit-all"), Pad.str_output) do
          binding.pry
        end
      end
      @o.sample

      Pad.str_output.string.should =~ /def @o.sample/
    end

    it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
      Pad.str_output = @str_output

      def @o.sample
        redirect_pry_io(InputTester.new("show-source -l", "exit-all"), Pad.str_output) do
          binding.pry
        end
      end
      @o.sample

      Pad.str_output.string.should =~ /def @o.sample/
    end

    it "should find methods even if there are spaces in the arguments" do
      def @o.foo(*bars)
        "Mr flibble"
        self
      end

      redirect_pry_io(InputTester.new("show-source @o.foo('bar', 'baz bam').foo",
                                      "exit-all"), @str_output) do
        binding.pry
      end

      @str_output.string.should =~ /Mr flibble/
    end

    it "should find methods even if the object has an overridden method method" do
      c = Class.new{
        def method;
          98
        end
      }

      mock_pry(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 =~ /could not be found/
    end

    it "should find instance_methods even if the class has an override instance_method method" do
      c = Class.new{
        def method;
          98
        end

        def self.instance_method; 789; end
      }

      mock_pry(binding, "show-source c#method").should =~ /98/
    end

    it "should find instance methods with -M" do
      c = Class.new{ def moo; "ve over!"; end }
      mock_pry(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 }
      mock_pry(binding, "cd c", "show-source -m moo").should =~ /could not be found/
    end

    it "should find normal methods with -m" do
      c = Class.new{ def self.moo; "ve over!"; end }
      mock_pry(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 }
      mock_pry(binding, "cd c", "show-source -M moo").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 }
      mock_pry(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 }
      mock_pry(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

      mock_pry(binding, "show-source --super o.foo").should =~ /:super_wibble/
    end

    it "should not raise an exception when a non-extant super method is requested" do
      def @o.foo(*bars); end

      mock_pry(binding, "show-source --super @o.foo").should =~ /'self.foo' has 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 a method\'s source for a method defined inside pry' do
        redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def dyna_method/
        Object.remove_method :dyna_method
      end

      it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
        redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def dyna_method/
        Object.remove_method :dyna_method
      end

      it 'should output an instance method\'s source for a method defined inside pry' do
        Object.remove_const :A if defined?(A)
        redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def yo/
        Object.remove_const :A
      end

      it 'should output an instance method\'s source for a method defined inside pry using define_method' do
        Object.remove_const :A if defined?(A)
        redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /define_method\(:yup\)/
        Object.remove_const :A
      end
    end

    describe "on sourcable objects" do

      if RUBY_VERSION =~ /1.9/
        it "should output source defined inside pry" do
          redirect_pry_io(InputTester.new("hello = proc { puts 'hello world!' }", "show-source hello"), @str_output) do
            TOPLEVEL_BINDING.pry
          end

          @str_output.string.should =~ /proc { puts 'hello world!' }/
        end
      end

      it "should output source for procs/lambdas stored in variables" do
        hello = proc { puts 'hello world!' }
        mock_pry(binding, "show-source hello").should =~ /proc { puts 'hello world!' }/
      end

      it "should output source for procs/lambdas stored in constants" do
        HELLO = proc { puts 'hello world!' }
        mock_pry(binding, "show-source HELLO").should =~ /proc { puts 'hello world!' }/
        Object.remove_const(:HELLO)
      end

      it "should output source for method objects" do
        def @o.hi; puts 'hi world'; end
        meth = @o.method(:hi)
        mock_pry(binding, "show-source meth").should =~ /puts 'hi world'/
      end<commit_msg>Eliminate possibility of A not being a module...<commit_after>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
      redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /def sample/
    end

    it 'should output help' do
      mock_pry('show-source -h').should =~ /Usage: show-source/
    end

    it 'should output a method\'s source with line numbers' do
      redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /\d+: def sample/
    end

    it 'should output a method\'s source with line numbers starting at 1' do
      redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), @str_output) do
        pry
      end

      @str_output.string.should =~ /1: def sample/
    end

    it 'should output a method\'s source if inside method without needing to use method name' do
      Pad.str_output = @str_output

      def @o.sample
        redirect_pry_io(InputTester.new("show-source", "exit-all"), Pad.str_output) do
          binding.pry
        end
      end
      @o.sample

      Pad.str_output.string.should =~ /def @o.sample/
    end

    it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
      Pad.str_output = @str_output

      def @o.sample
        redirect_pry_io(InputTester.new("show-source -l", "exit-all"), Pad.str_output) do
          binding.pry
        end
      end
      @o.sample

      Pad.str_output.string.should =~ /def @o.sample/
    end

    it "should find methods even if there are spaces in the arguments" do
      def @o.foo(*bars)
        "Mr flibble"
        self
      end

      redirect_pry_io(InputTester.new("show-source @o.foo('bar', 'baz bam').foo",
                                      "exit-all"), @str_output) do
        binding.pry
      end

      @str_output.string.should =~ /Mr flibble/
    end

    it "should find methods even if the object has an overridden method method" do
      c = Class.new{
        def method;
          98
        end
      }

      mock_pry(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 =~ /could not be found/
    end

    it "should find instance_methods even if the class has an override instance_method method" do
      c = Class.new{
        def method;
          98
        end

        def self.instance_method; 789; end
      }

      mock_pry(binding, "show-source c#method").should =~ /98/
    end

    it "should find instance methods with -M" do
      c = Class.new{ def moo; "ve over!"; end }
      mock_pry(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 }
      mock_pry(binding, "cd c", "show-source -m moo").should =~ /could not be found/
    end

    it "should find normal methods with -m" do
      c = Class.new{ def self.moo; "ve over!"; end }
      mock_pry(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 }
      mock_pry(binding, "cd c", "show-source -M moo").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 }
      mock_pry(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 }
      mock_pry(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

      mock_pry(binding, "show-source --super o.foo").should =~ /:super_wibble/
    end

    it "should not raise an exception when a non-extant super method is requested" do
      def @o.foo(*bars); end

      mock_pry(binding, "show-source --super @o.foo").should =~ /'self.foo' has 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 a method\'s source for a method defined inside pry' do
        redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def dyna_method/
        Object.remove_method :dyna_method
      end

      it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
        redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def dyna_method/
        Object.remove_method :dyna_method
      end

      it 'should output an instance method\'s source for a method defined inside pry' do
        redirect_pry_io(InputTester.new("class Test::A", "def yo", "end", "end", "show-source Test::A#yo"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /def yo/
      end

      it 'should output an instance method\'s source for a method defined inside pry using define_method' do
        redirect_pry_io(InputTester.new("class Test::A", "define_method(:yup) {}", "end", "show-source Test::A#yup"), @str_output) do
          TOPLEVEL_BINDING.pry
        end

        @str_output.string.should =~ /define_method\(:yup\)/
      end
    end

    describe "on sourcable objects" do

      if RUBY_VERSION =~ /1.9/
        it "should output source defined inside pry" do
          redirect_pry_io(InputTester.new("hello = proc { puts 'hello world!' }", "show-source hello"), @str_output) do
            TOPLEVEL_BINDING.pry
          end

          @str_output.string.should =~ /proc { puts 'hello world!' }/
        end
      end

      it "should output source for procs/lambdas stored in variables" do
        hello = proc { puts 'hello world!' }
        mock_pry(binding, "show-source hello").should =~ /proc { puts 'hello world!' }/
      end

      it "should output source for procs/lambdas stored in constants" do
        HELLO = proc { puts 'hello world!' }
        mock_pry(binding, "show-source HELLO").should =~ /proc { puts 'hello world!' }/
        Object.remove_const(:HELLO)
      end

      it "should output source for method objects" do
        def @o.hi; puts 'hi world'; end
        meth = @o.method(:hi)
        mock_pry(binding, "show-source meth").should =~ /puts 'hi world'/
      end
{'fraction_non_alphanumeric': 0.1111562690510059, 'fraction_numerical': 0.003928740770846034, 'mean_word_length': 2.2830776072937513, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683032', 'n_tokens_mistral': 4849, 'n_tokens_neox': 4686, 'n_words': 1494}