Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scoreboard colors #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions lib/nyan_cat_formatter/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module Common
ERROR = '!'
PENDING = '+'

VT100_CODES =
{
VT100_CODES = {
:black => 30,
:red => 31,
:green => 32,
Expand All @@ -22,8 +21,6 @@ module Common
:bold => 1,
}

VT100_CODE_VALUES = VT100_CODES.invert

def self.included(base)
base.class_eval do
attr_reader :current, :example_results, :color_index, :pending_count, :failure_count,
Expand Down Expand Up @@ -230,30 +227,24 @@ def cat_length
end

def success_color(text)
wrap(text, :success)
wrap(text, :green)
end

def pending_color(text)
wrap(text, :pending)
wrap(text, :yellow)
end

def failure_color(text)
wrap(text, :failure)
wrap(text, :red)
end

def console_code_for(code_or_symbol)
if VT100_CODE_VALUES.has_key?(code_or_symbol)
code_or_symbol
else
VT100_CODES.fetch(code_or_symbol) do
console_code_for(:white)
end
end
def console_color_code_for(symbol)
VT100_CODES[symbol] || VT100_CODES.fetch(:white)
end

def wrap(text, code_or_symbol)
def wrap(text, symbol)
if RSpec.configuration.color_enabled?
"\e[#{console_code_for(code_or_symbol)}m#{text}\e[0m"
"\e[#{console_color_code_for(symbol)}m#{text}\e[0m"
else
text
end
Expand Down
24 changes: 24 additions & 0 deletions spec/nyan_cat_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@
end
end

describe 'colors' do
before do
allow(RSpec.configuration).to receive(:color_enabled?).and_return(true)
@success_index = 1
@pending_index = 2
@failure_index = 3
@success_color_escape_string = "\e[32" # green
@pending_color_escape_string = "\e[33" # yellow
@failure_color_escape_string = "\e[31" # red
end

it 'should wrap success score in success color' do
expect(@formatter.scoreboard[@success_index][0..3]).to eq(@success_color_escape_string)
end

it 'should wrap pending score in pending color' do
expect(@formatter.scoreboard[@pending_index][0..3]).to eq(@pending_color_escape_string)
end

it 'should wrap failure score in failure color' do
expect(@formatter.scoreboard[@failure_index][0..3]).to eq(@failure_color_escape_string)
end
end

describe 'example_pending' do
it_behaves_like 'a test'

Expand Down