Permalink
Browse files

Ensured that requires precede formatter

Allowed formatter to take class as a string
  • Loading branch information...
1 parent b66e44d commit 7910d7f5ac64a1d0202208f344560ffbb6ec260d Thibaud Guillaume-Gentil committed with dchelimsky Jun 15, 2010
@@ -174,7 +174,9 @@ def full_description=(description)
end
def formatter=(formatter_to_use)
- if formatter_to_use.is_a?(Class)
+ if string_const?(formatter_to_use) && Object.const_defined?(formatter_to_use)
+ formatter_class = Object.const_get(formatter_to_use)
+ elsif formatter_to_use.is_a?(Class)
formatter_class = formatter_to_use
else
formatter_class = case formatter_to_use.to_s
@@ -188,7 +190,11 @@ def formatter=(formatter_to_use)
end
self.formatter_class = formatter_class
end
-
+
+ def string_const?(str)
+ str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str
+ end
+
def formatter
@formatter ||= formatter_class.new(output)
end
@@ -13,17 +13,16 @@ class ConfigurationOptions
def initialize(args)
@args = args
end
-
+
def configure(config)
- sorted_keys.each do |key|
+ keys = options.keys
+ keys.unshift(:requires) if keys.delete(:requires)
+ keys.unshift(:libs) if keys.delete(:libs)
+ keys.each do |key|
config.send("#{key}=", options[key])
end
end
-
- def sorted_keys
- options.keys.sort{|a,b| a.to_s <=> b.to_s}
- end
-
+
def drb_argv
argv = []
argv << "--color" if options[:color_enabled]
@@ -22,6 +22,14 @@ def options_from_args(*args)
config.should_receive(:requires=).ordered
opts.configure(config)
end
+
+ it "sends requires before formatter" do
+ opts = config_options_object(*%w[--require a/path -f a/formatter])
+ config = double("config").as_null_object
+ config.should_receive(:requires=).ordered
+ config.should_receive(:formatter=).ordered
+ opts.configure(config)
+ end
end
describe 'color_enabled' do
@@ -246,6 +246,12 @@ def that_thing
config.formatter.should be_an_instance_of(formatter_class)
end
+ it "sets a formatter based on its class name" do
+ Object.const_set("CustomFormatter",Class.new(Formatters::BaseFormatter))
+ config.formatter = "CustomFormatter"
+ config.formatter.should be_an_instance_of(CustomFormatter)
+ end
+
it "raises ArgumentError if formatter is unknown" do
lambda { config.formatter = :progresss }.should raise_error(ArgumentError)
end

0 comments on commit 7910d7f

Please sign in to comment.