Permalink
Browse files

Add validate_includes_matcher with specs and refactor specs again

  • Loading branch information...
1 parent e63c02d commit ab7005794f8712cb30e3e1880e24309fb229fdb9 Joseph HALTER committed Aug 13, 2009
@@ -4,7 +4,7 @@ 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 << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
desc
end
@@ -3,8 +3,8 @@ module Matchers
class ValidateFormatMatcher < RspecSequel::Validation
def description
- desc = "validate format of #{@attribute.inspect} with #{@additionnal.inspect}"
- desc << " and option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc = "validate format of #{@attribute.inspect} against #{@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 ValidateIncludesMatcher < RspecSequel::Validation
+ def description
+ desc = "validate that #{@attribute.inspect} is included in #{@additionnal.inspect}"
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc
+ end
+
+ def additionnal_param_type
+ Enumerable
+ end
+
+ def validation_type
+ :validates_includes
+ end
+ end
+
+ def validate_includes(*args)
+ ValidateIncludesMatcher.new(*args)
+ end
+
+ end
+end
@@ -4,7 +4,7 @@ module Matchers
class ValidatePresenceMatcher < RspecSequel::Validation
def description
desc = "validate presence of #{@attribute.inspect}"
- desc << " with #{hash_to_nice_string @options}" unless @options.empty?
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
desc
end
@@ -17,8 +17,8 @@
it "should set failure messages" do
@matcher = have_column :name
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to have a column :name"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
end
end
describe "with type as symbol" do
@@ -29,8 +29,8 @@
it "should set failure messages" do
@matcher = have_column :password, :type => :string
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to have a column :password with type string"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
end
end
describe "with type as object" do
@@ -41,38 +41,39 @@
it "should set failure messages" do
@matcher = have_column :password, :type => String
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to have a column :password with type String"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
end
it "should explicit found type if different than expected" do
@matcher = have_column :name, :type => Integer
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to have a column :name with type Integer (type found: string, varchar(255))"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ explanation = " (type found: string, varchar(255))"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
end
end
describe "on anonymous Sequel::Model class" do
it "should set failure messages" do
@matcher = have_column :password
@matcher.matches? Sequel::Model(:comments)
- @matcher.failure_message.should == "expected comments to have a column :password"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected comments to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected comments to not " + @matcher.description
end
end
describe "on Sequel::Model class" do
it "should set failure messages" do
@matcher = have_column :password
@matcher.matches? Item
- @matcher.failure_message.should == "expected Item to have a column :password"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
end
end
describe "on Sequel::Model instance" do
it "should set failure messages" do
@matcher = have_column :password
@matcher.matches? Item.new
- @matcher.failure_message.should == "expected #<Item @values={}> to have a column :password"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected #<Item @values={}> to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected #<Item @values={}> to not " + @matcher.description
end
end
end
@@ -20,8 +20,8 @@
it "should set failure messages" do
@matcher = have_many_to_many :items
@matcher.matches? subject
- @matcher.failure_message.should == "expected Comment to have a many_to_many association :items"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
end
end
describe "with options" do
@@ -32,14 +32,15 @@
it "should set failure messages" do
@matcher = have_many_to_many :items, :class_name => "Item"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Comment to have a many_to_many association :items with :class_name => "Item"'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
end
it "should explicit used options if different than expected" do
@matcher = have_many_to_many :items, :class_name => "Price"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Comment to have a many_to_many association :items with :class_name => "Price" expected :class_name == "Price" but found "Item" instead'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ explanation = ' expected :class_name == "Price" but found "Item" instead'
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description + explanation
end
end
end
@@ -20,8 +20,8 @@
it "should set failure messages" do
@matcher = have_many_to_one :item
@matcher.matches? subject
- @matcher.failure_message.should == "expected Comment to have a many_to_one association :item"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
end
end
describe "with options" do
@@ -32,14 +32,15 @@
it "should set failure messages" do
@matcher = have_many_to_one :item, :class_name => "Item"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Comment to have a many_to_one association :item with :class_name => "Item"'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
end
it "should explicit used options if different than expected" do
@matcher = have_many_to_one :item, :class_name => "Price"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Comment to have a many_to_one association :item with :class_name => "Price" expected :class_name == "Price" but found "Item" instead'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ explanation = ' expected :class_name == "Price" but found "Item" instead'
+ @matcher.failure_message.should == "expected Comment to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description + explanation
end
end
end
@@ -20,8 +20,8 @@
it "should set failure messages" do
@matcher = have_one_to_many :comments
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to have a one_to_many association :comments"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @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
@@ -32,14 +32,15 @@
it "should set failure messages" do
@matcher = have_one_to_many :comments, :class_name => "Comment"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Item to have a one_to_many association :comments with :class_name => "Comment"'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ @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 = have_one_to_many :comments, :class_name => "Price"
@matcher.matches? subject
- @matcher.failure_message.should == 'expected Item to have a one_to_many association :comments with :class_name => "Price" expected :class_name == "Price" but found "Comment" instead'
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
+ explanation = ' expected :class_name == "Price" but found "Comment" instead'
+ @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
@@ -45,32 +45,34 @@ def validate
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")
+ @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_exact_length 4, :name, :allow_nil => true
- @matcher.description.should == "validate exact length of :name to 4 with :allow_nil => true"
+ @matcher.description.should == "validate exact length of :name to 4 with option(s) :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")
+ @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_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")
+ 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_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")
+ 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
@@ -40,37 +40,39 @@ def validate
describe "without option" do
it "should contain a description" do
@matcher = validate_format /[abc]+/, :name
- @matcher.description.should == "validate format of :name with /[abc]+/"
+ @matcher.description.should == "validate format of :name against /[abc]+/"
end
it "should set failure messages" do
@matcher = validate_format /[abc]+/, :name
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to validate format of :name with /[abc]+/"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
+ @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_format /[abc]+/, :name, :allow_nil => true
- @matcher.description.should == "validate format of :name with /[abc]+/ and option(s) :allow_nil => true"
+ @matcher.description.should == "validate format of :name against /[abc]+/ with option(s) :allow_nil => true"
end
it "should set failure messages" do
@matcher = validate_format /[abc]+/, :price, :allow_nil => true
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to validate format of :price with /[abc]+/ and option(s) :allow_nil => true"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
+ @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_format /[abc]+/, :name, :allow_blank => true
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to validate format of :name with /[abc]+/ and option(s) :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")
+ 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_format /[abc]+/, :name, :allow_anything => true
@matcher.matches? subject
- @matcher.failure_message.should == "expected Item to validate format of :name with /[abc]+/ and option(s) :allow_anything => true but option :allow_anything is not valid"
- @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
+ 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
Oops, something went wrong.

0 comments on commit ab70057

Please sign in to comment.