Sha256: 54ef997963f7bc2f1ce4640ab142ddfd2f5003320dc0ab5bb15e9ab68ad244fb

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

RSpec.describe Mutant::RequireHighjack do
  let(:highjacked_calls) { [] }
  let(:require_calls)    { [] }

  let(:target_module) do
    acc = require_calls
    Module.new do
      define_method(:require, &acc.method(:<<))
      define_singleton_method(:require, &acc.method(:<<))
    end
  end

  def singleton_require(logical_name)
    target_module.require(logical_name)
  end

  def instance_require(logical_name)
    Object.new.extend(target_module).require(logical_name)
  end

  describe '.call' do
    let(:logical_name) { instance_double(String) }

    def apply
      described_class.call(target_module, highjacked_calls.method(:<<))
    end

    it 'prevents warnings' do
      expect(Mutant::WarningFilter.use(&method(:apply))).to eql([])
    end

    it 'returns the original implementation from singleton' do
      expect { apply.call(logical_name) }
        .to change { require_calls }
        .from([])
        .to([logical_name])
    end

    context '#require' do
      it 'does highjack calls' do
        apply
        expect { instance_require(logical_name) }
          .to change { highjacked_calls }
          .from([])
          .to([logical_name])
      end

      it 'does not call original require' do
        apply
        expect { instance_require(logical_name) }
          .not_to change { require_calls }.from([])
      end
    end

    context '.require' do
      it 'does highjack calls' do
        apply
        expect { singleton_require(logical_name) }
          .to change { highjacked_calls }
          .from([])
          .to([logical_name])
      end

      it 'does not call original require' do
        apply
        expect { singleton_require(logical_name) }
          .not_to change { require_calls }.from([])
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mutant-0.8.24 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.23 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.22 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.21 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.20 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.19 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.18 spec/unit/mutant/require_highjack_spec.rb
mutant-0.8.17 spec/unit/mutant/require_highjack_spec.rb