2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2011-10-16 17:49:58 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
module ActiveModel
|
2014-05-10 08:59:21 +00:00
|
|
|
# The `validate_exclusion_of` matcher tests usage of the
|
2014-01-23 18:07:36 +00:00
|
|
|
# `validates_exclusion_of` validation, asserting that an attribute cannot
|
|
|
|
# take a blacklist of values, and inversely, can take values outside of
|
|
|
|
# this list.
|
|
|
|
#
|
|
|
|
# If your blacklist is an array of values, use `in_array`:
|
|
|
|
#
|
|
|
|
# class Game
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :supported_os
|
|
|
|
#
|
|
|
|
# validates_exclusion_of :supported_os, in: ['Mac', 'Linux']
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
|
|
|
# describe Game do
|
|
|
|
# it do
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:supported_os).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_array(['Mac', 'Linux'])
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2014-01-23 18:07:36 +00:00
|
|
|
# class GameTest < ActiveSupport::TestCase
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:supported_os).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_array(['Mac', 'Linux'])
|
|
|
|
# end
|
|
|
|
#
|
2014-11-06 09:19:12 +00:00
|
|
|
# If your blacklist is a range of values, use `in_range`:
|
2014-01-23 18:07:36 +00:00
|
|
|
#
|
|
|
|
# class Game
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :supported_os
|
|
|
|
#
|
|
|
|
# validates_exclusion_of :supported_os, in: ['Mac', 'Linux']
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
|
|
|
# describe Game do
|
|
|
|
# it do
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:floors_with_enemies).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_range(5..8)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2014-01-23 18:07:36 +00:00
|
|
|
# class GameTest < ActiveSupport::TestCase
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:floors_with_enemies).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_range(5..8)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# #### Qualifiers
|
|
|
|
#
|
2015-03-28 14:43:31 +00:00
|
|
|
# ##### on
|
|
|
|
#
|
|
|
|
# Use `on` if your validation applies only under a certain context.
|
|
|
|
#
|
|
|
|
# class Game
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :weapon
|
|
|
|
#
|
|
|
|
# validates_exclusion_of :weapon,
|
|
|
|
# in: ['pistol', 'paintball gun', 'stick'],
|
|
|
|
# on: :create
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
|
|
|
# describe Game do
|
|
|
|
# it do
|
|
|
|
# should validate_exclusion_of(:weapon).
|
|
|
|
# in_array(['pistol', 'paintball gun', 'stick']).
|
|
|
|
# on(:create)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2015-03-28 14:43:31 +00:00
|
|
|
# class GameTest < ActiveSupport::TestCase
|
|
|
|
# should validate_exclusion_of(:weapon).
|
|
|
|
# in_array(['pistol', 'paintball gun', 'stick']).
|
|
|
|
# on(:create)
|
|
|
|
# end
|
|
|
|
#
|
2014-01-23 18:07:36 +00:00
|
|
|
# ##### with_message
|
|
|
|
#
|
|
|
|
# Use `with_message` if you are using a custom validation message.
|
|
|
|
#
|
|
|
|
# class Game
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :weapon
|
|
|
|
#
|
|
|
|
# validates_exclusion_of :weapon,
|
|
|
|
# in: ['pistol', 'paintball gun', 'stick'],
|
|
|
|
# message: 'You chose a puny weapon'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
|
|
|
# describe Game do
|
|
|
|
# it do
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:weapon).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_array(['pistol', 'paintball gun', 'stick']).
|
|
|
|
# with_message('You chose a puny weapon')
|
|
|
|
# end
|
|
|
|
# end
|
2011-10-16 17:49:58 +00:00
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2014-01-23 18:07:36 +00:00
|
|
|
# class GameTest < ActiveSupport::TestCase
|
2014-05-10 08:59:21 +00:00
|
|
|
# should validate_exclusion_of(:weapon).
|
2014-01-23 18:07:36 +00:00
|
|
|
# in_array(['pistol', 'paintball gun', 'stick']).
|
|
|
|
# with_message('You chose a puny weapon')
|
|
|
|
# end
|
2011-10-16 17:49:58 +00:00
|
|
|
#
|
2014-05-10 08:59:21 +00:00
|
|
|
# @return [ValidateExclusionOfMatcher]
|
2011-10-16 17:49:58 +00:00
|
|
|
#
|
2014-05-10 08:59:21 +00:00
|
|
|
def validate_exclusion_of(attr)
|
|
|
|
ValidateExclusionOfMatcher.new(attr)
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
2014-07-23 04:52:16 +00:00
|
|
|
|
2014-01-23 18:07:36 +00:00
|
|
|
# @private
|
2014-05-10 08:59:21 +00:00
|
|
|
class ValidateExclusionOfMatcher < ValidationMatcher
|
2014-06-27 22:39:39 +00:00
|
|
|
def initialize(attribute)
|
|
|
|
super(attribute)
|
2015-12-13 23:47:59 +00:00
|
|
|
@expected_message = :exclusion
|
2014-06-27 22:39:39 +00:00
|
|
|
@array = nil
|
|
|
|
@range = nil
|
|
|
|
end
|
|
|
|
|
2012-08-19 09:07:05 +00:00
|
|
|
def in_array(array)
|
|
|
|
@array = array
|
|
|
|
self
|
|
|
|
end
|
2011-10-16 17:49:58 +00:00
|
|
|
|
|
|
|
def in_range(range)
|
|
|
|
@range = range
|
|
|
|
@minimum = range.first
|
2013-03-30 18:03:38 +00:00
|
|
|
@maximum = range.max
|
2011-10-16 17:49:58 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2015-12-13 23:47:59 +00:00
|
|
|
def simple_description
|
|
|
|
if @range
|
|
|
|
"validate that :#{@attribute} lies outside the range #{@range.inspect}"
|
|
|
|
else
|
|
|
|
description = "validate that :#{@attribute}"
|
2011-10-16 17:49:58 +00:00
|
|
|
|
2015-12-13 23:47:59 +00:00
|
|
|
if @array.many?
|
|
|
|
description << " is neither " + @array.map(&:inspect).to_sentence(
|
|
|
|
two_words_connector: " nor ",
|
|
|
|
last_word_connector: ", nor "
|
|
|
|
)
|
|
|
|
else
|
|
|
|
description << " is not #{@array.first.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
description
|
|
|
|
end
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(subject)
|
|
|
|
super(subject)
|
|
|
|
|
2012-08-19 09:07:05 +00:00
|
|
|
if @range
|
|
|
|
allows_lower_value &&
|
|
|
|
disallows_minimum_value &&
|
|
|
|
allows_higher_value &&
|
|
|
|
disallows_maximum_value
|
|
|
|
elsif @array
|
|
|
|
disallows_all_values_in_array?
|
|
|
|
end
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-08-19 09:07:05 +00:00
|
|
|
def disallows_all_values_in_array?
|
|
|
|
@array.all? do |value|
|
2015-12-13 23:47:59 +00:00
|
|
|
disallows_value_of(value, @expected_message)
|
2012-08-19 09:07:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-16 17:49:58 +00:00
|
|
|
def allows_lower_value
|
2015-12-13 23:47:59 +00:00
|
|
|
@minimum == 0 || allows_value_of(@minimum - 1, @expected_message)
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allows_higher_value
|
2015-12-13 23:47:59 +00:00
|
|
|
allows_value_of(@maximum + 1, @expected_message)
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def disallows_minimum_value
|
2015-12-13 23:47:59 +00:00
|
|
|
disallows_value_of(@minimum, @expected_message)
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def disallows_maximum_value
|
2015-12-13 23:47:59 +00:00
|
|
|
disallows_value_of(@maximum, @expected_message)
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
|
2012-08-19 09:07:05 +00:00
|
|
|
def inspect_message
|
|
|
|
if @range
|
|
|
|
@range.inspect
|
|
|
|
else
|
|
|
|
@array.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-16 17:49:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|