Permalink
Browse files

Code gardening.

  - documentation and examples
  - readability improvments:
    - Hash .each -> .each_value removes unused variable, "color"
    - "method" -> "method_name" because it's a string, not a Method
    - "m" -> "string" makes "grey()" like Term::ANSIColor methods
    - move "FORMATS" near its use -> groking "format_for()" is easier
  • Loading branch information...
1 parent ade73ac commit 99523cd02bdfa927f46d85fe9a38ab7b9f582d6a @richardkmichael committed Apr 3, 2012
Showing with 50 additions and 19 deletions.
  1. +25 −12 lib/cucumber/formatter/ansicolor.rb
  2. +22 −4 lib/cucumber/formatter/console.rb
  3. +3 −3 lib/cucumber/step_match.rb
@@ -44,17 +44,19 @@ module Formatter
#
# For instance, if your shell has a black background and a green font (like the
# "Homebrew" settings for OS X' Terminal.app), you may want to override passed
- # steps to be white instead of green. Examples:
+ # steps to be white instead of green.
+ #
+ # Although not listed, you can also use <tt>grey</tt>.
+ #
+ # Examples: (On Windows, use SET instead of export.)
#
# export CUCUMBER_COLORS="passed=white"
# export CUCUMBER_COLORS="passed=white,bold:passed_param=white,bold,underline"
#
- # (If you're on Windows, use SET instead of export).
# To see what colours and effects are available, just run this in your shell:
#
# ruby -e "require 'rubygems'; require 'term/ansicolor'; puts Cucumber::Term::ANSIColor.attributes"
#
- # Although not listed, you can also use <tt>grey</tt>
module ANSIColor
include Cucumber::Term::ANSIColor
@@ -80,15 +82,26 @@ module ANSIColor
end
end
- ALIASES.each do |method, color|
- unless method =~ /.*_param/
+ # Eval to define the color-named methods required by Term::ANSIColor.
+ #
+ # Examples:
+ #
+ # def failed(string=nil, &proc)
+ # red(string, &proc)
+ # end
+ #
+ # def failed_param(string=nil, &proc)
+ # red(bold(string, &proc)) + red
+ # end
+ ALIASES.each_value do |method_name|
+ unless method_name =~ /.*_param/
code = <<-EOF
- def #{method}(string=nil, &proc)
- #{ALIASES[method].split(",").join("(") + "(string, &proc" + ")" * ALIASES[method].split(",").length}
+ def #{method_name}(string=nil, &proc)
+ #{ALIASES[method_name].split(",").join("(") + "(string, &proc" + ")" * ALIASES[method_name].split(",").length}
end
# This resets the colour to the non-param colour
- def #{method}_param(string=nil, &proc)
- #{ALIASES[method+'_param'].split(",").join("(") + "(string, &proc" + ")" * ALIASES[method+'_param'].split(",").length} + #{ALIASES[method].split(",").join(' + ')}
+ def #{method_name}_param(string=nil, &proc)
+ #{ALIASES[method_name+'_param'].split(",").join("(") + "(string, &proc" + ")" * ALIASES[method_name+'_param'].split(",").length} + #{ALIASES[method_name].split(",").join(' + ')}
end
EOF
eval(code)
@@ -124,11 +137,11 @@ def self.define_grey #:nodoc:
end
def self.define_real_grey #:nodoc:
- def grey(m) #:nodoc:
+ def grey(string) #:nodoc:
if ::Cucumber::Term::ANSIColor.coloring?
- "\e[90m#{m}\e[0m"
+ "\e[90m#{string}\e[0m"
else
- m
+ string
end
end
end
@@ -4,15 +4,32 @@
module Cucumber
module Formatter
- # This module contains helper methods that are used by formatters
- # that print output to the terminal.
+
+ # This module contains helper methods that are used by formatters that
+ # print output to the terminal.
+ #
+ # FORMAT is a hash of Proc objects, keyed by step-definition types, e.g.
+ # "FORMAT[:passed]". The Proc is called for each line of the step's
+ # output.
+ #
+ # format_step calls format_string, format_string calls format_for to obtain
+ # the formatting Proc.
+ #
+ # Example:
+ #
+ # The ANSI color console formatter defines a map of step-type to output
+ # color (e.g. "passed" to "green"), then builds methods named for the
+ # step-types (e.g. "def passed"), which themselves wrap the corresponding
+ # color-named methods provided by Term::ANSIColor (e.g. "def red").
+ #
+ # During output, each line is processed by passing it to the formatter Proc
+ # which returns the formatted (e.g. colored) string.
+
module Console
extend ANSIColor
include Duration
include Summary
- FORMATS = Hash.new{|hash, format| hash[format] = method(format).to_proc}
-
def format_step(keyword, step_match, status, source_indent)
comment = if source_indent
c = (' # ' + step_match.file_colon_line).indent(source_indent)
@@ -177,6 +194,7 @@ def empty_messages
private
+ FORMATS = Hash.new{ |hash, format| hash[format] = method(format).to_proc }
def format_for(*keys)
key = keys.join('_').to_sym
@@ -2,9 +2,9 @@ module Cucumber
class StepMatch #:nodoc:
attr_reader :step_definition, :step_arguments
- # Creates a new StepMatch. The +name_to_report+ argument is what's reported, unless it's is,
- # in which case +name_to_report+ is used instead.
- #
+ # Creates a new StepMatch. The +name_to_report+ argument is what's
+ # reported, unless it's is, in which case +name_to_report+ is used instead.
+ #
def initialize(step_definition, name_to_match, name_to_report, step_arguments)
raise "name_to_match can't be nil" if name_to_match.nil?
raise "step_arguments can't be nil (but it can be an empty array)" if step_arguments.nil?

0 comments on commit 99523cd

Please sign in to comment.