2014-08-10 21:04:05 +00:00
|
|
|
RSpec.describe Mutant::RequireHighjack do
|
2014-04-04 12:38:06 +00:00
|
|
|
let(:highjacked_calls) { [] }
|
|
|
|
let(:require_calls) { [] }
|
|
|
|
|
2015-06-06 22:29:47 +00:00
|
|
|
let(:target_module) do
|
2015-01-14 11:57:53 +00:00
|
|
|
acc = require_calls
|
2014-04-04 12:38:06 +00:00
|
|
|
Module.new do
|
2015-01-14 11:57:53 +00:00
|
|
|
define_method(:require, &acc.method(:<<))
|
2015-06-06 22:29:47 +00:00
|
|
|
|
2014-04-04 12:38:06 +00:00
|
|
|
module_function :require
|
2015-06-06 22:29:47 +00:00
|
|
|
public :require
|
2014-04-04 12:38:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-06 22:29:47 +00:00
|
|
|
def target_require(logical_name)
|
|
|
|
Object.new.extend(target_module).require(logical_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.call' do
|
2015-11-15 20:16:42 +00:00
|
|
|
let(:logical_name) { instance_double(String) }
|
2014-04-04 12:38:06 +00:00
|
|
|
|
2015-06-06 22:29:47 +00:00
|
|
|
def apply
|
|
|
|
described_class.call(target_module, highjacked_calls.method(:<<))
|
2014-04-04 12:38:06 +00:00
|
|
|
end
|
|
|
|
|
2015-06-06 22:29:47 +00: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 12:38:06 +00:00
|
|
|
end
|
|
|
|
|
2015-06-06 22:29:47 +00: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 12:38:06 +00:00
|
|
|
end
|
|
|
|
|
2015-06-06 22:29:47 +00:00
|
|
|
it 'does not call original require' do
|
|
|
|
apply
|
|
|
|
expect { target_require(logical_name) }
|
|
|
|
.not_to change { require_calls }.from([])
|
2014-04-04 12:38:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|