Skip to content

Commit

Permalink
Separation code that depends on RSpec 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gongo committed Jul 29, 2014
1 parent 5b5da0e commit fca9155
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
43 changes: 8 additions & 35 deletions lib/rspec/core/formatters/turnip_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'turnip_formatter/scenario/pending'
require 'turnip_formatter/printer/index'
require 'turnip_formatter/printer/scenario'
require_relative './turnip_formatter/for_rspec2'

module RSpec
module Core
Expand All @@ -15,6 +16,8 @@ class TurnipFormatter < BaseFormatter

SCENARIO_TEMPORARY_OUTPUT_DIR = File.expand_path('./turnip_tmp')

include TurnipFormatter::ForRSpec2

def initialize(output)
super(output)
@scenarios = []
Expand All @@ -23,37 +26,13 @@ def initialize(output)
FileUtils.mkdir_p(SCENARIO_TEMPORARY_OUTPUT_DIR)
end

def dump_summary(duration, example_count, failure_count, pending_count)
print_params = {
scenarios: scenarios,
failed_count: failure_count,
pending_count: pending_count,
total_time: duration,
scenario_files: scenario_output_files
}
output.puts ::TurnipFormatter::Printer::Index.print_out(print_params)
FileUtils.rm_rf(SCENARIO_TEMPORARY_OUTPUT_DIR)
end

def example_passed(example)
scenario = ::TurnipFormatter::Scenario::Pass.new(example)
output_scenario(scenario)
end

def example_pending(example)
clean_backtrace(example)
scenario = ::TurnipFormatter::Scenario::Pending.new(example)
output_scenario(scenario)
end

def example_failed(example)
clean_backtrace(example)
scenario = ::TurnipFormatter::Scenario::Failure.new(example)
output_scenario(scenario)
end

private

def output_html(params)
output.puts ::TurnipFormatter::Printer::Index.print_out(params)
FileUtils.rm_rf(SCENARIO_TEMPORARY_OUTPUT_DIR)
end

def output_scenario(scenario)
filepath = SCENARIO_TEMPORARY_OUTPUT_DIR + "/#{scenario.id}.html"

Expand All @@ -64,12 +43,6 @@ def output_scenario(scenario)
@scenario_output_files << filepath
@scenarios << scenario
end

def clean_backtrace(example)
return if example.exception.nil?
formatted = format_backtrace(example.exception.backtrace, example)
example.exception.set_backtrace(formatted)
end
end
end
end
Expand Down
47 changes: 47 additions & 0 deletions lib/rspec/core/formatters/turnip_formatter/for_rspec2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-

module RSpec
module Core
module Formatters
class TurnipFormatter < BaseFormatter
module ForRSpec2
def dump_summary(duration, _, failure_count, pending_count)
print_params = {
scenarios: scenarios,
failed_count: failure_count,
pending_count: pending_count,
total_time: duration,
scenario_files: scenario_output_files
}
output_html(print_params)
end

def example_passed(example)
scenario = ::TurnipFormatter::Scenario::Pass.new(example)
output_scenario(scenario)
end

def example_pending(example)
clean_backtrace(example)
scenario = ::TurnipFormatter::Scenario::Pending.new(example)
output_scenario(scenario)
end

def example_failed(example)
clean_backtrace(example)
scenario = ::TurnipFormatter::Scenario::Failure.new(example)
output_scenario(scenario)
end

private

def clean_backtrace(example)
return if example.exception.nil?
formatted = format_backtrace(example.exception.backtrace, example)
example.exception.set_backtrace(formatted)
end
end
end
end
end
end

0 comments on commit fca9155

Please sign in to comment.