gettext_activerecord 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. data/COPYING +55 -0
  2. data/ChangeLog +4 -0
  3. data/README.rdoc +172 -0
  4. data/Rakefile +110 -0
  5. data/data/locale/bg/LC_MESSAGES/gettext_activerecord.mo +0 -0
  6. data/data/locale/bs/LC_MESSAGES/gettext_activerecord.mo +0 -0
  7. data/data/locale/ca/LC_MESSAGES/gettext_activerecord.mo +0 -0
  8. data/data/locale/cs/LC_MESSAGES/gettext_activerecord.mo +0 -0
  9. data/data/locale/de/LC_MESSAGES/gettext_activerecord.mo +0 -0
  10. data/data/locale/el/LC_MESSAGES/gettext_activerecord.mo +0 -0
  11. data/data/locale/eo/LC_MESSAGES/gettext_activerecord.mo +0 -0
  12. data/data/locale/es/LC_MESSAGES/gettext_activerecord.mo +0 -0
  13. data/data/locale/et/LC_MESSAGES/gettext_activerecord.mo +0 -0
  14. data/data/locale/fr/LC_MESSAGES/gettext_activerecord.mo +0 -0
  15. data/data/locale/hr/LC_MESSAGES/gettext_activerecord.mo +0 -0
  16. data/data/locale/hu/LC_MESSAGES/gettext_activerecord.mo +0 -0
  17. data/data/locale/it/LC_MESSAGES/gettext_activerecord.mo +0 -0
  18. data/data/locale/ja/LC_MESSAGES/gettext_activerecord.mo +0 -0
  19. data/data/locale/ko/LC_MESSAGES/gettext_activerecord.mo +0 -0
  20. data/data/locale/lv/LC_MESSAGES/gettext_activerecord.mo +0 -0
  21. data/data/locale/nb/LC_MESSAGES/gettext_activerecord.mo +0 -0
  22. data/data/locale/nl/LC_MESSAGES/gettext_activerecord.mo +0 -0
  23. data/data/locale/pt_BR/LC_MESSAGES/gettext_activerecord.mo +0 -0
  24. data/data/locale/ru/LC_MESSAGES/gettext_activerecord.mo +0 -0
  25. data/data/locale/sr/LC_MESSAGES/gettext_activerecord.mo +0 -0
  26. data/data/locale/ua/LC_MESSAGES/gettext_activerecord.mo +0 -0
  27. data/data/locale/vi/LC_MESSAGES/gettext_activerecord.mo +0 -0
  28. data/data/locale/zh/LC_MESSAGES/gettext_activerecord.mo +0 -0
  29. data/data/locale/zh_TW/LC_MESSAGES/gettext_activerecord.mo +0 -0
  30. data/lib/gettext_activerecord/base.rb +66 -0
  31. data/lib/gettext_activerecord/i18n.rb +27 -0
  32. data/lib/gettext_activerecord/migration.rb +16 -0
  33. data/lib/gettext_activerecord/parser.rb +216 -0
  34. data/lib/gettext_activerecord/schema_definitions.rb +28 -0
  35. data/lib/gettext_activerecord/tools.rb +25 -0
  36. data/lib/gettext_activerecord/validations.rb +189 -0
  37. data/lib/gettext_activerecord/version.rb +12 -0
  38. data/lib/gettext_activerecord.rb +19 -0
  39. data/po/bg/gettext_activerecord.po +115 -0
  40. data/po/bs/gettext_activerecord.po +125 -0
  41. data/po/ca/gettext_activerecord.po +116 -0
  42. data/po/cs/gettext_activerecord.po +124 -0
  43. data/po/de/gettext_activerecord.po +117 -0
  44. data/po/el/gettext_activerecord.po +115 -0
  45. data/po/eo/gettext_activerecord.po +116 -0
  46. data/po/es/gettext_activerecord.po +116 -0
  47. data/po/et/gettext_activerecord.po +116 -0
  48. data/po/fr/gettext_activerecord.po +118 -0
  49. data/po/gettext_activerecord.pot +113 -0
  50. data/po/hr/gettext_activerecord.po +125 -0
  51. data/po/hu/gettext_activerecord.po +116 -0
  52. data/po/it/gettext_activerecord.po +122 -0
  53. data/po/ja/gettext_activerecord.po +116 -0
  54. data/po/ko/gettext_activerecord.po +123 -0
  55. data/po/lv/gettext_activerecord.po +116 -0
  56. data/po/nb/gettext_activerecord.po +117 -0
  57. data/po/nl/gettext_activerecord.po +123 -0
  58. data/po/pt_BR/gettext_activerecord.po +117 -0
  59. data/po/ru/gettext_activerecord.po +117 -0
  60. data/po/sr/gettext_activerecord.po +117 -0
  61. data/po/test.rb +8 -0
  62. data/po/ua/gettext_activerecord.po +120 -0
  63. data/po/vi/gettext_activerecord.po +116 -0
  64. data/po/zh/gettext_activerecord.po +119 -0
  65. data/po/zh_TW/gettext_activerecord.po +119 -0
  66. data/replace.rb +21 -0
  67. data/sample/README.rdoc +9 -0
  68. data/sample/Rakefile +32 -0
  69. data/sample/book.rb +3 -0
  70. data/sample/config/database.yml +3 -0
  71. data/sample/data/locale/ja/LC_MESSAGES/sample_ar.mo +0 -0
  72. data/sample/db/development.sqlite3 +0 -0
  73. data/sample/db/schema.rb +5 -0
  74. data/sample/locale/ja/LC_MESSAGES/sample_ar.mo +0 -0
  75. data/sample/po/ja/sample_ar.po +29 -0
  76. data/sample/po/sample_ar.pot +29 -0
  77. data/sample/sample.rb +22 -0
  78. data/test/Rakefile +43 -0
  79. data/test/db/migrate.rb +41 -0
  80. data/test/db/sqlite.rb +8 -0
  81. data/test/helper.rb +38 -0
  82. data/test/locale/ja/LC_MESSAGES/active_record.mo +0 -0
  83. data/test/models/book.rb +3 -0
  84. data/test/models/developer.rb +5 -0
  85. data/test/models/inept_wizard.rb +3 -0
  86. data/test/models/reply.rb +40 -0
  87. data/test/models/topic.rb +68 -0
  88. data/test/models/user.rb +3 -0
  89. data/test/models/wizard.rb +5 -0
  90. data/test/po/active_record.pot +388 -0
  91. data/test/po/ja/active_record.po +387 -0
  92. data/test/test_parser.rb +95 -0
  93. data/test/test_validations.rb +2575 -0
  94. data/test/vendor/repair_helper.rb +50 -0
  95. data/test.rb +2 -0
  96. metadata +268 -0
@@ -0,0 +1,2575 @@
1
+ require 'helper'
2
+
3
+ require 'models/topic'
4
+ require 'models/reply'
5
+ require 'models/developer'
6
+ require 'models/wizard'
7
+ require 'models/inept_wizard'
8
+
9
+ # the cases/repairhelper is missing in the current AR 2.2.2 release
10
+ # try to get it or load our copy
11
+ ar_dir = $LOAD_PATH.select{|v| v =~ /gems\/activerecord-.*\/lib/}[0].sub(/lib$/, "")
12
+ if File.exist?(File.join(ar_dir,'test','cases','repair_helper.rb'))
13
+ #make the AR/test dir available, and load
14
+ $LOAD_PATH.unshift File.join(ar_dir, "test")
15
+ require 'cases/repair_helper'
16
+ else
17
+ require 'vendor/repair_helper'
18
+ end
19
+ include ActiveRecord::Testing::RepairHelper
20
+
21
+ # The following methods in Topic are used in test_conditional_validation_*
22
+ class Topic
23
+ def condition_is_true
24
+ return true
25
+ end
26
+
27
+ def condition_is_true_but_its_not
28
+ return false
29
+ end
30
+ end
31
+
32
+ class ProtectedPerson < ActiveRecord::Base
33
+ set_table_name 'people'
34
+ attr_accessor :addon
35
+ attr_protected :first_name
36
+ end
37
+
38
+ # TODO rename to MockModel
39
+ class MyModel
40
+ attr_accessor :title
41
+ def save; end
42
+ def save!; end
43
+ def update_attribute(name, value); end
44
+ def new_record?
45
+ false
46
+ end
47
+
48
+ class << self
49
+ def human_name
50
+ name
51
+ end
52
+ def human_attribute_name(name)
53
+ name
54
+ end
55
+ end
56
+
57
+ def self.self_and_descendants_from_active_record
58
+ [self]
59
+ end
60
+
61
+ include ActiveRecord::Validations
62
+
63
+ validates_presence_of :title
64
+ end
65
+
66
+ class Conjurer < IneptWizard
67
+ end
68
+
69
+ class Thaumaturgist < IneptWizard
70
+ end
71
+
72
+ class ValidationsTest < ActiveRecord::TestCase
73
+ include GetText
74
+ bindtextdomain_to(ActiveRecord, "active_record", :path => "locale")
75
+
76
+ def setup
77
+ if AR_TEST_VERSION < "2.1.0"
78
+ Topic.write_inheritable_attribute(:validate, nil)
79
+ Topic.write_inheritable_attribute(:validate_on_create, nil)
80
+ Topic.write_inheritable_attribute(:validate_on_update, nil)
81
+ else
82
+ Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
83
+ Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
84
+ Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
85
+ end
86
+ end
87
+
88
+ def test_single_attr_validation_and_error_msg
89
+ GetText.set_locale "ja_JP.UTF-8"
90
+ r = Reply.new
91
+ r.title = "There's no content!"
92
+ r.save
93
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
94
+ assert_equal "空です。", r.errors.on("content"), "A reply without content should contain an error"
95
+ assert_equal "Reply内容 空です。", r.errors.full_messages[0], "A reply without content should contain an error"
96
+ assert_equal 1, r.errors.count
97
+
98
+ GetText.set_locale "en"
99
+ r = Reply.new
100
+ r.title = "There's no content!"
101
+ r.save
102
+ assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error"
103
+ end
104
+
105
+ def test_double_attr_validation_and_error_msg
106
+ GetText.set_locale "ja_JP.UTF-8"
107
+ r = Reply.new
108
+ assert !r.save
109
+
110
+ assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid"
111
+ assert_equal "Replyタイトル 空です。", r.errors.full_messages[0]
112
+ assert_equal "空です。", r.errors.on("title")
113
+
114
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
115
+ assert_equal "Reply内容 空です。", r.errors.full_messages[1]
116
+ assert_equal "空です。", r.errors.on("content")
117
+
118
+ assert_equal 2, r.errors.count
119
+
120
+ GetText.set_locale "en"
121
+ r = Reply.new
122
+ assert !r.save
123
+
124
+ assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid"
125
+ assert_equal "Title Empty", r.errors.full_messages[0]
126
+ assert_equal "Empty", r.errors.on("title"), "A reply without title should contain an error"
127
+
128
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
129
+ assert_equal "Content Empty", r.errors.full_messages[1]
130
+ assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error"
131
+
132
+ assert_equal 2, r.errors.count
133
+ end
134
+
135
+ def test_error_on_create
136
+ GetText.set_locale "ja_JP.UTF-8"
137
+ r = Reply.new
138
+ r.title = "Wrong Create"
139
+ assert !r.save
140
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
141
+ assert_equal "Replyタイトル が不正に生成されました。", r.errors.full_messages[0]
142
+ assert_equal "が不正に生成されました。", r.errors.on("title")
143
+
144
+ GetText.set_locale "en"
145
+ r = Reply.new
146
+ r.title = "Wrong Create"
147
+ assert !r.save
148
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
149
+ assert_equal "Title is Wrong Create", r.errors.full_messages[0]
150
+ assert_equal "is Wrong Create", r.errors.on("title")
151
+ end
152
+
153
+
154
+ def test_error_on_update
155
+ GetText.set_locale "ja_JP.UTF-8"
156
+ r = Reply.new
157
+ r.title = "Bad"
158
+ r.content = "Good"
159
+ assert r.save, "First save should be successful"
160
+ r.title = "Wrong Update"
161
+ assert !r.save, "Second save should fail"
162
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
163
+ assert_equal "Replyタイトル が不正に更新されました。", r.errors.full_messages[0]
164
+ assert_equal "が不正に更新されました。", r.errors.on("title")
165
+
166
+ GetText.set_locale "en"
167
+ r = Reply.new
168
+ r.title = "Bad"
169
+ r.content = "Good"
170
+ assert r.save, "First save should be successful"
171
+
172
+ r.title = "Wrong Update"
173
+ assert !r.save, "Second save should fail"
174
+
175
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
176
+ assert_equal "Title is Wrong Update", r.errors.full_messages[0]
177
+ assert_equal "is Wrong Update", r.errors.on("title")
178
+ end
179
+
180
+ def test_invalid_record_exception
181
+ assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
182
+ assert_raises(ActiveRecord::RecordInvalid) { Reply.new.save! }
183
+
184
+ GetText.set_locale "ja_JP.UTF-8"
185
+ begin
186
+ r = Reply.new
187
+ r.save!
188
+ flunk
189
+ rescue ActiveRecord::RecordInvalid => invalid
190
+ assert_equal r, invalid.record
191
+ assert_equal "入力値が正しくありません。: Replyタイトル 空です。, Reply内容 空です。", invalid.message
192
+ end
193
+
194
+ GetText.set_locale "en"
195
+ begin
196
+ r = Reply.new
197
+ r.save!
198
+ flunk
199
+ rescue ActiveRecord::RecordInvalid => invalid
200
+ assert_equal r, invalid.record
201
+ assert_equal "Validation failed: Title Empty, Content Empty", invalid.message
202
+ end
203
+ end
204
+
205
+ def test_exception_on_create_bang_many
206
+ assert_raises(ActiveRecord::RecordInvalid) do
207
+ Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
208
+ end
209
+
210
+ GetText.set_locale "ja_JP.UTF-8"
211
+ begin
212
+ Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
213
+ rescue ActiveRecord::RecordInvalid => invalid
214
+ assert_equal "入力値が正しくありません。: Reply内容 空です。", invalid.message
215
+ end
216
+ GetText.set_locale "en"
217
+ begin
218
+ Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
219
+ rescue ActiveRecord::RecordInvalid => invalid
220
+ assert_equal "Validation failed: Content Empty", invalid.message
221
+ end
222
+ end
223
+
224
+
225
+ def test_exception_on_create_bang_with_block
226
+ assert_raises(ActiveRecord::RecordInvalid) do
227
+ Reply.create!({ "title" => "OK" }) do |r|
228
+ r.content = nil
229
+ end
230
+ end
231
+
232
+ GetText.set_locale "ja_JP.UTF-8"
233
+ begin
234
+ Reply.create!({ "title" => "OK" }) do |r|
235
+ r.content = nil
236
+ end
237
+ rescue ActiveRecord::RecordInvalid => invalid
238
+ assert_equal "入力値が正しくありません。: Reply内容 空です。", invalid.message
239
+ end
240
+
241
+ GetText.set_locale "en"
242
+ begin
243
+ Reply.create!({ "title" => "OK" }) do |r|
244
+ r.content = nil
245
+ end
246
+ rescue ActiveRecord::RecordInvalid => invalid
247
+ assert_equal "Validation failed: Content Empty", invalid.message
248
+ end
249
+ end
250
+
251
+ def test_exception_on_create_bang_many_with_block
252
+ assert_raises(ActiveRecord::RecordInvalid) do
253
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
254
+ r.content = nil
255
+ end
256
+ end
257
+ GetText.set_locale "ja_JP.UTF-8"
258
+ begin
259
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
260
+ r.content = nil
261
+ end
262
+ rescue ActiveRecord::RecordInvalid => invalid
263
+ assert_equal "入力値が正しくありません。: Reply内容 空です。", invalid.message
264
+ end
265
+
266
+ GetText.set_locale "en"
267
+ begin
268
+ Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
269
+ r.content = nil
270
+ end
271
+ rescue ActiveRecord::RecordInvalid => invalid
272
+ assert_equal "Validation failed: Content Empty", invalid.message
273
+ end
274
+ end
275
+
276
+ def test_scoped_create_without_attributes
277
+ Reply.with_scope(:create => {}) do
278
+ assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
279
+ end
280
+
281
+ GetText.set_locale "ja_JP.UTF-8"
282
+ Reply.with_scope(:create => {}) do
283
+ begin
284
+ Reply.create!
285
+ rescue ActiveRecord::RecordInvalid => invalid
286
+ assert_equal "入力値が正しくありません。: Replyタイトル 空です。, Reply内容 空です。", invalid.message
287
+ end
288
+ end
289
+
290
+ GetText.set_locale "en"
291
+ Reply.with_scope(:create => {}) do
292
+ begin
293
+ Reply.create!
294
+ rescue ActiveRecord::RecordInvalid => invalid
295
+ assert_equal "Validation failed: Title Empty, Content Empty", invalid.message
296
+ end
297
+ end
298
+ end
299
+
300
+ def test_single_error_per_attr_iteration
301
+ GetText.set_locale "ja_JP.UTF-8"
302
+ r = Reply.new
303
+ r.save
304
+
305
+ errors = []
306
+ r.errors.each { |attr, msg| errors << [attr, msg] }
307
+
308
+ assert errors.include?(["title", "空です。"])
309
+ assert errors.include?(["content", "空です。"])
310
+
311
+ GetText.set_locale "en"
312
+ r = Reply.new
313
+ r.save
314
+
315
+ errors = []
316
+ r.errors.each { |attr, msg| errors << [attr, msg] }
317
+
318
+ assert errors.include?(["title", "Empty"])
319
+ assert errors.include?(["content", "Empty"])
320
+ end
321
+
322
+ def test_multiple_errors_per_attr_iteration_with_full_error_composition
323
+ GetText.set_locale "ja_JP.UTF-8"
324
+ r = Reply.new
325
+ r.title = "Wrong Create"
326
+ r.content = "Mismatch"
327
+ r.save
328
+
329
+ errors = []
330
+ r.errors.each_full { |error| errors << error }
331
+
332
+ assert_equal "Replyタイトル が不正に生成されました。", errors[0]
333
+ assert_equal "Replyタイトル は内容がミスマッチです。", errors[1]
334
+ assert_equal 2, r.errors.count
335
+
336
+ GetText.set_locale "en"
337
+ r = Reply.new
338
+ r.title = "Wrong Create"
339
+ r.content = "Mismatch"
340
+ r.save
341
+
342
+ errors = []
343
+ r.errors.each_full { |error| errors << error }
344
+
345
+ assert_equal "Title is Wrong Create", errors[0]
346
+ assert_equal "Title is Content Mismatch", errors[1]
347
+ assert_equal 2, r.errors.count
348
+ end
349
+
350
+ def test_errors_on_base
351
+ GetText.set_locale "ja_JP.UTF-8"
352
+ r = Reply.new
353
+ r.content = "Mismatch"
354
+ r.save
355
+ r.errors.add_to_base "リプライはdignifyされてません。"
356
+
357
+ errors = []
358
+ r.errors.each_full { |error| errors << error }
359
+
360
+ assert_equal "リプライはdignifyされてません。", r.errors.on_base
361
+
362
+ assert errors.include?("Replyタイトル 空です。")
363
+ assert errors.include?("リプライはdignifyされてません。")
364
+ assert_equal 2, r.errors.count
365
+
366
+ GetText.set_locale "en"
367
+ r = Reply.new
368
+ r.content = "Mismatch"
369
+ r.save
370
+ r.errors.add_to_base "Reply is not dignifying"
371
+
372
+ errors = []
373
+ r.errors.each_full { |error| errors << error }
374
+
375
+ assert_equal "Reply is not dignifying", r.errors.on_base
376
+
377
+ assert errors.include?("Title Empty")
378
+ assert errors.include?("Reply is not dignifying")
379
+ assert_equal 2, r.errors.count
380
+ end
381
+
382
+ def test_validates_each
383
+ perform = true
384
+ hits = 0
385
+ Topic.validates_each(:title, :content, [:title, :content]) do |record, attr|
386
+ if perform
387
+ record.errors.add attr, N_('gotcha')
388
+ hits += 1
389
+ end
390
+ end
391
+
392
+ GetText.set_locale "ja_JP.UTF-8"
393
+ t = Topic.new("title" => "valid", "content" => "whatever")
394
+ assert !t.save
395
+ assert_equal 4, hits
396
+ assert_equal ["タイトル ごっちゃ", "タイトル ごっちゃ",
397
+ "内容 ごっちゃ", "内容 ごっちゃ"], t.errors.full_messages
398
+ assert_equal ["ごっちゃ", "ごっちゃ"], t.errors.on(:title)
399
+ assert_equal ["ごっちゃ", "ごっちゃ"], t.errors.on(:content)
400
+
401
+ GetText.set_locale "en"
402
+ hits = 0
403
+ t = Topic.new("title" => "valid", "content" => "whatever")
404
+ assert !t.save
405
+ assert_equal 4, hits
406
+ assert_equal ["Title gotcha", "Title gotcha",
407
+ "Content gotcha", "Content gotcha"], t.errors.full_messages
408
+
409
+ assert_equal ["gotcha", "gotcha"], t.errors.on(:title)
410
+ assert_equal ["gotcha", "gotcha"], t.errors.on(:content)
411
+
412
+ ensure
413
+ perform = false
414
+ end
415
+
416
+ =begin
417
+ #Don't need this
418
+ def test_no_title_confirmation
419
+ end
420
+ def test_title_confirmation
421
+ end
422
+ def test_terms_of_service_agreement_no_acceptance
423
+ end
424
+ =end
425
+
426
+ def test_errors_on_boundary_breaking
427
+ GetText.set_locale "ja_JP.UTF-8"
428
+ developer = Developer.new("name" => "xs")
429
+ assert !developer.save
430
+ assert_equal "開発者名は3文字以上で入力してください。", developer.errors.full_messages[0]
431
+ assert_equal "開発者名は3文字以上で入力してください。", developer.errors.on("name")
432
+
433
+ developer.name = "All too very long for this boundary, it really is"
434
+ assert !developer.save
435
+ assert_equal "開発者名は20文字以内で入力してください。", developer.errors.full_messages[0]
436
+ assert_equal "開発者名は20文字以内で入力してください。", developer.errors.on("name")
437
+
438
+ developer.name = "ちょうどぴったり12"
439
+ assert developer.save
440
+
441
+ GetText.set_locale "en"
442
+ developer = Developer.new("name" => "xs")
443
+ assert !developer.save
444
+ assert_equal "Name is too short (minimum is 3 characters)", developer.errors.full_messages[0]
445
+ assert_equal "Name is too short (minimum is 3 characters)", developer.errors.on("name")
446
+
447
+ developer.name = "All too very long for this boundary, it really is"
448
+ assert !developer.save
449
+ assert_equal "Name is too long (maximum is 20 characters)", developer.errors.full_messages[0]
450
+ assert_equal "Name is too long (maximum is 20 characters)", developer.errors.on("name")
451
+
452
+ developer.name = "Just right"
453
+ assert developer.save
454
+ end
455
+
456
+ def test_terms_of_service_agreement
457
+ GetText.set_locale "ja_JP.UTF-8"
458
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
459
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
460
+ assert !t.save
461
+ assert_equal "利用規約を受諾してください。", t.errors.full_messages[0]
462
+ assert_equal "利用規約を受諾してください。", t.errors.on(:terms_of_service)
463
+
464
+ t.terms_of_service = "1"
465
+ assert t.save
466
+
467
+ GetText.set_locale "en"
468
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
469
+ assert !t.save
470
+ assert_equal "Terms of service must be accepted", t.errors.full_messages[0]
471
+ assert_equal "Terms of service must be accepted", t.errors.on(:terms_of_service)
472
+
473
+ t.terms_of_service = "1"
474
+ assert t.save
475
+ end
476
+
477
+ def test_eula_fn
478
+ GetText.set_locale "ja_JP.UTF-8"
479
+ Topic.validates_acceptance_of(:eula, :message => N_("%{fn} must be abided"), :on => :create)
480
+
481
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
482
+ assert !t.save
483
+ assert_equal "Eulaに従ってください。", t.errors.full_messages[0]
484
+ assert_equal "Eulaに従ってください。", t.errors.on(:eula)
485
+
486
+ t.eula = "1"
487
+ assert t.save
488
+
489
+ GetText.set_locale "en"
490
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
491
+ assert !t.save
492
+ assert_equal "Eula must be abided", t.errors.full_messages[0]
493
+ assert_equal "Eula must be abided", t.errors.on(:eula)
494
+
495
+ t.eula = "1"
496
+ assert t.save
497
+ end
498
+
499
+ def test_eula
500
+ GetText.set_locale "ja_JP.UTF-8"
501
+ Topic.validates_acceptance_of(:eula, :message => N_("must be abided"), :on => :create)
502
+
503
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
504
+ assert !t.save
505
+ assert_equal "Eula に従ってください。", t.errors.full_messages[0]
506
+ assert_equal "に従ってください。", t.errors.on(:eula)
507
+
508
+ t.eula = "1"
509
+ assert t.save
510
+
511
+ GetText.set_locale "en"
512
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
513
+ assert !t.save
514
+ assert_equal "Eula must be abided", t.errors.full_messages[0]
515
+ assert_equal "must be abided", t.errors.on(:eula)
516
+
517
+ t.eula = "1"
518
+ assert t.save
519
+ end
520
+
521
+ def test_terms_of_service_agreement_with_accept_value
522
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
523
+
524
+ GetText.set_locale "ja_JP.UTF-8"
525
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
526
+ assert !t.save
527
+ assert_equal "利用規約を受諾してください。", t.errors.full_messages[0]
528
+ assert_equal "利用規約を受諾してください。", t.errors.on(:terms_of_service)
529
+
530
+ t.terms_of_service = "I agree."
531
+ assert t.save
532
+
533
+ GetText.set_locale "en"
534
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
535
+ assert !t.save
536
+ assert_equal "Terms of service must be accepted", t.errors.full_messages[0]
537
+ assert_equal "Terms of service must be accepted", t.errors.on(:terms_of_service)
538
+
539
+ t.terms_of_service = "I agree."
540
+ assert t.save
541
+ end
542
+
543
+ =begin
544
+ #Don't need this
545
+ def test_validates_acceptance_of_as_database_column
546
+ end
547
+ def test_validates_acceptance_of_with_non_existant_table
548
+ end
549
+ =end
550
+
551
+ def test_validate_presences
552
+ Topic.validates_presence_of(:title, :content)
553
+
554
+ GetText.set_locale "ja_JP.UTF-8"
555
+ t = Topic.create
556
+ assert !t.save
557
+ assert_equal "タイトルを入力してください。", t.errors.full_messages[0]
558
+ assert_equal "内容を入力してください。", t.errors.full_messages[1]
559
+ assert_equal "タイトルを入力してください。", t.errors.on(:title)
560
+ assert_equal "内容を入力してください。", t.errors.on(:content)
561
+
562
+ t.title = "something"
563
+ t.content = " "
564
+
565
+ assert !t.save
566
+ assert_equal "内容を入力してください。", t.errors.full_messages[0]
567
+ assert_equal "内容を入力してください。", t.errors.on(:content)
568
+
569
+ t.content = "like stuff"
570
+
571
+ assert t.save
572
+
573
+ GetText.set_locale "en"
574
+ t = Topic.create
575
+ assert !t.save
576
+ assert_equal "Title can't be blank", t.errors.full_messages[0]
577
+ assert_equal "Content can't be blank", t.errors.full_messages[1]
578
+ assert_equal "Title can't be blank", t.errors.on(:title)
579
+ assert_equal "Content can't be blank", t.errors.on(:content)
580
+
581
+ t.title = "something"
582
+ t.content = " "
583
+
584
+ assert !t.save
585
+ assert_equal "Content can't be blank", t.errors.full_messages[0]
586
+ assert_equal "Content can't be blank", t.errors.on(:content)
587
+
588
+ t.content = "like stuff"
589
+
590
+ assert t.save
591
+ end
592
+
593
+ def test_validate_uniqueness
594
+ Topic.validates_uniqueness_of(:title)
595
+
596
+ GetText.set_locale "ja_JP.UTF-8"
597
+ t = Topic.new("title" => "I'm unique!")
598
+ assert t.save, "Should save t as unique"
599
+
600
+ t.content = "Remaining unique"
601
+ assert t.save, "Should still save t as unique"
602
+
603
+ t2 = Topic.new("title" => "I'm unique!")
604
+ assert !t2.valid?, "Shouldn't be valid"
605
+ assert !t2.save, "Shouldn't save t2 as unique"
606
+ assert_equal "タイトルはすでに存在します。", t2.errors.full_messages[0]
607
+ assert_equal "タイトルはすでに存在します。", t2.errors.on(:title)
608
+
609
+ GetText.set_locale "en"
610
+ t = Topic.new("title" => "I'm unique2!")
611
+ assert t.save, "Should save t as unique"
612
+
613
+ t.content = "Remaining unique2"
614
+ assert t.save, "Should still save t as unique"
615
+
616
+ t2 = Topic.new("title" => "I'm unique2!")
617
+ assert !t2.valid?, "Shouldn't be valid"
618
+ assert !t2.save, "Shouldn't save t2 as unique"
619
+ assert_equal "Title has already been taken", t2.errors.full_messages[0]
620
+ assert_equal "Title has already been taken", t2.errors.on(:title)
621
+ end
622
+
623
+ =begin
624
+ #Don't need this
625
+ def test_validate_uniqueness_with_scope
626
+ end
627
+ def test_validate_uniqueness_scoped_to_defining_class
628
+ end
629
+ def test_validate_uniqueness_with_scope_array
630
+ end
631
+ def test_validate_case_insensitive_uniqueness
632
+ end
633
+ def test_validate_case_sensitive_uniqueness
634
+ end
635
+ =end
636
+
637
+ def test_validate_straight_inheritance_uniqueness
638
+ GetText.set_locale "ja_JP.UTF-8"
639
+ w1 = IneptWizard.create(:name => "I18nRincewind", :city => "I18nAnkh-Morpork")
640
+ assert w1.valid?, "Saving w1"
641
+
642
+ # Should use validation from base class (which is abstract)
643
+ w2 = IneptWizard.new(:name => "I18nRincewind", :city => "I18nQuirm")
644
+ assert !w2.valid?, "w2 shouldn't be valid"
645
+ assert w2.errors.on(:name), "Should have errors for name"
646
+ assert_equal "不器用な魔術師名はすでに存在します。", w2.errors.on(:name), "Should have uniqueness message for name"
647
+
648
+ w3 = Conjurer.new(:name => "I18nRincewind", :city => "I18nQuirm")
649
+ assert !w3.valid?, "w3 shouldn't be valid"
650
+ assert w3.errors.on(:name), "Should have errors for name"
651
+ assert_equal "手品師名はすでに存在します。", w3.errors.on(:name), "Should have uniqueness message for name"
652
+
653
+ w4 = Conjurer.create(:name => "I18nThe Amazing Bonko", :city => "I18nQuirm")
654
+ assert w4.valid?, "Saving w4"
655
+
656
+ w5 = Thaumaturgist.new(:name => "I18nThe Amazing Bonko", :city => "I18nLancre")
657
+ assert !w5.valid?, "w5 shouldn't be valid"
658
+ assert w5.errors.on(:name), "Should have errors for name"
659
+ assert_equal "奇術師名はすでに存在します。", w5.errors.on(:name), "Should have uniqueness message for name"
660
+
661
+ w6 = Thaumaturgist.new(:name => "I18nMustrum Ridcully", :city => "I18nQuirm")
662
+ assert !w6.valid?, "w6 shouldn't be valid"
663
+ assert w6.errors.on(:city), "Should have errors for city"
664
+ assert_equal "奇術師町はすでに存在します。", w6.errors.on(:city), "Should have uniqueness message for city"
665
+
666
+ GetText.set_locale "en"
667
+ w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
668
+ assert w1.valid?, "Saving w1"
669
+
670
+ # Should use validation from base class (which is abstract)
671
+ w2 = IneptWizard.new(:name => "Rincewind", :city => "Quirm")
672
+ assert !w2.valid?, "w2 shouldn't be valid"
673
+ assert w2.errors.on(:name), "Should have errors for name"
674
+ assert_equal "Name has already been taken", w2.errors.on(:name), "Should have uniqueness message for name"
675
+
676
+ w3 = Conjurer.new(:name => "Rincewind", :city => "Quirm")
677
+ assert !w3.valid?, "w3 shouldn't be valid"
678
+ assert w3.errors.on(:name), "Should have errors for name"
679
+ assert_equal "Name has already been taken", w3.errors.on(:name), "Should have uniqueness message for name"
680
+
681
+ w4 = Conjurer.create(:name => "The Amazing Bonko", :city => "Quirm")
682
+ assert w4.valid?, "Saving w4"
683
+
684
+ w5 = Thaumaturgist.new(:name => "The Amazing Bonko", :city => "Lancre")
685
+ assert !w5.valid?, "w5 shouldn't be valid"
686
+ assert w5.errors.on(:name), "Should have errors for name"
687
+ assert_equal "Name has already been taken", w5.errors.on(:name), "Should have uniqueness message for name"
688
+
689
+ w6 = Thaumaturgist.new(:name => "Mustrum Ridcully", :city => "Quirm")
690
+ assert !w6.valid?, "w6 shouldn't be valid"
691
+ assert w6.errors.on(:city), "Should have errors for city"
692
+ assert_equal "City has already been taken", w6.errors.on(:city), "Should have uniqueness message for city"
693
+ end
694
+
695
+
696
+ def test_validate_format_fn
697
+ Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => N_("%{fn} is bad data"))
698
+
699
+ GetText.set_locale "ja_JP.UTF-8"
700
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
701
+ assert !t.valid?, "Shouldn't be valid"
702
+ assert !t.save, "Shouldn't save because it's invalid"
703
+ assert_equal "タイトルは悪いデータです。", t.errors.full_messages[0]
704
+ assert_equal "タイトルは悪いデータです。", t.errors.on(:title)
705
+ assert_nil t.errors.on(:content)
706
+
707
+ GetText.set_locale "en"
708
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
709
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
710
+ assert !t.valid?, "Shouldn't be valid"
711
+ assert !t.save, "Shouldn't save because it's invalid"
712
+ assert_equal "Title is bad data", t.errors.full_messages[0]
713
+ assert_equal "Title is bad data", t.errors.on(:title)
714
+ assert_nil t.errors.on(:content)
715
+ end
716
+
717
+ def test_validate_format
718
+ Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => N_("is bad data"))
719
+
720
+ GetText.set_locale "ja_JP.UTF-8"
721
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
722
+ assert !t.valid?, "Shouldn't be valid"
723
+ assert !t.save, "Shouldn't save because it's invalid"
724
+ assert_equal "タイトル は悪いデータです。", t.errors.full_messages[0]
725
+ assert_equal "は悪いデータです。", t.errors.on(:title)
726
+ assert_nil t.errors.on(:content)
727
+
728
+ GetText.set_locale "en"
729
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
730
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
731
+ assert !t.valid?, "Shouldn't be valid"
732
+ assert !t.save, "Shouldn't save because it's invalid"
733
+ assert_equal "Title is bad data", t.errors.full_messages[0]
734
+ assert_equal "is bad data", t.errors.on(:title)
735
+ assert_nil t.errors.on(:content)
736
+
737
+ t.title = "Validation macros rule!"
738
+
739
+ assert t.save
740
+ assert_nil t.errors.on(:title)
741
+
742
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
743
+ end
744
+
745
+ =begin
746
+ # Don't need this
747
+ def test_validate_format_with_allow_blank
748
+ end
749
+ =end
750
+ def test_validate_format_numeric_fn
751
+ Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => N_("%{fn} is bad data"))
752
+
753
+ GetText.set_locale "ja_JP.UTF-8"
754
+ t = Topic.create("title" => "72x", "content" => "6789")
755
+ assert !t.valid?, "Shouldn't be valid"
756
+ assert !t.save, "Shouldn't save because it's invalid"
757
+ assert_equal "タイトルは悪いデータです。", t.errors.on(:title)
758
+ assert_nil t.errors.on(:content)
759
+
760
+ t.title = "-11"
761
+ assert !t.valid?, "Shouldn't be valid"
762
+
763
+ t.title = "03"
764
+ assert !t.valid?, "Shouldn't be valid"
765
+
766
+ t.title = "z44"
767
+ assert !t.valid?, "Shouldn't be valid"
768
+
769
+ t.title = "5v7"
770
+ assert !t.valid?, "Shouldn't be valid"
771
+
772
+ t.title = "1"
773
+
774
+ assert t.save
775
+ assert_nil t.errors.on(:title)
776
+
777
+ GetText.set_locale "en"
778
+ t = Topic.create("title" => "72x", "content" => "6789")
779
+ assert !t.valid?, "Shouldn't be valid"
780
+ assert !t.save, "Shouldn't save because it's invalid"
781
+ assert_equal "Title is bad data", t.errors.on(:title)
782
+ assert_nil t.errors.on(:content)
783
+
784
+ t.title = "-11"
785
+ assert !t.valid?, "Shouldn't be valid"
786
+
787
+ t.title = "03"
788
+ assert !t.valid?, "Shouldn't be valid"
789
+
790
+ t.title = "z44"
791
+ assert !t.valid?, "Shouldn't be valid"
792
+
793
+ t.title = "5v7"
794
+ assert !t.valid?, "Shouldn't be valid"
795
+
796
+ t.title = "1"
797
+
798
+ assert t.save
799
+ assert_nil t.errors.on(:title)
800
+ end
801
+
802
+ def test_validate_format_numeric
803
+ Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => N_("is bad data"))
804
+
805
+ GetText.set_locale "ja_JP.UTF-8"
806
+ t = Topic.create("title" => "72x", "content" => "6789")
807
+ assert !t.valid?, "Shouldn't be valid"
808
+ assert !t.save, "Shouldn't save because it's invalid"
809
+ assert_equal "は悪いデータです。", t.errors.on(:title)
810
+ assert_nil t.errors.on(:content)
811
+
812
+ t.title = "-11"
813
+ assert !t.valid?, "Shouldn't be valid"
814
+
815
+ t.title = "03"
816
+ assert !t.valid?, "Shouldn't be valid"
817
+
818
+ t.title = "z44"
819
+ assert !t.valid?, "Shouldn't be valid"
820
+
821
+ t.title = "5v7"
822
+ assert !t.valid?, "Shouldn't be valid"
823
+
824
+ t.title = "1"
825
+
826
+ assert t.save
827
+ assert_nil t.errors.on(:title)
828
+
829
+ GetText.set_locale "en"
830
+ t = Topic.create("title" => "72x", "content" => "6789")
831
+ assert !t.valid?, "Shouldn't be valid"
832
+ assert !t.save, "Shouldn't save because it's invalid"
833
+ assert_equal "is bad data", t.errors.on(:title)
834
+ assert_nil t.errors.on(:content)
835
+
836
+ t.title = "-11"
837
+ assert !t.valid?, "Shouldn't be valid"
838
+
839
+ t.title = "03"
840
+ assert !t.valid?, "Shouldn't be valid"
841
+
842
+ t.title = "z44"
843
+ assert !t.valid?, "Shouldn't be valid"
844
+
845
+ t.title = "5v7"
846
+ assert !t.valid?, "Shouldn't be valid"
847
+
848
+ t.title = "1"
849
+
850
+ assert t.save
851
+ assert_nil t.errors.on(:title)
852
+ end
853
+
854
+ def test_validate_format_with_formatted_message_fn
855
+ Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => N_("%{fn} can not be %{val}"))
856
+
857
+ GetText.set_locale "ja_JP.UTF-8"
858
+ t = Topic.create(:title => 'Invalid title')
859
+ assert_equal "Invalid titleはタイトルではありません。", t.errors.full_messages[0]
860
+ assert_equal "Invalid titleはタイトルではありません。", t.errors.on(:title)
861
+
862
+ GetText.set_locale "en"
863
+ t = Topic.create(:title => 'Invalid title')
864
+ assert_equal "Title can not be Invalid title", t.errors.full_messages[0]
865
+ assert_equal "Title can not be Invalid title", t.errors.on(:title)
866
+ end
867
+
868
+ def test_validate_format_with_formatted_message
869
+ Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => N_("can not be %{val}"))
870
+
871
+ GetText.set_locale "ja_JP.UTF-8"
872
+ t = Topic.create(:title => 'Invalid title')
873
+ assert_equal "タイトル はInvalid titleではありません。", t.errors.full_messages[0]
874
+ assert_equal "はInvalid titleではありません。", t.errors.on(:title)
875
+
876
+ GetText.set_locale "en"
877
+ t = Topic.create(:title => 'Invalid title')
878
+ assert_equal "Title can not be Invalid title", t.errors.full_messages[0]
879
+ assert_equal "can not be Invalid title", t.errors.on(:title)
880
+ end
881
+
882
+ def test_validates_inclusion_of
883
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
884
+
885
+ GetText.set_locale "ja_JP.UTF-8"
886
+ t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
887
+ assert t.valid?
888
+ t.title = "uhoh"
889
+ assert !t.valid?
890
+ assert t.errors.on(:title)
891
+ assert_equal "タイトルは一覧にありません。", t.errors.full_messages[0]
892
+ assert_equal "タイトルは一覧にありません。", t.errors.on(:title)
893
+
894
+ GetText.set_locale "en"
895
+ t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
896
+ assert t.valid?
897
+ t.title = "uhoh"
898
+ assert !t.valid?
899
+ assert t.errors.on(:title)
900
+ assert_equal "Title is not included in the list", t.errors.full_messages[0]
901
+ assert_equal "Title is not included in the list", t.errors.on(:title)
902
+ end
903
+
904
+ =begin
905
+ # Don't need this
906
+ def test_validates_inclusion_of_with_allow_nil
907
+ end
908
+ def test_numericality_with_getter_method
909
+ end
910
+ def test_validates_length_of_with_allow_nil
911
+ end
912
+ def test_validates_length_of_with_allow_blank
913
+ end
914
+ =end
915
+
916
+ def test_validates_inclusion_of_with_formatted_message_fn
917
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => N_("%{fn} option %{val} is not in the list") )
918
+ GetText.set_locale "ja_JP.UTF-8"
919
+
920
+ assert Topic.create("title" => "a", "content" => "abc").valid?
921
+
922
+ t = Topic.create("title" => "uhoh", "content" => "abc")
923
+ assert !t.valid?
924
+ assert t.errors.on(:title)
925
+ assert_equal "オプションuhohはタイトル一覧にありません。", t.errors.full_messages[0]
926
+ assert_equal "オプションuhohはタイトル一覧にありません。", t.errors["title"]
927
+
928
+ GetText.set_locale "en"
929
+ assert Topic.create("title" => "a", "content" => "abc").valid?
930
+
931
+ t = Topic.create("title" => "uhoh", "content" => "abc")
932
+ assert !t.valid?
933
+ assert t.errors.on(:title)
934
+ assert_equal "Title option uhoh is not in the list", t.errors.full_messages[0]
935
+ assert_equal "Title option uhoh is not in the list", t.errors["title"]
936
+ end
937
+
938
+ def test_validates_inclusion_of_with_formatted_message
939
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => N_("option %{val} is not in the list") )
940
+ GetText.set_locale "ja_JP.UTF-8"
941
+
942
+ assert Topic.create("title" => "a", "content" => "abc").valid?
943
+
944
+ t = Topic.create("title" => "uhoh", "content" => "abc")
945
+ assert !t.valid?
946
+ assert t.errors.on(:title)
947
+ assert_equal "タイトル オプションuhohは一覧にありません。", t.errors.full_messages[0]
948
+ assert_equal "オプションuhohは一覧にありません。", t.errors["title"]
949
+
950
+ GetText.set_locale "en"
951
+ assert Topic.create("title" => "a", "content" => "abc").valid?
952
+
953
+ t = Topic.create("title" => "uhoh", "content" => "abc")
954
+ assert !t.valid?
955
+ assert t.errors.on(:title)
956
+ assert_equal "Title option uhoh is not in the list", t.errors.full_messages[0]
957
+ assert_equal "option uhoh is not in the list", t.errors["title"]
958
+ end
959
+
960
+ =begin
961
+ Don't need this
962
+ def test_numericality_with_allow_nil_and_getter_method
963
+ end
964
+ def test_validates_exclusion_of
965
+ end
966
+ =end
967
+ def test_validates_exclusion_of_with_formatted_message_fn
968
+ GetText.set_locale "ja_JP.UTF-8"
969
+ Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => N_("%{fn} option %{val} is restricted") )
970
+
971
+ assert Topic.create("title" => "something", "content" => "abc")
972
+
973
+ t = Topic.create("title" => "monkey")
974
+ assert !t.valid?
975
+ assert t.errors.on(:title)
976
+ assert_equal "オプションタイトルmonkeyは制限されています。", t.errors.full_messages[0]
977
+ assert_equal "オプションタイトルmonkeyは制限されています。", t.errors["title"]
978
+
979
+ GetText.set_locale "en"
980
+ assert Topic.create("title" => "something", "content" => "abc")
981
+
982
+ t = Topic.create("title" => "monkey")
983
+ assert !t.valid?
984
+ assert t.errors.on(:title)
985
+ assert_equal "Title option monkey is restricted", t.errors.full_messages[0]
986
+ assert_equal "Title option monkey is restricted", t.errors["title"]
987
+ end
988
+
989
+ def test_validates_exclusion_of_with_formatted_message
990
+ GetText.set_locale "ja_JP.UTF-8"
991
+ Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => N_("option %{val} is restricted") )
992
+
993
+ assert Topic.create("title" => "something", "content" => "abc")
994
+
995
+ t = Topic.create("title" => "monkey")
996
+ assert !t.valid?
997
+ assert t.errors.on(:title)
998
+ assert_equal "タイトル オプションmonkeyは制限されています。", t.errors.full_messages[0]
999
+ assert_equal "オプションmonkeyは制限されています。", t.errors["title"]
1000
+
1001
+ GetText.set_locale "en"
1002
+ assert Topic.create("title" => "something", "content" => "abc")
1003
+
1004
+ t = Topic.create("title" => "monkey")
1005
+ assert !t.valid?
1006
+ assert t.errors.on(:title)
1007
+ assert_equal "Title option monkey is restricted", t.errors.full_messages[0]
1008
+ assert_equal "option monkey is restricted", t.errors["title"]
1009
+ end
1010
+
1011
+ def test_validates_length_of_using_minimum
1012
+ Topic.validates_length_of :title, :minimum => 5
1013
+
1014
+ GetText.set_locale "ja_JP.UTF-8"
1015
+ t = Topic.create("title" => "valid", "content" => "whatever")
1016
+ assert t.valid?
1017
+
1018
+ t.title = "not"
1019
+ assert !t.valid?
1020
+ assert t.errors.on(:title)
1021
+ assert_equal "タイトルは5文字以上で入力してください。", t.errors.full_messages[0]
1022
+ assert_equal "タイトルは5文字以上で入力してください。", t.errors.on("title")
1023
+
1024
+ GetText.set_locale "en"
1025
+ t = Topic.create("title" => "valid", "content" => "whatever")
1026
+ assert t.valid?
1027
+
1028
+ t.title = "not"
1029
+ assert !t.valid?
1030
+ assert t.errors.on(:title)
1031
+ assert_equal "Title is too short (minimum is 5 characters)", t.errors.full_messages[0]
1032
+ assert_equal "Title is too short (minimum is 5 characters)", t.errors.on("title")
1033
+ end
1034
+
1035
+ def test_validates_length_of_using_maximum
1036
+ Topic.validates_length_of :title, :maximum => 5
1037
+ GetText.set_locale "ja_JP.UTF-8"
1038
+ t = Topic.create("title" => "valid", "content" => "whatever")
1039
+ assert t.valid?
1040
+
1041
+ t.title = "notvalid"
1042
+ assert !t.valid?
1043
+ assert t.errors.on(:title)
1044
+ assert_equal "タイトルは5文字以内で入力してください。", t.errors.full_messages[0]
1045
+ assert_equal "タイトルは5文字以内で入力してください。", t.errors.on("title")
1046
+
1047
+ GetText.set_locale "en"
1048
+ t = Topic.create("title" => "valid", "content" => "whatever")
1049
+ assert t.valid?
1050
+
1051
+ t.title = "notvalid"
1052
+ assert !t.valid?
1053
+ assert t.errors.on(:title)
1054
+ assert_equal "Title is too long (maximum is 5 characters)", t.errors.full_messages[0]
1055
+ assert_equal "Title is too long (maximum is 5 characters)", t.errors.on("title")
1056
+ end
1057
+
1058
+ =begin
1059
+ # Don't need this
1060
+ def test_optionally_validates_length_of_using_minimum
1061
+ end
1062
+ =end
1063
+
1064
+ def test_validates_length_of_using_within
1065
+ Topic.validates_length_of(:title, :content, :within => 3..5)
1066
+
1067
+ GetText.set_locale "ja_JP.UTF-8"
1068
+ t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
1069
+ assert !t.valid?
1070
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.full_messages[0]
1071
+ assert_equal "内容は5文字以内で入力してください。", t.errors.full_messages[1]
1072
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.on(:title)
1073
+ assert_equal "内容は5文字以内で入力してください。", t.errors.on(:content)
1074
+
1075
+ t.title = nil
1076
+ t.content = nil
1077
+ assert !t.valid?
1078
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.full_messages[0]
1079
+ assert_equal "内容は3文字以上で入力してください。", t.errors.full_messages[1]
1080
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.on(:title)
1081
+ assert_equal "内容は3文字以上で入力してください。", t.errors.on(:content)
1082
+
1083
+ GetText.set_locale "en"
1084
+ t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
1085
+ assert !t.valid?
1086
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.full_messages[0]
1087
+ assert_equal "Content is too long (maximum is 5 characters)", t.errors.full_messages[1]
1088
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.on(:title)
1089
+ assert_equal "Content is too long (maximum is 5 characters)", t.errors.on(:content)
1090
+
1091
+ t.title = nil
1092
+ t.content = nil
1093
+ assert !t.valid?
1094
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.full_messages[0]
1095
+ assert_equal "Content is too short (minimum is 3 characters)", t.errors.full_messages[1]
1096
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.on(:title)
1097
+ assert_equal "Content is too short (minimum is 3 characters)", t.errors.on(:content)
1098
+ end
1099
+
1100
+ =begin
1101
+ # Don't need this
1102
+ def test_optionally_validates_length_of_using_within
1103
+ end
1104
+ =end
1105
+
1106
+ def test_optionally_validates_length_of_using_within_on_create_fn
1107
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_short => N_("my string(%{fn}) is too short: %d"), :too_long => N_("my string(%{fn}) is too long: %d")
1108
+
1109
+ GetText.set_locale "ja_JP.UTF-8"
1110
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
1111
+ assert !t.save
1112
+ assert t.errors.on(:title)
1113
+ assert_equal "文字列:タイトルは長すぎ: 10", t.errors.full_messages[0]
1114
+ assert_equal "文字列:タイトルは長すぎ: 10", t.errors.on(:title)
1115
+
1116
+ GetText.set_locale "en"
1117
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
1118
+ assert !t.save
1119
+ assert t.errors.on(:title)
1120
+ assert_equal "my string(Title) is too long: 10", t.errors.full_messages[0]
1121
+ assert_equal "my string(Title) is too long: 10", t.errors.on(:title)
1122
+ end
1123
+
1124
+ def test_optionally_validates_length_of_using_within_on_create
1125
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_short => N_("my string is too short: %d"), :too_long => N_("my string is too long: %d")
1126
+
1127
+ GetText.set_locale "ja_JP.UTF-8"
1128
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
1129
+ assert !t.save
1130
+ assert t.errors.on(:title)
1131
+ assert_equal "タイトル 文字列は長すぎ: 10", t.errors.full_messages[0]
1132
+ assert_equal "文字列は長すぎ: 10", t.errors.on(:title)
1133
+
1134
+ GetText.set_locale "en"
1135
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
1136
+ assert !t.save
1137
+ assert t.errors.on(:title)
1138
+ assert_equal "Title my string is too long: 10", t.errors.full_messages[0]
1139
+ assert_equal "my string is too long: 10", t.errors.on(:title)
1140
+ end
1141
+
1142
+ def test_optionally_validates_length_of_using_within_on_update_fn
1143
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => N_("my string(%{fn}) is too short: %d"), :too_long => N_("my string(%{fn}) is too long: %d")
1144
+ GetText.set_locale "ja_JP.UTF-8"
1145
+ t = Topic.create("title" => "vali", "content" => "whatever")
1146
+ assert !t.save
1147
+ assert t.errors.on(:title)
1148
+
1149
+ t.title = "not"
1150
+ assert !t.save
1151
+ assert t.errors.on(:title)
1152
+ assert_equal "文字列:タイトルは短すぎ: 5", t.errors.full_messages[0]
1153
+ assert_equal "文字列:タイトルは短すぎ: 5", t.errors.on(:title)
1154
+
1155
+ GetText.set_locale "en"
1156
+ t = Topic.create("title" => "vali", "content" => "whatever")
1157
+ assert !t.save
1158
+ assert t.errors.on(:title)
1159
+
1160
+ t.title = "not"
1161
+ assert !t.save
1162
+ assert t.errors.on(:title)
1163
+ assert_equal "my string(Title) is too short: 5", t.errors.full_messages[0]
1164
+ assert_equal "my string(Title) is too short: 5", t.errors.on(:title)
1165
+
1166
+ end
1167
+
1168
+ def test_optionally_validates_length_of_using_within_on_update
1169
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => N_("my string is too short: %d"), :too_long => N_("my string is too long: %d")
1170
+ GetText.set_locale "ja_JP.UTF-8"
1171
+ t = Topic.create("title" => "vali", "content" => "whatever")
1172
+ assert !t.save
1173
+ assert t.errors.on(:title)
1174
+
1175
+ t.title = "not"
1176
+ assert !t.save
1177
+ assert t.errors.on(:title)
1178
+ assert_equal "タイトル 文字列は短すぎ: 5", t.errors.full_messages[0]
1179
+ assert_equal "文字列は短すぎ: 5", t.errors.on(:title)
1180
+
1181
+ GetText.set_locale "en"
1182
+ t = Topic.create("title" => "vali", "content" => "whatever")
1183
+ assert !t.save
1184
+ assert t.errors.on(:title)
1185
+
1186
+ t.title = "not"
1187
+ assert !t.save
1188
+ assert t.errors.on(:title)
1189
+ assert_equal "Title my string is too short: 5", t.errors.full_messages[0]
1190
+ assert_equal "my string is too short: 5", t.errors.on(:title)
1191
+
1192
+ end
1193
+
1194
+ def test_validates_length_of_using_is
1195
+ Topic.validates_length_of :title, :is => 5
1196
+
1197
+ GetText.set_locale "ja_JP.UTF-8"
1198
+ t = Topic.create("title" => "valid", "content" => "whatever")
1199
+ assert t.valid?
1200
+
1201
+ t.title = "notvalid"
1202
+ assert !t.valid?
1203
+ assert t.errors.on(:title)
1204
+ assert_equal "タイトルは5文字で入力してください。", t.errors.full_messages[0]
1205
+ assert_equal "タイトルは5文字で入力してください。", t.errors.on("title")
1206
+
1207
+ GetText.set_locale "en"
1208
+ t = Topic.create("title" => "valid", "content" => "whatever")
1209
+ assert t.valid?
1210
+
1211
+ t.title = "notvalid"
1212
+ assert !t.valid?
1213
+ assert t.errors.on(:title)
1214
+ assert_equal "Title is the wrong length (should be 5 characters)", t.errors.full_messages[0]
1215
+ assert_equal "Title is the wrong length (should be 5 characters)", t.errors.on("title")
1216
+
1217
+ end
1218
+
1219
+ =begin
1220
+ # Don't need this
1221
+ def test_optionally_validates_length_of_using_is
1222
+ end
1223
+ def test_validates_length_of_using_bignum
1224
+ end
1225
+ =end
1226
+
1227
+ def test_validates_length_with_globaly_modified_error_message_fn
1228
+ ActiveRecord::Errors.default_error_messages[:too_short] = N_('%{fn} %d dayo')
1229
+ Topic.validates_length_of :title, :minimum => 10
1230
+
1231
+ GetText.set_locale "ja_JP.UTF-8"
1232
+ t = Topic.create(:title => 'too short')
1233
+ assert !t.valid?
1234
+ assert_equal 'タイトルは10以上だよ。', t.errors.full_messages[0]
1235
+ assert_equal 'タイトルは10以上だよ。', t.errors.on('title')
1236
+
1237
+ GetText.set_locale "en"
1238
+ t = Topic.create(:title => 'too short')
1239
+ assert !t.valid?
1240
+ assert_equal 'Title 10 dayo', t.errors.full_messages[0]
1241
+ assert_equal 'Title 10 dayo', t.errors.on('title')
1242
+ end
1243
+
1244
+ def test_validates_length_with_globaly_modified_error_message
1245
+ ActiveRecord::Errors.default_error_messages[:too_short] = N_('%d dayo')
1246
+ Topic.validates_length_of :title, :minimum => 10
1247
+
1248
+ GetText.set_locale "ja_JP.UTF-8"
1249
+ t = Topic.create(:title => 'too short')
1250
+ assert !t.valid?
1251
+ assert_equal 'タイトル は10以上だよ。', t.errors.full_messages[0]
1252
+ assert_equal 'は10以上だよ。', t.errors.on('title')
1253
+
1254
+ GetText.set_locale "en"
1255
+ t = Topic.create(:title => 'too short')
1256
+ assert !t.valid?
1257
+ assert_equal 'Title 10 dayo', t.errors.full_messages[0]
1258
+ assert_equal '10 dayo', t.errors.on('title')
1259
+ end
1260
+
1261
+ =begin
1262
+ def test_validates_size_of_association
1263
+ end
1264
+ def test_validates_size_of_association_using_within
1265
+ end
1266
+ def test_validates_length_of_nasty_params
1267
+ end
1268
+ =end
1269
+
1270
+ def test_validates_length_of_custom_errors_for_minimum_with_message_fn
1271
+ Topic.validates_length_of( :title, :minimum => 5, :message => N_("%{fn} hoo %d") )
1272
+ GetText.set_locale "ja_JP.UTF-8"
1273
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1274
+ assert !t.valid?
1275
+ assert t.errors.on(:title)
1276
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1277
+ assert_equal "タイトルふー5", t.errors.on("title")
1278
+
1279
+ GetText.set_locale "en"
1280
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1281
+ assert !t.valid?
1282
+ assert t.errors.on(:title)
1283
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1284
+ assert_equal "Title hoo 5", t.errors.on("title")
1285
+ end
1286
+
1287
+ def test_validates_length_of_custom_errors_for_minimum_with_message
1288
+ Topic.validates_length_of( :title, :minimum => 5, :message => N_("hoo %d") )
1289
+ GetText.set_locale "ja_JP.UTF-8"
1290
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1291
+ assert !t.valid?
1292
+ assert t.errors.on(:title)
1293
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1294
+ assert_equal "ふー5", t.errors.on("title")
1295
+
1296
+ GetText.set_locale "en"
1297
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1298
+ assert !t.valid?
1299
+ assert t.errors.on(:title)
1300
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1301
+ assert_equal "hoo 5", t.errors.on("title")
1302
+ end
1303
+
1304
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short_fn
1305
+ Topic.validates_length_of( :title, :minimum=>5, :too_short => N_("%{fn} hoo %d") )
1306
+
1307
+ GetText.set_locale "ja_JP.UTF-8"
1308
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1309
+ assert !t.valid?
1310
+ assert t.errors.on(:title)
1311
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1312
+ assert_equal "タイトルふー5", t.errors.on("title")
1313
+
1314
+ GetText.set_locale "en"
1315
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1316
+ assert !t.valid?
1317
+ assert t.errors.on(:title)
1318
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1319
+ assert_equal "Title hoo 5", t.errors.on("title")
1320
+ end
1321
+
1322
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short
1323
+ Topic.validates_length_of( :title, :minimum=>5, :too_short => N_("hoo %d") )
1324
+
1325
+ GetText.set_locale "ja_JP.UTF-8"
1326
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1327
+ assert !t.valid?
1328
+ assert t.errors.on(:title)
1329
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1330
+ assert_equal "ふー5", t.errors.on("title")
1331
+
1332
+ GetText.set_locale "en"
1333
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
1334
+ assert !t.valid?
1335
+ assert t.errors.on(:title)
1336
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1337
+ assert_equal "hoo 5", t.errors.on("title")
1338
+ end
1339
+
1340
+ def test_validates_length_of_custom_errors_for_maximum_with_message_fn
1341
+ Topic.validates_length_of( :title, :maximum=>5, :message => N_("%{fn} hoo %d"))
1342
+
1343
+ GetText.set_locale "ja_JP.UTF-8"
1344
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1345
+ assert !t.valid?
1346
+ assert t.errors.on(:title)
1347
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1348
+ assert_equal "タイトルふー5", t.errors.on("title")
1349
+
1350
+ GetText.set_locale "en"
1351
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1352
+ assert !t.valid?
1353
+ assert t.errors.on(:title)
1354
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1355
+ assert_equal "Title hoo 5", t.errors.on("title")
1356
+ end
1357
+
1358
+ def test_validates_length_of_custom_errors_for_maximum_with_message
1359
+ Topic.validates_length_of( :title, :maximum=>5, :message => N_("hoo %d"))
1360
+
1361
+ GetText.set_locale "ja_JP.UTF-8"
1362
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1363
+ assert !t.valid?
1364
+ assert t.errors.on(:title)
1365
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1366
+ assert_equal "ふー5", t.errors.on("title")
1367
+
1368
+ GetText.set_locale "en"
1369
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1370
+ assert !t.valid?
1371
+ assert t.errors.on(:title)
1372
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1373
+ assert_equal "hoo 5", t.errors.on("title")
1374
+ end
1375
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long_fn
1376
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("%{fn} hoo %d"))
1377
+
1378
+ GetText.set_locale "ja_JP.UTF-8"
1379
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1380
+ assert !t.valid?
1381
+ assert t.errors.on(:title)
1382
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1383
+ assert_equal "タイトルふー5", t.errors.on("title")
1384
+
1385
+ GetText.set_locale "en"
1386
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1387
+ assert !t.valid?
1388
+ assert t.errors.on(:title)
1389
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1390
+ assert_equal "Title hoo 5", t.errors.on("title")
1391
+ end
1392
+
1393
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long
1394
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("hoo %d"))
1395
+
1396
+ GetText.set_locale "ja_JP.UTF-8"
1397
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1398
+ assert !t.valid?
1399
+ assert t.errors.on(:title)
1400
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1401
+ assert_equal "ふー5", t.errors.on("title")
1402
+
1403
+ GetText.set_locale "en"
1404
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1405
+ assert !t.valid?
1406
+ assert t.errors.on(:title)
1407
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1408
+ assert_equal "hoo 5", t.errors.on("title")
1409
+ end
1410
+
1411
+ def test_validates_length_of_custom_errors_for_is_with_message_fn
1412
+ Topic.validates_length_of( :title, :is=>5, :message=> N_("%{fn} hoo %d") )
1413
+ GetText.set_locale "ja_JP.UTF-8"
1414
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1415
+ assert !t.valid?
1416
+ assert t.errors.on(:title)
1417
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1418
+ assert_equal "タイトルふー5", t.errors.on("title")
1419
+
1420
+ GetText.set_locale "en"
1421
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1422
+ assert !t.valid?
1423
+ assert t.errors.on(:title)
1424
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1425
+ assert_equal "Title hoo 5", t.errors.on("title")
1426
+ end
1427
+
1428
+ def test_validates_length_of_custom_errors_for_is_with_message
1429
+ Topic.validates_length_of( :title, :is=>5, :message=> N_("hoo %d") )
1430
+ GetText.set_locale "ja_JP.UTF-8"
1431
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1432
+ assert !t.valid?
1433
+ assert t.errors.on(:title)
1434
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1435
+ assert_equal "ふー5", t.errors.on("title")
1436
+
1437
+ GetText.set_locale "en"
1438
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1439
+ assert !t.valid?
1440
+ assert t.errors.on(:title)
1441
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1442
+ assert_equal "hoo 5", t.errors.on("title")
1443
+ end
1444
+
1445
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length_fn
1446
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=> N_("hoo %d") )
1447
+ GetText.set_locale "ja_JP.UTF-8"
1448
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1449
+ assert !t.valid?
1450
+ assert t.errors.on(:title)
1451
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1452
+ assert_equal "ふー5", t.errors.on("title")
1453
+
1454
+ GetText.set_locale "en"
1455
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1456
+ assert !t.valid?
1457
+ assert t.errors.on(:title)
1458
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1459
+ assert_equal "hoo 5", t.errors.on("title")
1460
+ end
1461
+
1462
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length
1463
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=> N_("hoo %d") )
1464
+ GetText.set_locale "ja_JP.UTF-8"
1465
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1466
+ assert !t.valid?
1467
+ assert t.errors.on(:title)
1468
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1469
+ assert_equal "ふー5", t.errors.on("title")
1470
+
1471
+ GetText.set_locale "en"
1472
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1473
+ assert !t.valid?
1474
+ assert t.errors.on(:title)
1475
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1476
+ assert_equal "hoo 5", t.errors.on("title")
1477
+ end
1478
+
1479
+ =begin
1480
+ def test_validates_length_of_using_minimum_utf8
1481
+ end
1482
+ def test_validates_length_of_using_maximum_utf8
1483
+ end
1484
+ def test_validates_length_of_using_within_utf8
1485
+ end
1486
+ def test_optionally_validates_length_of_using_within_utf8
1487
+ end
1488
+ def test_optionally_validates_length_of_using_within_on_create_utf8
1489
+ end
1490
+ def test_optionally_validates_length_of_using_within_on_update_utf8
1491
+ end
1492
+ def test_validates_length_of_using_is_utf8
1493
+ end
1494
+ def test_validates_size_of_association_utf8
1495
+ end
1496
+ def test_validates_associated_many
1497
+ end
1498
+ def test_validates_associated_one
1499
+ end
1500
+ =end
1501
+
1502
+ def test_validate_block_fn
1503
+ Topic.validate { |topic| topic.errors.add("title", N_("%{fn} will never be valid")) }
1504
+ GetText.set_locale "ja_JP.UTF-8"
1505
+ t = Topic.create("title" => "Title", "content" => "whatever")
1506
+ assert !t.valid?
1507
+ assert t.errors.on(:title)
1508
+ assert_equal "タイトルは決して正しくならないでしょう。", t.errors.full_messages[0]
1509
+ assert_equal "タイトルは決して正しくならないでしょう。", t.errors.on("title")
1510
+
1511
+ GetText.set_locale "en"
1512
+ t = Topic.create("title" => "Title", "content" => "whatever")
1513
+ assert !t.valid?
1514
+ assert t.errors.on(:title)
1515
+ assert_equal "Title will never be valid", t.errors.full_messages[0]
1516
+ assert_equal "Title will never be valid", t.errors.on("title")
1517
+ end
1518
+
1519
+ def test_validate_block
1520
+ Topic.validate { |topic| topic.errors.add("title", N_("will never be valid")) }
1521
+ GetText.set_locale "ja_JP.UTF-8"
1522
+ t = Topic.create("title" => "Title", "content" => "whatever")
1523
+ assert !t.valid?
1524
+ assert t.errors.on(:title)
1525
+ assert_equal "タイトル は決して正しくならないでしょう。", t.errors.full_messages[0]
1526
+ assert_equal "は決して正しくならないでしょう。", t.errors.on("title")
1527
+
1528
+ GetText.set_locale "en"
1529
+ t = Topic.create("title" => "Title", "content" => "whatever")
1530
+ assert !t.valid?
1531
+ assert t.errors.on(:title)
1532
+ assert_equal "Title will never be valid", t.errors.full_messages[0]
1533
+ assert_equal "will never be valid", t.errors.on("title")
1534
+ end
1535
+
1536
+ =begin
1537
+ def test_invalid_validator
1538
+ end
1539
+ def test_throw_away_typing
1540
+ end
1541
+ =end
1542
+
1543
+ def test_validates_acceptance_of_with_custom_error_using_quotes_fn
1544
+ Developer.validates_acceptance_of :salary, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1545
+
1546
+ GetText.set_locale "ja_JP.UTF-8"
1547
+ d = Developer.new
1548
+ d.salary = "0"
1549
+ assert !d.valid?
1550
+ if AR_6657
1551
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).last
1552
+ else
1553
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1554
+ end
1555
+ GetText.set_locale "en"
1556
+ d = Developer.new
1557
+ d.salary = "0"
1558
+ assert !d.valid?
1559
+ if AR_6657
1560
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).last
1561
+ else
1562
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1563
+ end
1564
+ end
1565
+
1566
+ def test_validates_acceptance_of_with_custom_error_using_quotes
1567
+ Developer.validates_acceptance_of :salary, :message=> N_("This string contains 'single' and \"double\" quotes")
1568
+
1569
+ GetText.set_locale "ja_JP.UTF-8"
1570
+ d = Developer.new
1571
+ d.salary = "0"
1572
+ assert !d.valid?
1573
+ if AR_6657
1574
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).last
1575
+ else
1576
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1577
+ end
1578
+ GetText.set_locale "en"
1579
+ d = Developer.new
1580
+ d.salary = "0"
1581
+ assert !d.valid?
1582
+ if AR_6657
1583
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
1584
+ else
1585
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1586
+ end
1587
+ end
1588
+
1589
+ def test_validates_confirmation_of_with_custom_error_using_quotes_fn
1590
+ Developer.validates_confirmation_of :name, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1591
+
1592
+ GetText.set_locale "ja_JP.UTF-8"
1593
+ d = Developer.new
1594
+ d.name = "John"
1595
+ d.name_confirmation = "Johnny"
1596
+ assert !d.valid?
1597
+ if AR_6657
1598
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1599
+ else
1600
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1601
+ end
1602
+ GetText.set_locale "en"
1603
+ d = Developer.new
1604
+ d.name = "John"
1605
+ d.name_confirmation = "Johnny"
1606
+ assert !d.valid?
1607
+ if AR_6657
1608
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).last
1609
+ else
1610
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1611
+ end
1612
+ end
1613
+
1614
+ def test_validates_confirmation_of_with_custom_error_using_quotes
1615
+ Developer.validates_confirmation_of :name, :message=> N_("This string contains 'single' and \"double\" quotes")
1616
+
1617
+ GetText.set_locale "ja_JP.UTF-8"
1618
+ d = Developer.new
1619
+ d.name = "John"
1620
+ d.name_confirmation = "Johnny"
1621
+ assert !d.valid?
1622
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name)
1623
+
1624
+ GetText.set_locale "en"
1625
+ d = Developer.new
1626
+ d.name = "John"
1627
+ d.name_confirmation = "Johnny"
1628
+ assert !d.valid?
1629
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
1630
+ end
1631
+
1632
+ def test_validates_format_of_with_custom_error_using_quotes_fn
1633
+ Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message => "%{fn} contains 'single' and \"double\" quotes"
1634
+
1635
+ GetText.set_locale "ja_JP.UTF-8"
1636
+ d = Developer.new
1637
+ d.name = "John 32"
1638
+ assert !d.valid?
1639
+ if AR_6657
1640
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1641
+ else
1642
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1643
+ end
1644
+
1645
+ GetText.set_locale "en"
1646
+ d = Developer.new
1647
+ d.name = "John 32"
1648
+ assert !d.valid?
1649
+ if AR_6657
1650
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).last
1651
+ else
1652
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1653
+ end
1654
+ end
1655
+
1656
+ def test_validates_format_of_with_custom_error_using_quotes
1657
+ Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message => "This string contains 'single' and \"double\" quotes"
1658
+
1659
+ GetText.set_locale "ja_JP.UTF-8"
1660
+ d = Developer.new
1661
+ d.name = "John 32"
1662
+ assert !d.valid?
1663
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name)
1664
+
1665
+ GetText.set_locale "en"
1666
+ d = Developer.new
1667
+ d.name = "John 32"
1668
+ assert !d.valid?
1669
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
1670
+ end
1671
+
1672
+ def test_validates_inclusion_of_with_custom_error_using_quotes_fn
1673
+ Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1674
+
1675
+ GetText.set_locale "ja_JP.UTF-8"
1676
+ d = Developer.new
1677
+ d.salary = "90,000"
1678
+ assert !d.valid?
1679
+ if AR_6657
1680
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).last
1681
+ else
1682
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1683
+ end
1684
+ GetText.set_locale "en"
1685
+ d = Developer.new
1686
+ d.salary = "90,000"
1687
+ assert !d.valid?
1688
+ if AR_6657
1689
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).last
1690
+ else
1691
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1692
+ end
1693
+ end
1694
+
1695
+ def test_validates_inclusion_of_with_custom_error_using_quotes
1696
+ Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> N_("This string contains 'single' and \"double\" quotes")
1697
+
1698
+ GetText.set_locale "ja_JP.UTF-8"
1699
+ d = Developer.new
1700
+ d.salary = "90,000"
1701
+ assert !d.valid?
1702
+ if AR_6657
1703
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).last
1704
+ else
1705
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1706
+ end
1707
+ GetText.set_locale "en"
1708
+ d = Developer.new
1709
+ d.salary = "90,000"
1710
+ assert !d.valid?
1711
+ if AR_6657
1712
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).last
1713
+ else
1714
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1715
+ end
1716
+ end
1717
+
1718
+ def test_validates_length_of_with_custom_too_long_using_quotes_fn
1719
+ Developer.validates_length_of :name, :maximum => 4, :too_long=> N_("%{fn} contains 'single' and \"double\" quotes")
1720
+
1721
+ GetText.set_locale "ja_JP.UTF-8"
1722
+ d = Developer.new
1723
+ d.name = "Jeffrey"
1724
+ assert !d.valid?
1725
+ if AR_6657
1726
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1727
+ else
1728
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1729
+ end
1730
+ GetText.set_locale "en"
1731
+ d = Developer.new
1732
+ d.name = "Jeffrey"
1733
+ assert !d.valid?
1734
+ if AR_6657
1735
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).last
1736
+ else
1737
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1738
+ end
1739
+ end
1740
+
1741
+ def test_validates_length_of_with_custom_too_long_using_quotes
1742
+ Developer.validates_length_of :name, :maximum => 4, :too_long=> N_("This string contains 'single' and \"double\" quotes")
1743
+
1744
+ GetText.set_locale "ja_JP.UTF-8"
1745
+ d = Developer.new
1746
+ d.name = "Jeffrey"
1747
+ assert !d.valid?
1748
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1749
+
1750
+ GetText.set_locale "en"
1751
+ d = Developer.new
1752
+ d.name = "Jeffrey"
1753
+ assert !d.valid?
1754
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
1755
+ end
1756
+
1757
+ def test_validates_length_of_with_custom_too_short_using_quotes_fn
1758
+ Developer.validates_length_of :name, :minimum => 4, :too_short=> N_("%{fn} contains 'single' and \"double\" quotes")
1759
+ GetText.set_locale "ja_JP.UTF-8"
1760
+ d = Developer.new
1761
+ d.name = "Joe"
1762
+ assert !d.valid?
1763
+ if AR_6657
1764
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1765
+ else
1766
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1767
+ end
1768
+ GetText.set_locale "en"
1769
+ d = Developer.new
1770
+ d.name = "Joe"
1771
+ assert !d.valid?
1772
+ if AR_6657
1773
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).last
1774
+ else
1775
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1776
+ end
1777
+ end
1778
+
1779
+ def test_validates_length_of_with_custom_too_short_using_quotes
1780
+ Developer.validates_length_of :name, :minimum => 4, :too_short=> N_("This string contains 'single' and \"double\" quotes")
1781
+ GetText.set_locale "ja_JP.UTF-8"
1782
+ d = Developer.new
1783
+ d.name = "Joe"
1784
+ assert !d.valid?
1785
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1786
+
1787
+ GetText.set_locale "en"
1788
+ d = Developer.new
1789
+ d.name = "Joe"
1790
+ assert !d.valid?
1791
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
1792
+ end
1793
+
1794
+ def test_validates_length_of_with_custom_message_using_quotes_fn
1795
+ GetText.set_locale "ja_JP.UTF-8"
1796
+ Developer.validates_length_of :name, :minimum => 4, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1797
+ d = Developer.new
1798
+ d.name = "Joe"
1799
+ assert !d.valid?
1800
+ if AR_6657
1801
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1802
+ else
1803
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1804
+ end
1805
+ GetText.set_locale "en"
1806
+ d = Developer.new
1807
+ d.name = "Joe"
1808
+ assert !d.valid?
1809
+ if AR_6657
1810
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).last
1811
+ else
1812
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1813
+ end
1814
+ end
1815
+
1816
+ def test_validates_length_of_with_custom_message_using_quotes
1817
+ GetText.set_locale "ja_JP.UTF-8"
1818
+ Developer.validates_length_of :name, :minimum => 4, :message=> N_("This string contains 'single' and \"double\" quotes")
1819
+ d = Developer.new
1820
+ d.name = "Joe"
1821
+ assert !d.valid?
1822
+ if AR_6657
1823
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).last
1824
+ else
1825
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1826
+ end
1827
+ GetText.set_locale "en"
1828
+ d = Developer.new
1829
+ d.name = "Joe"
1830
+ assert !d.valid?
1831
+ if AR_6657
1832
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).last
1833
+ else
1834
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first
1835
+ end
1836
+ end
1837
+
1838
+ def test_validates_presence_of_with_custom_message_using_quotes_fn
1839
+ # This test depends on test_validates_presence_of_with_custom_message_using_quotes
1840
+ GetText.set_locale "ja_JP.UTF-8"
1841
+ Developer.validates_presence_of :non_existent, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1842
+ d = Developer.new
1843
+ d.name = "Joe"
1844
+ assert !d.valid?
1845
+
1846
+ if AR_6657
1847
+ assert_equal "存在しないは'シングル' \"ダブル\"クオートを含む。", d.errors.on(:non_existent).last
1848
+ else
1849
+ assert_equal "存在しないは'シングル' \"ダブル\"クオートを含む。", d.errors.on(:non_existent).first
1850
+ end
1851
+ GetText.set_locale "en"
1852
+ d = Developer.new
1853
+ d.name = "Joe"
1854
+ assert !d.valid?
1855
+ if AR_6657
1856
+ assert_equal "Non existent contains 'single' and \"double\" quotes", d.errors.on(:non_existent).last
1857
+ else
1858
+ assert_equal "Non existent contains 'single' and \"double\" quotes", d.errors.on(:non_existent).first
1859
+ end
1860
+ end
1861
+
1862
+ def test_validates_presence_of_with_custom_message_using_quotes
1863
+ GetText.set_locale "ja_JP.UTF-8"
1864
+ Developer.validates_presence_of :non_existent, :message=> N_("This string contains 'single' and \"double\" quotes")
1865
+ d = Developer.new
1866
+ d.name = "Joe"
1867
+ assert !d.valid?
1868
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:non_existent)
1869
+ GetText.set_locale "en"
1870
+ d = Developer.new
1871
+ d.name = "Joe"
1872
+ assert !d.valid?
1873
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent)
1874
+ end
1875
+
1876
+ def test_validates_uniqueness_of_with_custom_message_using_quotes_fn
1877
+ Developer.validates_uniqueness_of :name, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1878
+
1879
+ GetText.set_locale "ja_JP.UTF-8"
1880
+ d = Developer.new
1881
+ d.name = "David"
1882
+ assert !d.valid?
1883
+ if AR_6657
1884
+ assert_equal d.errors.on(:name).last, "開発者名は'シングル' \"ダブル\"クオートを含む。"
1885
+ else
1886
+ assert_equal d.errors.on(:name).first, "開発者名は'シングル' \"ダブル\"クオートを含む。"
1887
+ end
1888
+
1889
+ GetText.set_locale "en"
1890
+ d = Developer.new
1891
+ d.name = "David"
1892
+ assert !d.valid?
1893
+ if AR_6657
1894
+ assert_equal d.errors.on(:name).last, "Name contains 'single' and \"double\" quotes"
1895
+ else
1896
+ assert_equal d.errors.on(:name).first, "Name contains 'single' and \"double\" quotes"
1897
+ end
1898
+ end
1899
+
1900
+ def test_validates_uniqueness_of_with_custom_message_using_quotes
1901
+ Developer.validates_uniqueness_of :name, :message=> N_("This string contains 'single' and \"double\" quotes")
1902
+
1903
+ GetText.set_locale "ja_JP.UTF-8"
1904
+ d = Developer.new
1905
+ d.name = "David"
1906
+ assert !d.valid?
1907
+ if AR_6657
1908
+ assert_equal d.errors.on(:name).first, "この文字列は'シングル' \"ダブル\"クオートを含む。"
1909
+ else
1910
+ assert_equal d.errors.on(:name).last, "この文字列は'シングル' \"ダブル\"クオートを含む。"
1911
+ end
1912
+ GetText.set_locale "en"
1913
+ d = Developer.new
1914
+ d.name = "David"
1915
+ assert !d.valid?
1916
+ if AR_6657
1917
+ assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes"
1918
+ else
1919
+ assert_equal d.errors.on(:name).last, "This string contains 'single' and \"double\" quotes"
1920
+ end
1921
+ end
1922
+
1923
+ def test_validates_associated_with_custom_message_using_quotes_fn
1924
+ Reply.validates_associated :topic, :message => N_("%{fn} contains 'single' and \"double\" quotes")
1925
+ Topic.validates_presence_of :content
1926
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1927
+ r.topic = Topic.create("title" => "uhohuhoh")
1928
+ assert !r.valid?
1929
+ assert r.errors.on(:topic)
1930
+ r.topic.content = "non-empty"
1931
+ assert r.valid?
1932
+
1933
+ GetText.set_locale "ja_JP.UTF-8"
1934
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1935
+ r.topic = Topic.create("title" => "uhohuhoh")
1936
+ assert !r.valid?
1937
+ if AR_6657
1938
+ assert_equal "Replyトピックは'シングル' \"ダブル\"クオートを含む。", r.errors.on(:topic).last
1939
+ else
1940
+ assert_equal "Replyトピックは'シングル' \"ダブル\"クオートを含む。", r.errors.on(:topic).first
1941
+ end
1942
+
1943
+ GetText.set_locale "en"
1944
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1945
+ r.topic = Topic.create("title" => "uhohuhoh")
1946
+ assert !r.valid?
1947
+ if AR_6657
1948
+ assert_equal "Topic contains 'single' and \"double\" quotes", r.errors.on(:topic).last
1949
+ else
1950
+ assert_equal "Topic contains 'single' and \"double\" quotes", r.errors.on(:topic).first
1951
+ end
1952
+ end
1953
+
1954
+ def test_validates_associated_with_custom_message_using_quotes
1955
+ Reply.validates_associated :topic, :message => N_("This string contains 'single' and \"double\" quotes")
1956
+ Topic.validates_presence_of :content
1957
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1958
+ r.topic = Topic.create("title" => "uhohuhoh")
1959
+ assert !r.valid?
1960
+ assert r.errors.on(:topic)
1961
+ r.topic.content = "non-empty"
1962
+ assert r.valid?
1963
+
1964
+ GetText.set_locale "ja_JP.UTF-8"
1965
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1966
+ r.topic = Topic.create("title" => "uhohuhoh")
1967
+ assert !r.valid?
1968
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", r.errors.on(:topic)
1969
+
1970
+ GetText.set_locale "en"
1971
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1972
+ r.topic = Topic.create("title" => "uhohuhoh")
1973
+ assert !r.valid?
1974
+ assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic)
1975
+ end
1976
+
1977
+ =begin
1978
+ def test_if_validation_using_method_true
1979
+ end
1980
+ def test_unless_validation_using_method_true
1981
+ end
1982
+ def test_if_validation_using_method_false
1983
+ end
1984
+ =end
1985
+ def test_unless_validation_using_method_false_fn
1986
+ # When the method returns false
1987
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("%{fn} hoo %d"), :unless => :condition_is_true_but_its_not )
1988
+
1989
+ GetText.set_locale "ja_JP.UTF-8"
1990
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1991
+ assert !t.valid?
1992
+ assert t.errors.on(:title)
1993
+
1994
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1995
+ assert_equal "タイトルふー5", t.errors["title"]
1996
+
1997
+ GetText.set_locale "en"
1998
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1999
+ assert !t.valid?
2000
+ assert t.errors.on(:title)
2001
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2002
+ assert_equal "Title hoo 5", t.errors["title"]
2003
+ end
2004
+
2005
+ def test_unless_validation_using_method_false
2006
+ # When the method returns false
2007
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("hoo %d"), :unless => :condition_is_true_but_its_not )
2008
+
2009
+ GetText.set_locale "ja_JP.UTF-8"
2010
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2011
+ assert !t.valid?
2012
+ assert t.errors.on(:title)
2013
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2014
+ assert_equal "ふー5", t.errors["title"]
2015
+
2016
+ GetText.set_locale "en"
2017
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2018
+ assert !t.valid?
2019
+ assert t.errors.on(:title)
2020
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2021
+ assert_equal "hoo 5", t.errors["title"]
2022
+ end
2023
+
2024
+ def test_if_validation_using_string_true_fn
2025
+ # When the evaluated string returns true
2026
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("%{fn} hoo %d"), :if => "a = 1; a == 1" )
2027
+
2028
+ GetText.set_locale "ja_JP.UTF-8"
2029
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2030
+ assert !t.valid?
2031
+ assert t.errors.on(:title)
2032
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2033
+ assert_equal "タイトルふー5", t.errors["title"]
2034
+
2035
+ GetText.set_locale "en"
2036
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2037
+ assert !t.valid?
2038
+ assert t.errors.on(:title)
2039
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2040
+ assert_equal "Title hoo 5", t.errors["title"]
2041
+ end
2042
+
2043
+ def test_if_validation_using_string_true
2044
+ # When the evaluated string returns true
2045
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("hoo %d"), :if => "a = 1; a == 1" )
2046
+
2047
+ GetText.set_locale "ja_JP.UTF-8"
2048
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2049
+ assert !t.valid?
2050
+ assert t.errors.on(:title)
2051
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2052
+ assert_equal "ふー5", t.errors["title"]
2053
+
2054
+ GetText.set_locale "en"
2055
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2056
+ assert !t.valid?
2057
+ assert t.errors.on(:title)
2058
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2059
+ assert_equal "hoo 5", t.errors["title"]
2060
+ end
2061
+
2062
+ =begin
2063
+ def test_unless_validation_using_string_true
2064
+ end
2065
+ def test_if_validation_using_string_false
2066
+ end
2067
+ =end
2068
+ def test_unless_validation_using_string_false
2069
+ # When the evaluated string returns false
2070
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("hoo %d"), :unless => "false")
2071
+
2072
+ GetText.set_locale "ja_JP.UTF-8"
2073
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2074
+ assert !t.valid?
2075
+ assert t.errors.on(:title)
2076
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2077
+ assert_equal "ふー5", t.errors["title"]
2078
+
2079
+ GetText.set_locale "en"
2080
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2081
+ assert !t.valid?
2082
+ assert t.errors.on(:title)
2083
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2084
+ assert_equal "hoo 5", t.errors["title"]
2085
+ end
2086
+
2087
+ def test_if_validation_using_block_true_fn
2088
+ # When the block returns true
2089
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("%{fn} hoo %d"),
2090
+ :if => Proc.new { |r| r.content.size > 4 } )
2091
+
2092
+ GetText.set_locale "ja_JP.UTF-8"
2093
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2094
+ assert !t.valid?
2095
+ assert t.errors.on(:title)
2096
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2097
+ assert_equal "タイトルふー5", t.errors["title"]
2098
+
2099
+ GetText.set_locale "en"
2100
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2101
+ assert !t.valid?
2102
+ assert t.errors.on(:title)
2103
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2104
+ assert_equal "Title hoo 5", t.errors["title"]
2105
+ end
2106
+
2107
+ def test_if_validation_using_block_true
2108
+ # When the block returns true
2109
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("hoo %d"),
2110
+ :if => Proc.new { |r| r.content.size > 4 } )
2111
+
2112
+ GetText.set_locale "ja_JP.UTF-8"
2113
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2114
+ assert !t.valid?
2115
+ assert t.errors.on(:title)
2116
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2117
+ assert_equal "ふー5", t.errors["title"]
2118
+
2119
+ GetText.set_locale "en"
2120
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2121
+ assert !t.valid?
2122
+ assert t.errors.on(:title)
2123
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2124
+ assert_equal "hoo 5", t.errors["title"]
2125
+ end
2126
+
2127
+ def test_conditional_validation_using_method_true_fn
2128
+ # When the method returns true
2129
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("%{fn} hoo %d"), :if => :condition_is_true )
2130
+ GetText.set_locale "ja_JP.UTF-8"
2131
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2132
+ assert !t.valid?
2133
+ assert t.errors.on(:title)
2134
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2135
+ assert_equal "タイトルふー5", t.errors.on("title")
2136
+
2137
+ GetText.set_locale "en"
2138
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2139
+ assert !t.valid?
2140
+ assert t.errors.on(:title)
2141
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2142
+ assert_equal "Title hoo 5", t.errors.on("title")
2143
+ end
2144
+
2145
+ =begin
2146
+ Don't need this
2147
+ def test_unless_validation_using_block_true
2148
+ end
2149
+ def test_if_validation_using_block_false
2150
+ end
2151
+ =end
2152
+
2153
+ def test_unless_validation_using_block_false_fn
2154
+ # When the block returns false
2155
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("%{fn} hoo %d"),
2156
+ :unless => Proc.new { |r| r.title != "uhohuhoh"} )
2157
+
2158
+ GetText.set_locale "ja_JP.UTF-8"
2159
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2160
+ assert !t.valid?
2161
+ assert t.errors.on(:title)
2162
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2163
+ assert_equal "タイトルふー5", t.errors.on("title")
2164
+
2165
+ GetText.set_locale "en"
2166
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2167
+ assert !t.valid?
2168
+ assert t.errors.on(:title)
2169
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2170
+ assert_equal "Title hoo 5", t.errors["title"]
2171
+ end
2172
+
2173
+ def test_unless_validation_using_block_false
2174
+ # When the block returns false
2175
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>N_("hoo %d"),
2176
+ :unless => Proc.new { |r| r.title != "uhohuhoh"} )
2177
+
2178
+ GetText.set_locale "ja_JP.UTF-8"
2179
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2180
+ assert !t.valid?
2181
+ assert t.errors.on(:title)
2182
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2183
+ assert_equal "ふー5", t.errors.on("title")
2184
+
2185
+ GetText.set_locale "en"
2186
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2187
+ assert !t.valid?
2188
+ assert t.errors.on(:title)
2189
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2190
+ assert_equal "hoo 5", t.errors["title"]
2191
+ end
2192
+
2193
+ def test_conditional_validation_using_method_true
2194
+ # When the method returns true
2195
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("hoo %d"), :if => :condition_is_true )
2196
+ GetText.set_locale "ja_JP.UTF-8"
2197
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2198
+ assert !t.valid?
2199
+ assert t.errors.on(:title)
2200
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2201
+ assert_equal "ふー5", t.errors.on("title")
2202
+
2203
+ GetText.set_locale "en"
2204
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2205
+ assert !t.valid?
2206
+ assert t.errors.on(:title)
2207
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2208
+ assert_equal "hoo 5", t.errors.on("title")
2209
+ end
2210
+
2211
+ def test_conditional_validation_using_string_true_fn
2212
+ # When the evaluated string returns true
2213
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("%{fn} hoo %d"), :if => "a = 1; a == 1" )
2214
+
2215
+ GetText.set_locale "ja_JP.UTF-8"
2216
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2217
+ assert !t.valid?
2218
+ assert t.errors.on(:title)
2219
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2220
+ assert_equal "タイトルふー5", t.errors.on("title")
2221
+
2222
+ GetText.set_locale "en"
2223
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2224
+ assert !t.valid?
2225
+ assert t.errors.on(:title)
2226
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2227
+ assert_equal "Title hoo 5", t.errors.on("title")
2228
+ end
2229
+
2230
+ def test_conditional_validation_using_string_true
2231
+ # When the evaluated string returns true
2232
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("hoo %d"), :if => "a = 1; a == 1" )
2233
+
2234
+ GetText.set_locale "ja_JP.UTF-8"
2235
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2236
+ assert !t.valid?
2237
+ assert t.errors.on(:title)
2238
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2239
+ assert_equal "ふー5", t.errors.on("title")
2240
+
2241
+ GetText.set_locale "en"
2242
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2243
+ assert !t.valid?
2244
+ assert t.errors.on(:title)
2245
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2246
+ assert_equal "hoo 5", t.errors.on("title")
2247
+ end
2248
+
2249
+ def test_conditional_validation_using_block_true_fn
2250
+ # When the block returns true
2251
+ GetText.set_locale "ja_JP.UTF-8"
2252
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("%{fn} hoo %d"),
2253
+ :if => Proc.new { |r| r.content.size > 4 } )
2254
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2255
+ assert !t.valid?
2256
+ assert t.errors.on(:title)
2257
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
2258
+ assert_equal "タイトルふー5", t.errors.on("title")
2259
+
2260
+ GetText.set_locale "en"
2261
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2262
+ assert !t.valid?
2263
+ assert t.errors.on(:title)
2264
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2265
+ assert_equal "Title hoo 5", t.errors.on("title")
2266
+
2267
+ end
2268
+
2269
+ def test_conditional_validation_using_block_true
2270
+ # When the block returns true
2271
+ GetText.set_locale "ja_JP.UTF-8"
2272
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("hoo %d"),
2273
+ :if => Proc.new { |r| r.content.size > 4 } )
2274
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2275
+ assert !t.valid?
2276
+ assert t.errors.on(:title)
2277
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
2278
+ assert_equal "ふー5", t.errors.on("title")
2279
+
2280
+ GetText.set_locale "en"
2281
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
2282
+ assert !t.valid?
2283
+ assert t.errors.on(:title)
2284
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
2285
+ assert_equal "hoo 5", t.errors.on("title")
2286
+
2287
+ end
2288
+
2289
+ def test_validates_associated_missing
2290
+ GetText.set_locale "ja_JP.UTF-8"
2291
+ Reply.validates_presence_of(:topic)
2292
+ r = Reply.create("title" => "A reply", "content" => "with content!")
2293
+ # In this case, rgettext doesn't pick up the names of relations as msgid,
2294
+ # so you need to define N_() in the model class
2295
+ assert_equal "Replyトピックを入力してください。", r.errors.on(:topic)
2296
+
2297
+ GetText.set_locale "en"
2298
+ r = Reply.create("title" => "A reply", "content" => "with content!")
2299
+ assert_equal "Topic can't be blank", r.errors.on(:topic)
2300
+ assert_equal "Topic can't be blank", r.errors.full_messages[0]
2301
+ end
2302
+
2303
+ def test_errors_to_xml
2304
+ GetText.set_locale "ja_JP.UTF-8"
2305
+ r = Reply.new :title => "Wrong Create"
2306
+ assert !r.valid?
2307
+ xml = r.errors.to_xml(:skip_instruct => true)
2308
+ assert_equal "<errors>", xml.first(8)
2309
+ assert xml.include?("<error>Reply&#12479;&#12452;&#12488;&#12523; &#12364;&#19981;&#27491;&#12395;&#29983;&#25104;&#12373;&#12428;&#12414;&#12375;&#12383;&#12290;</error>")
2310
+ assert xml.include?("<error>Reply&#20869;&#23481; &#31354;&#12391;&#12377;&#12290;</error>")
2311
+
2312
+ GetText.set_locale "en"
2313
+ r = Reply.new :title => "Wrong Create"
2314
+ assert !r.valid?
2315
+ xml = r.errors.to_xml(:skip_instruct => true)
2316
+ assert_equal "<errors>", xml.first(8)
2317
+ assert xml.include?("<error>Title is Wrong Create</error>")
2318
+ assert xml.include?("<error>Content Empty</error>")
2319
+ end
2320
+
2321
+ def test_validation_order
2322
+ if AR_6657
2323
+ Topic.validates_presence_of :title
2324
+ Topic.validates_length_of :title, :minimum => 2
2325
+
2326
+ GetText.set_locale "ja_JP.UTF-8"
2327
+ t = Topic.new("title" => "")
2328
+ assert !t.valid?
2329
+ assert_equal "タイトルを入力してください。", t.errors.on("title").first
2330
+
2331
+ GetText.set_locale "en"
2332
+ t = Topic.new("title" => "")
2333
+ assert !t.valid?
2334
+ assert_equal "Title can't be blank", t.errors.on("title").first
2335
+ end
2336
+ end
2337
+
2338
+ =begin
2339
+ #Don't need this
2340
+ def test_validation_with_if_as_string
2341
+ end
2342
+ =end
2343
+
2344
+ def test_default_validates_numericality_of
2345
+ GetText.set_locale "ja_JP.UTF-8"
2346
+ Topic.validates_numericality_of :approved
2347
+ topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => "aaa")
2348
+ assert_equal "承認は数値で入力してください。", topic.errors.on(:approved)
2349
+
2350
+ GetText.set_locale "en"
2351
+ topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => "aaa")
2352
+ assert_equal "Approved is not a number", topic.errors.on(:approved)
2353
+ assert_equal "Approved is not a number", topic.errors.full_messages[0]
2354
+ end
2355
+
2356
+ def test_inherited_messages
2357
+ GetText.set_locale "ja_JP.UTF-8"
2358
+ Topic.validates_presence_of(:title, :content)
2359
+
2360
+ GetText.set_locale "ja_JP.UTF-8"
2361
+ t = Topic.create
2362
+ assert !t.save
2363
+ assert_equal "タイトルを入力してください。", t.errors.full_messages[0]
2364
+ assert_equal "内容を入力してください。", t.errors.full_messages[1]
2365
+ assert_equal "タイトルを入力してください。", t.errors.on(:title)
2366
+ assert_equal "内容を入力してください。", t.errors.on(:content)
2367
+
2368
+ t = Reply.create
2369
+ assert !t.save
2370
+ assert_equal "Replyタイトルを入力してください。", t.errors.full_messages[0]
2371
+ assert_equal "Replyタイトル 空です。", t.errors.full_messages[1]
2372
+ assert_equal "Reply内容を入力してください。", t.errors.full_messages[2]
2373
+ assert_equal "Reply内容 空です。", t.errors.full_messages[3]
2374
+ assert_equal ["Replyタイトルを入力してください。","空です。"], t.errors.on(:title)
2375
+ assert_equal ["Reply内容を入力してください。", "空です。"], t.errors.on(:content)
2376
+
2377
+ t = Reply.create
2378
+ t.title = "Wrong Create"
2379
+ assert !t.save
2380
+ assert_equal "Replyタイトル が不正に生成されました。", t.errors.full_messages[0]
2381
+ assert_equal "Reply内容を入力してください。", t.errors.full_messages[1]
2382
+ assert_equal "Reply内容 空です。", t.errors.full_messages[2]
2383
+
2384
+ t = SillyReply.create
2385
+ assert !t.save
2386
+ assert_equal "Sillyタイトルを入力してください。", t.errors.full_messages[0]
2387
+ assert_equal "Sillyタイトル 空です。", t.errors.full_messages[1]
2388
+ assert_equal "Silly内容を入力してください。", t.errors.full_messages[2]
2389
+ assert_equal "Silly内容 空です。", t.errors.full_messages[3]
2390
+ assert_equal ["Sillyタイトルを入力してください。","空です。"], t.errors.on(:title)
2391
+ assert_equal ["Silly内容を入力してください。","空です。"], t.errors.on(:content)
2392
+
2393
+ t = SillyReply.create
2394
+ t.title = "Wrong Create"
2395
+ assert !t.save
2396
+ assert_equal "Sillyタイトル が不正に生成されました。", t.errors.full_messages[0]
2397
+ assert_equal "Silly内容を入力してください。", t.errors.full_messages[1]
2398
+ assert_equal "Silly内容 空です。", t.errors.full_messages[2]
2399
+ end
2400
+
2401
+ def test_original_model_with_validation
2402
+ GetText.set_locale "ja_JP.UTF-8"
2403
+ t = MyModel.new
2404
+ t.title = nil
2405
+ t.save
2406
+ assert_equal "Titleを入力してください。", t.errors.full_messages[0]
2407
+ assert_equal "Titleを入力してください。", t.errors.on(:title)
2408
+
2409
+ GetText.set_locale "en"
2410
+ t = MyModel.new
2411
+ t.title = nil
2412
+ t.save
2413
+ assert_equal "Title can't be blank", t.errors.full_messages[0]
2414
+ assert_equal "Title can't be blank", t.errors.on(:title)
2415
+ end
2416
+ end
2417
+
2418
+ class ValidatesNumericalityTest < ActiveRecord::TestCase
2419
+ include GetText
2420
+
2421
+ NIL = [nil]
2422
+ BLANK = ["", " ", " \t \r \n"]
2423
+ BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significent digits
2424
+ FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
2425
+ INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
2426
+ FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
2427
+ INTEGERS = [0, 10, -10] + INTEGER_STRINGS
2428
+ BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
2429
+ JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
2430
+ INFINITY = [1.0/0.0]
2431
+
2432
+ repair_validations(Topic)
2433
+ bindtextdomain_to(Topic, "active_record", :path => "locale")
2434
+
2435
+ def test_default_validates_numericality_of
2436
+ Topic.validates_numericality_of :approved
2437
+
2438
+ invalid!(NIL + BLANK + JUNK)
2439
+ # valid!(FLOATS + INTEGERS + BIGDECIMAL)
2440
+ end
2441
+
2442
+ def test_validates_numericality_of_with_nil_allowed
2443
+ Topic.validates_numericality_of :approved, :allow_nil => true
2444
+
2445
+ invalid!(JUNK)
2446
+ # valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
2447
+ end
2448
+
2449
+ def test_validates_numericality_of_with_integer_only
2450
+ Topic.validates_numericality_of :approved, :only_integer => true
2451
+
2452
+ invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL)
2453
+ # valid!(INTEGERS)
2454
+ end
2455
+
2456
+ def test_validates_numericality_of_with_integer_only_and_nil_allowed
2457
+ Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
2458
+
2459
+ invalid!(JUNK + FLOATS + BIGDECIMAL + INFINITY)
2460
+ # valid!(NIL + BLANK + INTEGERS)
2461
+ end
2462
+
2463
+ def test_validates_numericality_with_greater_than
2464
+ Topic.validates_numericality_of :approved, :greater_than => 10
2465
+
2466
+ invalid!([-10, 10], 'Approved must be greater than 10',
2467
+ '承認は10より大きい値にしてください。')
2468
+ # valid!([11])
2469
+ end
2470
+
2471
+ def test_validates_numericality_with_greater_than_or_equal
2472
+ Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10
2473
+
2474
+ invalid!([-9, 9], 'Approved must be greater than or equal to 10',
2475
+ "承認は10以上の値にしてください。")
2476
+ # valid!([10])
2477
+ end
2478
+
2479
+ def test_validates_numericality_with_equal_to
2480
+ Topic.validates_numericality_of :approved, :equal_to => 10
2481
+
2482
+ invalid!([-10, 11], 'Approved must be equal to 10',
2483
+ "承認は10にしてください。")
2484
+ # valid!([10])
2485
+ end
2486
+
2487
+ def test_validates_numericality_with_less_than
2488
+ Topic.validates_numericality_of :approved, :less_than => 10
2489
+
2490
+ invalid!([10], 'Approved must be less than 10',
2491
+ "承認は10より小さい値にしてください。")
2492
+ # valid!([-9, 9])
2493
+ end
2494
+
2495
+ def test_validates_numericality_with_less_than_or_equal_to
2496
+ Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10
2497
+
2498
+ invalid!([11], 'Approved must be less than or equal to 10')
2499
+ # valid!([-10, 10])
2500
+ end
2501
+
2502
+ def test_validates_numericality_with_odd
2503
+ Topic.validates_numericality_of :approved, :odd => true
2504
+
2505
+ invalid!([-2, 2], 'Approved must be odd',
2506
+ "承認は奇数にしてください。")
2507
+ # valid!([-1, 1])
2508
+ end
2509
+
2510
+ def test_validates_numericality_with_even
2511
+ Topic.validates_numericality_of :approved, :even => true
2512
+
2513
+ invalid!([-1, 1], 'Approved must be even',
2514
+ "承認は偶数にしてください。")
2515
+ # valid!([-2, 2])
2516
+ end
2517
+
2518
+ def test_validates_numericality_with_greater_than_less_than_and_even
2519
+ Topic.validates_numericality_of :approved, :greater_than => 1, :less_than => 4, :even => true
2520
+
2521
+ invalid!([1, 3, 4])
2522
+ # valid!([2])
2523
+ end
2524
+
2525
+ def test_validates_numericality_with_numeric_message
2526
+ Topic.validates_numericality_of :approved, :less_than => 4, :message => N_("smaller than %{count}")
2527
+ topic = Topic.new("title" => "numeric test", "approved" => 10)
2528
+
2529
+ assert !topic.valid?
2530
+ assert_equal "Approved smaller than 4", topic.errors.full_messages[0]
2531
+ assert_equal "smaller than 4", topic.errors.on(:approved)
2532
+
2533
+ Topic.validates_numericality_of :approved, :greater_than => 4, :message => N_("greater than %{count}")
2534
+ topic = Topic.new("title" => "numeric test", "approved" => 1)
2535
+
2536
+ assert !topic.valid?
2537
+ assert_equal "Approved greater than 4", topic.errors.full_messages[0]
2538
+ assert_equal "greater than 4", topic.errors.on(:approved)
2539
+ end
2540
+
2541
+ private
2542
+ def invalid!(values, error=nil, errorj=nil)
2543
+ with_each_topic_approved_value(values) do |topic, value|
2544
+ GetText.set_locale "ja_JP.UTF-8"
2545
+ assert !topic.valid?, "#{value.inspect} not rejected as a number"
2546
+ assert topic.errors.on(:approved)
2547
+ assert_equal errorj, topic.errors.full_messages[0] if errorj
2548
+ assert_equal errorj, topic.errors.on(:approved) if errorj
2549
+ end
2550
+
2551
+ with_each_topic_approved_value(values) do |topic, value|
2552
+ GetText.set_locale "en"
2553
+ assert !topic.valid?, "#{value.inspect} not rejected as a number"
2554
+ assert topic.errors.on(:approved)
2555
+ assert_equal error, topic.errors.full_messages[0] if error
2556
+ assert_equal error, topic.errors.on(:approved) if error
2557
+ end
2558
+ end
2559
+ =begin
2560
+ # Don't need this
2561
+ def valid!(values)
2562
+ with_each_topic_approved_value(values) do |topic, value|
2563
+ assert topic.valid?, "#{value.inspect} not accepted as a number"
2564
+ end
2565
+ end
2566
+ =end
2567
+ def with_each_topic_approved_value(values)
2568
+ topic = Topic.new("title" => "numeric test", "content" => "whatever")
2569
+ values.each do |value|
2570
+ topic.approved = value
2571
+ yield topic, value
2572
+ end
2573
+ end
2574
+ end
2575
+