Class: RSpec::Formatters::IllustratedHtmlFormatter

Inherits:
HtmlFormatter
  • Object
show all
Includes:
RSpec::Formatters
Defined in:
lib/rspec/formatters/illustrated_html_formatter.rb

Overview

An extension to the Core::Formatters::HtmlFormatter that renders the illustrations after each example. A title/label for each illustration can be set by setting the label option (@see RSpec::Illustrate#illustrate). If you want to filter the illustrations based on the test result, you can use the options show_when_passed, show_when_failed and show_when_pending.

Instance Method Summary (collapse)

Methods included from RSpec::Formatters

filter, illustrations_of

Constructor Details

- (IllustratedHtmlFormatter) initialize(output)

Returns a new instance of IllustratedHtmlFormatter



42
43
44
# File 'lib/rspec/formatters/illustrated_html_formatter.rb', line 42

def initialize(output)
  super(output)
end

Instance Method Details

- (Object) example_failed(failure)

See Also:

  • Core::Formatters::HtmlFormatter#example_failed


53
54
55
56
# File 'lib/rspec/formatters/illustrated_html_formatter.rb', line 53

def example_failed(failure)
  super(failure)
  write_illustrations(failure, :show_when_failed)
end

- (Object) example_passed(passed)

See Also:

  • Core::Formatters::HtmlFormatter#example_passed


47
48
49
50
# File 'lib/rspec/formatters/illustrated_html_formatter.rb', line 47

def example_passed(passed)
  super(passed)
  write_illustrations(passed, :show_when_passed)
end

- (Object) example_pending(pending)

See Also:

  • Core::Formatters::HtmlFormatter#example_pending


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

def example_pending(pending)
  super(pending)
  write_illustrations(pending, :show_when_pending)
end

- (String) formatted(illustrations)

Convert the illustrations to a string using the configurable illustration html formatter Configuration#illustration_html_formatter.

Parameters:

  • illustrations (Array<Hash>)

    The illustrations

Returns:

  • (String)

    a html concatenation of the illustrations



85
86
87
88
89
90
91
# File 'lib/rspec/formatters/illustrated_html_formatter.rb', line 85

def formatted(illustrations)
  formatter_proc = RSpec.configuration.illustration_html_formatter

  illustrations.collect{|illustration|
    formatter_proc.call(illustration)
  }.join("\n")
end

- (Object) write_illustrations(notification, filter_key)

Writes the filtered illustrations of an example to the output stream.

Parameters:

  • notification (RSpec::Core::Notifications::ExampleNotification)

    The example notificiation that contains the illustrations.

  • filter_key (Symbol)

    The option that each illustration should have a truthy value of if they are to be written



73
74
75
76
77
78
# File 'lib/rspec/formatters/illustrated_html_formatter.rb', line 73

def write_illustrations(notification, filter_key)
  illustrations = filter(illustrations_of(notification), filter_key)
  return if illustrations.empty?

  output.puts formatted(illustrations)
end