2018-09-12 10:21:24 -04:00
|
|
|
# frozen_string_literal: true
|
2018-09-12 09:15:43 -04:00
|
|
|
|
2015-06-06 18:29:47 -04:00
|
|
|
module MutantSpec
|
|
|
|
# Not a real VM, just kidding. It connects the require / eval triggers
|
|
|
|
# require semantics Zombifier relies on in a way we can avoid having to
|
2015-06-13 01:09:18 -04:00
|
|
|
# mock around everywhere to test every detail.
|
2015-06-06 18:29:47 -04:00
|
|
|
class RubyVM
|
2016-01-24 16:08:56 -05:00
|
|
|
include Concord::Public.new(:expected_events)
|
2015-06-06 18:29:47 -04:00
|
|
|
|
|
|
|
# An event being observed by the VM handlers
|
|
|
|
class EventObservation
|
|
|
|
include Concord::Public.new(:type, :payload)
|
2016-04-10 17:33:47 -04:00
|
|
|
end # EventObservation
|
2015-06-06 18:29:47 -04:00
|
|
|
|
|
|
|
# An event being expected, can advance the VM
|
|
|
|
class EventExpectation
|
2015-11-15 15:13:48 -05:00
|
|
|
include AbstractType, Anima.new(
|
|
|
|
:expected_payload,
|
2016-01-24 16:08:56 -05:00
|
|
|
:trigger_requires,
|
|
|
|
:return_value
|
2015-11-15 15:13:48 -05:00
|
|
|
)
|
2015-06-06 18:29:47 -04:00
|
|
|
|
|
|
|
DEFAULTS = IceNine.deep_freeze(trigger_requires: [])
|
|
|
|
|
|
|
|
def initialize(attributes)
|
|
|
|
super(DEFAULTS.merge(attributes))
|
|
|
|
end
|
|
|
|
|
2018-09-12 09:15:43 -04:00
|
|
|
# rubocop:disable Naming/UncommunicativeMethodParamName
|
2015-06-06 18:29:47 -04:00
|
|
|
def handle(vm, observation)
|
|
|
|
unless match?(observation)
|
|
|
|
fail "Unexpected event observation: #{observation.inspect}, expected #{inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
trigger_requires.each(&vm.method(:require))
|
2016-01-24 16:08:56 -05:00
|
|
|
|
|
|
|
self
|
2015-06-06 18:29:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def match?(observation)
|
|
|
|
observation.type.eql?(self.class) && observation.payload.eql?(expected_payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Expectation and advance on require calls
|
|
|
|
class Require < self
|
2016-04-10 17:33:47 -04:00
|
|
|
end # Require
|
2015-06-06 18:29:47 -04:00
|
|
|
|
|
|
|
# Expectation and advance on eval calls
|
|
|
|
class Eval < self
|
2016-04-10 17:33:47 -04:00
|
|
|
end # Eval
|
|
|
|
end # EventExpectation
|
2015-06-06 18:29:47 -04:00
|
|
|
|
|
|
|
# A fake implementation of Kernel#require
|
|
|
|
def require(logical_name)
|
|
|
|
handle_event(EventObservation.new(EventExpectation::Require, logical_name: logical_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
# A fake implementation of Kernel#eval
|
|
|
|
def eval(source, binding, location)
|
|
|
|
handle_event(
|
|
|
|
EventObservation.new(
|
|
|
|
EventExpectation::Eval,
|
2015-11-15 15:13:48 -05:00
|
|
|
binding: binding,
|
|
|
|
source: source,
|
2015-06-06 18:29:47 -04:00
|
|
|
source_location: location
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test if VM events where fully processed
|
|
|
|
def done?
|
|
|
|
expected_events.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def handle_event(observation)
|
|
|
|
fail "Unexpected event: #{observation.type} / #{observation.payload}" if expected_events.empty?
|
|
|
|
|
2016-01-24 16:08:56 -05:00
|
|
|
expected_events.slice!(0).handle(self, observation).return_value
|
2015-06-06 18:29:47 -04:00
|
|
|
end
|
2016-04-10 17:33:47 -04:00
|
|
|
end # RubyVM
|
2015-06-06 18:29:47 -04:00
|
|
|
end # MutantSpec
|