Class: Beagle::Cucumber::TimedFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/beagle/cucumber/timed_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, path_or_io, options) ⇒ TimedFormatter

Returns a new instance of TimedFormatter.



7
8
9
10
11
12
# File 'lib/beagle/cucumber/timed_formatter.rb', line 7

def initialize(step_mother, path_or_io, options)
  @step_mother = step_mother
  @path_or_io = path_or_io
  @options = options
  @scenarios = Hash.new{|h,k| h[k] = []}
end

Instance Method Details

#after_examples(examples) ⇒ Object



54
55
56
57
# File 'lib/beagle/cucumber/timed_formatter.rb', line 54

def after_examples(examples)
  @scenario_outline[:time] = (Time.now - @start_time) if @scenario_outline
  @examples = false
end

#after_features(features) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/beagle/cucumber/timed_formatter.rb', line 63

def after_features(features)
  @times = []
  @by_tag = Hash.new{|h,k| h[k] = []}
  @scenarios.each do |file, scenarios|
    scenarios.each do |scenario|
      tags = scenario[:tags].empty? ? ['none'] : scenario[:tags]
      tags.each do |tag|
        @by_tag[tag] << {:scenario => scenario[:name], :file => file, :time => scenario[:time], :count => scenario[:count]}
      end
    end
    time = scenarios.inject(0) {|sum, scenario| sum + scenario[:time] }
    count = scenarios.inject(0) {|sum, scenario| sum + scenario[:count] }
    tags = scenarios.map {|scenario| scenario[:tags] }.flatten.uniq
    @times << [file, count, tags, time, scenarios]
  end
  @times.sort! {|a,b| b[3] <=> a[3]}
  out_dir = "#{Rails.root}/reports/cucumber"
  template = File.read(File.expand_path('../../templates/cuke.rhtml', File.dirname(__FILE__)))
  rhtml = ERB.new(template, nil, ">")
  FileUtils.mkdir_p out_dir
  outfile = "#{out_dir}/index.html"
  FileUtils.rm_rf outfile
  File.open(outfile, 'w') {|f| f.write(rhtml.result(binding)) }
end

#after_outline_table(outline_table) ⇒ Object



59
60
61
# File 'lib/beagle/cucumber/timed_formatter.rb', line 59

def after_outline_table(outline_table)
  @scenario_outline[:count] = outline_table.example_rows.size
end

#after_steps(steps) ⇒ Object



45
46
47
# File 'lib/beagle/cucumber/timed_formatter.rb', line 45

def after_steps(steps)
  @scenario[:time] = (Time.now - @start_time) if @scenario
end

#before_examples(examples) ⇒ Object



49
50
51
52
# File 'lib/beagle/cucumber/timed_formatter.rb', line 49

def before_examples(examples)
  @examples = true
  @start_time = Time.now
end

#before_feature(feature) ⇒ Object



14
15
16
# File 'lib/beagle/cucumber/timed_formatter.rb', line 14

def before_feature(feature)
  @tags = []
end

#before_feature_element(feature_element) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/beagle/cucumber/timed_formatter.rb', line 18

def before_feature_element(feature_element)
  file = feature_element.feature.file
  @scenarios[file] << {:name => feature_element.name, :line_number => 0, :time => 0, :count => 1, :tags => @tags.flatten.uniq}

  case feature_element
  when Cucumber::Ast::Scenario
    @scenario = @scenarios[file].last
    @scenario_outline = nil
  when Cucumber::Ast::ScenarioOutline
    @scenario_outline = @scenarios[file].last
    @scenario = nil
  end
end

#before_steps(steps) ⇒ Object



41
42
43
# File 'lib/beagle/cucumber/timed_formatter.rb', line 41

def before_steps(steps)
  @start_time = Time.now
end

#scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



36
37
38
39
# File 'lib/beagle/cucumber/timed_formatter.rb', line 36

def scenario_name(keyword, name, file_colon_line, source_indent)
  file, line_number = file_colon_line.split(':')
  @scenarios[file].last[:line_number] = line_number
end

#tag_name(name) ⇒ Object



32
33
34
# File 'lib/beagle/cucumber/timed_formatter.rb', line 32

def tag_name(name)
  @tags << name
end