-
-
Notifications
You must be signed in to change notification settings - Fork 912
/
ensure_inclusion_of_matcher.rb
267 lines (229 loc) · 7.32 KB
/
ensure_inclusion_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
require 'bigdecimal'
module Shoulda # :nodoc:
module Matchers
module ActiveModel # :nodoc:
# Ensure that the attribute's value is in the range specified
#
# Options:
# * <tt>in_array</tt> - the array of allowed values for this attribute
# * <tt>in_range</tt> - the range of allowed values for this attribute
# * <tt>with_low_message</tt> - value the test expects to find in
# <tt>errors.on(:attribute)</tt>. Regexp or string. Defaults to the
# translation for :inclusion.
# * <tt>with_high_message</tt> - value the test expects to find in
# <tt>errors.on(:attribute)</tt>. Regexp or string. Defaults to the
# translation for :inclusion.
#
# Example:
# it { should ensure_inclusion_of(:age).in_range(0..100) }
#
def ensure_inclusion_of(attr)
EnsureInclusionOfMatcher.new(attr)
end
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
ARBITRARY_OUTSIDE_STRING = 'shouldamatchersteststring'
ARBITRARY_OUTSIDE_FIXNUM = 123456789
ARBITRARY_OUTSIDE_DECIMAL = BigDecimal.new('0.123456789')
BOOLEAN_ALLOWS_BOOLEAN_MESSAGE = <<EOT
You are using `ensure_inclusion_of` to assert that a boolean column allows
boolean values and disallows non-boolean ones. Assuming you are using
`validates_format_of` in your model, be aware that it is not possible to fully
test this, and in fact the validation is superfluous, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you should
consider removing this test and the corresponding validation.
EOT
BOOLEAN_ALLOWS_NIL_MESSAGE = <<EOT
You are using `ensure_inclusion_of` to assert that a boolean column allows nil.
Be aware that it is not possible to fully test this, as anything other than
true, false or nil will be converted to false. Hence, you should consider
removing this test and the corresponding validation.
EOT
def initialize(attribute)
super(attribute)
@options = {}
end
def in_array(array)
@array = array
self
end
def in_range(range)
@range = range
@minimum = range.first
@maximum = range.max
self
end
def allow_blank(allow_blank = true)
@options[:allow_blank] = allow_blank
self
end
def allow_nil(allow_nil = true)
@options[:allow_nil] = allow_nil
self
end
def with_message(message)
if message
@low_message = message
@high_message = message
end
self
end
def with_low_message(message)
@low_message = message if message
self
end
def with_high_message(message)
@high_message = message if message
self
end
def description
"ensure inclusion of #{@attribute} in #{inspect_message}"
end
def matches?(subject)
super(subject)
if @range
@low_message ||= :inclusion
@high_message ||= :inclusion
matches_for_range?
elsif @array
if matches_for_array?
true
else
@failure_message = "#{@array} doesn't match array in validation"
false
end
end
end
private
def matches_for_range?
disallows_lower_value &&
allows_minimum_value &&
disallows_higher_value &&
allows_maximum_value
end
def matches_for_array?
allows_all_values_in_array? &&
allows_blank_value? &&
allows_nil_value? &&
disallows_value_outside_of_array?
end
def allows_blank_value?
if @options.key?(:allow_blank)
blank_values = ['', ' ', "\n", "\r", "\t", "\f"]
@options[:allow_blank] == blank_values.all? { |value| allows_value_of(value) }
else
true
end
end
def allows_nil_value?
if @options.key?(:allow_nil)
@options[:allow_nil] == allows_value_of(nil)
else
true
end
end
def inspect_message
@range.nil? ? @array.inspect : @range.inspect
end
def allows_all_values_in_array?
@array.all? do |value|
allows_value_of(value, @low_message)
end
end
def disallows_lower_value
@minimum == 0 || disallows_value_of(@minimum - 1, @low_message)
end
def disallows_higher_value
disallows_value_of(@maximum + 1, @high_message)
end
def allows_minimum_value
allows_value_of(@minimum, @low_message)
end
def allows_maximum_value
allows_value_of(@maximum, @high_message)
end
def disallows_value_outside_of_array?
if attribute_type == :boolean
case @array
when [true, false]
Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
return true
when [nil]
if attribute_column.null
Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE
return true
else
raise NonNullableBooleanError.create(@attribute)
end
end
end
!allows_value_of(*values_outside_of_array)
end
def values_outside_of_array
if !(@array & outside_values).empty?
raise CouldNotDetermineValueOutsideOfArray
else
outside_values
end
end
def outside_values
case attribute_type
when :boolean
boolean_outside_values
when :fixnum
[ARBITRARY_OUTSIDE_FIXNUM]
when :decimal
[ARBITRARY_OUTSIDE_DECIMAL]
else
[ARBITRARY_OUTSIDE_STRING]
end
end
def boolean_outside_values
values = []
values << case @array
when [true] then false
when [false] then true
else raise CouldNotDetermineValueOutsideOfArray
end
if attribute_allows_nil?
values << nil
end
values
end
def attribute_type
if attribute_column
column_type_to_attribute_type(attribute_column.type)
else
value_to_attribute_type(@subject.__send__(@attribute))
end
end
def attribute_allows_nil?
if attribute_column
attribute_column.null
else
true
end
end
def attribute_column
if @subject.class.respond_to?(:columns_hash)
@subject.class.columns_hash[@attribute.to_s]
end
end
def column_type_to_attribute_type(type)
case type
when :boolean, :decimal then type
when :integer, :float then :fixnum
else :default
end
end
def value_to_attribute_type(value)
case value
when true, false then :boolean
when BigDecimal then :decimal
when Fixnum then :fixnum
else :default
end
end
end
end
end
end