Class: Cucumber::Formatter::Usage

Inherits:
Progress show all
Includes:
Console
Defined in:
lib/cucumber/formatter/usage.rb

Direct Known Subclasses

Stepdefs

Defined Under Namespace

Classes: StepDefKey

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Attribute Summary

Attributes inherited from Progress

#step_mother

Instance Method Summary collapse

Methods included from Console

#announce, #embed, #empty_announcements, #format_step, #format_string, #print_announcement, #print_announcements, #print_counts, #print_elements, #print_exception, #print_passing_wip, #print_snippets, #print_stats, #print_table_row_announcements

Methods included from ANSIColor

#cukes, define_grey, define_real_grey, #green_cukes, #grey, #red_cukes, #yellow_cukes

Methods included from Summary

#scenario_summary, #step_summary

Methods included from Duration

#format_duration

Methods inherited from Progress

#after_feature_element, #after_features, #after_outline_table, #after_steps, #before_feature_element, #before_outline_table, #before_steps, #exception, #table_cell_value

Methods included from Io

#ensure_dir, #ensure_file, #ensure_io

Constructor Details

#initialize(step_mother, path_or_io, options) ⇒ Usage

Returns a new instance of Usage.



13
14
15
16
17
18
# File 'lib/cucumber/formatter/usage.rb', line 13

def initialize(step_mother, path_or_io, options)
  @step_mother = step_mother
  @io = ensure_io(path_or_io, "usage")
  @options = options
  @stepdef_to_match = Hash.new{|h,stepdef_key| h[stepdef_key] = []}
end

Instance Method Details

#add_unused_stepdefsObject



119
120
121
122
123
124
# File 'lib/cucumber/formatter/usage.rb', line 119

def add_unused_stepdefs
  @step_mother.unmatched_step_definitions.each do |step_definition|
    stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)
    @stepdef_to_match[stepdef_key] = []
  end
end

#after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cucumber/formatter/usage.rb', line 29

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  if step_match.name.nil? # nil if it's from a scenario outline
    stepdef_key = StepDefKey.new(step_match.step_definition.regexp_source, step_match.step_definition.file_colon_line)

    @stepdef_to_match[stepdef_key] << {
      :keyword => keyword, 
      :step_match => step_match, 
      :status => status, 
      :file_colon_line => @step.file_colon_line,
      :duration => @duration
    }
  end
  super
end

#aggregate_infoObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cucumber/formatter/usage.rb', line 106

def aggregate_info
  @stepdef_to_match.each do |key, steps|
    if steps.empty?
      key.status = :skipped
      key.mean_duration = 0
    else
      key.status = Ast::StepInvocation.worst_status(steps.map{|step| step[:status]})
      total_duration = steps.inject(0) {|sum, step| step[:duration] + sum}
      key.mean_duration = total_duration / steps.length
    end
  end
end

#before_step(step) ⇒ Object



20
21
22
23
# File 'lib/cucumber/formatter/usage.rb', line 20

def before_step(step)
  @step = step
  @start_time = Time.now
end

#before_step_result(*args) ⇒ Object



25
26
27
# File 'lib/cucumber/formatter/usage.rb', line 25

def before_step_result(*args)
  @duration = Time.now - @start_time
end

#max_lengthObject



92
93
94
# File 'lib/cucumber/formatter/usage.rb', line 92

def max_length
  [max_stepdef_length, max_step_length].compact.max
end

#max_step_lengthObject



100
101
102
103
104
# File 'lib/cucumber/formatter/usage.rb', line 100

def max_step_length
  @stepdef_to_match.values.to_a.flatten.map do |step|
    step[:keyword].unpack('U*').length + step[:step_match].format_args.unpack('U*').length
  end.max
end

#max_stepdef_lengthObject



96
97
98
# File 'lib/cucumber/formatter/usage.rb', line 96

def max_stepdef_length
  @stepdef_to_match.keys.flatten.map{|key| key.regexp_source.unpack('U*').length}.max
end


67
68
69
70
71
72
73
74
75
76
# File 'lib/cucumber/formatter/usage.rb', line 67

def print_step_definition(stepdef_key)
  @io.print format_string(sprintf("%.7f", stepdef_key.mean_duration), :skipped) + " " unless @options[:dry_run]
  @io.print format_string(stepdef_key.regexp_source, stepdef_key.status)
  if @options[:source]
    indent = max_length - stepdef_key.regexp_source.unpack('U*').length
    line_comment = "   # #{stepdef_key.file_colon_line}".indent(indent)
    @io.print(format_string(line_comment, :comment))
  end
  @io.puts
end


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cucumber/formatter/usage.rb', line 78

def print_steps(stepdef_key)
  @stepdef_to_match[stepdef_key].each do |step|
    @io.print "  "
    @io.print format_string(sprintf("%.7f", step[:duration]), :skipped) + " " unless @options[:dry_run]
    @io.print format_step(step[:keyword], step[:step_match], step[:status], nil)
    if @options[:source]
      indent = max_length - (step[:keyword].unpack('U*').length + step[:step_match].format_args.unpack('U*').length)
      line_comment = " # #{step[:file_colon_line]}".indent(indent)
      @io.print(format_string(line_comment, :comment))
    end
    @io.puts
  end
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cucumber/formatter/usage.rb', line 44

def print_summary(features)
  add_unused_stepdefs
  aggregate_info

  if @options[:dry_run]
    keys = @stepdef_to_match.keys.sort {|a,b| a.regexp_source <=> b.regexp_source}
  else
    keys = @stepdef_to_match.keys.sort {|a,b| a.mean_duration <=> b.mean_duration}.reverse
  end

  keys.each do |stepdef_key|
    print_step_definition(stepdef_key)

    if @stepdef_to_match[stepdef_key].any?
      print_steps(stepdef_key)
    else
      @io.puts("  " + format_string("NOT MATCHED BY ANY STEPS", :failed))
    end
  end
  @io.puts
  super
end