Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplified rspec formatter, good for VS code terminal integration
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
def green
colorize(32)
end
def yellow
colorize(33)
end
def blue
colorize(34)
end
def pink
colorize(35)
end
def light_blue
colorize(36)
end
end
class CustomFormatter
# This registers the notifications this formatter supports, and tells
# us that this was written against the RSpec 3.x formatter API.
RSpec::Core::Formatters.register self, :example_group_started, :example_group_finished, :example_started, :example_failed, :example_passed
def initialize(output)
@output = output
@group_level = 0
end
def example_group_started(notification)
@output.puts if @group_level == 0
@output.puts "#{current_indentation}#{notification.group.description.strip}".green
@group_level += 1
end
def example_group_finished(_notification)
@group_level -= 1 if @group_level > 0
end
def example_started(notification)
@output << (current_indentation + notification.example.description).yellow
end
def example_failed(notification)
@output.puts ' Failed!!!!'.red
@output << notification.exception.to_s.red
@output.puts
# custome your own rule
backtraces = notification.formatted_backtrace.grep_v(/spec\/support/).grep_v(/rvm\/gems/)
@output << backtraces.join("\r\n").blue
end
def example_passed(_notification)
@output.puts ' Passed!'.light_blue
end
def current_indentation
' ' * @group_level
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment