Class: RSpec::Core::Formatters::HtmlFormatter

Inherits:
BaseTextFormatter show all
Defined in:
lib/rspec/legacy_formatters/html_formatter.rb

Constant Summary

Constants inherited from BaseTextFormatter

BaseTextFormatter::VT100_COLORS, BaseTextFormatter::VT100_COLOR_CODES

Constants included from Helpers

RSpec::Core::Formatters::Helpers::DEFAULT_PRECISION, RSpec::Core::Formatters::Helpers::SUB_SECOND_PRECISION

Instance Attribute Summary

Attributes inherited from BaseFormatter

#duration, #example_count, #example_group, #examples, #failed_examples, #failure_count, #output, #pending_count, #pending_examples

Instance Method Summary collapse

Methods inherited from BaseTextFormatter

#close, #color_code_for, #colorise_summary, #colorize, #dump_commands_to_rerun_failed_examples, #dump_profile, #dump_profile_slowest_example_groups, #dump_profile_slowest_examples, #seed, #summary_line

Methods inherited from BaseFormatter

#close, #example_group_finished, #format_backtrace, #stop

Methods included from Helpers

#format_duration, #format_seconds, #pluralize, #strip_trailing_zeroes

Methods included from LegacyBacktraceFormatter

#format_backtrace

Constructor Details

#initialize(output) ⇒ HtmlFormatter

Returns a new instance of HtmlFormatter.



12
13
14
15
16
17
18
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 12

def initialize(output)
  super(output)
  @example_group_number = 0
  @example_number = 0
  @header_red = nil
  @printer = HtmlPrinter.new(output)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ void (private)



21
22
23
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 21

def method_missing(m, *a, &b)
  # no-op
end

Instance Method Details

#dump_failuresvoid



139
140
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 139

def dump_failures
end

#dump_pendingvoid



142
143
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 142

def dump_pending
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ void



145
146
147
148
149
150
151
152
153
154
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 145

def dump_summary(duration, example_count, failure_count, pending_count)
  @printer.print_summary(
    dry_run?,
    duration,
    example_count,
    failure_count,
    pending_count
  )
  @printer.flush
end

#example_failed(example) ⇒ void



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 73

def example_failed(example)
  super(example)

  unless @header_red
    @header_red = true
    @printer.make_header_red
  end

  unless @example_group_red
    @example_group_red = true
    @printer.make_example_group_header_red(example_group_number)
  end

  @printer.move_progress(percent_done)

  exception = example.execution_result.exception
  exception_details = if exception
    {
      :message => exception.message,
      :backtrace => format_backtrace(exception.backtrace, example).join("\n")
    }
  else
    false
  end
  extra = extra_failure_content(exception)

  @printer.print_example_failed(
    example.execution_result.pending_fixed?,
    example.description,
    example.execution_result.run_time,
    @failed_examples.size,
    exception_details,
    (extra == "") ? false : extra,
    true
  )
  @printer.flush
end

#example_group_numbervoid

The number of the currently running example_group



30
31
32
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 30

def example_group_number
  @example_group_number
end

#example_group_started(example_group) ⇒ void



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 45

def example_group_started(example_group)
  super(example_group)
  @example_group_red = false
  @example_group_number += 1

  unless example_group_number == 1
    @printer.print_example_group_end
  end
  @printer.print_example_group_start( example_group_number, example_group.description, example_group.parent_groups.size )
  @printer.flush
end

#example_numbervoid

The number of the currently running example (a global counter)



35
36
37
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 35

def example_number
  @example_number
end

#example_passed(example) ⇒ void



67
68
69
70
71
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 67

def example_passed(example)
  @printer.move_progress(percent_done)
  @printer.print_example_passed( example.description, example.execution_result.run_time )
  @printer.flush
end

#example_pending(example) ⇒ void



111
112
113
114
115
116
117
118
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 111

def example_pending(example)

  @printer.make_header_yellow unless @header_red
  @printer.make_example_group_header_yellow(example_group_number) unless @example_group_red
  @printer.move_progress(percent_done)
  @printer.print_example_pending( example.description, example.execution_result.pending_message )
  @printer.flush
end

#example_started(example) ⇒ void



62
63
64
65
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 62

def example_started(example)
  super(example)
  @example_number += 1
end

#extra_failure_content(exception) ⇒ void

Override this method if you wish to output extra HTML for a failed spec. For example, you could output links to images or other files produced during the specs.



123
124
125
126
127
128
129
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 123

def extra_failure_content(exception)
  require 'rspec/legacy_formatters/snippet_extractor'
  backtrace = exception.backtrace.map {|line| backtrace_line(line)}
  backtrace.compact!
  @snippet_extractor ||= SnippetExtractor.new
  "    <pre class=\"ruby\"><code>#{@snippet_extractor.snippet(backtrace)}</code></pre>"
end

#message(message) ⇒ void



26
27
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 26

def message(message)
end

#percent_donevoid



131
132
133
134
135
136
137
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 131

def percent_done
  result = 100.0
  if @example_count > 0
    result = (((example_number).to_f / @example_count.to_f * 1000).to_i / 10.0).to_f
  end
  result
end

#start(example_count) ⇒ void



39
40
41
42
43
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 39

def start(example_count)
  super(example_count)
  @printer.print_html_start
  @printer.flush
end

#start_dumpvoid



57
58
59
60
# File 'lib/rspec/legacy_formatters/html_formatter.rb', line 57

def start_dump
  @printer.print_example_group_end
  @printer.flush
end