<commit_before>require 'spec_helper'

describe MethodHooks do

  before do
    Object.send(:remove_const, :Base) if Object.const_defined?(:Base)

    module AnotherModuleWIthMethodAddedHook
      def method_added(method_name)
        _methods << method_name
        super
      end

      def _methods
        @_methods ||= []
      end
    end

    class Base

      extend AnotherModuleWIthMethodAddedHook
      extend MethodHooks

      attr_reader :events

      def initialize
        @events = []
      end

      def save
        @events << 'save'
      end
    end
  end

  describe '::before' do

    it 'should call the before block' do
      Base.instance_eval do
        before(:save) { @events << 'before' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['before', 'save'])
    end

    it 'should allow multiple before hooks on the same method' do
      Base.instance_eval do
        before(:save) { @events << 'first before' }
        before(:save) { @events << 'second before' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['first before', 'second before', 'save'])
    end

  end

  describe '::around' do

    it 'should call the around block' do
      Base.instance_eval do
        around(:save) do |method|
          @events << 'before_around'
          method.call
          @events << 'after_around'
        end
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['before_around', 'save', 'after_around'])
    end

  end

  describe '::after' do

    it 'should call the after block' do
      Base.instance_eval do
        after(:save) { @events << 'after' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['save', 'after'])
    end

  end

  it 'does not override existing .method_added hook' do
    Base.class_eval do
      def some_method
      end
    end

    expect(Base._methods).to include(:some_method)
  end

  describe 'inheritance' do
      it 'should pass parent hooks to the child' do
          Base.instance_eval do
              before(:save) { @events << 'before' }
              around(:save) do |method|
                  @events << 'before_around'
                  method.call
                  @events << 'after_around'
              end

              after(:save) { @events << 'after' }
          end

          Object.send(:remove_const, :Child) if Object.const_defined?(:Child)
          class Child < Base; end

          child = Child.new
          child.save

          expect(child.events).to eq(['before', 'before_around', 'save', 'after_around', 'after'])
      end
  end
end<commit_msg>Fix formatting<commit_after>require 'spec_helper'

describe MethodHooks do

  before do
    Object.send(:remove_const, :Base) if Object.const_defined?(:Base)

    module AnotherModuleWIthMethodAddedHook
      def method_added(method_name)
        _methods << method_name
        super
      end

      def _methods
        @_methods ||= []
      end
    end

    class Base

      extend AnotherModuleWIthMethodAddedHook
      extend MethodHooks

      attr_reader :events

      def initialize
        @events = []
      end

      def save
        @events << 'save'
      end
    end
  end

  describe '::before' do

    it 'should call the before block' do
      Base.instance_eval do
        before(:save) { @events << 'before' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['before', 'save'])
    end

    it 'should allow multiple before hooks on the same method' do
      Base.instance_eval do
        before(:save) { @events << 'first before' }
        before(:save) { @events << 'second before' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['first before', 'second before', 'save'])
    end

  end

  describe '::around' do

    it 'should call the around block' do
      Base.instance_eval do
        around(:save) do |method|
          @events << 'before_around'
          method.call
          @events << 'after_around'
        end
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['before_around', 'save', 'after_around'])
    end

  end

  describe '::after' do

    it 'should call the after block' do
      Base.instance_eval do
        after(:save) { @events << 'after' }
      end

      base = Base.new
      base.save

      expect(base.events).to eq(['save', 'after'])
    end

  end

  it 'does not override existing .method_added hook' do
    Base.class_eval do
      def some_method
      end
    end

    expect(Base._methods).to include(:some_method)
  end

  describe 'inheritance' do
    it 'should pass parent hooks to the child' do
      Base.instance_eval do
        before(:save) { @events << 'before' }
        
        around(:save) do |method|
          @events << 'before_around'
          method.call
          @events << 'after_around'
        end

        after(:save) { @events << 'after' }
      end

      Object.send(:remove_const, :Child) if Object.const_defined?(:Child)
      class Child < Base; end

      child = Child.new
      child.save

      expect(child.events).to eq(['before', 'before_around', 'save', 'after_around', 'after'])
    end
  end
end
{'fraction_non_alphanumeric': 0.10430839002267574, 'fraction_numerical': 0.0, 'mean_word_length': 1.9920859242509892, 'pattern_counts': {'":': 0, '<': 53, '<?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': '3719168', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1509, 'n_words': 442}