Created
January 15, 2018 04:13
-
-
Save bingxie/e25e1a43fe75aea38b3aa30b9b0536e4 to your computer and use it in GitHub Desktop.
Simplified rspec formatter, good for VS code terminal integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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