lib/cucumber/formatter/json.rb in cucumber-2.3.3 vs lib/cucumber/formatter/json.rb in cucumber-2.4.0
- old
+ new
@@ -1,23 +1,29 @@
require 'multi_json'
require 'base64'
+require 'cucumber/formatter/backtrace_filter'
require 'cucumber/formatter/io'
require 'cucumber/formatter/hook_query_visitor'
module Cucumber
module Formatter
# The formatter used for <tt>--format json</tt>
class Json
include Io
- def initialize(runtime, io, _options)
- @runtime = runtime
- @io = ensure_io(io)
+ def initialize(config)
+ config.on_event :before_test_case, &method(:on_before_test_case)
+ config.on_event :after_test_case, &method(:on_after_test_case)
+ config.on_event :before_test_step, &method(:on_before_test_step)
+ config.on_event :after_test_step, &method(:on_after_test_step)
+ config.on_event :finished_testing, &method(:on_finished_testing)
+ @io = ensure_io(config.out_stream)
@feature_hashes = []
end
- def before_test_case(test_case)
+ def on_before_test_case(event)
+ test_case = event.test_case
builder = Builder.new(test_case)
unless same_feature_as_previous_test_case?(test_case.feature)
@feature_hash = builder.feature_hash
@feature_hashes << @feature_hash
end
@@ -30,11 +36,12 @@
@element_hash = @test_case_hash
end
@any_step_failed = false
end
- def before_test_step(test_step)
+ def on_before_test_step(event)
+ test_step = event.test_step
return if internal_hook?(test_step)
hook_query = HookQueryVisitor.new(test_step)
if hook_query.hook?
@step_or_hook_hash = {}
hooks_of_type(hook_query) << @step_or_hook_hash
@@ -47,20 +54,23 @@
@step_or_hook_hash = create_step_hash(test_step.source.last)
steps << @step_or_hook_hash
@step_hash = @step_or_hook_hash
end
- def after_test_step(test_step, result)
+ def on_after_test_step(event)
+ test_step = event.test_step
+ result = event.result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
return if internal_hook?(test_step)
add_match_and_result(test_step, result)
@any_step_failed = true if result.failed?
end
- def after_test_case(test_case, result)
+ def on_after_test_case(event)
+ result = event.result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
add_failed_around_hook(result) if result.failed? && !@any_step_failed
end
- def done
+ def on_finished_testing(event)
@io.write(MultiJson.dump(@feature_hashes, pretty: true))
end
def puts(message)
test_step_output << message