Skip to content

Commit

Permalink
Renames color methods with descriptive intent rather than color name
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Maffitt authored and alindeman committed Dec 20, 2012
1 parent 97400c0 commit d431cd1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
6 changes: 6 additions & 0 deletions lib/rspec/core/configuration.rb
Expand Up @@ -132,6 +132,9 @@ def self.add_setting(name, opts={})
add_setting :success_color
add_setting :pending_color
add_setting :failure_color
add_setting :default_color
add_setting :fixed_color
add_setting :detail_color

# Seed for random ordering (default: generated randomly each run).
#
Expand Down Expand Up @@ -203,6 +206,9 @@ def initialize
@failure_color = :red
@success_color = :green
@pending_color = :yellow
@default_color = :white
@fixed_color = :blue
@detail_color = :cyan
end

# @private
Expand Down
68 changes: 30 additions & 38 deletions lib/rspec/core/formatters/base_text_formatter.rb
Expand Up @@ -33,11 +33,11 @@ def dump_failures
# @param [String] string
def colorise_summary(summary)
if failure_count > 0
custom_color(summary, RSpec.configuration.failure_color)
color(summary, RSpec.configuration.failure_color)
elsif pending_count > 0
custom_color(summary, RSpec.configuration.pending_color)
color(summary, RSpec.configuration.pending_color)
else
custom_color(summary, RSpec.configuration.success_color)
color(summary, RSpec.configuration.success_color)
end
end

Expand All @@ -61,7 +61,7 @@ def dump_commands_to_rerun_failed_examples
output.puts

failed_examples.each do |example|
output.puts(red("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + cyan("# #{example.full_description}"))
output.puts(failure_color("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + detail_color("# #{example.full_description}"))
end
end

Expand All @@ -83,7 +83,7 @@ def dump_profile

sorted_examples.each do |example|
output.puts " #{example.full_description}"
output.puts cyan(" #{red(format_seconds(example.execution_result[:run_time]))} #{red("seconds")} #{format_caller(example.location)}")
output.puts detail_color(" #{failure_color(format_seconds(example.execution_result[:run_time]))} #{failure_color("seconds")} #{format_caller(example.location)}")
end
end

Expand All @@ -103,9 +103,9 @@ def dump_pending
output.puts
output.puts "Pending:"
pending_examples.each do |pending_example|
output.puts yellow(" #{pending_example.full_description}")
output.puts cyan(" # #{pending_example.execution_result[:pending_message]}")
output.puts cyan(" # #{format_caller(pending_example.location)}")
output.puts pending_color(" #{pending_example.full_description}")
output.puts detail_color(" # #{pending_example.execution_result[:pending_message]}")
output.puts detail_color(" # #{format_caller(pending_example.location)}")
if pending_example.execution_result[:exception] \
&& RSpec.configuration.show_failures_in_pending_blocks?
dump_failure_info(pending_example)
Expand Down Expand Up @@ -147,44 +147,36 @@ def colorize(text, code_or_symbol)

protected

def color(text, color_code)
color_enabled? ? "#{color_code}#{text}\e[0m" : text
end

def custom_color(text, color_code)
color_enabled? ? colorize(text, color_code) : text
end

def bold(text)
color(text, "\e[1m")
color_enabled? ? "\e[1m#{text}\e[0m"
end

def red(text)
color(text, "\e[31m")
def color(text, color_code)
color_enabled? ? colorize(text, color_code) : text
end

def green(text)
color(text, "\e[32m")
def failure_color(text)
color(text, RSpec.configuration.failure_color)
end

def yellow(text)
color(text, "\e[33m")
def success_color(text)
color(text, RSpec.configuration.success_color)
end

def blue(text)
color(text, "\e[34m")
def pending_color(text)
color(text, RSpec.configuration.pending_color)
end

def magenta(text)
color(text, "\e[35m")
def fixed_color(text)
color(text, RSpec.configuration.fixed_color)
end

def cyan(text)
color(text, "\e[36m")
def detail_color(text)
color(text, RSpec.configuration.detail_color)
end

def white(text)
color(text, "\e[37m")
def default_color(text)
color(text, RSpec.configuration.default_color)
end

def short_padding
Expand All @@ -203,13 +195,13 @@ def format_caller(caller_info)

def dump_backtrace(example)
format_backtrace(example.execution_result[:exception].backtrace, example).each do |backtrace_info|
output.puts cyan("#{long_padding}# #{backtrace_info}")
output.puts detail_color("#{long_padding}# #{backtrace_info}")
end
end

def dump_pending_fixed(example, index)
output.puts "#{short_padding}#{index.next}) #{example.full_description} FIXED"
output.puts blue("#{long_padding}Expected pending '#{example.metadata[:execution_result][:pending_message]}' to fail. No Error was raised.")
output.puts fixed_color("#{long_padding}Expected pending '#{example.metadata[:execution_result][:pending_message]}' to fail. No Error was raised.")
end

def pending_fixed?(example)
Expand All @@ -223,9 +215,9 @@ def dump_failure(example, index)

def dump_failure_info(example)
exception = example.execution_result[:exception]
output.puts "#{long_padding}#{red("Failure/Error:")} #{red(read_failed_line(exception, example).strip)}"
output.puts "#{long_padding}#{red(exception.class.name << ":")}" unless exception.class.name =~ /RSpec/
exception.message.to_s.split("\n").each { |line| output.puts "#{long_padding} #{red(line)}" } if exception.message
output.puts "#{long_padding}#{failure_color("Failure/Error:")} #{failure_color(read_failed_line(exception, example).strip)}"
output.puts "#{long_padding}#{failure_color(exception.class.name << ":")}" unless exception.class.name =~ /RSpec/
exception.message.to_s.split("\n").each { |line| output.puts "#{long_padding} #{failure_color(line)}" } if exception.message
if shared_group = find_shared_group(example)
dump_shared_failure_info(shared_group)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec/core/formatters/documentation_formatter.rb
Expand Up @@ -38,7 +38,7 @@ def example_failed(example)
end

def failure_output(example, exception)
red("#{current_indentation}#{example.description.strip} (FAILED - #{next_failure_index})")
failure_color("#{current_indentation}#{example.description.strip} (FAILED - #{next_failure_index})")
end

def next_failure_index
Expand All @@ -47,11 +47,11 @@ def next_failure_index
end

def passed_output(example)
green("#{current_indentation}#{example.description.strip}")
success_color("#{current_indentation}#{example.description.strip}")
end

def pending_output(example, message)
yellow("#{current_indentation}#{example.description.strip} (PENDING: #{message})")
pending_color("#{current_indentation}#{example.description.strip} (PENDING: #{message})")
end

def current_indentation
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec/core/formatters/progress_formatter.rb
Expand Up @@ -7,17 +7,17 @@ class ProgressFormatter < BaseTextFormatter

def example_passed(example)
super(example)
output.print green('.')
output.print success_color('.')
end

def example_pending(example)
super(example)
output.print yellow('*')
output.print pending_color('*')
end

def example_failed(example)
super(example)
output.print red('F')
output.print failure_color('F')
end

def start_dump
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/core/formatters/base_text_formatter_spec.rb
Expand Up @@ -377,7 +377,7 @@ def run_all_and_dump_pending
end
end

describe "colorize" do
describe "#colorize" do
it "accepts a VT100 integer code and formats the text with it" do
formatter.colorize('abc', 32).should == "\e[32mabc\e[0m"
end
Expand Down

0 comments on commit d431cd1

Please sign in to comment.