bureaucrat 0.0.3 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/fields_test.rb CHANGED
@@ -1,577 +1,716 @@
1
- require File.dirname(__FILE__) + "/test_helper"
1
+ require_relative 'test_helper'
2
2
 
3
- class TestFields < BureaucratTestCase
4
- describe 'Field' do
5
- describe 'with empty options' do
6
- setup do
7
- @field = Fields::Field.new
8
- end
3
+ module FieldTests
4
+ class Test_with_empty_options < BureaucratTestCase
5
+ def setup
6
+ @field = Fields::Field.new
7
+ end
9
8
 
10
- should 'be required' do
11
- blank_value = ''
12
- assert_raise(Fields::FieldValidationError) do
13
- @field.clean(blank_value)
14
- end
9
+ def test_be_required
10
+ blank_value = ''
11
+ assert_raises(ValidationError) do
12
+ @field.clean(blank_value)
15
13
  end
16
14
  end
15
+ end
17
16
 
18
- describe 'with required as false' do
19
- setup do
20
- @field = Fields::Field.new(:required => false)
21
- end
17
+ class Test_with_required_as_false < BureaucratTestCase
18
+ def setup
19
+ @field = Fields::Field.new(required: false)
20
+ end
22
21
 
23
- should 'not be required' do
24
- blank_value = ''
25
- assert_nothing_raised do
26
- @field.clean(blank_value)
27
- end
22
+ def test_not_be_required
23
+ blank_value = ''
24
+ assert_nothing_raised do
25
+ @field.clean(blank_value)
28
26
  end
29
27
  end
28
+ end
30
29
 
31
- describe 'on clean' do
32
- setup do
33
- @field = Fields::Field.new
34
- end
30
+ class Test_on_clean < BureaucratTestCase
31
+ def setup
32
+ @field = Fields::Field.new
33
+ end
35
34
 
36
- should 'return the original value if valid' do
37
- value = 'test'
38
- assert_equal(value, @field.clean(value))
39
- end
35
+ def test_return_the_original_value_if_valid
36
+ value = 'test'
37
+ assert_equal(value, @field.clean(value))
40
38
  end
39
+ end
41
40
 
42
- describe 'when copied' do
43
- setup do
44
- @field = Fields::Field.new(:initial => 'initial',
45
- :label => 'label')
46
- @field_copy = @field.dup
47
- end
41
+ class Test_when_copied < BureaucratTestCase
42
+ def setup
43
+ @field = Fields::Field.new(initial: 'initial',
44
+ label: 'label')
45
+ @field_copy = @field.dup
46
+ end
48
47
 
49
- should 'have its own copy of initial value' do
50
- assert_not_equal(@field.initial.object_id, @field_copy.initial.object_id)
51
- end
48
+ def test_have_its_own_copy_of_initial_value
49
+ assert_not_equal(@field.initial.object_id, @field_copy.initial.object_id)
50
+ end
52
51
 
53
- should 'have its own copy of the label' do
54
- assert_not_equal(@field.label.object_id, @field_copy.label.object_id)
55
- end
52
+ def test_have_its_own_copy_of_the_label
53
+ assert_not_equal(@field.label.object_id, @field_copy.label.object_id)
54
+ end
56
55
 
57
- should 'have its own copy of the error messaes' do
58
- assert_not_equal(@field.error_messages.object_id, @field_copy.error_messages.object_id)
59
- end
56
+ def test_have_its_own_copy_of_the_widget
57
+ assert_not_equal(@field.widget.object_id, @field_copy.widget.object_id)
60
58
  end
61
- end
62
59
 
63
- describe 'CharField' do
64
- describe 'with empty options' do
65
- setup do
66
- @field = Fields::CharField.new
67
- end
60
+ def test_have_its_own_copy_of_validators
61
+ assert_not_equal(@field.validators.object_id, @field_copy.validators.object_id)
62
+ end
68
63
 
69
- should 'not validate max length' do
70
- assert_nothing_raised do
71
- @field.clean("string" * 1000)
72
- end
73
- end
64
+ def test_have_its_own_copy_of_the_error_messaes
65
+ assert_not_equal(@field.error_messages.object_id, @field_copy.error_messages.object_id)
66
+ end
67
+ end
68
+ end
74
69
 
75
- should 'not validate min length' do
76
- assert_nothing_raised do
77
- @field.clean("1")
78
- end
79
- end
70
+ module CharFieldTests
71
+ class Test_with_empty_options < BureaucratTestCase
72
+ def setup
73
+ @field = Fields::CharField.new
80
74
  end
81
75
 
82
- describe 'with max length of 10' do
83
- setup do
84
- @field = Fields::CharField.new(:max_length => 10)
76
+ def test_not_validate_max_length
77
+ assert_nothing_raised do
78
+ @field.clean("string" * 1000)
85
79
  end
80
+ end
86
81
 
87
- should 'allow values with length <= 10' do
88
- assert_nothing_raised do
89
- @field.clean('a' * 10)
90
- end
82
+ def test_not_validate_min_length
83
+ assert_nothing_raised do
84
+ @field.clean("1")
91
85
  end
86
+ end
87
+ end
92
88
 
93
- should 'not allow values with length > 10' do
94
- assert_raise(Fields::FieldValidationError) do
95
- @field.clean('a' * 11)
96
- end
97
- end
89
+ class Test_with_max_length < BureaucratTestCase
90
+ def setup
91
+ @field = Fields::CharField.new(max_length: 10)
98
92
  end
99
93
 
100
- describe 'with min length of 10' do
101
- setup do
102
- @field = Fields::CharField.new(:min_length => 10)
94
+ def test_allow_values_with_length_less_than_or_equal_to_max_length
95
+ assert_nothing_raised do
96
+ @field.clean('a' * 10)
97
+ @field.clean('a' * 5)
103
98
  end
99
+ end
104
100
 
105
- should 'allow values with length >= 10' do
106
- assert_nothing_raised do
107
- @field.clean('a' * 10)
108
- end
101
+ def test_not_allow_values_with_length_greater_than_max_length
102
+ assert_raises(ValidationError) do
103
+ @field.clean('a' * 11)
109
104
  end
105
+ end
106
+ end
110
107
 
111
- should 'not allow values with length < 10' do
112
- assert_raise(Fields::FieldValidationError) do
113
- @field.clean('a' * 9)
114
- end
115
- end
108
+ class Test_with_min_length < BureaucratTestCase
109
+ def setup
110
+ @field = Fields::CharField.new(min_length: 10)
116
111
  end
117
112
 
118
- describe 'on clean' do
119
- setup do
120
- @field = Fields::CharField.new
113
+ def test_allow_values_with_length_greater_or_equal_to_min_length
114
+ assert_nothing_raised do
115
+ @field.clean('a' * 10)
116
+ @field.clean('a' * 20)
121
117
  end
118
+ end
122
119
 
123
- should 'return the original value if valid' do
124
- valid_value = 'test'
125
- assert_equal(valid_value, @field.clean(valid_value))
120
+ def test_not_allow_values_with_length_less_than_min_length
121
+ assert_raises(ValidationError) do
122
+ @field.clean('a' * 9)
126
123
  end
124
+ end
125
+ end
127
126
 
128
- should 'return a blank string if value is nil and required is false' do
129
- @field.required = false
130
- nil_value = nil
131
- assert_equal('', @field.clean(nil_value))
132
- end
127
+ class Test_on_clean < BureaucratTestCase
128
+ def setup
129
+ @field = Fields::CharField.new
130
+ end
133
131
 
134
- should 'return a blank string if value is empty and required is false' do
135
- @field.required = false
136
- empty_value = ''
137
- assert_equal('', @field.clean(empty_value))
138
- end
132
+ def test_return_the_original_value_if_valid
133
+ valid_value = 'test'
134
+ assert_equal(valid_value, @field.clean(valid_value))
139
135
  end
140
136
 
137
+ def test_return_a_blank_string_if_value_is_nil_and_required_is_false
138
+ @field.required = false
139
+ nil_value = nil
140
+ assert_equal('', @field.clean(nil_value))
141
+ end
142
+
143
+ def test_return_a_blank_string_if_value_is_empty_and_required_is_false
144
+ @field.required = false
145
+ empty_value = ''
146
+ assert_equal('', @field.clean(empty_value))
147
+ end
141
148
  end
142
149
 
143
- describe 'IntegerField' do
144
- describe 'with max value of 10' do
145
- setup do
146
- @field = Fields::IntegerField.new(:max_value => 10)
147
- end
150
+ end
148
151
 
149
- should 'allow values <= 10' do
150
- assert_nothing_raised do
151
- @field.clean('10')
152
- end
153
- end
152
+ module IntegerFieldTests
153
+ class Test_with_max_value < BureaucratTestCase
154
+ def setup
155
+ @field = Fields::IntegerField.new(max_value: 10)
156
+ end
154
157
 
155
- should 'not allow values > 10' do
156
- assert_raise(Fields::FieldValidationError) do
157
- @field.clean('11')
158
- end
158
+ def test_allow_values_less_or_equal_to_max_value
159
+ assert_nothing_raised do
160
+ @field.clean('10')
161
+ @field.clean('4')
159
162
  end
160
163
  end
161
164
 
162
- describe 'with min value of 10' do
163
- setup do
164
- @field = Fields::IntegerField.new(:min_value => 10)
165
+ def test_not_allow_values_greater_than_max_value
166
+ assert_raises(ValidationError) do
167
+ @field.clean('11')
165
168
  end
169
+ end
170
+ end
166
171
 
167
- should 'allow values >= 10' do
168
- assert_nothing_raised do
169
- @field.clean('10')
170
- end
171
- end
172
+ class Test_with_min_value < BureaucratTestCase
173
+ def setup
174
+ @field = Fields::IntegerField.new(min_value: 10)
175
+ end
172
176
 
173
- should 'not allow values < 10' do
174
- assert_raise(Fields::FieldValidationError) do
175
- @field.clean('9')
176
- end
177
+ def test_allow_values_greater_or_equal_to_min_value
178
+ assert_nothing_raised do
179
+ @field.clean('10')
180
+ @field.clean('20')
177
181
  end
178
-
179
182
  end
180
183
 
181
- describe 'on clean' do
182
- setup do
183
- @field = Fields::IntegerField.new
184
+ def test_not_allow_values_less_than_min_value
185
+ assert_raises(ValidationError) do
186
+ @field.clean('9')
184
187
  end
188
+ end
185
189
 
186
- should 'return an integer if valid' do
187
- valid_value = '123'
188
- assert_equal(123, @field.clean(valid_value))
189
- end
190
+ end
190
191
 
191
- should 'return nil if value is nil and required is false' do
192
- @field.required = false
193
- assert_nil(@field.clean(nil))
194
- end
192
+ class Test_on_clean < BureaucratTestCase
193
+ def setup
194
+ @field = Fields::IntegerField.new
195
+ end
195
196
 
196
- should 'return nil if value is empty and required is false' do
197
- @field.required = false
198
- empty_value = ''
199
- assert_nil(@field.clean(empty_value))
200
- end
197
+ def test_return_an_integer_if_valid
198
+ valid_value = '123'
199
+ assert_equal(123, @field.clean(valid_value))
200
+ end
201
201
 
202
- should 'not validate invalid formats' do
203
- invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
204
- 'joe@example.com', '___3232___323',
205
- '123.0', '123..4']
202
+ def test_return_nil_if_value_is_nil_and_required_is_false
203
+ @field.required = false
204
+ assert_nil(@field.clean(nil))
205
+ end
206
206
 
207
- invalid_formats.each do |invalid|
208
- assert_raise(Fields::FieldValidationError) do
209
- @field.clean(invalid)
210
- end
211
- end
212
- end
207
+ def test_return_nil_if_value_is_empty_and_required_is_false
208
+ @field.required = false
209
+ empty_value = ''
210
+ assert_nil(@field.clean(empty_value))
211
+ end
213
212
 
214
- should 'validate valid formats' do
215
- valid_formats = ['3', '100', '-100', '0', '-0']
213
+ def test_not_validate_invalid_formats
214
+ invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
215
+ 'joe@example.com', '___3232___323',
216
+ '123.0', '123..4']
216
217
 
217
- assert_nothing_raised do
218
- valid_formats.each do |valid|
219
- @field.clean(valid)
220
- end
218
+ invalid_formats.each do |invalid|
219
+ assert_raises(ValidationError) do
220
+ @field.clean(invalid)
221
221
  end
222
222
  end
223
+ end
224
+
225
+ def test_validate_valid_formats
226
+ valid_formats = ['3', '100', '-100', '0', '-0']
223
227
 
224
- should 'return an instance of Integer if valid' do
225
- result = @field.clean('7')
226
- assert_kind_of(Integer, result)
228
+ assert_nothing_raised do
229
+ valid_formats.each do |valid|
230
+ @field.clean(valid)
231
+ end
227
232
  end
228
233
  end
229
234
 
235
+ def test_return_an_instance_of_Integer_if_valid
236
+ result = @field.clean('7')
237
+ assert_kind_of(Integer, result)
238
+ end
230
239
  end
231
240
 
232
- describe 'FloatField' do
233
- describe 'with max value of 10.5' do
234
- setup do
235
- @field = Fields::FloatField.new(:max_value => 10.5)
236
- end
241
+ end
237
242
 
238
- should 'allow values <= 10.5' do
239
- assert_nothing_raised do
240
- @field.clean('10.5')
241
- end
242
- end
243
+ module FloatFieldTests
244
+ class Test_with_max_value < BureaucratTestCase
245
+ def setup
246
+ @field = Fields::FloatField.new(max_value: 10.5)
247
+ end
243
248
 
244
- should 'not allow values > 10.5' do
245
- assert_raise(Fields::FieldValidationError) do
246
- @field.clean('10.55')
247
- end
249
+ def test_allow_values_less_or_equal_to_max_value
250
+ assert_nothing_raised do
251
+ @field.clean('10.5')
252
+ @field.clean('5')
248
253
  end
249
254
  end
250
255
 
251
- describe 'with min value of 10.5' do
252
- setup do
253
- @field = Fields::FloatField.new(:min_value => 10.5)
256
+ def test_not_allow_values_greater_than_max_value
257
+ assert_raises(ValidationError) do
258
+ @field.clean('10.55')
254
259
  end
260
+ end
261
+ end
255
262
 
256
- should 'allow values >= 10.5' do
257
- assert_nothing_raised do
258
- @field.clean('10.5')
259
- end
260
- end
263
+ class Test_with_min_value < BureaucratTestCase
264
+ def setup
265
+ @field = Fields::FloatField.new(min_value: 10.5)
266
+ end
261
267
 
262
- should 'not allow values < 10.5' do
263
- assert_raise(Fields::FieldValidationError) do
264
- @field.clean('10.49')
265
- end
268
+ def test_allow_values_greater_or_equal_than_min_value
269
+ assert_nothing_raised do
270
+ @field.clean('10.5')
271
+ @field.clean('20.5')
266
272
  end
267
273
  end
268
274
 
269
- describe 'on clean' do
270
- setup do
271
- @field = Fields::FloatField.new
275
+ def test_not_allow_values_less_than_min_value
276
+ assert_raises(ValidationError) do
277
+ @field.clean('10.49')
272
278
  end
279
+ end
280
+ end
273
281
 
274
- should 'return nil if value is nil and required is false' do
275
- @field.required = false
276
- assert_nil(@field.clean(nil))
277
- end
282
+ class Test_on_clean < BureaucratTestCase
283
+ def setup
284
+ @field = Fields::FloatField.new
285
+ end
278
286
 
279
- should 'return nil if value is empty and required is false' do
280
- @field.required = false
281
- empty_value = ''
282
- assert_nil(@field.clean(empty_value))
283
- end
287
+ def test_return_nil_if_value_is_nil_and_required_is_false
288
+ @field.required = false
289
+ assert_nil(@field.clean(nil))
290
+ end
291
+
292
+ def test_return_nil_if_value_is_empty_and_required_is_false
293
+ @field.required = false
294
+ empty_value = ''
295
+ assert_nil(@field.clean(empty_value))
296
+ end
284
297
 
285
- should 'not validate invalid formats' do
286
- invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
287
- 'joe@example.com', '___3232___323',
288
- '123..', '123..4']
298
+ def test_not_validate_invalid_formats
299
+ invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
300
+ 'joe@example.com', '___3232___323',
301
+ '123..', '123..4']
289
302
 
290
- invalid_formats.each do |invalid|
291
- assert_raise(Fields::FieldValidationError) do
292
- @field.clean(invalid)
293
- end
303
+ invalid_formats.each do |invalid|
304
+ assert_raises(ValidationError) do
305
+ @field.clean(invalid)
294
306
  end
295
307
  end
308
+ end
296
309
 
297
- should 'validate valid formats' do
298
- valid_formats = ['3.14', "100", "1233.", ".3333", "0.434", "0.0"]
310
+ def test_validate_valid_formats
311
+ valid_formats = ['3.14', "100", "1233.", ".3333", "0.434", "0.0"]
299
312
 
300
- assert_nothing_raised do
301
- valid_formats.each do |valid|
302
- @field.clean(valid)
303
- end
313
+ assert_nothing_raised do
314
+ valid_formats.each do |valid|
315
+ @field.clean(valid)
304
316
  end
305
317
  end
318
+ end
306
319
 
307
- should 'return an instance of Float if valid' do
308
- result = @field.clean('3.14')
309
- assert_instance_of(Float, result)
310
- end
320
+ def test_return_an_instance_of_Float_if_valid
321
+ result = @field.clean('3.14')
322
+ assert_instance_of(Float, result)
311
323
  end
312
324
  end
325
+ end
313
326
 
314
- describe 'BigDecimalField' do
315
- describe 'with max value of 10.5' do
316
- setup do
317
- @field = Fields::BigDecimalField.new(:max_value => 10.5)
318
- end
327
+ module BigDecimalFieldTests
328
+ class Test_with_max_value < BureaucratTestCase
329
+ def setup
330
+ @field = Fields::BigDecimalField.new(max_value: 10.5)
331
+ end
319
332
 
320
- should 'allow values <= 10.5' do
321
- assert_nothing_raised do
322
- @field.clean('10.5')
323
- end
333
+ def test_allow_values_less_or_equal_to_max_value
334
+ assert_nothing_raised do
335
+ @field.clean('10.5')
336
+ @field.clean('5')
324
337
  end
338
+ end
325
339
 
326
- should 'not allow values > 10.5' do
327
- assert_raise(Fields::FieldValidationError) do
328
- @field.clean('10.55')
329
- end
340
+ def test_not_allow_values_greater_than_max_value
341
+ assert_raises(ValidationError) do
342
+ @field.clean('10.55')
330
343
  end
331
344
  end
345
+ end
332
346
 
333
- describe 'with min value of 10.5' do
334
- setup do
335
- @field = Fields::BigDecimalField.new(:min_value => 10.5)
336
- end
347
+ class Test_with_min_value < BureaucratTestCase
348
+ def setup
349
+ @field = Fields::BigDecimalField.new(min_value: 10.5)
350
+ end
337
351
 
338
- should 'allow values >= 10.5' do
339
- assert_nothing_raised do
340
- @field.clean('10.5')
341
- end
352
+ def test_allow_values_greater_or_equal_to_min_value
353
+ assert_nothing_raised do
354
+ @field.clean('10.5')
355
+ @field.clean('20.5')
342
356
  end
357
+ end
343
358
 
344
- should 'not allow values < 10.5' do
345
- assert_raise(Fields::FieldValidationError) do
346
- @field.clean('10.49')
347
- end
359
+ def test_not_allow_values_less_than_min_value
360
+ assert_raises(ValidationError) do
361
+ @field.clean('10.49')
348
362
  end
349
363
  end
364
+ end
350
365
 
351
- describe 'on clean' do
352
- setup do
353
- @field = Fields::BigDecimalField.new
354
- end
366
+ class Test_on_clean < BureaucratTestCase
367
+ def setup
368
+ @field = Fields::BigDecimalField.new
369
+ end
355
370
 
356
- should 'return nil if value is nil and required is false' do
357
- @field.required = false
358
- assert_nil(@field.clean(nil))
359
- end
371
+ def test_return_nil_if_value_is_nil_and_required_is_false
372
+ @field.required = false
373
+ assert_nil(@field.clean(nil))
374
+ end
360
375
 
361
- should 'return nil if value is empty and required is false' do
362
- @field.required = false
363
- empty_value = ''
364
- assert_nil(@field.clean(empty_value))
365
- end
376
+ def test_return_nil_if_value_is_empty_and_required_is_false
377
+ @field.required = false
378
+ empty_value = ''
379
+ assert_nil(@field.clean(empty_value))
380
+ end
366
381
 
367
- should 'not validate invalid formats' do
368
- invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
369
- 'joe@example.com', '___3232___323',
370
- '123..', '123..4']
382
+ def test_not_validate_invalid_formats
383
+ invalid_formats = ['a', 'hello', '23eeee', '.', 'hi323',
384
+ 'joe@example.com', '___3232___323',
385
+ '123..', '123..4']
371
386
 
372
- invalid_formats.each do |invalid|
373
- assert_raise(Fields::FieldValidationError) do
374
- @field.clean(invalid)
375
- end
387
+ invalid_formats.each do |invalid|
388
+ assert_raises(ValidationError) do
389
+ @field.clean(invalid)
376
390
  end
377
391
  end
392
+ end
378
393
 
379
- should 'validate valid formats' do
380
- valid_formats = ['3.14', "100", "1233.", ".3333", "0.434", "0.0"]
394
+ def test_validate_valid_formats
395
+ valid_formats = ['3.14', "100", "1233.", ".3333", "0.434", "0.0"]
381
396
 
382
- assert_nothing_raised do
383
- valid_formats.each do |valid|
384
- @field.clean(valid)
385
- end
397
+ assert_nothing_raised do
398
+ valid_formats.each do |valid|
399
+ @field.clean(valid)
386
400
  end
387
401
  end
402
+ end
388
403
 
389
- should 'return an instance of BigDecimal if valid' do
390
- result = @field.clean('3.14')
391
- assert_instance_of(BigDecimal, result)
392
- end
404
+ def test_return_an_instance_of_BigDecimal_if_valid
405
+ result = @field.clean('3.14')
406
+ assert_instance_of(BigDecimal, result)
393
407
  end
394
408
  end
409
+ end
395
410
 
396
- describe 'RegexField' do
397
- setup do
411
+ module RegexFieldTests
412
+ class Test_on_clean < BureaucratTestCase
413
+ def setup
398
414
  @field = Fields::RegexField.new(/ba(na){2,}/)
399
415
  end
400
416
 
401
- describe 'on clean' do
402
- should 'validate matching values' do
403
- valid_values = ['banana', 'bananananana']
404
- valid_values.each do |valid|
405
- assert_nothing_raised do
406
- @field.clean(valid)
407
- end
417
+ def test_validate_matching_values
418
+ valid_values = ['banana', 'bananananana']
419
+ valid_values.each do |valid|
420
+ assert_nothing_raised do
421
+ @field.clean(valid)
408
422
  end
409
423
  end
424
+ end
410
425
 
411
- should 'not validate non-matching values' do
412
- invalid_values = ['bana', 'spoon']
413
- assert_raise(Fields::FieldValidationError) do
414
- invalid_values.each do |invalid|
415
- @field.clean(invalid)
416
- end
426
+ def test_not_validate_non_matching_values
427
+ invalid_values = ['bana', 'spoon']
428
+ assert_raises(ValidationError) do
429
+ invalid_values.each do |invalid|
430
+ @field.clean(invalid)
417
431
  end
418
432
  end
433
+ end
419
434
 
420
- should 'return a blank string if value is empty and required is false' do
421
- @field.required = false
422
- empty_value = ''
423
- assert_equal('', @field.clean(empty_value))
424
- end
435
+ def test_return_a_blank_string_if_value_is_empty_and_required_is_false
436
+ @field.required = false
437
+ empty_value = ''
438
+ assert_equal('', @field.clean(empty_value))
425
439
  end
426
440
  end
441
+ end
427
442
 
428
- describe 'EmailField' do
429
- setup do
443
+ module EmailFieldTests
444
+ class Test_on_clean < BureaucratTestCase
445
+ def setup
430
446
  @field = Fields::EmailField.new
431
447
  end
432
448
 
433
- describe 'on clean' do
434
- should 'validate email-matching values' do
435
- valid_values = ['email@domain.com', 'email+extra@domain.com',
436
- 'email@domain.fm', 'email@domain.co.uk']
437
- valid_values.each do |valid|
438
- assert_nothing_raised do
439
- @field.clean(valid)
440
- end
449
+ def test_validate_email_matching_values
450
+ valid_values = ['email@domain.com', 'email+extra@domain.com',
451
+ 'email@domain.fm', 'email@domain.co.uk']
452
+ valid_values.each do |valid|
453
+ assert_nothing_raised do
454
+ @field.clean(valid)
441
455
  end
442
456
  end
457
+ end
443
458
 
444
- should 'not validate non-email-matching values' do
445
- invalid_values = ['banana', 'spoon', 'invalid#bla@domain.com',
446
- 'invalid@@domain.com', 'invalid@domain',
447
- 'invalid@.com']
448
- assert_raise(Fields::FieldValidationError) do
449
- invalid_values.each do |invalid|
450
- @field.clean(invalid)
451
- end
459
+ def test_not_validate_non_email_matching_values
460
+ invalid_values = ['banana', 'spoon', 'invalid@dom#ain.com',
461
+ 'invalid@@domain.com', 'invalid@domain',
462
+ 'invalid@.com']
463
+ invalid_values.each do |invalid|
464
+ assert_raises(ValidationError) do
465
+ @field.clean(invalid)
452
466
  end
453
467
  end
454
468
  end
455
469
  end
470
+ end
456
471
 
457
- describe 'BooleanField' do
458
- setup do
472
+ module BooleanFieldTests
473
+ class Test_on_clean < BureaucratTestCase
474
+ def setup
459
475
  @true_values = [1, true, 'true', '1']
460
476
  @false_values = [nil, 0, false, 'false', '0']
461
477
  @field = Fields::BooleanField.new
462
478
  end
463
479
 
464
- describe 'on clean' do
465
- should 'return true for true values' do
466
- @true_values.each do |true_value|
467
- assert_equal(true, @field.clean(true_value))
468
- end
480
+ def test_return_true_for_true_values
481
+ @true_values.each do |true_value|
482
+ assert_equal(true, @field.clean(true_value))
469
483
  end
484
+ end
470
485
 
471
- should 'return false for false values' do
472
- @field.required = false
473
- @false_values.each do |false_value|
474
- assert_equal(false, @field.clean(false_value))
475
- end
486
+ def test_return_false_for_false_values
487
+ @field.required = false
488
+ @false_values.each do |false_value|
489
+ assert_equal(false, @field.clean(false_value))
476
490
  end
491
+ end
477
492
 
478
- should 'validate on true values when required' do
479
- assert_nothing_raised do
480
- @true_values.each do |true_value|
481
- @field.clean(true_value)
482
- end
493
+ def test_validate_on_true_values_when_required
494
+ assert_nothing_raised do
495
+ @true_values.each do |true_value|
496
+ @field.clean(true_value)
483
497
  end
484
498
  end
499
+ end
485
500
 
486
- should 'not validate on false values when required' do
487
- @false_values.each do |false_value|
488
- assert_raise(Fields::FieldValidationError) do
489
- @field.clean(false_value)
490
- end
501
+ def test_not_validate_on_false_values_when_required
502
+ @false_values.each do |false_value|
503
+ assert_raises(ValidationError) do
504
+ @field.clean(false_value)
491
505
  end
492
506
  end
507
+ end
493
508
 
494
- should 'validate on false values when not required' do
495
- @field.required = false
496
- assert_nothing_raised do
497
- @false_values.each do |false_value|
498
- @field.clean(false_value)
499
- end
509
+ def test_validate_on_false_values_when_not_required
510
+ @field.required = false
511
+ assert_nothing_raised do
512
+ @false_values.each do |false_value|
513
+ @field.clean(false_value)
500
514
  end
501
515
  end
502
516
  end
503
517
  end
518
+ end
504
519
 
505
- describe 'NullBooleanField' do
506
- setup do
520
+ module NullBooleanFieldTests
521
+ class Test_on_clean < BureaucratTestCase
522
+ def setup
507
523
  @true_values = [true, 'true', '1']
508
524
  @false_values = [false, 'false', '0']
509
525
  @null_values = [nil, '', 'banana']
510
526
  @field = Fields::NullBooleanField.new
511
527
  end
512
528
 
513
- describe 'on clean' do
514
- should 'return true for true values' do
515
- @true_values.each do |true_value|
516
- assert_equal(true, @field.clean(true_value))
517
- end
529
+ def test_return_true_for_true_values
530
+ @true_values.each do |true_value|
531
+ assert_equal(true, @field.clean(true_value))
518
532
  end
533
+ end
519
534
 
520
- should 'return false for false values' do
521
- @false_values.each do |false_value|
522
- assert_equal(false, @field.clean(false_value))
523
- end
535
+ def test_return_false_for_false_values
536
+ @false_values.each do |false_value|
537
+ assert_equal(false, @field.clean(false_value))
524
538
  end
539
+ end
525
540
 
526
- should 'return nil for null values' do
527
- @null_values.each do |null_value|
528
- assert_equal(nil, @field.clean(null_value))
529
- end
541
+ def test_return_nil_for_null_values
542
+ @null_values.each do |null_value|
543
+ assert_equal(nil, @field.clean(null_value))
530
544
  end
545
+ end
531
546
 
532
- should 'validate on all values' do
533
- all_values = @true_values + @false_values + @null_values
534
- assert_nothing_raised do
535
- all_values.each do |value|
536
- @field.clean(value)
537
- end
547
+ def test_validate_on_all_values
548
+ all_values = @true_values + @false_values + @null_values
549
+ assert_nothing_raised do
550
+ all_values.each do |value|
551
+ @field.clean(value)
538
552
  end
539
553
  end
540
554
  end
541
555
  end
556
+ end
542
557
 
543
- describe 'ChoiceField' do
544
- setup do
558
+ module ChoiceFieldTests
559
+ class Test_when_copied < BureaucratTestCase
560
+ def setup
545
561
  @choices = [['tea', 'Tea'], ['milk', 'Milk']]
546
562
  @field = Fields::ChoiceField.new(@choices)
563
+ @field_copy = @field.dup
547
564
  end
548
565
 
549
- describe 'on clean' do
550
- should 'validate all values in choices list' do
551
- assert_nothing_raised do
552
- @choices.collect(&:first).each do |valid|
553
- @field.clean(valid)
554
- end
566
+ def test_have_its_own_copy_of_choices
567
+ assert_not_equal(@field.choices.object_id, @field_copy.choices.object_id)
568
+ end
569
+ end
570
+
571
+ class Test_on_clean < BureaucratTestCase
572
+ def setup
573
+ @choices = [['tea', 'Tea'], ['milk', 'Milk']]
574
+ @choices_hash = [[{ value: "able" }, "able"], [{ value: "baker" }, "Baker"]]
575
+ @field = Fields::ChoiceField.new(@choices)
576
+ @field_hash = Fields::ChoiceField.new(@choices_hash)
577
+ end
578
+
579
+ def test_validate_all_values_in_choices_list
580
+ assert_nothing_raised do
581
+ @choices.collect(&:first).each do |valid|
582
+ @field.clean(valid)
583
+ end
584
+ end
585
+ end
586
+
587
+ def test_validate_all_values_in_a_hash_choices_list
588
+ assert_nothing_raised do
589
+ @choices_hash.collect(&:first).each do |valid|
590
+ @field_hash.clean(valid[:value])
591
+ end
592
+ end
593
+ end
594
+
595
+ def test_not_validate_a_value_not_in_choices_list
596
+ assert_raises(ValidationError) do
597
+ @field.clean('not_in_choices')
598
+ end
599
+ end
600
+
601
+ def test_not_validate_a_value_not_in_a_hash_choices_list
602
+ assert_raises(ValidationError) do
603
+ @field_hash.clean('not_in_choices')
604
+ end
605
+ end
606
+
607
+ def test_return_the_original_value_if_valid
608
+ value = 'tea'
609
+ result = @field.clean(value)
610
+ assert_equal(value, result)
611
+ end
612
+
613
+ def test_return_the_original_value_if_valid_from_a_hash_choices_list
614
+ value = 'baker'
615
+ result = @field_hash.clean(value)
616
+ assert_equal(value, result)
617
+ end
618
+
619
+ def test_return_an_empty_string_if_value_is_empty_and_not_required
620
+ @field.required = false
621
+ result = @field.clean('')
622
+ assert_equal('', result)
623
+ end
624
+
625
+ def test_return_an_empty_string_if_value_is_empty_and_not_required_from_a_hash_choices_list
626
+ @field_hash.required = false
627
+ result = @field_hash.clean('')
628
+ assert_equal('', result)
629
+ end
630
+ end
631
+ end
632
+
633
+ module TypedChoiceFieldTests
634
+ class Test_on_clean < BureaucratTestCase
635
+ def setup
636
+ @choices = [[1, 'One'], [2, 'Two'], ['3', 'Three']]
637
+ to_int = lambda{|val| Integer(val)}
638
+ @field = Fields::TypedChoiceField.new(@choices,
639
+ coerce: to_int)
640
+ end
641
+
642
+ def test_validate_all_values_in_choices_list
643
+ assert_nothing_raised do
644
+ @choices.collect(&:first).each do |valid|
645
+ @field.clean(valid)
555
646
  end
556
647
  end
648
+ end
649
+
650
+ def test_not_validate_a_value_not_in_choices_list
651
+ assert_raises(ValidationError) do
652
+ @field.clean('four')
653
+ end
654
+ end
655
+
656
+ def test_return_the_original_value_if_valid
657
+ value = 1
658
+ result = @field.clean(value)
659
+ assert_equal(value, result)
660
+ end
557
661
 
558
- should 'not validate a value not in choices list' do
559
- assert_raise(Fields::FieldValidationError) do
560
- @field.clean('not_in_choices')
662
+ def test_return_a_coerced_version_of_the_original_value_if_valid_but_of_different_type
663
+ value = 2
664
+ result = @field.clean(value.to_s)
665
+ assert_equal(value, result)
666
+ end
667
+
668
+ def test_return_an_empty_string_if_value_is_empty_and_not_required
669
+ @field.required = false
670
+ result = @field.clean('')
671
+ assert_equal('', result)
672
+ end
673
+ end
674
+ end
675
+
676
+ module MultipleChoiceFieldTests
677
+ class Test_on_clean < BureaucratTestCase
678
+ def setup
679
+ @choices = [['tea', 'Tea'], ['milk', 'Milk'], ['coffee', 'Coffee']]
680
+ @field = Fields::MultipleChoiceField.new(@choices)
681
+ end
682
+
683
+ def test_validate_all_single_values_in_choices_list
684
+ assert_nothing_raised do
685
+ @choices.collect(&:first).each do |valid|
686
+ @field.clean([valid])
561
687
  end
562
688
  end
689
+ end
563
690
 
564
- should 'return the original value if valid' do
565
- value = 'tea'
566
- result = @field.clean(value)
567
- assert_equal(value, result)
691
+ def test_validate_multiple_values
692
+ values = ['tea', 'coffee']
693
+ assert_nothing_raised do
694
+ @field.clean(values)
568
695
  end
696
+ end
569
697
 
570
- should 'return an empty string if value is empty and not required' do
571
- @field.required = false
572
- result = @field.clean('')
573
- assert_equal('', result)
698
+ def test_not_validate_a_value_not_in_choices_list
699
+ assert_raises(ValidationError) do
700
+ @field.clean(['tea', 'not_in_choices'])
574
701
  end
575
702
  end
703
+
704
+ def test_return_the_original_value_if_valid
705
+ value = 'tea'
706
+ result = @field.clean([value])
707
+ assert_equal([value], result)
708
+ end
709
+
710
+ def test_return_an_empty_list_if_value_is_empty_and_not_required
711
+ @field.required = false
712
+ result = @field.clean([])
713
+ assert_equal([], result)
714
+ end
576
715
  end
577
716
  end