ensure_exclusion_of => validate_exclusion_of

This commit is contained in:
Maxim Kaschenko 2014-05-10 15:59:21 +07:00 committed by Elliot Winkler
parent fcdd86021d
commit acb02d0448
4 changed files with 36 additions and 27 deletions

View File

@ -14,8 +14,8 @@ complex, and error-prone.
the `validates_format_of` validation. the `validates_format_of` validation.
* **[validate_inclusion_of](lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb)** * **[validate_inclusion_of](lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb)**
tests usage of `validates_inclusion_of`. tests usage of `validates_inclusion_of`.
* **[ensure_exclusion_of](lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb)** tests * **[validate_exclusion_of](lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb)**
usage of `validates_exclusion_of`. tests usage of `validates_exclusion_of`.
* **[ensure_length_of](lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb)** tests usage * **[ensure_length_of](lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb)** tests usage
of `validates_length_of`. of `validates_length_of`.
* **[have_secure_password](lib/shoulda/matchers/active_model/have_secure_password_matcher.rb)** tests * **[have_secure_password](lib/shoulda/matchers/active_model/have_secure_password_matcher.rb)** tests

View File

@ -6,7 +6,7 @@ require 'shoulda/matchers/active_model/allow_value_matcher'
require 'shoulda/matchers/active_model/disallow_value_matcher' require 'shoulda/matchers/active_model/disallow_value_matcher'
require 'shoulda/matchers/active_model/ensure_length_of_matcher' require 'shoulda/matchers/active_model/ensure_length_of_matcher'
require 'shoulda/matchers/active_model/validate_inclusion_of_matcher' require 'shoulda/matchers/active_model/validate_inclusion_of_matcher'
require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher' require 'shoulda/matchers/active_model/validate_exclusion_of_matcher'
require 'shoulda/matchers/active_model/validate_absence_of_matcher' require 'shoulda/matchers/active_model/validate_absence_of_matcher'
require 'shoulda/matchers/active_model/validate_presence_of_matcher' require 'shoulda/matchers/active_model/validate_presence_of_matcher'
require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher' require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'

View File

@ -1,7 +1,7 @@
module Shoulda module Shoulda
module Matchers module Matchers
module ActiveModel module ActiveModel
# The `ensure_exclusion_of` matcher tests usage of the # The `validate_exclusion_of` matcher tests usage of the
# `validates_exclusion_of` validation, asserting that an attribute cannot # `validates_exclusion_of` validation, asserting that an attribute cannot
# take a blacklist of values, and inversely, can take values outside of # take a blacklist of values, and inversely, can take values outside of
# this list. # this list.
@ -18,14 +18,14 @@ module Shoulda
# # RSpec # # RSpec
# describe Game do # describe Game do
# it do # it do
# should ensure_exclusion_of(:supported_os). # should validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux']) # in_array(['Mac', 'Linux'])
# end # end
# end # end
# #
# # Test::Unit # # Test::Unit
# class GameTest < ActiveSupport::TestCase # class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:supported_os). # should validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux']) # in_array(['Mac', 'Linux'])
# end # end
# #
@ -41,14 +41,14 @@ module Shoulda
# # RSpec # # RSpec
# describe Game do # describe Game do
# it do # it do
# should ensure_exclusion_of(:floors_with_enemies). # should validate_exclusion_of(:floors_with_enemies).
# in_range(5..8) # in_range(5..8)
# end # end
# end # end
# #
# # Test::Unit # # Test::Unit
# class GameTest < ActiveSupport::TestCase # class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:floors_with_enemies). # should validate_exclusion_of(:floors_with_enemies).
# in_range(5..8) # in_range(5..8)
# end # end
# #
@ -70,7 +70,7 @@ module Shoulda
# # RSpec # # RSpec
# describe Game do # describe Game do
# it do # it do
# should ensure_exclusion_of(:weapon). # should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']). # in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon') # with_message('You chose a puny weapon')
# end # end
@ -78,19 +78,20 @@ module Shoulda
# #
# # Test::Unit # # Test::Unit
# class GameTest < ActiveSupport::TestCase # class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:weapon). # should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']). # in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon') # with_message('You chose a puny weapon')
# end # end
# #
# @return [EnsureExclusionOfMatcher] # @return [ValidateExclusionOfMatcher]
# #
def ensure_exclusion_of(attr) def validate_exclusion_of(attr)
EnsureExclusionOfMatcher.new(attr) ValidateExclusionOfMatcher.new(attr)
end end
alias_method :ensure_exclusion_of, :validate_exclusion_of
# @private # @private
class EnsureExclusionOfMatcher < ValidationMatcher class ValidateExclusionOfMatcher < ValidationMatcher
def initialize(attribute) def initialize(attribute)
super(attribute) super(attribute)
@array = nil @array = nil

View File

@ -1,39 +1,47 @@
require 'spec_helper' require 'spec_helper'
describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do describe Shoulda::Matchers::ActiveModel do
describe '#ensure_exclusion_of' do
it 'is aliased to #validate_exclusion_of' do
expect(method(:ensure_exclusion_of)).to eq(method(:validate_exclusion_of))
end
end
end
describe Shoulda::Matchers::ActiveModel::ValidateExclusionOfMatcher do
context 'an attribute which must be excluded from a range' do context 'an attribute which must be excluded from a range' do
it 'accepts ensuring the correct range' do it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2..5)). expect(validating_exclusion(in: 2..5)).
to ensure_exclusion_of(:attr).in_range(2..5) to validate_exclusion_of(:attr).in_range(2..5)
end end
it 'rejects ensuring excluded value' do it 'rejects ensuring excluded value' do
expect(validating_exclusion(in: 2..5)). expect(validating_exclusion(in: 2..5)).
not_to ensure_exclusion_of(:attr).in_range(2..6) not_to validate_exclusion_of(:attr).in_range(2..6)
end end
it 'does not override the default message with a blank' do it 'does not override the default message with a blank' do
expect(validating_exclusion(in: 2..5)). expect(validating_exclusion(in: 2..5)).
to ensure_exclusion_of(:attr).in_range(2..5).with_message(nil) to validate_exclusion_of(:attr).in_range(2..5).with_message(nil)
end end
end end
context 'an attribute which must be excluded from a range with excluded end' do context 'an attribute which must be excluded from a range with excluded end' do
it 'accepts ensuring the correct range' do it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2...5)). expect(validating_exclusion(in: 2...5)).
to ensure_exclusion_of(:attr).in_range(2...5) to validate_exclusion_of(:attr).in_range(2...5)
end end
it 'rejects ensuring excluded value' do it 'rejects ensuring excluded value' do
expect(validating_exclusion(in: 2...5)). expect(validating_exclusion(in: 2...5)).
not_to ensure_exclusion_of(:attr).in_range(2...4) not_to validate_exclusion_of(:attr).in_range(2...4)
end end
end end
context 'an attribute with a custom validation message' do context 'an attribute with a custom validation message' do
it 'accepts ensuring the correct range' do it 'accepts ensuring the correct range' do
expect(validating_exclusion(in: 2..4, message: 'not good')). expect(validating_exclusion(in: 2..4, message: 'not good')).
to ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/) to validate_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
end end
end end
@ -45,7 +53,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
end end
end end
expect(model).to ensure_exclusion_of(:attr).in_range(2..5). expect(model).to validate_exclusion_of(:attr).in_range(2..5).
with_message(/should be out of this range/) with_message(/should be out of this range/)
model = custom_validation do model = custom_validation do
@ -54,7 +62,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
end end
end end
expect(model).to ensure_exclusion_of(:attr).in_range(2...5). expect(model).to validate_exclusion_of(:attr).in_range(2...5).
with_message(/should be out of this range/) with_message(/should be out of this range/)
end end
end end
@ -62,21 +70,21 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
context 'an attribute which must be excluded from an array' do context 'an attribute which must be excluded from an array' do
it 'accepts with correct array' do it 'accepts with correct array' do
expect(validating_exclusion(in: %w(one two))). expect(validating_exclusion(in: %w(one two))).
to ensure_exclusion_of(:attr).in_array(%w(one two)) to validate_exclusion_of(:attr).in_array(%w(one two))
end end
it 'rejects when only part of array matches' do it 'rejects when only part of array matches' do
expect(validating_exclusion(in: %w(one two))). expect(validating_exclusion(in: %w(one two))).
not_to ensure_exclusion_of(:attr).in_array(%w(one wrong_value)) not_to validate_exclusion_of(:attr).in_array(%w(one wrong_value))
end end
it 'rejects when array does not match at all' do it 'rejects when array does not match at all' do
expect(validating_exclusion(in: %w(one two))). expect(validating_exclusion(in: %w(one two))).
not_to ensure_exclusion_of(:attr).in_array(%w(cat dog)) not_to validate_exclusion_of(:attr).in_array(%w(cat dog))
end end
it 'has correct description' do it 'has correct description' do
expect(ensure_exclusion_of(:attr).in_array([true, 'dog']).description). expect(validate_exclusion_of(:attr).in_array([true, 'dog']).description).
to eq 'ensure exclusion of attr in [true, "dog"]' to eq 'ensure exclusion of attr in [true, "dog"]'
end end