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

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

Direct Known Subclasses

TextMateFormatter

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 BacktraceFormatter

#format_backtrace

Constructor Details

#initialize(output) ⇒ HtmlFormatter

Returns a new instance of HtmlFormatter.



9
10
11
12
13
14
15
# File 'lib/rspec/core/formatters/html_formatter.rb', line 9

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



18
19
20
# File 'lib/rspec/core/formatters/html_formatter.rb', line 18

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

Instance Method Details

#dump_failuresObject



136
137
# File 'lib/rspec/core/formatters/html_formatter.rb', line 136

def dump_failures
end

#dump_pendingObject



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

def dump_pending
end

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



142
143
144
145
146
147
148
149
150
151
# File 'lib/rspec/core/formatters/html_formatter.rb', line 142

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) ⇒ Object



70
71
72
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
# File 'lib/rspec/core/formatters/html_formatter.rb', line 70

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_numberObject

The number of the currently running example_group



27
28
29
# File 'lib/rspec/core/formatters/html_formatter.rb', line 27

def example_group_number
  @example_group_number
end

#example_group_started(example_group) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec/core/formatters/html_formatter.rb', line 42

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_numberObject

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



32
33
34
# File 'lib/rspec/core/formatters/html_formatter.rb', line 32

def example_number
  @example_number
end

#example_passed(example) ⇒ Object



64
65
66
67
68
# File 'lib/rspec/core/formatters/html_formatter.rb', line 64

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) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/rspec/core/formatters/html_formatter.rb', line 108

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) ⇒ Object



59
60
61
62
# File 'lib/rspec/core/formatters/html_formatter.rb', line 59

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

#extra_failure_content(exception) ⇒ Object

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.



120
121
122
123
124
125
126
# File 'lib/rspec/core/formatters/html_formatter.rb', line 120

def extra_failure_content(exception)
  require 'rspec/core/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) ⇒ Object



23
24
# File 'lib/rspec/core/formatters/html_formatter.rb', line 23

def message(message)
end

#percent_doneObject



128
129
130
131
132
133
134
# File 'lib/rspec/core/formatters/html_formatter.rb', line 128

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) ⇒ Object



36
37
38
39
40
# File 'lib/rspec/core/formatters/html_formatter.rb', line 36

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

#start_dumpObject



54
55
56
57
# File 'lib/rspec/core/formatters/html_formatter.rb', line 54

def start_dump
  @printer.print_example_group_end
  @printer.flush
end