From 6a3cdcf10116471ce5d1286d00605626c8e9ffb1 Mon Sep 17 00:00:00 2001 From: Jared Wyatt Date: Fri, 15 Dec 2017 16:22:54 -0500 Subject: [PATCH 1/2] use defined keys to set success/pending/failure colors --- lib/nyan_cat_formatter/common.rb | 6 +++--- spec/nyan_cat_formatter_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/nyan_cat_formatter/common.rb b/lib/nyan_cat_formatter/common.rb index 692b2b3..879e4cd 100644 --- a/lib/nyan_cat_formatter/common.rb +++ b/lib/nyan_cat_formatter/common.rb @@ -230,15 +230,15 @@ 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) diff --git a/spec/nyan_cat_formatter_spec.rb b/spec/nyan_cat_formatter_spec.rb index 49c0af5..0343b3e 100644 --- a/spec/nyan_cat_formatter_spec.rb +++ b/spec/nyan_cat_formatter_spec.rb @@ -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' From 4c726a19a385df0b6175990405e6a2f1d7f7d47a Mon Sep 17 00:00:00 2001 From: Jared Wyatt Date: Fri, 15 Dec 2017 17:13:45 -0500 Subject: [PATCH 2/2] simplify logic to fetch color codes --- lib/nyan_cat_formatter/common.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/nyan_cat_formatter/common.rb b/lib/nyan_cat_formatter/common.rb index 879e4cd..06d6eba 100644 --- a/lib/nyan_cat_formatter/common.rb +++ b/lib/nyan_cat_formatter/common.rb @@ -9,8 +9,7 @@ module Common ERROR = '!' PENDING = '+' - VT100_CODES = - { + VT100_CODES = { :black => 30, :red => 31, :green => 32, @@ -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, @@ -241,19 +238,13 @@ def failure_color(text) 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