Class: Cucumber::Formatter::Usage

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

Overview

The formatter used for --format usage

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

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, #print_tag_limit_warnings, #record_tag_occurrences

Methods included from ANSIColor

define_grey, define_real_grey, #grey

Methods included from Duration

#format_duration

Constructor Details

#initialize(step_mother, io, options) ⇒ Usage

Returns a new instance of Usage.



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

def initialize(step_mother, io, options)
  @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

#after_features(features) ⇒ Object



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

def after_features(features)
  print_summary(features)
end

#before_step(step) ⇒ Object



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

def before_step(step)
  @step = step
end


38
39
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
# File 'lib/cucumber/formatter/usage.rb', line 38

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_source
    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_source
    @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


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

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_source, :failed)
      @io.puts format_string("  # #{step_definition.file_colon_line}".indent(max_length - step_definition.text_length), :comment)
    end
  end
end

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



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

def 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