Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using mongomapper with activemodel, so removed the old validation mat…

…chers in order to use the new remarkable/active_model matchers
  • Loading branch information...
commit 4e614cccb2b1e6f64cf8c888d0268d6774398151 1 parent cbd5dba
kaid authored
2  lib/remarkable/mongo_mapper/base.rb
View
@@ -164,7 +164,7 @@ def assert_bad_value(model, attribute, value, error_message_to_expect=:invalid)
# Using the underlying mechanism inside ActiveRecord makes us free from
# all thos errors.
#
- # We replace {{count}} interpolation for 12345 which later is replaced
+ # We replace %{count} interpolation for 12345 which later is replaced
# by a regexp which contains \d+.
#
def error_message_from_model(model, attribute, message) #:nodoc:
8 lib/remarkable/mongo_mapper/describe.rb
View
@@ -33,7 +33,7 @@ def self.after_include(target) #:nodoc:
# remarkable:
# mongo_mapper:
# describe:
- # each: "{{key}} is {{value}}"
+ # each: "%{key} is %{value}"
# prepend: "when "
# connector: " and "
#
@@ -98,7 +98,7 @@ module ClassMethods
# remarkable:
# mongo_mapper:
# describe:
- # each: "{{key}} is {{value}}"
+ # each: "%{key} is %{value}"
# prepend: "when "
# connector: " and "
#
@@ -126,7 +126,7 @@ def describe(*args, &block)
end
pieces << Remarkable.t("remarkable.mongo_mapper.describe.each",
- :default => "{{key}} is {{value}}",
+ :default => "%{key} is %{value}",
:key => translated_key.downcase, :value => value.inspect)
end
@@ -196,4 +196,4 @@ def subject_attributes
end
end
-end
+end
86 lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb
View
@@ -1,86 +0,0 @@
-module Remarkable
- module MongoMapper
- module Matchers
- class AllowValuesForMatcher < Remarkable::MongoMapper::Base #:nodoc:
- include Remarkable::Negative
- arguments :collection => :attributes, :as => :attribute
-
- optional :message
- optional :in, :splat => true
- optional :allow_nil, :allow_blank, :default => true
-
- collection_assertions :is_valid?, :is_invalid?, :allow_nil?, :allow_blank?
-
- default_options :message => "is invalid"
-
- before_assert do
- first_value = @options[:in].is_a?(Array) ? @options[:in].first : @options[:in]
- @in_range = first_value.is_a?(Range)
-
- @options[:in] = if @in_range
- first_value.to_a[0,2] + first_value.to_a[-2,2]
- else
- [*@options[:in]].compact
- end
-
- @options[:in].uniq!
- end
-
- protected
-
- def is_valid?
- assert_collection :value, valid_values do |value|
- good?(value)
- end
- end
-
- def is_invalid?
- assert_collection :value, invalid_values do |value|
- bad?(value)
- end
- end
-
- def valid_values
- @options[:in]
- end
-
- def invalid_values
- []
- end
-
- def interpolation_options
- options = if @in_range
- { :in => (@options[:in].first..@options[:in].last).inspect }
- elsif @options[:in].is_a?(Array)
- { :in => array_to_sentence(@options[:in], true, '[]') }
- else
- { :in => @options[:in].inspect }
- end
-
- options.merge!(:behavior => @behavior.to_s)
- end
-
- end
-
- # Ensures that the attribute can be set to the given values.
- #
- # == Options
- #
- # * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
- # * <tt>:allow_blank</tt> - when supplied, validates if it allows blank or not.
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
- #
- # == Examples
- #
- # should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
- # it { should allow_values_for(:isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0") }
- #
- def allow_values_for(attribute, *args, &block)
- options = args.extract_options!
- AllowValuesForMatcher.new(attribute, options.merge!(:in => args), &block).spec(self)
- end
-
- end
- end
-end
44 lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb
View
@@ -1,44 +0,0 @@
-module Remarkable
- module MongoMapper
- module Matchers
- class ValidateConfirmationOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
- arguments :collection => :attributes, :as => :attribute
-
- optional :message
- collection_assertions :responds_to_confirmation?, :confirms?
-
- default_options :message => "doesn't match confirmation"
-
- protected
-
- def responds_to_confirmation?
- @subject.respond_to?(:"#{@attribute}_confirmation=")
- end
-
- def confirms?
- @subject.send(:"#{@attribute}_confirmation=", 'something')
- bad?('different')
- end
-
- end
-
- # Ensures that the model cannot be saved if one of the attributes is not confirmed.
- #
- # == Options
- #
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp, string or symbol. Default = "doesn't match confirmation"
- #
- # == Examples
- #
- # should_validate_confirmation_of :email, :password
- #
- # it { should validate_confirmation_of(:email, :password) }
- #
- def validate_confirmation_of(*attributes, &block)
- ValidateConfirmationOfMatcher.new(*attributes, &block).spec(self)
- end
-
- end
- end
-end
105 lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb
View
@@ -1,105 +0,0 @@
-module Remarkable
- module MongoMapper
- module Matchers
- class ValidateLengthOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
- arguments :collection => :attributes, :as => :attribute
-
- optional :within, :minimum, :maximum, :is
- optional :allow_nil, :allow_blank, :default => true
- optional :message
-
- default_options :message => "is invalid"
-
- collection_assertions :allow_nil?, :allow_blank?, :less_than_min_length?, :exactly_min_length?,
- :more_than_max_length?, :exactly_max_length?
-
- before_assert do
- if @options[:is]
- @min_value, @max_value = @options[:is], @options[:is]
- elsif @options[:within]
- @min_value, @max_value = @options[:within].first, @options[:within].last
- elsif @options[:maximum]
- @min_value, @max_value = nil, @options[:maximum]
- elsif @options[:minimum]
- @min_value, @max_value = @options[:minimum], nil
- end
- end
-
- protected
- def allow_nil?
- super(default_message_for(:too_short))
- end
-
- def allow_blank?
- super(default_message_for(:too_short))
- end
-
- def less_than_min_length?
- @min_value.nil? || @min_value <= 1 || bad?(@min_value - 1, default_message_for(:too_short))
- end
-
- def exactly_min_length?
- @min_value.nil? || @min_value <= 0 || good?(@min_value, default_message_for(:too_short))
- end
-
- def more_than_max_length?
- @max_value.nil? || bad?(@max_value + 1, default_message_for(:too_long))
- end
-
- def exactly_max_length?
- @max_value.nil? || @min_value == @max_value || good?(@max_value, default_message_for(:too_long))
- end
-
- def interpolation_options
- { :minimum => @min_value, :maximum => @max_value }
- end
-
- # Returns the default message for the validation type.
- # If user supplied :message, it will return it. Otherwise it will return
- # wrong_length on :is validation and :too_short or :too_long in the other
- # types.
- #
- def default_message_for(validation_type)
- return :message if @options[:message]
- end
- end
-
- # Validates the length of the given attributes. You have also to supply
- # one of the following options: minimum, maximum, is or within.
- #
- # Note: this method is also aliased as <tt>validate_size_of</tt>.
- #
- # == Options
- #
- # * <tt>:minimum</tt> - The minimum size of the attribute.
- # * <tt>:maximum</tt> - The maximum size of the attribute.
- # * <tt>:is</tt> - The exact size of the attribute.
- # * <tt>:within</tt> - A range specifying the minimum and maximum size of the attribute.
- # * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
- # * <tt>:allow_blank</tt> - when supplied, validates if it allows blank or not.
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp, string or symbol. Default = "is invalid"</tt>
- #
- # == Examples
- #
- # it { should validate_length_of(:password).within(6..20) }
- # it { should validate_length_of(:password).maximum(20) }
- # it { should validate_length_of(:password).minimum(6) }
- # it { should validate_length_of(:age).is(18) }
- #
- # should_validate_length_of :password, :within => 6..20
- # should_validate_length_of :password, :maximum => 20
- # should_validate_length_of :password, :minimum => 6
- # should_validate_length_of :age, :is => 18
- #
- # should_validate_length_of :password do |m|
- # m.minimum 6
- # m.maximum 20
- # end
- #
- def validate_length_of(*attributes, &block)
- ValidateLengthOfMatcher.new(*attributes, &block).spec(self)
- end
- end
- end
-end
37 lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb
View
@@ -1,37 +0,0 @@
-module Remarkable
- module MongoMapper
- module Matchers
- class ValidatePresenceOfMatcher < Remarkable::MongoMapper::Base
- arguments :collection => :attributes, :as => :attribute
- optional :message
-
- collection_assertions :allow_nil?
- default_options :message => "can't be empty"
-
- protected
-
- def allow_nil?
- bad?(nil, :message)
- end
-
- end
-
- # Ensures that the model cannot be saved if one of the attributes listed is not present.
- #
- # == Options
- #
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp, string or symbol. Default = "can't be empty"
- #
- # == Examples
- #
- # should_validate_presence_of :name, :phone_number
- # it { should validate_presence_of(:name, :phone_number) }
- #
- def validate_presence_of(*args, &block)
- ValidatePresenceOfMatcher.new(*args, &block).spec(self)
- end
-
- end
- end
-end
116 locales/en.yml
View
@@ -2,12 +2,12 @@ en:
remarkable:
mongo_mapper:
describe:
- each: "{{key}} is {{value}}"
+ each: "%{key} is %{value}"
prepend: "when "
connector: " and "
expectations:
- allow_nil: "{{subject_name}} to {{not}}allow nil values for {{attribute}}"
- allow_blank: "{{subject_name}} to {{not}}allow blank values for {{attribute}}"
+ allow_nil: "%{subject_name} to %{not}allow nil values for %{attribute}"
+ allow_blank: "%{subject_name} to %{not}allow blank values for %{attribute}"
optionals:
allow_nil:
positive: "allowing nil values"
@@ -17,86 +17,86 @@ en:
negative: "not allowing blank values"
allow_values_for:
- description: "allow {{in}} as values for {{attributes}}"
+ description: "allow %{in} as values for %{attributes}"
expectations:
- is_valid: "{{subject_name}} to be valid when {{attribute}} is set to {{value}}"
+ is_valid: "%{subject_name} to be valid when %{attribute} is set to %{value}"
association:
many: have many
belongs_to: belong to
- description: "{{type}} {{associations}}"
+ description: "%{type} %{associations}"
expectations:
- association_exists: "{{subject_name}} records {{type}} {{association}}, but the association does not exist"
- type_matches: "{{subject_name}} records {{type}} {{association}}, got {{subject_name}} records {{actual_type}} {{association}}"
- klass_exists: "{{subject_name}} records {{type}} {{association}}, but the association class does not exist"
- options_match: "{{subject_name}} records {{type}} {{association}} with options {{options}}, got {{actual}}"
+ association_exists: "%{subject_name} records %{type} %{association}, but the association does not exist"
+ type_matches: "%{subject_name} records %{type} %{association}, got %{subject_name} records %{actual_type} %{association}"
+ klass_exists: "%{subject_name} records %{type} %{association}, but the association class does not exist"
+ options_match: "%{subject_name} records %{type} %{association} with options %{options}, got %{actual}"
have_key:
- description: "have key(s) {{attributes}} with type {{type}}"
+ description: "have key(s) %{attributes} with type %{type}"
expectations:
- has_key: "{{subject_name}} to have key named {{attribute}} with type {{type}}"
+ has_key: "%{subject_name} to have key named %{attribute} with type %{type}"
validate_acceptance_of:
- description: "require {{attributes}} to be accepted"
+ description: "require %{attributes} to be accepted"
expectations:
- requires_acceptance: "{{subject_name}} to be invalid if {{attribute}} is not accepted"
- accept_is_valid: "{{subject_name}} to be valid when {{attribute}} is accepted with value {{accept}}"
+ requires_acceptance: "%{subject_name} to be invalid if %{attribute} is not accepted"
+ accept_is_valid: "%{subject_name} to be valid when %{attribute} is accepted with value %{accept}"
optionals:
accept:
- positive: "with value {{inspect}}"
+ positive: "with value %{inspect}"
validate_associated:
- description: "require associated {{associations}} to be valid"
+ description: "require associated %{associations} to be valid"
expectations:
- is_valid: "{{subject_name}} to be invalid when {{association}} is invalid"
+ is_valid: "%{subject_name} to be invalid when %{association} is invalid"
validate_confirmation_of:
- description: "require {{attributes}} to be confirmed"
+ description: "require %{attributes} to be confirmed"
expectations:
- responds_to_confirmation: "{{subject_name}} instance responds to {{attribute}}_confirmation"
- confirms: "{{subject_name}} to be valid only when {{attribute}} is confirmed"
+ responds_to_confirmation: "%{subject_name} instance responds to %{attribute}_confirmation"
+ confirms: "%{subject_name} to be valid only when %{attribute} is confirmed"
validate_exclusion_of:
- description: "ensure exclusion of {{attributes}} in {{in}}"
+ description: "ensure exclusion of %{attributes} in %{in}"
expectations:
- is_valid: "{{subject_name}} to be valid when {{attribute}} is set to {{value}}"
- is_invalid: "{{subject_name}} to be invalid when {{attribute}} is set to {{value}}"
+ is_valid: "%{subject_name} to be valid when %{attribute} is set to %{value}"
+ is_invalid: "%{subject_name} to be invalid when %{attribute} is set to %{value}"
validate_inclusion_of:
- description: "ensure inclusion of {{attributes}} in {{in}}"
+ description: "ensure inclusion of %{attributes} in %{in}"
expectations:
- is_valid: "{{subject_name}} to be valid when {{attribute}} is set to {{value}}"
- is_invalid: "{{subject_name}} to be invalid when {{attribute}} is set to {{value}}"
+ is_valid: "%{subject_name} to be valid when %{attribute} is set to %{value}"
+ is_invalid: "%{subject_name} to be invalid when %{attribute} is set to %{value}"
validate_length_of:
- description: "ensure length of {{attributes}}"
+ description: "ensure length of %{attributes}"
expectations:
- less_than_min_length: "{{subject_name}} to be invalid when {{attribute}} length is less than {{minimum}} characters"
- exactly_min_length: "{{subject_name}} to be valid when {{attribute}} length is {{minimum}} characters"
- more_than_max_length: "{{subject_name}} to be invalid when {{attribute}} length is more than {{maximum}} characters"
- exactly_max_length: "{{subject_name}} to be valid when {{attribute}} length is {{maximum}} characters"
+ less_than_min_length: "%{subject_name} to be invalid when %{attribute} length is less than %{minimum} characters"
+ exactly_min_length: "%{subject_name} to be valid when %{attribute} length is %{minimum} characters"
+ more_than_max_length: "%{subject_name} to be invalid when %{attribute} length is more than %{maximum} characters"
+ exactly_max_length: "%{subject_name} to be valid when %{attribute} length is %{maximum} characters"
optionals:
within:
- positive: "is within {{inspect}} characters"
+ positive: "is within %{inspect} characters"
maximum:
- positive: "is maximum {{inspect}} characters"
+ positive: "is maximum %{inspect} characters"
minimum:
- positive: "is minimum {{inspect}} characters"
+ positive: "is minimum %{inspect} characters"
is:
- positive: "is equal to {{inspect}} characters"
+ positive: "is equal to %{inspect} characters"
with_kind_of:
- positive: "with kind of {{value}}"
+ positive: "with kind of %{value}"
validate_numericality_of:
- description: "ensure numericality of {{attributes}}"
+ description: "ensure numericality of %{attributes}"
expectations:
- only_numeric_values: "{{subject_name}} to allow only numeric values for {{attribute}}"
- only_integer: "{{subject_name}} to {{not}}allow only integer values for {{attribute}}"
- only_even: "{{subject_name}} to allow only even values for {{attribute}}"
- only_odd: "{{subject_name}} to allow only odd values for {{attribute}}"
- equals_to: "{{subject_name}} to be valid only when {{attribute}} is equal to {{count}}"
- more_than_maximum: "{{subject_name}} to be invalid when {{attribute}} is greater than {{count}}"
- less_than_minimum: "{{subject_name}} to be invalid when {{attribute}} is less than {{count}}"
+ only_numeric_values: "%{subject_name} to allow only numeric values for %{attribute}"
+ only_integer: "%{subject_name} to %{not}allow only integer values for %{attribute}"
+ only_even: "%{subject_name} to allow only even values for %{attribute}"
+ only_odd: "%{subject_name} to allow only odd values for %{attribute}"
+ equals_to: "%{subject_name} to be valid only when %{attribute} is equal to %{count}"
+ more_than_maximum: "%{subject_name} to be invalid when %{attribute} is greater than %{count}"
+ less_than_minimum: "%{subject_name} to be invalid when %{attribute} is less than %{count}"
optionals:
only_integer:
positive: "allowing only integer values"
@@ -105,31 +105,31 @@ en:
even:
positive: "allowing only even values"
equal_to:
- positive: "is equal to {{inspect}}"
+ positive: "is equal to %{inspect}"
less_than:
- positive: "is less than {{inspect}}"
+ positive: "is less than %{inspect}"
greater_than:
- positive: "is greater than {{inspect}}"
+ positive: "is greater than %{inspect}"
less_than_or_equal_to:
- positive: "is less than or equal to {{inspect}}"
+ positive: "is less than or equal to %{inspect}"
greater_than_or_equal_to:
- positive: "is greater than or equal to {{inspect}}"
+ positive: "is greater than or equal to %{inspect}"
validate_presence_of:
- description: "require {{attributes}} to be set"
+ description: "require %{attributes} to be set"
expectations:
- allow_nil: "{{subject_name}} to require {{attribute}} to be set"
+ allow_nil: "%{subject_name} to require %{attribute} to be set"
validate_uniqueness_of:
- description: "require unique values for {{attributes}}"
+ description: "require unique values for %{attributes}"
expectations:
- responds_to_scope: "{{subject_name}} instance responds to {{method}}"
- is_unique: "{{subject_name}} to require unique values for {{attribute}}"
- case_sensitive: "{{subject_name}} to {{not}}be case sensitive on {{attribute}} validation"
- valid_with_new_scope: "{{subject_name}} to be valid when {{attribute}} scope ({{method}}) change"
+ responds_to_scope: "%{subject_name} instance responds to %{method}"
+ is_unique: "%{subject_name} to require unique values for %{attribute}"
+ case_sensitive: "%{subject_name} to %{not}be case sensitive on %{attribute} validation"
+ valid_with_new_scope: "%{subject_name} to be valid when %{attribute} scope (%{method}) change"
optionals:
scope:
- positive: "scoped to {{sentence}}"
+ positive: "scoped to %{sentence}"
case_sensitive:
positive: "case sensitive"
negative: "case insensitive"
8 remarkable_mongo.gemspec
View
@@ -25,12 +25,8 @@ Gem::Specification.new do |s|
"lib/remarkable/mongo_mapper/base.rb",
"lib/remarkable/mongo_mapper/describe.rb",
"lib/remarkable/mongo_mapper/human_names.rb",
- "lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb",
"lib/remarkable/mongo_mapper/matchers/association_matcher.rb",
"lib/remarkable/mongo_mapper/matchers/have_key_matcher.rb",
- "lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb",
- "lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb",
- "lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb",
"locales/en.yml",
"remarkable_mongo.gemspec",
"spec/matchers/allow_values_for_matcher_spec.rb",
@@ -50,12 +46,8 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.6}
s.summary = %q{Remarkable Matchers for MongoDB ORMs}
s.test_files = [
- "spec/matchers/allow_values_for_matcher_spec.rb",
"spec/matchers/association_matcher_spec.rb",
"spec/matchers/have_key_matcher_spec.rb",
- "spec/matchers/validate_confirmation_of_matcher_spec.rb",
- "spec/matchers/validate_length_of_matcher_spec.rb",
- "spec/matchers/validate_presence_of_matcher_spec.rb",
"spec/model_builder.rb",
"spec/models.rb",
"spec/spec_helper.rb"
71 spec/matchers/allow_values_for_matcher_spec.rb
View
@@ -1,71 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe 'allow_values_for' do
- include ModelBuilder
-
- # Defines a model, create a validation and returns a raw matcher
- def define_and_validate(options={})
- @model = define_model :product do
- include MongoMapper::Document
-
- key :title, String
- key :category, String
-
- validates_format_of :title, options
- end
-
- allow_values_for(:title)
- end
-
- describe 'messages' do
- before(:each){ @matcher = define_and_validate(:with => /X|Y|Z/) }
-
- it 'should contain a description' do
- @matcher = allow_values_for(:title, "X", "Y", "Z")
- @matcher.description.should == 'allow "X", "Y", and "Z" as values for title'
- end
-
- it 'should set is_valid? message' do
- @matcher.in("A").matches?(subject)
- @matcher.failure_message.should == 'Expected Product to be valid when title is set to "A"'
- end
-
- it 'should set allow_nil? message' do
- @matcher.allow_nil.matches?(subject)
- @matcher.failure_message.should == 'Expected Product to allow nil values for title'
- end
-
- it 'should set allow_blank? message' do
- @matcher.allow_blank.matches?(subject)
- @matcher.failure_message.should == 'Expected Product to allow blank values for title'
- end
- end
-
- describe 'matchers' do
- it { should define_and_validate(:with => /X|Y|Z/).in('X', 'Y', 'Z') }
- it { should_not define_and_validate(:with => /X|Y|Z/).in('A') }
-
- it { should define_and_validate(:with => /X|Y|Z/, :message => 'valid').in('X', 'Y', 'Z').message('valid') }
-
- create_optional_boolean_specs(:allow_nil, self, :with => /X|Y|Z/)
- create_optional_boolean_specs(:allow_blank, self, :with => /X|Y|Z/)
- end
-
- describe 'macros' do
- before(:each){ define_and_validate(:with => /X|Y|Z/) }
-
- should_allow_values_for :title, 'X'
- should_not_allow_values_for :title, 'A'
- end
-
- describe 'failures' do
- it "should fail if any of the values are valid on invalid cases" do
- define_and_validate(:with => /X|Y|Z/)
-
- lambda {
- should_not allow_values_for(:title, 'A', 'X', 'B')
- }.should raise_error(Spec::Expectations::ExpectationNotMetError, /Did not expect Product to be valid/)
- end
- end
-end
-
64 spec/matchers/validate_confirmation_of_matcher_spec.rb
View
@@ -1,64 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe 'validate_confirmation_of' do
- include ModelBuilder
-
- # Defines a model, create a validation and returns a raw matcher
- def define_and_validate(options={})
- @model = define_model :person do
- include MongoMapper::Document
-
- key :name, String
- key :email, String
- key :age, String
-
- validates_confirmation_of :name, :email, options
- end
-
- validate_confirmation_of(:name, :email)
- end
-
- describe 'messages' do
- before(:each){ @matcher = define_and_validate }
-
- it 'should contain a description' do
- @matcher.description.should == 'require name and email to be confirmed'
- end
-
- it 'should set responds_to_confirmation? message' do
- @matcher = validate_confirmation_of(:age)
- @matcher.matches?(@model)
- @matcher.failure_message.should == 'Expected Person instance responds to age_confirmation'
- end
-
- it 'should set confirms? message' do
- @model.instance_eval{ def age_confirmation=(*args); end }
- @matcher = validate_confirmation_of(:age)
- @matcher.matches?(@model)
- @matcher.failure_message.should == 'Expected Person to be valid only when age is confirmed'
- end
-
- end
-
- describe 'matchers' do
-
- describe 'without options' do
- before(:each){ define_and_validate }
-
- it { should validate_confirmation_of(:name) }
- it { should validate_confirmation_of(:name, :email) }
- it { should_not validate_confirmation_of(:name, :age) }
- end
-
- create_message_specs(self)
- end
-
- describe 'macros' do
- before(:each){ define_and_validate }
-
- should_validate_confirmation_of :name
- should_validate_confirmation_of :name, :email
- should_not_validate_confirmation_of :name, :age
- end
-
-end
147 spec/matchers/validate_length_of_matcher_spec.rb
View
@@ -1,147 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe 'validate_length_of' do
- include ModelBuilder
-
- # Defines a model, create a validation and returns a raw matcher
- def define_and_validate(options={})
- options = options.merge(:within => 3..5) if options.slice(:within, :maximum, :minimum, :is).empty?
-
- @model = define_model :product do
- include MongoMapper::Document
-
- key :size, String
- key :category, String
-
- validates_length_of :size, options
- end
-
- validate_length_of(:size)
- end
-
- describe 'messages' do
- before(:each){ @matcher = define_and_validate }
-
- it 'should contain a description' do
- @matcher.within(3..5)
- @matcher.description.should == 'ensure length of size is within 3..5 characters'
-
- @matcher.within(nil).is(3)
- @matcher.description.should == 'ensure length of size is equal to 3 characters'
-
- @matcher.is(nil).maximum(5)
- @matcher.description.should == 'ensure length of size is maximum 5 characters'
-
- @matcher.maximum(nil).minimum(3)
- @matcher.description.should == 'ensure length of size is minimum 3 characters'
-
- @matcher.allow_nil(false)
- @matcher.description.should == 'ensure length of size is minimum 3 characters and not allowing nil values'
-
- @matcher.allow_blank
- @matcher.description.should == 'ensure length of size is minimum 3 characters, not allowing nil values, and allowing blank values'
- end
-
- it 'should set less_than_min_length? message' do
- @matcher.within(4..5).matches?(@model)
- @matcher.failure_message.should == 'Expected Product to be invalid when size length is less than 4 characters'
- end
-
- it 'should set exactly_min_length? message' do
- @matcher.should_receive(:less_than_min_length?).and_return(true)
- @matcher.within(2..5).matches?(@model)
- @matcher.failure_message.should == 'Expected Product to be valid when size length is 2 characters'
- end
-
- it 'should set more_than_max_length? message' do
- @matcher.within(3..4).matches?(@model)
- @matcher.failure_message.should == 'Expected Product to be invalid when size length is more than 4 characters'
- end
-
- it 'should set exactly_max_length? message' do
- @matcher.should_receive(:more_than_max_length?).and_return(true)
- @matcher.within(3..6).matches?(@model)
- @matcher.failure_message.should == 'Expected Product to be valid when size length is 6 characters'
- end
-
- it 'should set allow_blank? message' do
- @matcher.within(3..5).allow_blank.matches?(@model)
- @matcher.failure_message.should == 'Expected Product to allow blank values for size'
- end
-
- it 'should set allow_nil? message' do
- @matcher.within(3..5).allow_nil.matches?(@model)
- @matcher.failure_message.should == 'Expected Product to allow nil values for size'
- end
- end
-
- describe 'matcher' do
- # Wrap specs without options. Usually a couple specs.
- describe 'without options' do
- before(:each){ define_and_validate }
-
- it { should validate_length_of(:size, :within => 3..5) }
- it { should_not validate_length_of(:category, :within => 3..5) }
- end
-
- describe "with message option" do
-
- # if RAILS_VERSION =~ /^2.3/
- # it { should define_and_validate(:message => 'not valid').within(3..5).message('not valid') }
- # it { should_not define_and_validate(:message => 'not valid').within(3..5).message('valid') }
- # else
- # it { should define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('not valid') }
- # it { should_not define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('valid') }
- # end
-
- it { should define_and_validate(:is => 4, :message => 'not valid').is(4).message('not valid') }
- it { should_not define_and_validate(:is => 4, :message => 'not valid').is(4).message('valid') }
- end
-
- describe "with within option" do
- it { should define_and_validate(:within => 3..5).within(3..5) }
- it { should_not define_and_validate(:within => 3..5).within(2..5) }
- it { should_not define_and_validate(:within => 3..5).within(4..5) }
- it { should_not define_and_validate(:within => 3..5).within(3..4) }
- it { should_not define_and_validate(:within => 3..5).within(3..6) }
- end
-
- describe "with minimum option" do
- it { should define_and_validate(:minimum => 3).minimum(3) }
- it { should_not define_and_validate(:minimum => 3).minimum(2) }
- it { should_not define_and_validate(:minimum => 3).minimum(4) }
- end
-
- describe "with maximum option" do
- it { should define_and_validate(:maximum => 3).maximum(3) }
- it { should_not define_and_validate(:maximum => 3).maximum(2) }
- it { should_not define_and_validate(:maximum => 3).maximum(4) }
- end
-
- describe "with is option" do
- it { should define_and_validate(:is => 3).is(3) }
- it { should_not define_and_validate(:is => 3).is(2) }
- it { should_not define_and_validate(:is => 3).is(4) }
- end
-
- # Those are macros to test optionals which accept only boolean values
- create_optional_boolean_specs(:allow_nil, self)
- create_optional_boolean_specs(:allow_blank, self)
- end
-
- # In macros we include just a few tests to assure that everything works properly
- describe 'macros' do
- before(:each) { define_and_validate }
-
- should_validate_length_of :size, :within => 3..5
-
- should_not_validate_length_of :size, :within => 2..5
- should_not_validate_length_of :size, :within => 4..5
- should_not_validate_length_of :size, :within => 3..4
- should_not_validate_length_of :size, :within => 3..6
-
- should_validate_length_of :size do |m|
- m.within = 3..5
- end
- end
-end
33 spec/matchers/validate_presence_of_matcher_spec.rb
View
@@ -1,33 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe 'validate_presence_of' do
- subject do
- Article.new
- end
-
- describe 'messages' do
-
- it 'should contain a description' do
- matcher = validate_presence_of(:title, :body)
- matcher.description.should == 'require title and body to be set'
- end
-
- it 'should set allow_nil? message' do
- matcher = validate_presence_of(:body)
- matcher.matches?(subject)
- matcher.failure_message.should == 'Expected Article to require body to be set'
- matcher.negative_failure_message.should == 'Did not expect Article to require body to be set'
- end
-
- end
-
- describe 'matchers' do
- it { should validate_presence_of(:title) }
- it { should_not validate_presence_of(:body) }
- end
-
- describe 'macros' do
- should_validate_presence_of(:title)
- should_not_validate_presence_of(:body)
- end
-end
2  spec/spec_helper.rb
View
@@ -14,4 +14,4 @@ def reset_test_db!
require File.join(File.dirname(__FILE__), "..", "lib", "remarkable/mongo_mapper")
require File.join(File.dirname(__FILE__), "models")
-require File.join(File.dirname(__FILE__), "model_builder")
+require File.join(File.dirname(__FILE__), "model_builder")
Please sign in to comment.
Something went wrong with that request. Please try again.