free_mutant/spec/unit/mutant/require_highjack_spec.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

RSpec.describe Mutant::RequireHighjack do
2014-04-04 08:38:06 -04:00
let(:highjacked_calls) { [] }
let(:require_calls) { [] }
let(:target_module) do
2015-01-14 06:57:53 -05:00
acc = require_calls
2014-04-04 08:38:06 -04:00
Module.new do
2015-01-14 06:57:53 -05:00
define_method(:require, &acc.method(:<<))
2014-04-04 08:38:06 -04:00
module_function :require
public :require
2014-04-04 08:38:06 -04:00
end
end
def target_require(logical_name)
Object.new.extend(target_module).require(logical_name)
end
describe '.call' do
2014-04-04 08:38:06 -04:00
let(:logical_name) { double('Logical Name') }
def apply
described_class.call(target_module, highjacked_calls.method(:<<))
2014-04-04 08:38:06 -04:00
end
it 'returns the original implementation from singleton' do
expect { apply.call(logical_name) }
.to change { require_calls }
.from([])
.to([logical_name])
2014-04-04 08:38:06 -04:00
end
it 'does highjack target object #requires calls' do
apply
expect { target_require(logical_name) }
.to change { highjacked_calls }
.from([])
.to([logical_name])
2014-04-04 08:38:06 -04:00
end
it 'does not call original require' do
apply
expect { target_require(logical_name) }
.not_to change { require_calls }.from([])
2014-04-04 08:38:06 -04:00
end
end
end