Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DRY Server::EventsHandler #8

Merged
merged 4 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ In order to enable your new hook:
1. Add the usage example to the [**Execution order** feature][feature]
1. Run the entire test suite and watch the tests fail (start worrying if they don't!)
1. Add the hook name to the corresponding list in the [definitions file][def]
1. Add the corresponding Dredd **event** to the [server][server]
1. Add the corresponding Dredd **event** to the [events definition][events-handler] (be careful, the hooks order does matter!)
1. Run the test suite and watch it pass : )

Finally, bump the [_minor_][semver] version number, update the `README`, the `CHANGELOG` and do anything you need to do in order to release!

[def]: ../lib/dredd_hooks/definitions.rb
[server]: ../lib/dredd_hooks/server.rb
[events-handler]: ../lib/dredd_hooks/server/events_handler.rb

[runner-spec]: ../spec/lib/dredd_hooks/runner_spec.rb
[methods-spec]: ../spec/lib/dredd_hooks/methods_spec.rb
Expand Down
22 changes: 22 additions & 0 deletions lib/dredd_hooks/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,27 @@ module DreddHooks

HOOKS_ON_MULTIPLE_TRANSACTIONS = [:before_each, :before_each_validation,
:after_each, :before_all, :after_all]

Server::EVENTS = {
beforeEach: [
:before_each,
:before,
],
beforeEachValidation: [
:before_each_validation,
:before_validation,
],
afterEach: [
:after,
:after_each,
],
afterAll: [
:after_all,
],
beforeAll: [
:before_all,
],
}

end

15 changes: 15 additions & 0 deletions lib/dredd_hooks/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module DreddHooks

class UnknownHookError < RuntimeError

def initialize(hook_name)
@hook_name = hook_name
end

def to_s
"Unknown hook: #{@hook_name}"
end
end

end

42 changes: 17 additions & 25 deletions lib/dredd_hooks/server/events_handler.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
require 'dredd_hooks/definitions'
require 'dredd_hooks/errors'
require 'dredd_hooks/runner'

module DreddHooks

class Server
class EventsHandler

attr_reader :runner
private :runner
attr_reader :events, :runner
private :events, :runner

def initialize
@runner = Runner.instance
def initialize(events=EVENTS, runner=Runner.instance)
@events = events
@runner = runner
end

def handle(event, transaction)

if event == "beforeEach"
transaction = runner.run_before_each_hooks_for_transaction(transaction)
transaction = runner.run_before_hooks_for_transaction(transaction)
end

if event == "beforeEachValidation"
transaction = runner.run_before_each_validation_hooks_for_transaction(transaction)
transaction = runner.run_before_validation_hooks_for_transaction(transaction)
end

if event == "afterEach"
transaction = runner.run_after_hooks_for_transaction(transaction)
transaction = runner.run_after_each_hooks_for_transaction(transaction)
end

if event == "beforeAll"
transaction = runner.run_before_all_hooks_for_transaction(transaction)
end

if event == "afterAll"
transaction = runner.run_after_all_hooks_for_transaction(transaction)
begin
events.fetch(event.to_sym).each do |hook_name|
begin
transaction = runner.send("run_#{hook_name}_hooks_for_transaction", transaction)
rescue NoMethodError
raise UnknownHookError.new(hook_name)
end
end
rescue KeyError => error
end

transaction
Expand Down
38 changes: 38 additions & 0 deletions spec/integration/definitions_consistency_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

module DreddHooks
describe 'All the defined hooks', protected: true do

DEFINED_HOOKS = HOOKS_ON_SINGLE_TRANSACTIONS + HOOKS_ON_MULTIPLE_TRANSACTIONS

describe 'are runned on least one event' do

let(:hooks_in_use) { Server::EVENTS.map{ |_event, hooks| hooks }.flatten.uniq }

DEFINED_HOOKS.each do |hook_name|
it ":#{hook_name} is refered to by at least one event" do
expect(hooks_in_use).to include(hook_name)
end
end

end
end

describe 'DreddHooks events definition', protected: true do

describe 'only refers to hooks that are already defined' do

let(:defined_hooks) { HOOKS_ON_SINGLE_TRANSACTIONS + HOOKS_ON_MULTIPLE_TRANSACTIONS }

Server::EVENTS.each do |_event, hook_names|
hook_names.each do |hook_name|
it ":#{hook_name} is defined" do
expect(defined_hooks).to include(hook_name)
end
end
end

end
end
end

44 changes: 37 additions & 7 deletions spec/lib/dredd_hooks/server/events_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
require 'spec_helper'

# Runs the 'known' hook but not the 'unknown' one.
class DummyRunner

def run_known_hooks_for_transaction(transaction)
transaction
end
end

module DreddHooks
describe Server::EventsHandler, private: true do

let(:events_handler) { described_class.new }
let(:events_handler) { described_class.new }

it { expect(events_handler).to respond_to :handle }
it { expect(events_handler).to respond_to :handle }

describe '#handle' do
describe '#handle' do

it 'returns a transaction' do
event = 'do_nothing'
it 'returns a transaction' do
event = 'doNothing'
transaction = double()
expect(events_handler.handle(event, transaction)).to eq transaction
end
end
end

context 'when attempting to handle a hook not defined by the DSL' do

let(:events_definition) {
{
beforeAll: [
:known,
],
afterAll: [
:unknown
]
}
}
let(:transaction) { double }
let(:runner) { DummyRunner.new }
let(:events_handler) { described_class.new(events_definition, runner) }

it 'raises a UnknownHookError' do
expect{ events_handler.handle('beforeAll', transaction) }.not_to raise_error
expect{ events_handler.handle('afterAll', transaction) }.to raise_error UnknownHookError
end
end
end
end
end