thoughtbot--shoulda-matchers/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb

167 lines
4.4 KiB
Ruby
Raw Normal View History

module Shoulda
2011-10-16 17:49:58 +00:00
module Matchers
module ActiveModel
# The `ensure_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.
#
# 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
# should ensure_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
# end
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
#
# If your blacklist is a range of values, use `in_rnage`:
#
# class Game
# include ActiveModel::Model
# attr_accessor :supported_os
#
# validates_exclusion_of :supported_os, in: ['Mac', 'Linux']
# end
#
# # RSpec
# describe Game do
# it do
# should ensure_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
# end
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
#
# #### Qualifiers
#
# ##### 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
# should ensure_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
# end
2011-10-16 17:49:58 +00:00
#
# # Test::Unit
# class GameTest < ActiveSupport::TestCase
# should ensure_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
2011-10-16 17:49:58 +00:00
#
# @return [EnsureExclusionOfMatcher]
2011-10-16 17:49:58 +00:00
#
def ensure_exclusion_of(attr)
EnsureExclusionOfMatcher.new(attr)
end
# @private
class EnsureExclusionOfMatcher < ValidationMatcher
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
@maximum = range.max
2011-10-16 17:49:58 +00:00
self
end
def with_message(message)
@expected_message = message if message
self
end
def description
2012-08-19 09:07:05 +00:00
"ensure exclusion of #{@attribute} in #{inspect_message}"
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|
disallows_value_of(value, expected_message)
end
end
2011-10-16 17:49:58 +00:00
def allows_lower_value
2012-04-24 21:33:47 +00:00
@minimum == 0 || allows_value_of(@minimum - 1, expected_message)
2011-10-16 17:49:58 +00:00
end
def allows_higher_value
2012-04-24 21:33:47 +00:00
allows_value_of(@maximum + 1, expected_message)
2011-10-16 17:49:58 +00:00
end
def disallows_minimum_value
2012-04-24 21:33:47 +00:00
disallows_value_of(@minimum, expected_message)
2011-10-16 17:49:58 +00:00
end
def disallows_maximum_value
2012-04-24 21:33:47 +00:00
disallows_value_of(@maximum, expected_message)
end
def expected_message
@expected_message || :exclusion
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