<commit_before>require 'spec_helper'

describe "and_call_original" do
  context "on a partial mock object" do
    let(:klass) do
      Class.new do
        def meth_1
          :original
        end

        def meth_2(x)
          yield x, 3
        end

        def self.new_instance
          new
        end
      end
    end

    let(:instance) { klass.new }

    it 'passes the received message through to the original method' do
      instance.should_receive(:meth_1).and_call_original
      expect(instance.meth_1).to eq(:original)
    end

    it 'passes args and blocks through to the original method' do
      instance.should_receive(:meth_2).and_call_original
      value = instance.meth_2(2) { |a, b| a * b }
      expect(value).to eq(6)
    end

    it 'works for singleton methods' do
      def instance.foo; :bar; end
      instance.should_receive(:foo).and_call_original
      expect(instance.foo).to eq(:bar)
    end

    it 'works for class methods defined on a superclass' do
      subclass = Class.new(klass)
      subclass.should_receive(:new_instance).and_call_original
      expect(subclass.new_instance).to be_a(subclass)
    end

    it 'works for class methods defined on a grandparent class' do
      sub_subclass = Class.new(Class.new(klass))
      sub_subclass.should_receive(:new_instance).and_call_original
      expect(sub_subclass.new_instance).to be_a(sub_subclass)
    end

    it 'works for class methods defined on the Class class' do
      klass.should_receive(:new).and_call_original
      expect(klass.new).to be_an_instance_of(klass)
    end

    it "works for instance methods defined on the object's class's superclass" do
      subclass = Class.new(klass)
      inst = subclass.new
      inst.should_receive(:meth_1).and_call_original
      expect(inst.meth_1).to eq(:original)
    end

    context 'on an object that defines method_missing' do
      before do
        klass.class_eval do
          private

          def method_missing(name, *args)
            return super unless name.to_s =~ /^greet_(.*)$/
            "Hello, #{$1}"
          end
        end
      end

      it 'works when the method_missing definition handles the message' do
        instance.should_receive(:greet_jack).and_call_original
        expect(instance.greet_jack).to eq("Hello, jack")
      end

      it 'raises an error on invocation if method_missing does not handle the message' do
        instance.should_receive(:not_a_handled_message).and_call_original
<commit_msg>Clarify specs based on @dchelimsky's feedback.<commit_after>require 'spec_helper'

describe "and_call_original" do
  context "on a partial mock object" do
    let(:klass) do
      Class.new do
        def meth_1
          :original
        end

        def meth_2(x)
          yield x, :additional_yielded_arg
        end

        def self.new_instance
          new
        end
      end
    end

    let(:instance) { klass.new }

    it 'passes the received message through to the original method' do
      instance.should_receive(:meth_1).and_call_original
      expect(instance.meth_1).to eq(:original)
    end

    it 'passes args and blocks through to the original method' do
      instance.should_receive(:meth_2).and_call_original
      value = instance.meth_2(:submitted_arg) { |a, b| [a, b] }
      expect(value).to eq([:submitted_arg, :additional_yielded_arg])
    end

    it 'works for singleton methods' do
      def instance.foo; :bar; end
      instance.should_receive(:foo).and_call_original
      expect(instance.foo).to eq(:bar)
    end

    it 'works for class methods defined on a superclass' do
      subclass = Class.new(klass)
      subclass.should_receive(:new_instance).and_call_original
      expect(subclass.new_instance).to be_a(subclass)
    end

    it 'works for class methods defined on a grandparent class' do
      sub_subclass = Class.new(Class.new(klass))
      sub_subclass.should_receive(:new_instance).and_call_original
      expect(sub_subclass.new_instance).to be_a(sub_subclass)
    end

    it 'works for class methods defined on the Class class' do
      klass.should_receive(:new).and_call_original
      expect(klass.new).to be_an_instance_of(klass)
    end

    it "works for instance methods defined on the object's class's superclass" do
      subclass = Class.new(klass)
      inst = subclass.new
      inst.should_receive(:meth_1).and_call_original
      expect(inst.meth_1).to eq(:original)
    end

    context 'on an object that defines method_missing' do
      before do
        klass.class_eval do
          private

          def method_missing(name, *args)
            if name.to_s == "greet_jack"
              "Hello, jack"
            else
              super
            end
          end
        end
      end

      it 'works when the method_missing definition handles the message' do
        instance.should_receive(:greet_jack).and_call_original
        expect(instance.greet_jack).to eq("Hello, jack")
      end

      it 'raises an error on invocation if method_missing does not handle the message' do
        instance.should_receive(:not_a_handled_message).and_call_original

{'fraction_non_alphanumeric': 0.07734162456276719, 'fraction_numerical': 0.00388651379712398, 'mean_word_length': 2.6119298245614035, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2639000', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1515, 'n_words': 423}