Class: RSpec::Formatters::IllustratedDocumentationFormatter

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

Overview

An extension to the Core::Formatters::DocumentationFormatter 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

- (IllustratedDocumentationFormatter) initialize(output)

Returns a new instance of IllustratedDocumentationFormatter



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

def initialize(output)
  super(output)
end

Instance Method Details

- (String) colored(text, color_type)

Returns The text wrapped in color codes.

Parameters:

  • text (String)

    The text to be colored

  • color_type (Symbol)

Returns:

  • (String)

    The text wrapped in color codes.



86
87
88
# File 'lib/rspec/formatters/illustrated_documentation_formatter.rb', line 86

def colored(text, color_type)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, color_type)
end

- (Object) example_failed(failure)

See Also:

  • Core::Formatters::DocumentationFormatter#example_failed


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

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

- (Object) example_passed(passed)

See Also:

  • Core::Formatters::DocumentationFormatter#example_passed


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

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

- (Object) example_pending(pending)

See Also:

  • Core::Formatters::DocumentationFormatter#example_pending


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

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

- (String) formatted(illustrations)

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

Parameters:

  • illustrations (Array<Hash>)

    The illustrations

Returns:

  • (String)

    a text concatenation of the illustrations



102
103
104
105
106
107
108
# File 'lib/rspec/formatters/illustrated_documentation_formatter.rb', line 102

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

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

- (String) indented(text)

Returns The text where each newline is properly indented.

Parameters:

  • text (String)

Returns:

  • (String)

    The text where each newline is properly indented.



93
94
95
# File 'lib/rspec/formatters/illustrated_documentation_formatter.rb', line 93

def indented(text)
  current_indentation << text.gsub(/\n/, "\n#{current_indentation}")
end

- (Object) write_illustrations(notification, filter_key, color_type)

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

  • color_type (Symbol)

    The symbol that corresponds to a configurable color



75
76
77
78
79
80
# File 'lib/rspec/formatters/illustrated_documentation_formatter.rb', line 75

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

  output.puts colored(indented(formatted(illustrations)), color_type)
end