Permalink
Browse files

Add both validate_max_length_matcher and validate_min_length_matcher …

…with specs
  • Loading branch information...
1 parent 36bf221 commit f85bb45c5ddb000ac29df684501a453b5ec39322 Joseph HALTER committed Aug 13, 2009
@@ -3,7 +3,7 @@ module Matchers
class ValidateExactLengthMatcher < RspecSequel::Validation
def description
- desc = "validate exact length of #{@attribute.inspect} to #{@additionnal.inspect}"
+ desc = "validate length of #{@attribute.inspect} is exactly #{@additionnal.inspect}"
desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
desc
end
@@ -3,7 +3,7 @@ module Matchers
class ValidateLengthRangeMatcher < RspecSequel::Validation
def description
- desc = "validate that #{@attribute.inspect} length is included in #{@additionnal.inspect}"
+ desc = "validate length of #{@attribute.inspect} is included in #{@additionnal.inspect}"
desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
desc
end
@@ -0,0 +1,25 @@
+module RspecSequel
+ module Matchers
+
+ class ValidateMaxLengthMatcher < RspecSequel::Validation
+ def description
+ desc = "validate length of #{@attribute.inspect} is less than or equal to #{@additionnal.inspect}"
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc
+ end
+
+ def additionnal_param_type
+ Fixnum
+ end
+
+ def validation_type
+ :validates_max_length
+ end
+ end
+
+ def validate_max_length(*args)
+ ValidateMaxLengthMatcher.new(*args)
+ end
+
+ end
+end
@@ -0,0 +1,25 @@
+module RspecSequel
+ module Matchers
+
+ class ValidateMinLengthMatcher < RspecSequel::Validation
+ def description
+ desc = "validate length of #{@attribute.inspect} is greater than or equal to #{@additionnal.inspect}"
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc
+ end
+
+ def additionnal_param_type
+ Fixnum
+ end
+
+ def validation_type
+ :validates_min_length
+ end
+ end
+
+ def validate_min_length(*args)
+ ValidateMinLengthMatcher.new(*args)
+ end
+
+ end
+end
@@ -40,7 +40,7 @@ def validate
describe "without option" do
it "should contain a description" do
@matcher = validate_exact_length 4, :name
- @matcher.description.should == "validate exact length of :name to 4"
+ @matcher.description.should == "validate length of :name is exactly 4"
end
it "should set failure messages" do
@matcher = validate_exact_length 4, :name
@@ -52,7 +52,7 @@ def validate
describe "with options" do
it "should contain a description" do
@matcher = validate_exact_length 4, :name, :allow_nil => true
- @matcher.description.should == "validate exact length of :name to 4 with option(s) :allow_nil => true"
+ @matcher.description.should == "validate length of :name is exactly 4 with option(s) :allow_nil => true"
end
it "should set failure messages" do
@matcher = validate_exact_length 4, :price, :allow_nil => true
@@ -40,7 +40,7 @@ def validate
describe "without option" do
it "should contain a description" do
@matcher = validate_length_range 1..10, :name
- @matcher.description.should == "validate that :name length is included in 1..10"
+ @matcher.description.should == "validate length of :name is included in 1..10"
end
it "should set failure messages" do
@matcher = validate_length_range 1..10, :name
@@ -52,7 +52,7 @@ def validate
describe "with options" do
it "should contain a description" do
@matcher = validate_length_range 1..10, :name, :allow_nil => true
- @matcher.description.should == "validate that :name length is included in 1..10 with option(s) :allow_nil => true"
+ @matcher.description.should == "validate length of :name is included in 1..10 with option(s) :allow_nil => true"
end
it "should set failure messages" do
@matcher = validate_length_range 1..10, :price, :allow_nil => true
@@ -0,0 +1,88 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "validate_max_length_matcher" do
+
+ before :all do
+ define_model :item do
+ plugin :validation_helpers
+ def validate
+ validates_max_length 4, :name, :allow_nil => true
+ end
+ end
+ end
+
+ subject{ Item }
+
+ describe "arguments" do
+ it "should require attribute" do
+ lambda{
+ @matcher = validate_max_length
+ }.should raise_error(ArgumentError)
+ end
+ it "should require additionnal parameters" do
+ lambda{
+ @matcher = validate_max_length :name
+ }.should raise_error(ArgumentError)
+ end
+ it "should refuse invalid additionnal parameters" do
+ lambda{
+ @matcher = validate_max_length :id, :name
+ }.should raise_error(ArgumentError)
+ end
+ it "should accept valid additionnal parameters" do
+ lambda{
+ @matcher = validate_max_length 4, :name
+ }.should_not raise_error(ArgumentError)
+ end
+ end
+
+ describe "messages" do
+ describe "without option" do
+ it "should contain a description" do
+ @matcher = validate_max_length 4, :name
+ @matcher.description.should == "validate length of :name is less than or equal to 4"
+ end
+ it "should set failure messages" do
+ @matcher = validate_max_length 4, :name
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ end
+ describe "with options" do
+ it "should contain a description" do
+ @matcher = validate_max_length 4, :name, :allow_nil => true
+ @matcher.description.should == "validate length of :name is less than or equal to 4 with option(s) :allow_nil => true"
+ end
+ it "should set failure messages" do
+ @matcher = validate_max_length 4, :price, :allow_nil => true
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ it "should explicit used options if different than expected" do
+ @matcher = validate_max_length 4, :name, :allow_blank => true
+ @matcher.matches? subject
+ explanation = " but called with option(s) :allow_nil => true instead"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ it "should warn if invalid options are used" do
+ @matcher = validate_max_length 4, :name, :allow_anything => true
+ @matcher.matches? subject
+ explanation = " but option :allow_anything is not valid"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ end
+ end
+
+ describe "matchers" do
+ it{ should validate_max_length(4, :name) }
+ it{ should validate_max_length(4, :name, :allow_nil => true) }
+ it{ should_not validate_max_length(4, :price) }
+ it{ should_not validate_max_length(3, :name) }
+ it{ should_not validate_max_length(4, :name, :allow_blank => true) }
+ end
+
+end
@@ -0,0 +1,88 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "validate_min_length_matcher" do
+
+ before :all do
+ define_model :item do
+ plugin :validation_helpers
+ def validate
+ validates_min_length 4, :name, :allow_nil => true
+ end
+ end
+ end
+
+ subject{ Item }
+
+ describe "arguments" do
+ it "should require attribute" do
+ lambda{
+ @matcher = validate_min_length
+ }.should raise_error(ArgumentError)
+ end
+ it "should require additionnal parameters" do
+ lambda{
+ @matcher = validate_min_length :name
+ }.should raise_error(ArgumentError)
+ end
+ it "should refuse invalid additionnal parameters" do
+ lambda{
+ @matcher = validate_min_length :id, :name
+ }.should raise_error(ArgumentError)
+ end
+ it "should accept valid additionnal parameters" do
+ lambda{
+ @matcher = validate_min_length 4, :name
+ }.should_not raise_error(ArgumentError)
+ end
+ end
+
+ describe "messages" do
+ describe "without option" do
+ it "should contain a description" do
+ @matcher = validate_min_length 4, :name
+ @matcher.description.should == "validate length of :name is greater than or equal to 4"
+ end
+ it "should set failure messages" do
+ @matcher = validate_min_length 4, :name
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ end
+ describe "with options" do
+ it "should contain a description" do
+ @matcher = validate_min_length 4, :name, :allow_nil => true
+ @matcher.description.should == "validate length of :name is greater than or equal to 4 with option(s) :allow_nil => true"
+ end
+ it "should set failure messages" do
+ @matcher = validate_min_length 4, :price, :allow_nil => true
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ it "should explicit used options if different than expected" do
+ @matcher = validate_min_length 4, :name, :allow_blank => true
+ @matcher.matches? subject
+ explanation = " but called with option(s) :allow_nil => true instead"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ it "should warn if invalid options are used" do
+ @matcher = validate_min_length 4, :name, :allow_anything => true
+ @matcher.matches? subject
+ explanation = " but option :allow_anything is not valid"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ end
+ end
+
+ describe "matchers" do
+ it{ should validate_min_length(4, :name) }
+ it{ should validate_min_length(4, :name, :allow_nil => true) }
+ it{ should_not validate_min_length(4, :price) }
+ it{ should_not validate_min_length(3, :name) }
+ it{ should_not validate_min_length(4, :name, :allow_blank => true) }
+ end
+
+end

0 comments on commit f85bb45

Please sign in to comment.