diff --git a/lib/aruba/errors.rb b/lib/aruba/errors.rb index 04d05900..d9d9897c 100644 --- a/lib/aruba/errors.rb +++ b/lib/aruba/errors.rb @@ -33,7 +33,4 @@ class CommandAlreadyStartedError < Error; end # Raised if an event name cannot be resolved class EventNameResolveError < StandardError; end - - # Raised if given object is not an event - class NoEventError < StandardError; end end diff --git a/lib/aruba/event_bus.rb b/lib/aruba/event_bus.rb index b6ed34b5..934976ab 100644 --- a/lib/aruba/event_bus.rb +++ b/lib/aruba/event_bus.rb @@ -2,6 +2,7 @@ require "aruba/event_bus/name_resolver" require "aruba/errors" +require "cucumber/core/event_bus" module Aruba # Event bus @@ -9,55 +10,15 @@ module Aruba # Implements and in-process pub-sub events broadcaster allowing multiple observers # to subscribe to different events that fire as your tests are executed. # - class EventBus - # Create EventBus - # - # @param [#transform] resolver - # A resolver which transforms Symbol, String, Class into an event Class. - def initialize(resolver) - @resolver = resolver - @handlers = Hash.new { |h, k| h[k] = [] } - end - - # Register for an event - # - # @param [String, Symbol, Class, Array] event_ids - # If Array, register multiple events witht the same handler. If String, - # Symbol, Class register handler for given event. - # - # @param [#call] handler_object - # The handler object, needs to have method `#call`. Either - # `handler_object` or `block` can be defined. The handler object gets the - # event passed to `#call`. - # - # @yield - # Handler block which gets the event passed as parameter. - def register(event_ids, handler_object = nil, &handler_proc) - handler = handler_proc || handler_object - - if handler.nil? || !handler.respond_to?(:call) - raise ArgumentError, "Please pass either an object#call or a handler block" + class EventBus < Cucumber::Core::EventBus + def register(ids, handler_object = nil, &handler_proc) + Array(ids).each do |event_id| + on(event_id, handler_object, &handler_proc) end - - Array(event_ids).flatten.each do |id| - @handlers[ - @resolver.transform(id).to_s - ] << handler - end - - nil end - # Broadcast an event - # - # @param [Object] event - # An object of registered event class. This object is passed to the event - # handler. - # def notify(event) - raise NoEventError, "Please pass an event object, not a class" if event.is_a?(Class) - - @handlers[event.class.to_s].each { |handler| handler.call(event) } + broadcast(event) end end end diff --git a/lib/aruba/event_bus/name_resolver.rb b/lib/aruba/event_bus/name_resolver.rb index 53410d92..f1fc4f19 100644 --- a/lib/aruba/event_bus/name_resolver.rb +++ b/lib/aruba/event_bus/name_resolver.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true require "aruba/errors" +require "cucumber/core/event_bus" # Event notification library module Aruba # EventBus - class EventBus + class EventBus < Cucumber::Core::EventBus # Resolve name to Event name class NameResolver # @private diff --git a/lib/aruba/events.rb b/lib/aruba/events.rb index 90364487..6ee270fe 100644 --- a/lib/aruba/events.rb +++ b/lib/aruba/events.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "cucumber/core/event" + # Aruba module Aruba # Events @@ -10,13 +12,7 @@ module Events # inherited by normal events # # @private - class BasicEvent - attr_reader :entity - - def initialize(entity) - @entity = entity - end - end + class BasicEvent < Cucumber::Core::Event.new(:entity); end # Command was stopped class CommandStopped < BasicEvent; end @@ -38,5 +34,15 @@ class ChangedWorkingDirectory < BasicEvent; end # The configuration was changed class ChangedConfiguration < BasicEvent; end + + def self.registry + [CommandStarted, + CommandStopped, + AddedEnvironmentVariable, + ChangedEnvironmentVariable, + DeletedEnvironmentVariable, + ChangedWorkingDirectory, + ChangedConfiguration].to_h { |klass| [klass.event_id, klass] } + end end end diff --git a/lib/aruba/runtime.rb b/lib/aruba/runtime.rb index d975a93e..df0c396a 100644 --- a/lib/aruba/runtime.rb +++ b/lib/aruba/runtime.rb @@ -42,7 +42,7 @@ class Runtime attr_accessor :config, :environment, :logger, :command_monitor, :announcer, :event_bus def initialize(opts = {}) - @event_bus = EventBus.new(EventBus::NameResolver.new(Aruba::Events)) + @event_bus = EventBus.new(Aruba::Events.registry) @announcer = opts.fetch(:announcer, Aruba.platform.announcer.new) @config = opts.fetch(:config, ConfigWrapper.new(Aruba.config.make_copy, @event_bus)) diff --git a/spec/aruba/event_bus_spec.rb b/spec/aruba/event_bus_spec.rb index 48ed347d..3ae6b79b 100644 --- a/spec/aruba/event_bus_spec.rb +++ b/spec/aruba/event_bus_spec.rb @@ -5,7 +5,10 @@ describe Aruba::EventBus do let(:bus) { described_class.new(name_resolver) } - let(:name_resolver) { Aruba::EventBus::NameResolver.new("Events") } + let(:name_resolver) do + { test_event: Events::TestEvent, + another_test_event: Events::AnotherTestEvent } + end let(:event_klass) { Events::TestEvent } let(:event_instance) { event_klass.new } @@ -16,7 +19,6 @@ before do stub_const("Events::TestEvent", Class.new) stub_const("Events::AnotherTestEvent", Class.new) - stub_const("Events::MalformedTestEvent", Module.new) stub_const("MyHandler", Class.new { def call(*); end }) stub_const("MyMalformedHandler", Class.new) end @@ -44,7 +46,7 @@ context "when event is not an event instance" do it "raises an error" do - expect { bus.notify event_klass }.to raise_error Aruba::NoEventError + expect { bus.notify event_klass }.to raise_error ArgumentError end end end @@ -107,13 +109,6 @@ it { expect { bus.notify event_instance }.not_to raise_error } end - context "when malformed custom handler" do - it "raises an ArgumentError" do - expect { bus.register(:test_event, MyMalformedHandler.new) } - .to raise_error ArgumentError - end - end - context "when no handler is given" do it "raises an ArgumentError" do expect { bus.register(event_klass) }.to raise_error ArgumentError