<commit_before>require 'support/before_all_shared_example_group'

RSpec.describe "Using the legacy should syntax" do
  include_context "with syntax", [:should, :expect]

  describe "#received_message?" do
    let(:dbl) { double("double").as_null_object }

    it "answers false for received_message? when no messages received" do
      expect(dbl.received_message?(:message)).to be_falsey
    end

    it "answers true for received_message? when message received" do
      dbl.message
      expect(dbl.received_message?(:message)).to be_truthy
    end

    it "answers true for received_message? when message received with correct args" do
      dbl.message 1,2,3
      expect(dbl.received_message?(:message, 1,2,3)).to be_truthy
    end

    it "answers false for received_message? when message received with incorrect args" do
      dbl.message 1,2,3
      expect(dbl.received_message?(:message, 1,2)).to be_falsey
    end
  end

  describe "#stub" do
    it "supports options" do
      double.stub(:foo, :expected_from => "bar")
    end

    it 'returns `nil` from all terminal actions to discourage further configuration' do
      expect(double.stub(:foo).and_return(1)).to be_nil
      expect(double.stub(:foo).and_raise("boom")).to be_nil
      expect(double.stub(:foo).and_throw(:foo)).to be_nil
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.stub(:foo)
      end
    end
  end

  describe "#stub_chain" do
    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.stub_chain(:foo, :bar)
      end
    end
  end

  describe "#unstub" do
    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.unstub(:foo)
      end
    end

    it "replaces the stubbed method with the original method" do
      obj = Object.new
      def obj.foo; :original; end
      obj.stub(:foo)
      obj.unstub(:foo)
      expect(obj.foo).to eq :original
    end

    it "removes all stubs with the supplied method name" do
      obj = Object.new
      def obj.foo; :original; end
      obj.stub(:foo).with(1)
      obj.stub(:foo).with(2)
      obj.unstub(:foo)
      expect(obj.foo).to eq :original
    end

    it "does not remove any expectations with the same method name" do
      obj = Object.new
      def obj.foo; :original; end
      obj.should_receive(:foo).with(3).and_return(:three)
      obj.stub(:foo).with(1)
      obj.stub(:foo).with(2)
      obj.unstub(:foo)
      expect(obj.foo(3)).to eq :three
    end

    it "restores the correct implementations when stubbed and unstubbed on a parent and child class" do
      parent = Class.new
      child  = Class.new(parent)

      parent.stub(:new)
      child.stub(:new)
      parent.unstub(:new)
      child.unstub(:new)

      expect(parent.new).to be_an_instance_of parent
      expect(child.new).to be_an_instance_of child
    end

    it "raises a MockExpectationError if the method has not been stubbed" do
      obj = Object.new
      expect {
        obj.unstub(:foo)
      }.to raise_error(RSpec::Mocks::MockExpectationError)
    end
  end

  describe "#should_receive" do
    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.should_receive(:foo)
      end
    end

    context "with an options hash" do
      it "reports the file and line submitted with :expected_from" do
        begin
          mock = RSpec::Mocks::Double.new("a mock")
          mock.should_receive(:message, :expected_from => "/path/to/blah.ext:37")
          verify mock
        rescue Exception => e
        ensure
          expect(e.backtrace.to_s).to match(/\/path\/to\/blah.ext:37/m)
        end
      end

      it "uses the message supplied with :message" do
        expect {
          m = RSpec::Mocks::Double.new("a mock")
          m.should_receive(:message, :message => "recebi nada")
          verify m
        }.to raise_error("recebi nada")
      end

      it "uses the message supplied with :message after a similar stub" do
        expect {
          m = RSpec::Mocks::Double.new("a mock")
          m.stub(:message)
          m.should_receive(:message, :message => "from mock")
          verify m
        }.to raise_error("from mock")
      end
    end
  end

  describe "#should_not_receive" do
    it "returns a negative message expectation" do
      expect(Object.new.should_not_receive(:foobar)).to be_negative
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.should_not_receive(:foo)
      end
    end
  end

  describe "#any_instance" do
    let(:klass) do
      Class.new do
        def existing_method; :existing_method_return_value; end
        def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
        def another_existing_method; end
        private
        def private_method; :private_method_return_value; end
      end
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.any_instance.should_receive(:foo)
      end
    end

    it "adds an class to the current space" do
      expect {
        klass.any_instance
      }.to change { RSpec::Mocks.space.any_instance_recorders.size }.by(1)
    end

    context "invocation order" do
      describe "#stub" do
        it "raises an error if 'stub' follows 'with'" do
          expect { klass.any_instance.with("1").stub(:foo) }.to raise_error(NoMethodError)
        end

        it "raises an error if 'with' follows 'and_return'" do
          expect { klass.any_instance.stub(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)
        end
<commit_msg>Add some basic sanity check specs for `should` syntax.<commit_after>require 'support/before_all_shared_example_group'

RSpec.describe "Using the legacy should syntax" do
  include_context "with syntax", [:should, :expect]

  describe "#received_message?" do
    let(:dbl) { double("double").as_null_object }

    it "answers false for received_message? when no messages received" do
      expect(dbl.received_message?(:message)).to be_falsey
    end

    it "answers true for received_message? when message received" do
      dbl.message
      expect(dbl.received_message?(:message)).to be_truthy
    end

    it "answers true for received_message? when message received with correct args" do
      dbl.message 1,2,3
      expect(dbl.received_message?(:message, 1,2,3)).to be_truthy
    end

    it "answers false for received_message? when message received with incorrect args" do
      dbl.message 1,2,3
      expect(dbl.received_message?(:message, 1,2)).to be_falsey
    end
  end

  describe "#stub" do
    it "supports options" do
      double.stub(:foo, :expected_from => "bar")
    end

    it 'returns `nil` from all terminal actions to discourage further configuration' do
      expect(double.stub(:foo).and_return(1)).to be_nil
      expect(double.stub(:foo).and_raise("boom")).to be_nil
      expect(double.stub(:foo).and_throw(:foo)).to be_nil
    end

    it 'sets up a canned response' do
      dbl = double
      dbl.stub(:foo).and_return(3)
      expect(dbl.foo).to eq(3)
    end

    it 'can stub multiple messages using a hash' do
      dbl = double
      dbl.stub(:foo => 2, :bar => 1)
      expect(dbl.foo).to eq(2)
      expect(dbl.bar).to eq(1)
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.stub(:foo)
      end
    end
  end

  describe "#stub_chain" do
    it 'can stub a sequence of messages' do
      dbl = double
      dbl.stub_chain(:foo, :bar, :baz => 17)
      expect(dbl.foo.bar.baz).to eq(17)
      expect {
        dbl.foo.baz.bar
      }.to raise_error(RSpec::Mocks::MockExpectationError)
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.stub_chain(:foo, :bar)
      end
    end
  end

  describe "#unstub" do
    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.unstub(:foo)
      end
    end

    it "replaces the stubbed method with the original method" do
      obj = Object.new
      def obj.foo; :original; end
      obj.stub(:foo)
      obj.unstub(:foo)
      expect(obj.foo).to eq :original
    end

    it "removes all stubs with the supplied method name" do
      obj = Object.new
      def obj.foo; :original; end
      obj.stub(:foo).with(1)
      obj.stub(:foo).with(2)
      obj.unstub(:foo)
      expect(obj.foo).to eq :original
    end

    it "does not remove any expectations with the same method name" do
      obj = Object.new
      def obj.foo; :original; end
      obj.should_receive(:foo).with(3).and_return(:three)
      obj.stub(:foo).with(1)
      obj.stub(:foo).with(2)
      obj.unstub(:foo)
      expect(obj.foo(3)).to eq :three
    end

    it "restores the correct implementations when stubbed and unstubbed on a parent and child class" do
      parent = Class.new
      child  = Class.new(parent)

      parent.stub(:new)
      child.stub(:new)
      parent.unstub(:new)
      child.unstub(:new)

      expect(parent.new).to be_an_instance_of parent
      expect(child.new).to be_an_instance_of child
    end

    it "raises a MockExpectationError if the method has not been stubbed" do
      obj = Object.new
      expect {
        obj.unstub(:foo)
      }.to raise_error(RSpec::Mocks::MockExpectationError)
    end
  end

  describe "#should_receive" do
    it 'fails on verification if the message is not received' do
      dbl = double
      dbl.should_receive(:foo)
      expect { verify_all }.to raise_error(RSpec::Mocks::MockExpectationError)
    end

    it 'does not fail on verification if the message is received' do
      dbl = double
      dbl.should_receive(:foo)
      dbl.foo
      expect { verify_all }.not_to raise_error
    end

    it 'can set a canned response' do
      dbl = double
      dbl.should_receive(:bar).and_return(3)
      expect(dbl.bar).to eq(3)
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.should_receive(:foo)
      end
    end

    context "with an options hash" do
      it "reports the file and line submitted with :expected_from" do
        begin
          mock = RSpec::Mocks::Double.new("a mock")
          mock.should_receive(:message, :expected_from => "/path/to/blah.ext:37")
          verify mock
        rescue Exception => e
        ensure
          expect(e.backtrace.to_s).to match(/\/path\/to\/blah.ext:37/m)
        end
      end

      it "uses the message supplied with :message" do
        expect {
          m = RSpec::Mocks::Double.new("a mock")
          m.should_receive(:message, :message => "recebi nada")
          verify m
        }.to raise_error("recebi nada")
      end

      it "uses the message supplied with :message after a similar stub" do
        expect {
          m = RSpec::Mocks::Double.new("a mock")
          m.stub(:message)
          m.should_receive(:message, :message => "from mock")
          verify m
        }.to raise_error("from mock")
      end
    end
  end

  describe "#should_not_receive" do
    it "returns a negative message expectation" do
      expect(Object.new.should_not_receive(:foobar)).to be_negative
    end

    it 'fails when the message is received' do
      dbl = double
      dbl.should_not_receive(:foo)
      expect { dbl.foo }.to raise_error(RSpec::Mocks::MockExpectationError)
    end

    it 'does not fail on verification if the message is not received' do
      dbl = double
      dbl.should_not_receive(:foo)
      expect { verify_all }.not_to raise_error
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.should_not_receive(:foo)
      end
    end
  end

  describe "#any_instance" do
    let(:klass) do
      Class.new do
        def existing_method; :existing_method_return_value; end
        def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
        def another_existing_method; end
        private
        def private_method; :private_method_return_value; end
      end
    end

    include_examples "fails in a before(:all) block" do
      def use_rspec_mocks
        Object.any_instance.should_receive(:foo)
      end
    end

    it "adds an class to the current space" do
      expect {
        klass.any_instance
      }.to change { RSpec::Mocks.space.any_instance_recorders.size }.by(1)
    end

    it 'can stub a method' do
      klass.any_instance.stub(:foo).and_return(2)
      expect(klass.new.foo).to eq(2)
    end

    it 'can mock a method' do
      klass.any_instance.should_receive(:foo)
      klass.new
      expect { verify_all }.to raise_error(RSpec::Mocks::MockExpectationError)
    end

    context "invocation order" do
      describe "#stub" do
        it "raises an error if 'stub' follows 'with'" do
          expect { klass.any_instance.with("1").stub(:foo) }.to raise_error(NoMethodError)
        end

        it "raises an error if 'with' follows 'and_return'" do
          expect { klass.any_instance.stub(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)
        end

{'fraction_non_alphanumeric': 0.0985968617984309, 'fraction_numerical': 0.004978877489438744, 'mean_word_length': 2.5887926367081753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '3615367', 'n_tokens_mistral': 4279, 'n_tokens_neox': 4183, 'n_words': 1163}