Skip to content

Commit

Permalink
Merge 630ffb3 into 44b9284
Browse files Browse the repository at this point in the history
  • Loading branch information
gongo committed Aug 4, 2014
2 parents 44b9284 + 630ffb3 commit 899999b
Show file tree
Hide file tree
Showing 53 changed files with 662 additions and 616 deletions.
39 changes: 39 additions & 0 deletions lib/rspec/core/formatters/turnip_base_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-

require 'rspec/core/formatters/base_formatter'
require 'turnip_formatter/scenario/pass'
require 'turnip_formatter/scenario/failure'
require 'turnip_formatter/scenario/pending'
require_relative './turnip_formatter/for_rspec2'
require_relative './turnip_formatter/for_rspec3'

module RSpec
module Core
module Formatters
class TurnipBaseFormatter < BaseFormatter
attr_reader :scenarios

def self.inherited(child)
if Formatters.respond_to?(:register)
# not called `child.include` for 1.9.3
child.send(:include, TurnipFormatter::ForRSpec3)
else
child.send(:include, TurnipFormatter::ForRSpec2)
end
end

def initialize(output)
super(output)
@scenarios = []
end

def output_summary(params)
end

def output_scenario(scenario)
@scenarios << scenario
end
end
end
end
end
54 changes: 0 additions & 54 deletions lib/rspec/core/formatters/turnip_formatter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/turnip_formatter/for_rspec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def dump_summary(duration, _, failure_count, pending_count)
total_time: duration,
scenario_files: scenario_output_files
}
output_html(print_params)
output_summary(print_params)
end

def example_passed(example)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/turnip_formatter/for_rspec3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def dump_summary(summary)
total_time: summary.duration,
scenario_files: scenario_output_files
}
output_html(print_params)
output_summary(print_params)
end

def example_passed(notification)
Expand Down
40 changes: 40 additions & 0 deletions lib/rspec/core/formatters/turnip_html_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-

require 'rspec/core/formatters/turnip_base_formatter'
require 'turnip_formatter/html/printer/index'
require 'turnip_formatter/html/printer/scenario'

module RSpec
module Core
module Formatters
class TurnipHtmlFormatter < TurnipBaseFormatter
attr_reader :scenario_output_files

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

def initialize(output)
super(output)
@scenario_output_files = []
FileUtils.mkdir_p(SCENARIO_TEMPORARY_OUTPUT_DIR)
end

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

def output_scenario(scenario)
super(scenario)

filepath = SCENARIO_TEMPORARY_OUTPUT_DIR + "/#{scenario.id}.html"

File.open(filepath, 'w') do |io|
io.puts ::TurnipFormatter::Html::Printer::Scenario.print_out(scenario)
end

@scenario_output_files << filepath
end
end
end
end
end
13 changes: 7 additions & 6 deletions lib/turnip_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ def configuration
end
end

require 'rspec/core/formatters/turnip_formatter'
require 'rspec/core/formatters/turnip_html_formatter'
require 'turnip_formatter/helper'
require 'turnip_formatter/template'
require 'turnip_formatter/step_template/exception'
require 'turnip_formatter/step_template/source'
require 'turnip_formatter/html/template'
require 'turnip_formatter/html/step_template/exception'
require 'turnip_formatter/html/step_template/source'
require 'turnip_formatter/ext/turnip/rspec'
require 'turnip_formatter/printer/index'
require 'turnip_formatter/html/printer/index'
end

RSpecTurnipFormatter = RSpec::Core::Formatters::TurnipFormatter
RSpecTurnipFormatter = RSpec::Core::Formatters::TurnipHtmlFormatter
RSpecTurnipHtmlFormatter = RSpec::Core::Formatters::TurnipHtmlFormatter

TurnipFormatter.configure do |config|
config.title = 'Turnip'
Expand Down
31 changes: 31 additions & 0 deletions lib/turnip_formatter/html/printer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'tilt'

module TurnipFormatter
module Html
module Printer
#
# @example
# render_template(:main, { name: 'user' })
# # => Tilt.new('/path/to/main.erb') render { name: 'user' }
#
def render_template(name, params = {})
render_template_list(name).render(self, params)
end

private

def render_template_list(name)
if templates[name].nil?
path = File.dirname(__FILE__) + "/template/#{name}.haml"
templates[name] = Tilt.new(path)
end

templates[name]
end

def templates
@templates ||= {}
end
end
end
end
28 changes: 28 additions & 0 deletions lib/turnip_formatter/html/printer/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'turnip_formatter/html/printer'
require 'turnip_formatter/html/printer/scenario'
require 'turnip_formatter/html/printer/tab_feature_statistics'
require 'turnip_formatter/html/printer/tab_tag_statistics'
require 'turnip_formatter/html/printer/tab_speed_statistics'

module TurnipFormatter
module Html
module Printer
class Index
class << self
include TurnipFormatter::Html::Printer

def print_out(params)
render_template(:index, {
scenarios: params[:scenarios],
failed_count: params[:failed_count],
pending_count: params[:pending_count],
total_time: params[:total_time],
scenario_files: params[:scenario_files]
}
)
end
end
end
end
end
end
41 changes: 41 additions & 0 deletions lib/turnip_formatter/html/printer/runtime_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'turnip_formatter/html/printer'
require 'turnip_formatter/html/printer/step_extra_args'

module TurnipFormatter
module Html
module Printer
class RuntimeError
class << self
include TurnipFormatter::Html::Printer

def print_out(example, exception)
render_template(:runtime_exception, {
example: example,
runtime_exception: runtime_exception(exception),
example_exception: example_exception(example),
}
)
end

private

def runtime_exception(exception)
render_template(:exception, { title: 'Runtime', exception: exception })
end

def example_exception(example)
unless example.exception
''
else
render_template(:exception, {
title: 'Example',
exception: example.exception
}
)
end
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/turnip_formatter/html/printer/scenario.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'turnip_formatter/html/printer'
require 'turnip_formatter/html/printer/step'
require 'turnip_formatter/html/printer/runtime_error'

module TurnipFormatter
module Html
module Printer
class Scenario
class << self
include TurnipFormatter::Html::Printer

def print_out(scenario)
#
# TODO output for scenario.valid? == false
#
render_template(:scenario, scenario: scenario) if scenario.valid?
rescue => e
TurnipFormatter::Html::Printer::RuntimeError.print_out(scenario.example, e)
end
end
end
end
end
end
38 changes: 38 additions & 0 deletions lib/turnip_formatter/html/printer/step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'turnip_formatter/html/printer'
require 'turnip_formatter/html/printer/step_extra_args'

module TurnipFormatter
module Html
module Printer
class Step
class << self
include TurnipFormatter::Html::Printer

def print_out(step)
step_templates = TurnipFormatter.step_templates_for(step.status)

render_template(:step,
{
step: step,
has_args_or_documents: has_args_or_documents?(step, step_templates),
step_docs: documents(step, step_templates)
}
)
end

private

def has_args_or_documents?(step, templates)
(step.extra_args.length + templates.length) > 0
end

def documents(step, templates)
templates.map do |template, method|
template.send(method, step.example)
end.join("\n")
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/turnip_formatter/html/printer/step_extra_args.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'turnip_formatter/html/printer'

module TurnipFormatter
module Html
module Printer
class StepExtraArgs
class << self
include TurnipFormatter::Html::Printer

def print_out(args)
return '' if args.nil?

args.map do |arg|
if arg.instance_of?(Turnip::Table)
render_template(:step_outline, { table: arg.to_a })
else
render_template(:step_multiline, { lines: arg })
end
end.join
end
end
end
end
end
end
Loading

0 comments on commit 899999b

Please sign in to comment.