forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_exclusion_of_matcher.rb
213 lines (197 loc) · 5.88 KB
/
validate_exclusion_of_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
module Shoulda
module Matchers
module ActiveModel
# 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.
#
# 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 validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
# end
#
# # Minitest (Shoulda)
# class GameTest < ActiveSupport::TestCase
# should validate_exclusion_of(:supported_os).
# in_array(['Mac', 'Linux'])
# end
#
# If your blacklist is a range of values, use `in_range`:
#
# 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 validate_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
# end
#
# # Minitest (Shoulda)
# class GameTest < ActiveSupport::TestCase
# should validate_exclusion_of(:floors_with_enemies).
# in_range(5..8)
# end
#
# #### Qualifiers
#
# ##### 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
#
# # Minitest (Shoulda)
# class GameTest < ActiveSupport::TestCase
# should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# on(:create)
# end
#
# ##### 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 validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
# end
#
# # Minitest (Shoulda)
# class GameTest < ActiveSupport::TestCase
# should validate_exclusion_of(:weapon).
# in_array(['pistol', 'paintball gun', 'stick']).
# with_message('You chose a puny weapon')
# end
#
# @return [ValidateExclusionOfMatcher]
#
def validate_exclusion_of(attr)
ValidateExclusionOfMatcher.new(attr)
end
# @private
class ValidateExclusionOfMatcher < ValidationMatcher
def initialize(attribute)
super(attribute)
@expected_message = :exclusion
@array = nil
@range = nil
end
def in_array(array)
@array = array
self
end
def in_range(range)
@range = range
@minimum = range.first
@maximum = range.max
self
end
def simple_description
if @range
"validate that :#{@attribute} lies outside the range " +
Shoulda::Matchers::Util.inspect_range(@range)
else
description = "validate that :#{@attribute}"
if @array.many?
description << " is neither #{inspected_array}"
else
description << " is not #{inspected_array}"
end
description
end
end
def matches?(subject)
super(subject)
if @range
allows_lower_value &&
disallows_minimum_value &&
allows_higher_value &&
disallows_maximum_value
elsif @array
disallows_all_values_in_array?
end
end
private
def disallows_all_values_in_array?
@array.all? do |value|
disallows_value_of(value, @expected_message)
end
end
def allows_lower_value
@minimum == 0 || allows_value_of(@minimum - 1, @expected_message)
end
def allows_higher_value
allows_value_of(@maximum + 1, @expected_message)
end
def disallows_minimum_value
disallows_value_of(@minimum, @expected_message)
end
def disallows_maximum_value
disallows_value_of(@maximum, @expected_message)
end
def inspect_message
if @range
@range.inspect
else
@array.inspect
end
end
def inspected_array
Shoulda::Matchers::Util.inspect_values(@array).to_sentence(
two_words_connector: " nor ",
last_word_connector: ", nor "
)
end
end
end
end
end