Commit 280a85a6 by Joe Ferris

Changed rspec matchers to accept an instance or a class

parent ae97ec66
...@@ -12,6 +12,7 @@ module Paperclip ...@@ -12,6 +12,7 @@ module Paperclip
def matches? subject def matches? subject
@subject = subject @subject = subject
@subject = @subject.class unless Class === @subject
responds? && has_column? && included? responds? && has_column? && included?
end end
......
...@@ -22,6 +22,7 @@ module Paperclip ...@@ -22,6 +22,7 @@ module Paperclip
def matches? subject def matches? subject
@subject = subject @subject = subject
@subject = @subject.class unless Class === @subject
@allowed_types && @rejected_types && @allowed_types && @rejected_types &&
allowed_types_allowed? && rejected_types_rejected? allowed_types_allowed? && rejected_types_rejected?
end end
......
...@@ -12,6 +12,7 @@ module Paperclip ...@@ -12,6 +12,7 @@ module Paperclip
def matches? subject def matches? subject
@subject = subject @subject = subject
@subject = @subject.class unless Class === @subject
error_when_not_valid? && no_error_when_valid? error_when_not_valid? && no_error_when_valid?
end end
......
...@@ -28,6 +28,7 @@ module Paperclip ...@@ -28,6 +28,7 @@ module Paperclip
def matches? subject def matches? subject
@subject = subject @subject = subject
@subject = @subject.class unless Class === @subject
lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high? lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
end end
......
...@@ -106,3 +106,23 @@ def silence_warnings ...@@ -106,3 +106,23 @@ def silence_warnings
ensure ensure
$VERBOSE = old_verbose $VERBOSE = old_verbose
end end
def should_accept_dummy_class
should "accept the class" do
assert_accepts @matcher, @dummy_class
end
should "accept an instance of that class" do
assert_accepts @matcher, @dummy_class.new
end
end
def should_reject_dummy_class
should "reject the class" do
assert_rejects @matcher, @dummy_class
end
should "reject an instance of that class" do
assert_rejects @matcher, @dummy_class.new
end
end
...@@ -8,14 +8,17 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase ...@@ -8,14 +8,17 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase
@matcher = self.class.have_attached_file(:avatar) @matcher = self.class.have_attached_file(:avatar)
end end
should "reject a class with no attachment" do context "given a class with no attachment" do
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "accept a class with an attachment" do context "given a class with an attachment" do
modify_table("dummies"){|d| d.string :avatar_file_name } setup do
@dummy_class.has_attached_file :avatar modify_table("dummies"){|d| d.string :avatar_file_name }
assert_accepts @matcher, @dummy_class @dummy_class.has_attached_file :avatar
end
should_accept_dummy_class
end end
end end
end end
...@@ -14,18 +14,24 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase ...@@ -14,18 +14,24 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
rejecting(%w(audio/mp3 application/octet-stream)) rejecting(%w(audio/mp3 application/octet-stream))
end end
should "reject a class with no validation" do context "given a class with no validation" do
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "reject a class with a validation that doesn't match" do context "given a class with a validation that doesn't match" do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*} setup do
assert_rejects @matcher, @dummy_class @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
end
should_reject_dummy_class
end end
should "accept a class with a validation" do context "given a class with a matching validation" do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*} setup do
assert_accepts @matcher, @dummy_class @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
end
should_accept_dummy_class
end end
end end
end end
...@@ -11,13 +11,16 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase ...@@ -11,13 +11,16 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
@matcher = self.class.validate_attachment_presence(:avatar) @matcher = self.class.validate_attachment_presence(:avatar)
end end
should "reject a class with no validation" do context "given a class with no validation" do
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "accept a class with a validation" do context "given a class with a matching validation" do
@dummy_class.validates_attachment_presence :avatar setup do
assert_accepts @matcher, @dummy_class @dummy_class.validates_attachment_presence :avatar
end
should_accept_dummy_class
end end
end end
end end
...@@ -14,37 +14,37 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase ...@@ -14,37 +14,37 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
context "of limited size" do context "of limited size" do
setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) } setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) }
should "reject a class with no validation" do context "given a class with no validation" do
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "reject a class with a validation that's too high" do context "given a class with a validation that's too high" do
@dummy_class.validates_attachment_size :avatar, :in => 256..2048 setup { @dummy_class.validates_attachment_size :avatar, :in => 256..2048 }
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "reject a class with a validation that's too low" do context "given a class with a validation that's too low" do
@dummy_class.validates_attachment_size :avatar, :in => 0..1024 setup { @dummy_class.validates_attachment_size :avatar, :in => 0..1024 }
assert_rejects @matcher, @dummy_class should_reject_dummy_class
end end
should "accept a class with a validation that matches" do context "given a class with a validation that matches" do
@dummy_class.validates_attachment_size :avatar, :in => 256..1024 setup { @dummy_class.validates_attachment_size :avatar, :in => 256..1024 }
assert_accepts @matcher, @dummy_class should_accept_dummy_class
end end
end end
context "validates_attachment_size with infinite range" do context "validates_attachment_size with infinite range" do
setup{ @matcher = self.class.validate_attachment_size(:avatar) } setup{ @matcher = self.class.validate_attachment_size(:avatar) }
should "accept a class with an upper limit" do context "given a class with an upper limit" do
@dummy_class.validates_attachment_size :avatar, :less_than => 1 setup { @dummy_class.validates_attachment_size :avatar, :less_than => 1 }
assert_accepts @matcher, @dummy_class should_accept_dummy_class
end end
should "accept a class with no upper limit" do context "given a class with no upper limit" do
@dummy_class.validates_attachment_size :avatar, :greater_than => 1 setup { @dummy_class.validates_attachment_size :avatar, :greater_than => 1 }
assert_accepts @matcher, @dummy_class should_accept_dummy_class
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment