From acb02d04488418b6dd528b76f951417538605c41 Mon Sep 17 00:00:00 2001 From: Maxim Kaschenko Date: Sat, 10 May 2014 15:59:21 +0700 Subject: [PATCH] ensure_exclusion_of => validate_exclusion_of --- README.md | 4 +-- lib/shoulda/matchers/active_model.rb | 2 +- ...er.rb => validate_exclusion_of_matcher.rb} | 23 +++++++------ ... => validate_exclusion_of_matcher_spec.rb} | 34 ++++++++++++------- 4 files changed, 36 insertions(+), 27 deletions(-) rename lib/shoulda/matchers/active_model/{ensure_exclusion_of_matcher.rb => validate_exclusion_of_matcher.rb} (85%) rename spec/shoulda/matchers/active_model/{ensure_exclusion_of_matcher_spec.rb => validate_exclusion_of_matcher_spec.rb} (67%) diff --git a/README.md b/README.md index 450761f3..fa843a72 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ complex, and error-prone. the `validates_format_of` validation. * **[validate_inclusion_of](lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb)** tests usage of `validates_inclusion_of`. -* **[ensure_exclusion_of](lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb)** tests - usage of `validates_exclusion_of`. +* **[validate_exclusion_of](lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb)** + tests usage of `validates_exclusion_of`. * **[ensure_length_of](lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb)** tests usage of `validates_length_of`. * **[have_secure_password](lib/shoulda/matchers/active_model/have_secure_password_matcher.rb)** tests diff --git a/lib/shoulda/matchers/active_model.rb b/lib/shoulda/matchers/active_model.rb index d43c2576..2fee93be 100644 --- a/lib/shoulda/matchers/active_model.rb +++ b/lib/shoulda/matchers/active_model.rb @@ -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/ensure_length_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_presence_of_matcher' require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher' diff --git a/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb similarity index 85% rename from lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb rename to lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb index 9973df00..512ed1c0 100644 --- a/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb @@ -1,7 +1,7 @@ module Shoulda module Matchers 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 # take a blacklist of values, and inversely, can take values outside of # this list. @@ -18,14 +18,14 @@ module Shoulda # # RSpec # describe Game do # it do - # should ensure_exclusion_of(:supported_os). + # should validate_exclusion_of(:supported_os). # in_array(['Mac', 'Linux']) # end # end # # # Test::Unit # class GameTest < ActiveSupport::TestCase - # should ensure_exclusion_of(:supported_os). + # should validate_exclusion_of(:supported_os). # in_array(['Mac', 'Linux']) # end # @@ -41,14 +41,14 @@ module Shoulda # # RSpec # describe Game do # it do - # should ensure_exclusion_of(:floors_with_enemies). + # should validate_exclusion_of(:floors_with_enemies). # in_range(5..8) # end # end # # # Test::Unit # class GameTest < ActiveSupport::TestCase - # should ensure_exclusion_of(:floors_with_enemies). + # should validate_exclusion_of(:floors_with_enemies). # in_range(5..8) # end # @@ -70,7 +70,7 @@ module Shoulda # # RSpec # describe Game do # it do - # should ensure_exclusion_of(:weapon). + # should validate_exclusion_of(:weapon). # in_array(['pistol', 'paintball gun', 'stick']). # with_message('You chose a puny weapon') # end @@ -78,19 +78,20 @@ module Shoulda # # # Test::Unit # class GameTest < ActiveSupport::TestCase - # should ensure_exclusion_of(:weapon). + # should validate_exclusion_of(:weapon). # in_array(['pistol', 'paintball gun', 'stick']). # with_message('You chose a puny weapon') # end # - # @return [EnsureExclusionOfMatcher] + # @return [ValidateExclusionOfMatcher] # - def ensure_exclusion_of(attr) - EnsureExclusionOfMatcher.new(attr) + def validate_exclusion_of(attr) + ValidateExclusionOfMatcher.new(attr) end + alias_method :ensure_exclusion_of, :validate_exclusion_of # @private - class EnsureExclusionOfMatcher < ValidationMatcher + class ValidateExclusionOfMatcher < ValidationMatcher def initialize(attribute) super(attribute) @array = nil diff --git a/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb b/spec/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb similarity index 67% rename from spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb rename to spec/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb index b4574e46..98138bc4 100644 --- a/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +++ b/spec/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb @@ -1,39 +1,47 @@ 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 it 'accepts ensuring the correct range' do 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 it 'rejects ensuring excluded value' do 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 it 'does not override the default message with a blank' do 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 context 'an attribute which must be excluded from a range with excluded end' do it 'accepts ensuring the correct range' do 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 it 'rejects ensuring excluded value' do 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 context 'an attribute with a custom validation message' do it 'accepts ensuring the correct range' do 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 @@ -45,7 +53,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do 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/) model = custom_validation do @@ -54,7 +62,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do 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/) end end @@ -62,21 +70,21 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do context 'an attribute which must be excluded from an array' do it 'accepts with correct array' do 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 it 'rejects when only part of array matches' do 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 it 'rejects when array does not match at all' do 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 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"]' end