Skip to content

Commit

Permalink
Added validate_exact_length_matcher_spec however specs aren't working…
Browse files Browse the repository at this point in the history
… together
  • Loading branch information
Joseph HALTER committed Aug 12, 2009
1 parent 6635566 commit b918db6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/rspec_sequel/matchers/validate_exact_length.rb
@@ -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
1 change: 1 addition & 0 deletions lib/rspec_sequel/validation.rb
Expand Up @@ -43,6 +43,7 @@ def valid?(db, i, c, attribute, options)
# check validation itself
called_count = 0
i = i.dup
i.class.columns # ensure colums are read again after .dup
i.stub!(validation_type).and_return{|*args|
called_options = args.last.is_a?(Hash) ? args.pop : {}
called_additionnal = args.shift if additionnal_param_required?
Expand Down
83 changes: 83 additions & 0 deletions spec/validate_exact_length_matcher_spec.rb
@@ -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

0 comments on commit b918db6

Please sign in to comment.