Class: Cucumber::Formatter::Usage

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

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Attribute Summary

Attributes inherited from Ast::Visitor

#options, #step_mother

Instance Method Summary collapse

Methods included from Console

#announce, #format_step, #format_string, #print_counts, #print_elements, #print_exception, #print_passing_wip, #print_snippets, #print_stats, #print_steps

Methods included from ANSIColor

#grey

Methods included from Duration

#format_duration

Methods inherited from Ast::Visitor

#announce, #matches_scenario_names?, #visit_background, #visit_background_name, #visit_comment, #visit_comment_line, #visit_examples, #visit_examples_array, #visit_examples_name, #visit_exception, #visit_feature, #visit_feature_element, #visit_feature_name, #visit_multiline_arg, #visit_outline_table, #visit_py_string, #visit_scenario_name, #visit_step_result, #visit_steps, #visit_table_cell, #visit_table_cell_value, #visit_table_row, #visit_tag_name, #visit_tags

Constructor Details

#initialize(step_mother, io, options) ⇒ Usage

Returns a new instance of Usage.



8
9
10
11
12
13
14
15
# File 'lib/cucumber/formatter/usage.rb', line 8

def initialize(step_mother, io, options)
  super(step_mother)
  @io = io
  @options = options
  @step_definitions = Hash.new { |h,step_definition| h[step_definition] = [] }
  @all_step_definitions = step_mother.step_definitions.dup
  @locations = []
end

Instance Method Details



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cucumber/formatter/usage.rb', line 40

def print_summary(features)
  sorted_defs = @step_definitions.keys.sort_by{|step_definition| step_definition.backtrace_line}
  
  sorted_defs.each do |step_definition|          
    step_matches_and_descriptions = @step_definitions[step_definition].sort_by do |step_match_and_description|
      step_match = step_match_and_description[0]
      step_match.step_definition.regexp.inspect
    end

    step_matches = step_matches_and_descriptions.map{|step_match_and_description| step_match_and_description[0]}

    lengths = step_matches_and_descriptions.map do |step_match_and_description| 
      step_match_and_description[2]
    end
    lengths << step_definition.text_length
    max_length = lengths.max

    @io.print step_definition.regexp.inspect
    @io.puts format_string("   # #{step_definition.file_colon_line}".indent(max_length - step_definition.text_length), :comment)
    da = step_matches_and_descriptions.map do |step_match_and_description|
      step_match      = step_match_and_description[0]
      description     = step_match_and_description[1]
      length          = step_match_and_description[2]
      file_colon_line = step_match_and_description[3]
      " #{description}" + format_string(" # #{file_colon_line}".indent(max_length - length), :comment)
    end
    da.sort.each{|d| puts d}
  end

  print_unused_step_definitions
end


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cucumber/formatter/usage.rb', line 72

def print_unused_step_definitions
  if @all_step_definitions.any?
    max_length = @all_step_definitions.map{|step_definition| step_definition.text_length}.max

    @io.puts format_string("(::) UNUSED (::)", :failed)
    @all_step_definitions.each do |step_definition|
      @io.print format_string(step_definition.regexp.inspect, :failed)
      @io.puts format_string("  # #{step_definition.file_colon_line}".indent(max_length - step_definition.text_length), :comment)
    end
  end
end

#visit_features(features) ⇒ Object



17
18
19
20
# File 'lib/cucumber/formatter/usage.rb', line 17

def visit_features(features)
  super
  print_summary(features)
end

#visit_step(step) ⇒ Object



22
23
24
25
# File 'lib/cucumber/formatter/usage.rb', line 22

def visit_step(step)
  @step = step
  super
end

#visit_step_name(keyword, step_match, status, source_indent, background) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cucumber/formatter/usage.rb', line 27

def visit_step_name(keyword, step_match, status, source_indent, background)
  if step_match.step_definition
    location = @step.file_colon_line
    return if @locations.index(location)
    @locations << location
    
    description = format_step(keyword, step_match, status, nil)
    length = (keyword + step_match.format_args).jlength
    @step_definitions[step_match.step_definition] << [step_match, description, length, location]
    @all_step_definitions.delete(step_match.step_definition)
  end
end