Permalink
Browse files

Flesh out the tests for the options class to ensure future refactorings

  • Loading branch information...
1 parent b948f96 commit 3e6d9333c095b514350bbddbce041008982b1494 @jyurek jyurek committed Sep 30, 2011
Showing with 71 additions and 3 deletions.
  1. +3 −3 lib/paperclip/options.rb
  2. +68 −0 test/options_test.rb
View
@@ -62,17 +62,17 @@ def method_missing(method, *args, &blk)
end
def processors
- @processors.respond_to?(:call) ? @processors.call(instance) : @processors
+ @processors.respond_to?(:call) ? @processors.call(@attachment.instance) : @processors
end
def styles
if @styles.respond_to?(:call) || !@normalized_styles
- normalized_styles = ActiveSupport::OrderedHash.new
+ @normalized_styles = ActiveSupport::OrderedHash.new
(@styles.respond_to?(:call) ? @styles.call(@attachment) : @styles).each do |name, args|
normalized_styles[name] = Paperclip::Style.new(name, args.dup, @attachment)
end
end
- normalized_styles
+ @normalized_styles
end
end
end
View
@@ -0,0 +1,68 @@
+# encoding: utf-8
+require './test/helper'
+
+class MockAttachment < Struct.new(:one, :two)
+ def instance
+ self
+ end
+end
+
+class OptionsTest < Test::Unit::TestCase
+ context "#styles with a plain hash" do
+ setup do
+ @attachment = MockAttachment.new(nil, nil)
+ @options = Paperclip::Options.new(@attachment,
+ :styles => {
+ :something => ["400x400", :png]
+ })
+ end
+
+ should "return the right data for the style's geometry" do
+ assert_equal "400x400", @options.styles[:something][:geometry]
+ end
+
+ should "return the right data for the style's format" do
+ assert_equal :png, @options.styles[:something][:format]
+ end
+ end
+
+ context "#styles is a proc" do
+ setup do
+ @attachment = MockAttachment.new("123x456", :doc)
+ @options = Paperclip::Options.new(@attachment,
+ :styles => lambda {|att|
+ {:something => {:geometry => att.one, :format => att.two}}
+ })
+ end
+
+ should "return the right data for the style's geometry" do
+ assert_equal "123x456", @options.styles[:something][:geometry]
+ end
+
+ should "return the right data for the style's format" do
+ assert_equal :doc, @options.styles[:something][:format]
+ end
+
+ should "run the proc each time, giving dynamic results" do
+ assert_equal :doc, @options.styles[:something][:format]
+ @attachment.two = :pdf
+ assert_equal :pdf, @options.styles[:something][:format]
+ end
+ end
+
+ context "#processors" do
+ setup do
+ @attachment = MockAttachment.new(nil, nil)
+ end
+ should "return processors if not a proc" do
+ @options = Paperclip::Options.new(@attachment, :processors => [:one])
+ assert_equal [:one], @options.processors
+ end
+ should "return processors if it is a proc" do
+ @options = Paperclip::Options.new(@attachment, :processors => lambda{|att| [att.one]})
+ assert_equal [nil], @options.processors
+ @attachment.one = :other
+ assert_equal [:other], @options.processors
+ end
+ end
+end

0 comments on commit 3e6d933

Please sign in to comment.