Skip to content

Commit

Permalink
Spike integration with cucumber/cucumber html-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
tooky committed Sep 9, 2016
1 parent 3ae61e3 commit 0c36d3a
Showing 1 changed file with 20 additions and 117 deletions.
137 changes: 20 additions & 117 deletions lib/cucumber/formatter/event_stream.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'securerandom'
require 'date'

module Cucumber
module Formatter
class EventStream
PROTOCOL_VERSION = "0.0.1"

def initialize(config)
@config = config
@series_id = SecureRandom.uuid
set_up_event_handlers
end

Expand All @@ -18,134 +22,33 @@ def emit(event_data)
@config.out_stream.puts(event_data.to_json)
end

def ms_since_epoch
Integer(DateTime.now.strftime('%Q'))
end

def set_up_event_handlers
on(:test_run_started) do
emit({
event: "TestRunStarted",
protocol_version: PROTOCOL_VERSION
type: "start",
timestamp: ms_since_epoch,
series: @series_id
})
end

on(:gherkin_source_read) do |event|
emit({
event: "GherkinSourceRead",
id: "#{event.path}:1",
source: event.source
})
end

on(:step_match) do
emit({
event: "StepDefinitionMatched",
})
end

on(:test_case_starting) do |event|
emit({
event: "TestCaseStarted",
id: event.test_case.location
})
end

on(:test_step_starting) do |event|
emit({
event: "TestStepStarted",
id: event.test_step.location
})
end

on(:test_step_finished) do |test_step, result|
event_builder = TestStepResultEvent.new(@config)
result.describe_to event_builder, test_step
emit(event_builder)
end

on(:test_case_finished) do |test_case, result|
event_builder = TestCaseResultEvent.new(@config)
result.describe_to event_builder, test_case
emit(event_builder)
end

on(:test_run_finished) do
emit({
event: "TestRunFinished"
type: "source",
timestamp: ms_since_epoch,
series: @series_id,
uri: event.path, # TODO: should be uri
data: event.source,
media: {
encoding: "utf-8",
type: "text/vnd.cucumber.gherkin+plain"
}
})
end
end

class TestStepResultEvent
def initialize(config)
@config = config
@data = {}
end

def passed(test_step)
data[:event] = "TestStepPassed"
data[:id] = test_step.location
end

def failed(test_step)
data[:event] = "TestStepPassed"
data[:id] = test_step.location
end

def duration(duration, test_step)
data[:duration] = duration.nanoseconds
end

def exception(exception, test_case)
data[:error_summary] = exception.message
data[:error_detail] = exception.backtrace
end

def to_json
data.to_json
end

private

attr_reader :data
private :data

def emit(event_data)
@config.out_stream.puts(event_data.to_json)
end
end

class TestCaseResultEvent
def initialize(config)
@config = config
@data = {}
end

def passed(test_step)
data[:event] = "TestCasePassed"
data[:id] = test_step.location
end

def failed(test_step)
data[:event] = "TestCasePassed"
data[:id] = test_step.location
end

def duration(duration, test_step)
data[:duration] = duration.nanoseconds
end

def to_json
data.to_json
end

private

attr_reader :data
private :data

def emit(event_data)
@config.out_stream.puts(event_data.to_json)
end
end

end
end
end

0 comments on commit 0c36d3a

Please sign in to comment.