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

Integrate new core events #973

Merged
merged 9 commits into from
Jun 6, 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
2 changes: 1 addition & 1 deletion cucumber.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.homepage = "http://cukes.info"
s.platform = Gem::Platform::RUBY
s.required_ruby_version = ">= 1.9.3"
s.add_dependency 'cucumber-core', '~> 1.4.0'
s.add_dependency 'cucumber-core', '~> 2.0'
s.add_dependency 'builder', '>= 2.1.2'
s.add_dependency 'diff-lcs', '>= 1.1.3'
s.add_dependency 'gherkin', '~> 4.0'
Expand Down
13 changes: 7 additions & 6 deletions features/docs/api/listen_for_events.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Feature: Listen for events

Cucumber's `config` object has an event bus that you can use to listen for
various events that happen during your test run.

Scenario: Step Matched Event
Given a file named "features/test.feature" with:
"""
Expand All @@ -16,9 +19,8 @@ Feature: Listen for events
"""
AfterConfiguration do |config|
io = config.out_stream
config.on_event Cucumber::Events::StepMatch do |event|
config.on_event :step_match do |event|
io.puts "Success!"
io.puts "Event type: #{event.class}"
io.puts "Step name: #{event.test_step.name}"
io.puts "Source location: #{event.step_match.location}"
end
Expand All @@ -28,7 +30,6 @@ Feature: Listen for events
Then it should pass with:
"""
Success!
Event type: Cucumber::Events::StepMatch
Step name: matching
Source location: features/step_definitions/steps.rb:1
"""
Expand All @@ -45,14 +46,14 @@ Feature: Listen for events
"""
AfterConfiguration do |config|
io = config.out_stream
config.on_event Cucumber::Events::AfterTestStep do |event|
io.puts "YO"
config.on_event :test_step_finished do |event|
io.puts event.result.passed?
end
end
"""
When I run `cucumber`
Then it should pass with:
"""
YO
true
"""

32 changes: 2 additions & 30 deletions features/docs/extending_cucumber/custom_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Custom Formatter
class Formatter
def initialize(config)
@io = config.out_stream
config.on_event Cucumber::Events::BeforeTestCase do |event|
config.on_event :test_case_starting do |event|
print_test_case_name(event.test_case)
end
end
Expand All @@ -41,34 +41,6 @@ Feature: Custom Formatter

"""

Scenario: Implement v2.0 formatter methods
Note that this method is likely to be deprecated in favour of events - see above.

Given a file named "features/support/custom_formatter.rb" with:
"""
module MyCustom
class Formatter
def initialize(config)
@io = config.out_stream
end

def before_test_case(test_case)
feature = test_case.source.first
scenario = test_case.source.last
@io.puts feature.short_name.upcase
@io.puts " #{scenario.name.upcase}"
end
end
end
"""
When I run `cucumber features/f.feature --format MyCustom::Formatter`
Then it should pass with exactly:
"""
I'LL USE MY OWN
JUST PRINT ME

"""

Scenario: Use the legacy API
This is deprecated and should no longer be used.

Expand Down Expand Up @@ -99,7 +71,7 @@ Feature: Custom Formatter
"""

Scenario: Use both old and new
You can use a specific shim to opt-in to both APIs at once.
You can both APIs at once, for now

Given a file named "features/support/custom_mixed_formatter.rb" with:
"""
Expand Down
1 change: 1 addition & 0 deletions lib/cucumber/cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'cucumber/cli/rerun_file'
require 'cucumber/constantize'
require 'cucumber/core/gherkin/tag_expression'
require 'cucumber'

module Cucumber
module Cli
Expand Down
21 changes: 12 additions & 9 deletions lib/cucumber/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'cucumber/constantize'
require 'cucumber/cli/rerun_file'
require 'cucumber/events'
require 'cucumber/core/event_bus'
require 'forwardable'
require 'cucumber/core/gherkin/tag_expression'
require 'cucumber'

module Cucumber
# The base class for configuring settings for a Cucumber run.
Expand All @@ -20,12 +22,14 @@ def self.default
#
# @param event_id [Symbol, Class, String] Identifier for the type of event to subscribe to
# @param handler_object [Object optional] an object to be called when the event occurs
# @yield [Object] Block to be called when th event occurs
# @yield [Object] Block to be called when the event occurs
# @method on_event
def_instance_delegator :event_bus, :register, :on_event
def_instance_delegator :event_bus, :on, :on_event

# @private
def_instance_delegator :event_bus, :notify
def notify(message, *args)
event_bus.send(message, *args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event bus does also have a broadcast method that takes an event class instance as argument. Should it also be made available through the configuration object?

end

def initialize(user_options = {})
@options = default_options.merge(Cucumber::Hash(user_options))
Expand Down Expand Up @@ -216,6 +220,10 @@ def register_snippet_generator(generator)
self
end

def event_bus
@options[:event_bus]
end

private

def default_options
Expand All @@ -235,15 +243,10 @@ def default_options
:snippets => true,
:source => true,
:duration => true,
:event_bus => Events::Bus.new(Cucumber::Events)
:event_bus => Core::EventBus.new(Core::Events.registry.merge(Cucumber::Events.registry))
}
end

def event_bus
@options[:event_bus]
end


def default_features_paths
["features"]
end
Expand Down
14 changes: 11 additions & 3 deletions lib/cucumber/events.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
Dir[File.dirname(__FILE__) + '/events/*.rb'].map(&method(:require))

module Cucumber

# Events tell you what's happening while Cucumber runs your features.
#
# They're designed to be read-only, appropriate for writing formatters and other
# output tools. If you need to be able to influence the result of a scenario, use a hook instead.
# output tools. If you need to be able to influence the result of a scenario, use a {RbSupport::RbDsl hook} instead.
#
# To subscribe to an event, use {Cucumber::Configuration#on_event}
#
# @example
# AfterConfiguration do |config|
# config.on_event :after_test_step do |event|
# config.on_event :test_case_finished do |event|
# puts event.result
# end
# end
#
module Events
def self.registry
Core::Events.build_registry(
StepMatch,
TestRunFinished,
)
end
end
end

Dir[File.dirname(__FILE__) + '/events/*.rb'].map(&method(:require))
25 changes: 0 additions & 25 deletions lib/cucumber/events/after_test_case.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/cucumber/events/after_test_step.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/cucumber/events/before_test_case.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/cucumber/events/before_test_step.rb

This file was deleted.

86 changes: 0 additions & 86 deletions lib/cucumber/events/bus.rb

This file was deleted.

Loading