Class: Lucid::Formatter::Usage

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

Direct Known Subclasses

Testdefs

Defined Under Namespace

Classes: StepDefKey

Constant Summary

Constants included from ANSIColor

ANSIColor::ALIASES

Constants included from Term::ANSIColor

Term::ANSIColor::ATTRIBUTES, Term::ANSIColor::ATTRIBUTE_NAMES, Term::ANSIColor::COLORED_REGEXP

Instance Attribute Summary

Attributes inherited from Progress

#runtime

Instance Method Summary collapse

Methods included from Console

#embed, #empty_messages, #format_step, #format_string, #linebreaks, #print_elements, #print_exception, #print_failing_strict, #print_matchers, #print_message, #print_messages, #print_passing_wip, #print_profile_information, #print_stats, #print_table_row_messages, #puts

Methods included from ANSIColor

define_grey, define_real_grey, #green_lucid, #grey, #lucid, #red_lucid, #yellow_lucid

Methods included from Term::ANSIColor

attributes, coloring=, coloring?, included, #uncolored

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(runtime, path_or_io, options) ⇒ Usage

Returns a new instance of Usage.



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

def initialize(runtime, path_or_io, options)
  @runtime = runtime
  @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



124
125
126
127
128
129
# File 'lib/lucid/formatter/usage.rb', line 124

def add_unused_stepdefs
  @runtime.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(step_result) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lucid/formatter/usage.rb', line 33

def after_step_result(step_result)
  step_definition = step_result.step_definition
  unless step_definition.nil? # nil if it's from a scenario outline
    stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)

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

#aggregate_infoObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lucid/formatter/usage.rb', line 111

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_features(features) ⇒ Object



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

def before_features(features)
  print_profile_information
end

#before_step(step) ⇒ Object



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

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

#before_step_result(step_result) ⇒ Object



29
30
31
# File 'lib/lucid/formatter/usage.rb', line 29

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

#max_lengthObject



97
98
99
# File 'lib/lucid/formatter/usage.rb', line 97

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

#max_step_lengthObject



105
106
107
108
109
# File 'lib/lucid/formatter/usage.rb', line 105

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



101
102
103
# File 'lib/lucid/formatter/usage.rb', line 101

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


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

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


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lucid/formatter/usage.rb', line 83

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


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lucid/formatter/usage.rb', line 49

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