2014-08-10 17:04:05 -04:00
|
|
|
RSpec.describe Mutant::RequireHighjack do
|
2014-04-04 08:38:06 -04:00
|
|
|
let(:highjacked_calls) { [] }
|
|
|
|
let(:require_calls) { [] }
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
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(:<<))
|
2015-06-06 18:29:47 -04:00
|
|
|
|
2014-04-04 08:38:06 -04:00
|
|
|
module_function :require
|
2015-06-06 18:29:47 -04:00
|
|
|
public :require
|
2014-04-04 08:38:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
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') }
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
def apply
|
|
|
|
described_class.call(target_module, highjacked_calls.method(:<<))
|
2014-04-04 08:38:06 -04:00
|
|
|
end
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
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
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
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
|
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
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
|