public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/rmm5t/shoulda.git
Further DRY'd Active Record several macros to re-use should_allow_values_for and 
should_not_allow_values_for
Ryan McGeary (author)
Mon Jul 28 21:32:25 -0700 2008
commit  063426f75110a9081eccda5607e18274c666b162
tree    ec542cd53b8ba2d1ae840563e2100f7fee30a5e6
parent  4c5768d1630bbfc522c6019eede33fb1b2e6c57f
...
32
33
34
35
36
37
38
39
40
41
 
42
43
44
...
144
145
146
 
 
147
148
149
150
151
152
 
153
154
155
156
 
 
157
158
159
...
163
164
165
 
 
 
 
 
 
166
167
168
169
170
 
 
171
172
173
 
 
174
175
 
176
177
178
...
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
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
 
260
 
 
 
261
262
263
...
274
275
276
277
278
279
280
281
282
283
 
 
284
285
286
287
288
289
 
 
290
291
292
293
294
295
 
 
296
297
298
...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
 
 
 
 
342
343
344
...
354
355
356
357
358
359
360
361
362
363
 
364
365
366
...
593
594
595
596
597
598
599
600
601
602
 
603
604
605
...
32
33
34
 
 
35
 
 
 
 
36
37
38
39
...
139
140
141
142
143
144
145
146
147
148
 
149
150
151
152
 
153
154
155
156
157
...
161
162
163
164
165
166
167
168
169
170
171
172
173
 
174
175
176
177
 
178
179
180
 
181
182
183
184
...
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
...
237
238
239
 
 
240
241
 
 
 
 
 
 
 
 
 
242
243
244
245
246
247
248
249
...
260
261
262
 
 
 
 
 
 
 
263
264
265
 
 
 
 
 
266
267
268
 
 
 
 
 
269
270
271
272
273
...
287
288
289
 
290
291
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
294
295
296
297
298
299
...
309
310
311
 
312
 
 
 
 
 
313
314
315
316
...
543
544
545
 
546
547
 
 
 
 
548
549
550
551
0
@@ -32,13 +32,8 @@ module ThoughtBot # :nodoc:
0
       def should_require_attributes(*attributes)
0
         message = get_options!(attributes, :message)
0
         message ||= /blank/
0
-        klass = model_class
0
-
0
         attributes.each do |attribute|
0
-          should "require #{attribute} to be set" do
0
-            object = klass.new
0
-            assert_bad_value(object, attribute, nil, message)
0
-          end
0
+          should_not_allow_values_for attribute, nil, :message => message, :should => "require #{attribute} to be set"
0
         end
0
       end
0
 
0
@@ -144,16 +139,19 @@ module ThoughtBot # :nodoc:
0
       # Options:
0
       # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
       #   Regexp or string.  Default = <tt>/invalid/</tt>
0
+      # * <tt>:should</tt> - an override of the default should test name.
0
+      #   Usually only used by other macros.
0
       #
0
       # Example:
0
       #   should_not_allow_values_for :isbn, "bad 1", "bad 2"
0
       #
0
       def should_not_allow_values_for(attribute, *bad_values)
0
-        message = get_options!(bad_values, :message)
0
+        message, name = get_options!(bad_values, :message, :should)
0
         message ||= /invalid/
0
         klass = model_class
0
         bad_values.each do |v|
0
-          should "not allow #{attribute} to be set to #{v.inspect}" do
0
+          name ||= "not allow #{attribute} to be set to #{v.inspect}"
0
+          should name do
0
             object = klass.new
0
             assert_bad_value(object, attribute, v, message)
0
           end
0
@@ -163,16 +161,24 @@ module ThoughtBot # :nodoc:
0
       # Ensures that the attribute can be set to the given values.
0
       # Requires an existing record
0
       #
0
+      # Options:
0
+      # * <tt>:message</tt> - value the test shouldn't find in <tt>errors.on(:attribute)</tt>.
0
+      #   Regexp or string.  Default = <tt>//</tt>
0
+      # * <tt>:should</tt> - an override of the default should test name.
0
+      #   Usually only used by other macros.
0
+      #
0
       # Example:
0
       #   should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
0
       #
0
       def should_allow_values_for(attribute, *good_values)
0
-        get_options!(good_values)
0
+        message, name = get_options!(good_values, :message, :should)
0
+        message ||= //
0
         klass = model_class
0
         good_values.each do |v|
0
-          should "allow #{attribute} to be set to #{v.inspect}" do
0
+          name ||= "allow #{attribute} to be set to #{v.inspect}"
0
+          should name do
0
             object = klass.new
0
-            assert_good_value(object, attribute, v)
0
+            assert_good_value(object, attribute, v, message)
0
           end
0
         end
0
       end
0
@@ -194,39 +200,26 @@ module ThoughtBot # :nodoc:
0
         short_message ||= /short/
0
         long_message  ||= /long/
0
 
0
-        klass = model_class
0
         min_length = range.first
0
         max_length = range.last
0
         same_length = (min_length == max_length)
0
 
0
         if min_length > 0
0
-          should "not allow #{attribute} to be less than #{min_length} chars long" do
0
-            min_value = "x" * (min_length - 1)
0
-            object = klass.new
0
-            assert_bad_value(object, attribute, min_value, short_message)
0
-          end
0
+          min_value = "x" * (min_length - 1)
0
+          should_not_allow_values_for attribute, min_value, :message => short_message, :should => "not allow #{attribute} to be less than #{min_length} chars long"
0
         end
0
 
0
         if min_length > 0
0
-          should "allow #{attribute} to be exactly #{min_length} chars long" do
0
-            min_value = "x" * min_length
0
-            object = klass.new
0
-            assert_good_value(object, attribute, min_value, short_message)
0
-          end
0
+          min_value = "x" * min_length
0
+          should_allow_values_for attribute, min_value, :message => short_message, :should => "allow #{attribute} to be exactly #{min_length} chars long"
0
         end
0
 
0
-        should "not allow #{attribute} to be more than #{max_length} chars long" do
0
-          max_value = "x" * (max_length + 1)
0
-          object = klass.new
0
-          assert_bad_value(object, attribute, max_value, long_message)
0
-        end
0
+        max_value = "x" * (max_length + 1)
0
+        should_not_allow_values_for attribute, max_value, :message => long_message, :should => "not allow #{attribute} to be more than #{max_length} chars long"
0
 
0
         unless same_length
0
-          should "allow #{attribute} to be exactly #{max_length} chars long" do
0
-            max_value = "x" * max_length
0
-            object = klass.new
0
-            assert_good_value(object, attribute, max_value, long_message)
0
-          end
0
+          max_value = "x" * max_length
0
+          should_allow_values_for attribute, max_value, :message => long_message, :should => "allow #{attribute} to be exactly #{max_length} chars long"
0
         end
0
       end
0
 
0
@@ -244,20 +237,13 @@ module ThoughtBot # :nodoc:
0
         short_message = get_options!([opts], :short_message)
0
         short_message ||= /short/
0
 
0
-        klass = model_class
0
-
0
         if min_length > 0
0
           min_value = "x" * (min_length - 1)
0
-          should "not allow #{attribute} to be less than #{min_length} chars long" do
0
-            object = klass.new
0
-            assert_bad_value(object, attribute, min_value, short_message)
0
-          end
0
-        end
0
-        should "allow #{attribute} to be at least #{min_length} chars long" do
0
-          valid_value = "x" * (min_length)
0
-          object = klass.new
0
-          assert_good_value(object, attribute, valid_value, short_message)
0
+          should_not_allow_values_for attribute, min_value, :message => short_message, :should => "not allow #{attribute} to be less than #{min_length} chars long"
0
         end
0
+
0
+        valid_value = "x" * (min_length)
0
+        should_allow_values_for attribute, valid_value, :message => short_message, :should => "allow #{attribute} to be at least #{min_length} chars long"
0
       end
0
 
0
       # Ensures that the length of the attribute is exactly a certain length
0
@@ -274,25 +260,14 @@ module ThoughtBot # :nodoc:
0
         message = get_options!([opts], :message)
0
         message ||= /wrong length/
0
 
0
-        klass = model_class
0
-
0
-        should "not allow #{attribute} to be less than #{length} chars long" do
0
-          min_value = "x" * (length - 1)
0
-          object = klass.new
0
-          assert_bad_value(object, attribute, min_value, message)
0
-        end
0
+        min_value = "x" * (length - 1)
0
+        should_not_allow_values_for attribute, min_value, :message => message, :should => "not allow #{attribute} to be less than #{length} chars long"
0
 
0
-        should "not allow #{attribute} to be greater than #{length} chars long" do
0
-          max_value = "x" * (length + 1)
0
-          object = klass.new
0
-          assert_bad_value(object, attribute, max_value, message)
0
-        end
0
+        max_value = "x" * (length + 1)
0
+        should_not_allow_values_for attribute, max_value, :message => message, :should => "not allow #{attribute} to be greater than #{length} chars long"
0
 
0
-        should "allow #{attribute} to be #{length} chars long" do
0
-          valid_value = "x" * (length)
0
-          object = klass.new
0
-          assert_good_value(object, attribute, valid_value, message)
0
-        end
0
+        valid_value = "x" * (length)
0
+        should_allow_values_for attribute, valid_value, :message => message, :should => "allow #{attribute} to be #{length} chars long"
0
       end
0
 
0
       # Ensure that the attribute is in the range specified
0
@@ -312,33 +287,13 @@ module ThoughtBot # :nodoc:
0
         low_message  ||= /included/
0
         high_message ||= /included/
0
 
0
-        klass = model_class
0
         min   = range.first
0
         max   = range.last
0
 
0
-        should "not allow #{attribute} to be less than #{min}" do
0
-          v = min - 1
0
-          object = klass.new
0
-          assert_bad_value(object, attribute, v, low_message)
0
-        end
0
-
0
-        should "allow #{attribute} to be #{min}" do
0
-          v = min
0
-          object = klass.new
0
-          assert_good_value(object, attribute, v, low_message)
0
-        end
0
-
0
-        should "not allow #{attribute} to be more than #{max}" do
0
-          v = max + 1
0
-          object = klass.new
0
-          assert_bad_value(object, attribute, v, high_message)
0
-        end
0
-
0
-        should "allow #{attribute} to be #{max}" do
0
-          v = max
0
-          object = klass.new
0
-          assert_good_value(object, attribute, v, high_message)
0
-        end
0
+        should_not_allow_values_for attribute, (min - 1), :message => low_message, :should => "not allow #{attribute} to be less than #{min}"
0
+        should_allow_values_for attribute, min, :message => low_message, :should => "allow #{attribute} to be #{min}"
0
+        should_not_allow_values_for attribute, (max + 1), :message => high_message, :should => "not allow #{attribute} to be more than #{max}"
0
+        should_allow_values_for attribute, max, :message => high_message, :should => "allow #{attribute} to be #{max}"
0
       end
0
 
0
       # Ensure that the attribute is numeric
0
@@ -354,13 +309,8 @@ module ThoughtBot # :nodoc:
0
       def should_only_allow_numeric_values_for(*attributes)
0
         message = get_options!(attributes, :message)
0
         message ||= /number/
0
-        klass = model_class
0
         attributes.each do |attribute|
0
-          attribute = attribute.to_sym
0
-          should "only allow numeric values for #{attribute}" do
0
-            object = klass.new
0
-            assert_bad_value(object, attribute, "abcd", message)
0
-          end
0
+          should_not_allow_values_for attribute, "abcd", :message => message, :should => "only allow numeric values for #{attribute}"
0
         end
0
       end
0
 
0
@@ -593,13 +543,9 @@ module ThoughtBot # :nodoc:
0
       def should_require_acceptance_of(*attributes)
0
         message = get_options!(attributes, :message)
0
         message ||= /must be accepted/
0
-        klass = model_class
0
 
0
         attributes.each do |attribute|
0
-          should "require #{attribute} to be accepted" do
0
-            object = klass.new
0
-            assert_bad_value(object, attribute, false, message)
0
-          end
0
+          should_not_allow_values_for attribute, false, :message => message, :should => "require #{attribute} to be accepted"
0
         end
0
       end
0
 

Comments