1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Remove 'validate_format_of' matcher

* See issue #252
This commit is contained in:
Melissa Xie 2013-03-15 17:29:58 -04:00 committed by Melissa Xie
parent 6b105ba7cd
commit 53246538cf
3 changed files with 0 additions and 185 deletions

View file

@ -9,7 +9,6 @@ require 'shoulda/matchers/active_model/ensure_length_of_matcher'
require 'shoulda/matchers/active_model/ensure_inclusion_of_matcher'
require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher'
require 'shoulda/matchers/active_model/validate_presence_of_matcher'
require 'shoulda/matchers/active_model/validate_format_of_matcher'
require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'
require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
@ -41,7 +40,6 @@ module Shoulda
# class User < ActiveRecord::Base
# validates_presence_of :name
# validates_presence_of :phone_number
# validates_format_of :phone_number, :with => /\\(\\d{3}\\) \\d{3}\\-\\d{4}/
# validates_inclusion_of :status, :in => %w(Activated Pending), :strict => true
# attr_accessible :name, :phone_number
# end

View file

@ -1,108 +0,0 @@
require 'active_support/deprecation'
module Shoulda # :nodoc:
module Matchers
module ActiveModel # :nodoc:
# Ensures that the model is not valid if the given attribute is not
# formatted correctly.
#
# Options:
# * <tt>with_message</tt> - value the test expects to find in
# <tt>allow_blank</tt> - allows a blank value
# <tt>allow_nil</tt> - allows a nil value
# <tt>errors.on(:attribute)</tt>. <tt>Regexp</tt> or <tt>String</tt>.
# Defaults to the translation for <tt>:invalid</tt>.
# * <tt>with(string to test against)</tt>
# * <tt>not_with(string to test against)</tt>
#
# Examples:
# it { should validate_format_of(:name).
# with('12345').
# with_message(/is not optional/) }
# it { should validate_format_of(:name).
# not_with('12D45').
# with_message(/is not optional/) }
#
def validate_format_of(attr)
ValidateFormatOfMatcher.new(attr)
end
class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
def initialize(attribute)
ActiveSupport::Deprecation.warn 'The validate_format_of matcher is deprecated and will be removed in 2.0'
super
@options = {}
end
def allow_blank(allow_blank = true)
@options[:allow_blank] = allow_blank
self
end
def allow_nil(allow_nil = true)
@options[:allow_nil] = allow_nil
self
end
def with_message(message)
if message
@expected_message = message
end
self
end
def with(value)
if @value_to_fail
raise 'You may not call both with and not_with'
else
@value_to_pass = value
self
end
end
def not_with(value)
if @value_to_pass
raise 'You may not call both with and not_with'
else
@value_to_fail = value
self
end
end
def matches?(subject)
super(subject)
@expected_message ||= :invalid
if @value_to_fail
disallows_value_of(@value_to_fail, @expected_message) && allows_blank_value? && allows_nil_value?
else
allows_value_of(@value_to_pass, @expected_message) && allows_blank_value? && allows_nil_value?
end
end
def description
"have a valid format for #{@attribute}"
end
private
def allows_blank_value?
if @options.key?(:allow_blank)
@options[:allow_blank] == allows_value_of('')
else
true
end
end
def allows_nil_value?
if @options.key?(:allow_nil)
@options[:allow_nil] == allows_value_of(nil)
else
true
end
end
end
end
end
end

View file

@ -1,75 +0,0 @@
require 'spec_helper'
describe Shoulda::Matchers::ActiveModel::ValidateFormatOfMatcher do
context 'a model with a format validation' do
it 'accepts when format matches ' do
validating_format(:with => /^\d{5}$/).should matcher.with('12345')
end
it 'rejects blank with should_not' do
validating_format(:with => /^\d{5}$/).should_not matcher.with(' ')
end
it 'rejects blank with not_with' do
validating_format(:with => /^\d{5}$/).should matcher.not_with(' ')
end
it 'rejects nil' do
validating_format(:with => /^\d{5}$/).should_not matcher.with(nil)
end
it 'rejects a non-matching format with should_not' do
validating_format(:with => /^\d{5}$/).should_not matcher.with('1234a')
end
it 'rejects a non-matching format with not_with' do
validating_format(:with => /^\d{5}$/).should matcher.not_with('1234a')
end
it 'raises an error if you try to call both with and not_with' do
expect {
validate_format_of(:attr).not_with('123456').with('12345')
}.to raise_error(RuntimeError)
end
end
context 'when allow_blank or allow_nil are set' do
it 'is valid when attr is nil' do
validating_format(:with => /abc/, :allow_nil => true).
should matcher.with(nil)
end
it 'is valid when attr is blank' do
validating_format(:with => /abc/, :allow_blank => true).
should matcher.with(' ')
end
end
context '#allow_blank' do
it 'accepts when allow_blank matches' do
validating_format(:with => /abc/, :allow_blank => true).
should matcher.allow_blank
end
it 'rejects when allow_blank does not match' do
validating_format(:with => /abc/, :allow_blank => false).
should_not matcher.allow_blank
end
end
context '#allow_nil' do
it 'accepts when allow_nil matches' do
validating_format(:with => /abc/, :allow_nil => true).
should matcher.allow_nil
end
it 'rejects when allow_nil does not match' do
validating_format(:with => /abc/, :allow_nil => false).
should_not matcher.allow_nil
end
end
def matcher
validate_format_of(:attr)
end
end