Skip to content

Commit

Permalink
Merge #155 'Add the GherkinSourceParsedEvent'.
Browse files Browse the repository at this point in the history
Also update the Changelog.md.
  • Loading branch information
brasmusson committed Jul 13, 2018
2 parents c84eaad + 3946851 commit e7816e8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO

### Added

* Add the GherkinSourceParsedEvent ([#155](https://github.com/cucumber/cucumber-ruby-core/pull/155) @brasmusson)
* Add #original_location to Cucumber::Core::Ast::Step and Cucumber::Core::Test::Step ([#150](https://github.com/cucumber/cucumber-ruby-core/pull/150), [#149](https://github.com/cucumber/cucumber-ruby-core/issues/149) @brasmusson)

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions lib/cucumber/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def execute(gherkin_documents, filters = [], event_bus = EventBus.new)
self
end

def compile(gherkin_documents, last_receiver, filters = [])
def compile(gherkin_documents, last_receiver, filters = [], event_bus = EventBus.new)
first_receiver = compose(filters, last_receiver)
compiler = Compiler.new(first_receiver)
parse gherkin_documents, compiler
parse gherkin_documents, compiler, event_bus
self
end

private

def parse(gherkin_documents, compiler)
parser = Core::Gherkin::Parser.new(compiler)
def parse(gherkin_documents, compiler, event_bus)
parser = Core::Gherkin::Parser.new(compiler, event_bus)
gherkin_documents.each do |document|
parser.document document
end
Expand Down
14 changes: 13 additions & 1 deletion lib/cucumber/core/events.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# coding: utf-8
require 'cucumber/core/event'

module Cucumber
module Core
module Events

# Signals that a gherkin source has been parsed
class GherkinSourceParsed < Event.new(:uri, :gherkin_document)
# The uri of the file
attr_reader :uri

# @return [GherkinDocument] the GherkinDocument Ast Node
attr_reader :gherkin_document

end

# Signals that a {Test::Case} is about to be executed
class TestCaseStarted < Event.new(:test_case)

Expand Down Expand Up @@ -43,10 +54,11 @@ class TestCaseFinished < Event.new(:test_case, :result)

end

# The registry contains all the events registered in the core,
# The registry contains all the events registered in the core,
# that will be used by the {EventBus} by default.
def self.registry
build_registry(
GherkinSourceParsed,
TestCaseStarted,
TestStepStarted,
TestStepFinished,
Expand Down
8 changes: 5 additions & 3 deletions lib/cucumber/core/gherkin/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ module Gherkin
ParseError = Class.new(StandardError)

class Parser
attr_reader :receiver
private :receiver
attr_reader :receiver, :event_bus
private :receiver, :event_bus

def initialize(receiver)
def initialize(receiver, event_bus)
@receiver = receiver
@event_bus = event_bus
end

def document(document)
Expand All @@ -26,6 +27,7 @@ def document(document)

begin
result = parser.parse(scanner, token_matcher)
event_bus.gherkin_source_parsed(document.uri, result.dup)

receiver.feature core_builder.feature(result)
rescue *PARSER_ERRORS => e
Expand Down
34 changes: 31 additions & 3 deletions spec/cucumber/core/gherkin/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ module Cucumber
module Core
module Gherkin
describe Parser do
let(:receiver) { double }
let(:parser) { Parser.new(receiver) }
let(:visitor) { double }
let(:receiver) { double }
let(:event_bus) { double }
let(:parser) { Parser.new(receiver, event_bus) }
let(:visitor) { double }

before do
allow( event_bus ).to receive(:gherkin_source_parsed)
end

def parse
parser.document(source)
Expand All @@ -27,6 +32,17 @@ def parse
end
end

context "for valid gherkin" do
let(:source) { Gherkin::Document.new(path, 'Feature:') }
let(:path) { 'path_to/the.feature' }

it "issues a gherkin_source_parsed event" do
allow( receiver ).to receive(:feature)
expect( event_bus ).to receive(:gherkin_source_parsed)
parse
end
end

RSpec::Matchers.define :a_null_feature do
match do |actual|
allow( visitor ).to receive(:feature).and_throw
Expand Down Expand Up @@ -57,6 +73,18 @@ def feature
result
end

context "for valid gherkin" do
source do
feature
end

it "issues a gherkin_source_parsed event" do
allow( receiver ).to receive(:feature)
expect(event_bus).to receive(:gherkin_source_parsed)
parse
end
end

context "when the Gherkin has a language header" do
source do
feature(language: 'ja', keyword: '機能')
Expand Down

0 comments on commit e7816e8

Please sign in to comment.