Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added validate_exact_length_matcher_spec however specs aren't working…
… together
- Loading branch information
Joseph HALTER
committed
Aug 12, 2009
1 parent
6635566
commit b918db6
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module RspecSequel | ||
module Matchers | ||
|
||
class ValidateExactLengthMatcher < RspecSequel::Validation | ||
def description | ||
desc = "validate exact length of #{@attribute.inspect} to #{@additionnal.inspect}" | ||
desc << " with #{hash_to_nice_string @options}" unless @options.empty? | ||
desc | ||
end | ||
|
||
def additionnal_param_type | ||
Fixnum | ||
end | ||
|
||
def validation_type | ||
:validates_exact_length | ||
end | ||
end | ||
|
||
def validate_exact_length(*args) | ||
ValidateExactLengthMatcher.new(*args) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
|
||
class Item < Sequel::Model | ||
def validate | ||
validates_exact_length 4, :name, :allow_nil => true | ||
end | ||
end | ||
|
||
describe "validate_exact_length_matcher" do | ||
|
||
subject{ Item } | ||
|
||
describe "arguments" do | ||
it "should require attribute" do | ||
lambda{ | ||
@matcher = validate_exact_length | ||
}.should raise_error(ArgumentError) | ||
end | ||
it "should require additionnal parameters" do | ||
lambda{ | ||
@matcher = validate_exact_length :name | ||
}.should raise_error(ArgumentError) | ||
end | ||
it "should refuse invalid additionnal parameters" do | ||
lambda{ | ||
@matcher = validate_exact_length :id, :name | ||
}.should raise_error(ArgumentError) | ||
end | ||
it "should accept valid additionnal parameters" do | ||
lambda{ | ||
@matcher = validate_exact_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_exact_length 4, :name | ||
@matcher.description.should == "validate exact length of :name to 4" | ||
end | ||
it "should set failure messages" do | ||
@matcher = validate_exact_length 4, :name | ||
@matcher.matches? subject | ||
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4" | ||
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate") | ||
end | ||
end | ||
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 :allow_nil => true" | ||
end | ||
it "should set failure messages" do | ||
@matcher = validate_exact_length 4, :price, :allow_nil => true | ||
@matcher.matches? subject | ||
@matcher.failure_message.should == "expected Item to validate exact length of :price to 4 with :allow_nil => true" | ||
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate") | ||
end | ||
it "should explicit used options if different than expected" do | ||
@matcher = validate_exact_length 4, :name, :allow_blank => true | ||
@matcher.matches? subject | ||
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4 with :allow_blank => true but called with option(s) :allow_nil => true instead" | ||
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate") | ||
end | ||
it "should warn if invalid options are used" do | ||
@matcher = validate_exact_length 4, :name, :allow_anything => true | ||
@matcher.matches? subject | ||
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4 with :allow_anything => true but option :allow_anything is not valid" | ||
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate") | ||
end | ||
end | ||
end | ||
|
||
describe "matchers" do | ||
it{ should validate_exact_length(4, :name) } | ||
it{ should validate_exact_length(4, :name, :allow_nil => true) } | ||
it{ should_not validate_exact_length(4, :price) } | ||
it{ should_not validate_exact_length(3, :name) } | ||
it{ should_not validate_exact_length(4, :name, :allow_blank => true) } | ||
end | ||
|
||
end |