class RSpec::Formatters::IllustratedDocumentationFormatter
An extension to the {RSpec::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.
Public Class Methods
# File lib/rspec/formatters/illustrated_documentation_formatter.rb, line 42 def initialize(output) super(output) end
Public Instance Methods
@private @param text [String] The text to be colored @param color_type [Symbol] @return [String] The text wrapped in color codes.
# File lib/rspec/formatters/illustrated_documentation_formatter.rb, line 86 def colored(text, color_type) RSpec::Core::Formatters::ConsoleCodes.wrap(text, color_type) end
@see RSpec::Core::Formatters::DocumentationFormatter#example_failed
# 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
@see RSpec::Core::Formatters::DocumentationFormatter#example_passed
# 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
@see RSpec::Core::Formatters::DocumentationFormatter#example_pending
# 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
@private Convert the illustrations to a string using the configurable illustration formatter {RSpec::Configuration#illustration_formatter}. @param illustrations [Array<Hash>] The illustrations @return [String] a text concatenation of the illustrations
# 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
@private @param text [String] @return [String] The text where each newline is properly indented.
# File lib/rspec/formatters/illustrated_documentation_formatter.rb, line 93 def indented(text) current_indentation << text.gsub(/\n/, "\n#{current_indentation}") end
@private Writes the filtered illustrations of an example to the output stream.
@param notification [RSpec::Core::Notifications::ExampleNotification]
The example notificiation that contains the illustrations.
@param filter_key [Symbol]
The option that each illustration should have a truthy value of if they are to be written
@param color_type [Symbol]
The symbol that corresponds to a configurable color
# 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