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

Add the GherkinSourceParsedEvent #155

Merged
merged 1 commit into from
Jul 13, 2018
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
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