gettext 1.7.0-mswin32 → 1.8.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/ChangeLog +139 -0
  2. data/NEWS +31 -0
  3. data/README +31 -15
  4. data/Rakefile +3 -2
  5. data/bin/rgettext +1 -1
  6. data/bin/rmsgfmt +1 -1
  7. data/bin/rmsgmerge +1 -1
  8. data/data/locale/cs/LC_MESSAGES/rails.mo +0 -0
  9. data/data/locale/cs/LC_MESSAGES/rgettext.mo +0 -0
  10. data/data/locale/de/LC_MESSAGES/rails.mo +0 -0
  11. data/data/locale/de/LC_MESSAGES/rgettext.mo +0 -0
  12. data/data/locale/el/LC_MESSAGES/rails.mo +0 -0
  13. data/data/locale/el/LC_MESSAGES/rgettext.mo +0 -0
  14. data/data/locale/es/LC_MESSAGES/rails.mo +0 -0
  15. data/data/locale/es/LC_MESSAGES/rgettext.mo +0 -0
  16. data/data/locale/et/LC_MESSAGES/rails.mo +0 -0
  17. data/data/locale/fr/LC_MESSAGES/rails.mo +0 -0
  18. data/data/locale/fr/LC_MESSAGES/rgettext.mo +0 -0
  19. data/data/locale/it/LC_MESSAGES/rails.mo +0 -0
  20. data/data/locale/it/LC_MESSAGES/rgettext.mo +0 -0
  21. data/data/locale/ja/LC_MESSAGES/rails.mo +0 -0
  22. data/data/locale/ja/LC_MESSAGES/rgettext.mo +0 -0
  23. data/data/locale/ko/LC_MESSAGES/rails.mo +0 -0
  24. data/data/locale/ko/LC_MESSAGES/rgettext.mo +0 -0
  25. data/data/locale/nl/LC_MESSAGES/rails.mo +0 -0
  26. data/data/locale/nl/LC_MESSAGES/rgettext.mo +0 -0
  27. data/data/locale/pt_BR/LC_MESSAGES/rails.mo +0 -0
  28. data/data/locale/pt_BR/LC_MESSAGES/rgettext.mo +0 -0
  29. data/data/locale/ru/LC_MESSAGES/rails.mo +0 -0
  30. data/data/locale/ru/LC_MESSAGES/rgettext.mo +0 -0
  31. data/data/locale/sv/LC_MESSAGES/rgettext.mo +0 -0
  32. data/data/locale/zh/LC_MESSAGES/rails.mo +0 -0
  33. data/data/locale/zh/LC_MESSAGES/rgettext.mo +0 -0
  34. data/data/locale/zh_TW/LC_MESSAGES/rails.mo +0 -0
  35. data/data/locale/zh_TW/LC_MESSAGES/rgettext.mo +0 -0
  36. data/lib/gettext.rb +50 -23
  37. data/lib/gettext/active_record.rb +275 -0
  38. data/lib/gettext/locale.rb +9 -2
  39. data/lib/gettext/locale_cgi.rb +5 -4
  40. data/lib/gettext/locale_object.rb +5 -4
  41. data/lib/gettext/parser/{activerecord.rb → active_record.rb} +31 -14
  42. data/lib/gettext/parser/ruby.rb +48 -44
  43. data/lib/gettext/rails.rb +111 -253
  44. data/lib/gettext/rgettext.rb +34 -6
  45. data/lib/gettext/string.rb +3 -2
  46. data/lib/gettext/textdomain.rb +3 -3
  47. data/lib/gettext/version.rb +1 -1
  48. data/po/cs/rails.po +61 -61
  49. data/po/cs/rgettext.po +37 -55
  50. data/po/de/rails.po +73 -72
  51. data/po/de/rgettext.po +39 -56
  52. data/po/el/rails.po +60 -60
  53. data/po/el/rgettext.po +26 -44
  54. data/po/es/rails.po +60 -60
  55. data/po/es/rgettext.po +28 -52
  56. data/po/et/rails.po +118 -0
  57. data/po/fr/rails.po +60 -60
  58. data/po/fr/rgettext.po +26 -46
  59. data/po/it/rails.po +68 -68
  60. data/po/it/rgettext.po +35 -53
  61. data/po/ja/rails.po +62 -61
  62. data/po/ja/rgettext.po +27 -45
  63. data/po/ko/rails.po +61 -60
  64. data/po/ko/rgettext.po +34 -54
  65. data/po/nl/rails.po +59 -60
  66. data/po/nl/rgettext.po +35 -55
  67. data/po/pt_BR/rails.po +74 -73
  68. data/po/pt_BR/rgettext.po +36 -52
  69. data/po/rails.pot +61 -61
  70. data/po/rgettext.pot +25 -43
  71. data/po/ru/rails.po +61 -62
  72. data/po/ru/rgettext.po +26 -51
  73. data/po/sv/rgettext.po +27 -44
  74. data/po/zh/rails.po +62 -60
  75. data/po/zh/rgettext.po +28 -47
  76. data/po/zh_TW/rails.po +114 -0
  77. data/po/zh_TW/rgettext.po +121 -0
  78. data/samples/cgi/locale/zh_TW/LC_MESSAGES/helloerb1.mo +0 -0
  79. data/samples/cgi/locale/zh_TW/LC_MESSAGES/helloerb2.mo +0 -0
  80. data/samples/cgi/locale/zh_TW/LC_MESSAGES/hellolib.mo +0 -0
  81. data/samples/cgi/locale/zh_TW/LC_MESSAGES/main.mo +0 -0
  82. data/samples/cgi/po/zh_TW/helloerb1.po +67 -0
  83. data/samples/cgi/po/zh_TW/helloerb2.po +54 -0
  84. data/samples/cgi/po/zh_TW/hellolib.po +26 -0
  85. data/samples/cgi/po/zh_TW/main.po +79 -0
  86. data/samples/locale/zh_TW/LC_MESSAGES/hello.mo +0 -0
  87. data/samples/locale/zh_TW/LC_MESSAGES/hello2.mo +0 -0
  88. data/samples/locale/zh_TW/LC_MESSAGES/hello_noop.mo +0 -0
  89. data/samples/locale/zh_TW/LC_MESSAGES/hello_plural.mo +0 -0
  90. data/samples/locale/zh_TW/LC_MESSAGES/helloglade2.mo +0 -0
  91. data/samples/locale/zh_TW/LC_MESSAGES/hellogtk.mo +0 -0
  92. data/samples/locale/zh_TW/LC_MESSAGES/hellotk.mo +0 -0
  93. data/samples/po/hellogtk.pot +1 -1
  94. data/samples/po/zh_TW/hello.po +26 -0
  95. data/samples/po/zh_TW/hello2.po +34 -0
  96. data/samples/po/zh_TW/hello_noop.po +30 -0
  97. data/samples/po/zh_TW/hello_plural.po +28 -0
  98. data/samples/po/zh_TW/helloglade2.po +40 -0
  99. data/samples/po/zh_TW/hellogtk.po +25 -0
  100. data/samples/po/zh_TW/hellotk.po +26 -0
  101. data/samples/rails/README +8 -11
  102. data/samples/rails/app/controllers/application.rb +6 -9
  103. data/samples/rails/app/views/layouts/blog.rhtml +1 -1
  104. data/samples/rails/config/database.yml +1 -1
  105. data/samples/rails/config/environment.rb +1 -0
  106. data/samples/rails/db/schema.rb +90 -0
  107. data/samples/rails/lib/tasks/gettext.rake +1 -1
  108. data/samples/rails/locale/cs/LC_MESSAGES/blog.mo +0 -0
  109. data/samples/rails/locale/de/LC_MESSAGES/blog.mo +0 -0
  110. data/samples/rails/locale/el/LC_MESSAGES/blog.mo +0 -0
  111. data/samples/rails/locale/en/LC_MESSAGES/blog.mo +0 -0
  112. data/samples/rails/locale/es/LC_MESSAGES/blog.mo +0 -0
  113. data/samples/rails/locale/fr/LC_MESSAGES/blog.mo +0 -0
  114. data/samples/rails/locale/it/LC_MESSAGES/blog.mo +0 -0
  115. data/samples/rails/locale/ja/LC_MESSAGES/blog.mo +0 -0
  116. data/samples/rails/locale/ko/LC_MESSAGES/blog.mo +0 -0
  117. data/samples/rails/locale/nl/LC_MESSAGES/blog.mo +0 -0
  118. data/samples/rails/locale/pt_BR/LC_MESSAGES/blog.mo +0 -0
  119. data/samples/rails/locale/ru/LC_MESSAGES/blog.mo +0 -0
  120. data/samples/rails/locale/zh/LC_MESSAGES/blog.mo +0 -0
  121. data/samples/rails/locale/zh_TW/LC_MESSAGES/blog.mo +0 -0
  122. data/samples/rails/po/blog.pot +106 -110
  123. data/samples/rails/po/cs/blog.po +110 -115
  124. data/samples/rails/po/de/blog.po +107 -112
  125. data/samples/rails/po/el/blog.po +105 -109
  126. data/samples/rails/po/en/blog.po +107 -111
  127. data/samples/rails/po/es/blog.po +105 -110
  128. data/samples/rails/po/fr/blog.po +105 -109
  129. data/samples/rails/po/it/blog.po +105 -109
  130. data/samples/rails/po/ja/blog.po +105 -109
  131. data/samples/rails/po/ko/blog.po +105 -109
  132. data/samples/rails/po/nl/blog.po +105 -110
  133. data/samples/rails/po/pt_BR/blog.po +105 -109
  134. data/samples/rails/po/ru/blog.po +105 -107
  135. data/samples/rails/po/zh/blog.po +105 -109
  136. data/samples/rails/po/zh_TW/blog.po +107 -0
  137. data/samples/rails/vendor/plugins/gettext/locale/zh_TW/LC_MESSAGES/gettext_plugin.mo +0 -0
  138. data/samples/rails/vendor/plugins/gettext/po/zh_TW/gettext_plugin.po +30 -0
  139. data/test/Rakefile +44 -0
  140. data/test/fixtures/developer.rb +5 -0
  141. data/test/fixtures/developers.yml +21 -0
  142. data/test/fixtures/reply.rb +40 -0
  143. data/test/fixtures/topic.rb +23 -0
  144. data/test/fixtures/topics.yml +22 -0
  145. data/test/gettext_test.rb +2 -2
  146. data/test/gettext_test_active_record.rb +1479 -0
  147. data/test/gettext_test_rails.rb +2 -0
  148. data/test/locale/ja/LC_MESSAGES/active_record.mo +0 -0
  149. data/test/po/active_record.pot +295 -0
  150. data/test/po/ja/active_record.po +294 -0
  151. data/test/test.sh +1 -0
  152. data/test/test_rubyparser.rb +10 -2
  153. metadata +66 -6
  154. data/samples/rails/db/mysql.sql +0 -8
  155. data/samples/rails/db/postgresql.sql +0 -7
@@ -0,0 +1,107 @@
1
+ # GetText Sample Blog on RoR.
2
+ #
3
+ # Copyright (C) 2006 Masao Mutoh
4
+ #
5
+ # This file is distributed under the same license as the Ruby-GetText-Package.
6
+ #
7
+ # LIN CHUNG-YI <xmarsh at gmail.com>, 2006.
8
+ #
9
+ msgid ""
10
+ msgstr ""
11
+ "Project-Id-Version: blog 1.2.0\n"
12
+ "POT-Creation-Date: 2006-08-22 18:35+0900\n"
13
+ "PO-Revision-Date: 2006-08-15 09:38+0800\n"
14
+ "Last-Translator: LIN CHUNG-YI <xmarsh at gmail.com>\n"
15
+ "Language-Team: zh_TW <xmarsh at gmail.com>\n"
16
+ "MIME-Version: 1.0\n"
17
+ "Content-Type: text/plain; charset=UTF-8\n"
18
+ "Content-Transfer-Encoding: 8bit\n"
19
+ "Plural-Forms: nplurals=1; plural=0;\n"
20
+ "X-Poedit-Language: Chinese\n"
21
+ "X-Poedit-Country: TAIWAN\n"
22
+
23
+ #: app/controllers/blog_controller.rb:33
24
+ msgid "Article was successfully created."
25
+ msgstr "文章已成功建立"
26
+
27
+ #: app/controllers/blog_controller.rb:47
28
+ msgid "Article was successfully updated."
29
+ msgstr "文章已成功更新"
30
+
31
+ #: app/helpers/blog_helper.rb:43
32
+ msgid "Ruby Links"
33
+ msgstr "Ruby連結"
34
+
35
+ #: app/helpers/blog_helper.rb:51
36
+ msgid "Old articles"
37
+ msgstr "舊文章"
38
+
39
+ # app/models/article.rb:-
40
+ msgid "article"
41
+ msgstr "文章"
42
+
43
+ # app/models/article.rb:-
44
+ msgid "Article|Title"
45
+ msgstr "標題"
46
+
47
+ # app/models/article.rb:-
48
+ msgid "Article|Description"
49
+ msgstr "描述"
50
+
51
+ # app/models/article.rb:-
52
+ msgid "Article|Lastupdate"
53
+ msgstr "最近更新"
54
+
55
+ #: app/views/blog/edit.rhtml:12
56
+ msgid "Editing article"
57
+ msgstr "編輯文章"
58
+
59
+ #: app/views/blog/edit.rhtml:16 app/views/blog/show.rhtml:16
60
+ msgid "Edit"
61
+ msgstr "編輯"
62
+
63
+ #: app/views/blog/edit.rhtml:19
64
+ msgid "Show"
65
+ msgstr "觀看"
66
+
67
+ #: app/views/blog/edit.rhtml:20
68
+ msgid "Destroy"
69
+ msgstr "刪除"
70
+
71
+ #: app/views/blog/edit.rhtml:20
72
+ msgid "Are you sure?"
73
+ msgstr "你確定嗎?"
74
+
75
+ #: app/views/blog/edit.rhtml:21 app/views/blog/new.rhtml:20
76
+ #: app/views/blog/show.rhtml:17
77
+ msgid "Back"
78
+ msgstr "返回"
79
+
80
+ #: app/views/blog/list.rhtml:13 app/views/blog/show.rhtml:13
81
+ #: app/views/layouts/blog.rhtml:14
82
+ msgid "GetText Sample Blog on RoR"
83
+ msgstr "GetText 的 RoR 部落格範例"
84
+
85
+ #: app/views/blog/list.rhtml:15 app/views/blog/new.rhtml:13
86
+ msgid "New article"
87
+ msgstr "新文章"
88
+
89
+ #: app/views/blog/list.rhtml:18
90
+ msgid "No articles were found."
91
+ msgstr "找不到文章"
92
+
93
+ #: app/views/blog/new.rhtml:17
94
+ msgid "Create"
95
+ msgstr "新增"
96
+
97
+ #: app/views/blog/_form.rhtml:16
98
+ msgid "Lastupdate"
99
+ msgstr "最近更新"
100
+
101
+ #: app/views/blog/_form.rhtml:18
102
+ msgid "Title: Required."
103
+ msgstr "標題需要填寫"
104
+
105
+ #: app/views/blog/_form.rhtml:21
106
+ msgid "Description: More than 10 characters."
107
+ msgstr "描述需多於十個字"
@@ -0,0 +1,30 @@
1
+ # GetText Sample Blog on RoR.
2
+ #
3
+ # Copyright (C) 2005,2006 Masao Mutoh
4
+ #
5
+ # This file is distributed under the same license as the Ruby-GetText-Package.
6
+ #
7
+ # LIN CHUNG-YI <xmarsh at gmail.com>, 2006.
8
+ #
9
+ msgid ""
10
+ msgstr ""
11
+ "Project-Id-Version: gettext_plugin 1.2.0\n"
12
+ "POT-Creation-Date: 2006-05-31 02:06+0900\n"
13
+ "PO-Revision-Date: 2006-08-18 14:43+0800\n"
14
+ "Last-Translator: LIN CHUNG-YI <xmarsh at gmail.com>\n"
15
+ "Language-Team: zh_TW <xmarsh at gmail.com>\n"
16
+ "MIME-Version: 1.0\n"
17
+ "Content-Type: text/plain; charset=UTF-8\n"
18
+ "Content-Transfer-Encoding: 8bit\n"
19
+ "Plural-Forms: nplurals=1; plural=0;\n"
20
+ "X-Poedit-Language: Chinese\n"
21
+ "X-Poedit-Country: TAIWAN\n"
22
+
23
+ #: lib/gettext_plugin.rb:18
24
+ msgid "Select locale"
25
+ msgstr "選擇字集"
26
+
27
+ #: lib/gettext_plugin.rb:30
28
+ msgid "Cookie &quot;lang&quot; is set: %s"
29
+ msgstr "Cookie &quot;lang&quot; 已被設定 : %s"
30
+
data/test/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'gettext/utils'
2
+
3
+ desc "Create mo files"
4
+ task :makemo do
5
+ GetText.create_mofiles(true, "po", "locale")
6
+ end
7
+
8
+ SCHEMA_PATH = File.join(File.dirname(__FILE__), "db")
9
+
10
+ desc 'Build the MySQL test databases'
11
+ task :createdb do
12
+ %x( mysqladmin -u root create activerecord_unittest )
13
+ %x( mysqladmin -u root create activerecord_unittest2 )
14
+ %x( mysql -u root activerecord_unittest < #{File.join(SCHEMA_PATH, 'mysql.sql')} )
15
+ end
16
+
17
+ desc 'Drop the MySQL test databases'
18
+ task :dropdb do
19
+ %x( mysqladmin -u root -f drop activerecord_unittest )
20
+ %x( mysqladmin -u root -f drop activerecord_unittest2 )
21
+ end
22
+
23
+ desc 'Rebuild the MySQL test databases'
24
+ task :rebuilddb => [:dropdb, :builddb]
25
+
26
+ require 'gettext/utils'
27
+
28
+ desc "Update pot/po files for ActiveRecord test."
29
+ task :updatepo do
30
+ GetText::ActiveRecordParser.init(
31
+ :adapter => "mysql",
32
+ :username => "root",
33
+ :encoding => "utf8",
34
+ :activerecord_classes => ["ActiveRecord::Base", "Reply"],
35
+ :socket => "/var/lib/mysql/mysql.sock",
36
+ :database => 'activerecord_unittest'
37
+ )
38
+ GetText.update_pofiles("active_record",
39
+ ["fixtures/developer.rb",
40
+ "fixtures/topic.rb",
41
+ "fixtures/reply.rb",
42
+ "gettext_test_active_record.rb"],
43
+ "active_record 1.0.0")
44
+ end
@@ -0,0 +1,5 @@
1
+ class Developer < ActiveRecord::Base
2
+ N_("Developer|Non existent")
3
+ validates_inclusion_of :salary, :in => 50000..200000
4
+ validates_length_of :name, :within => 3..20
5
+ end
@@ -0,0 +1,21 @@
1
+ david:
2
+ id: 1
3
+ name: David
4
+ salary: 80000
5
+
6
+ jamis:
7
+ id: 2
8
+ name: Jamis
9
+ salary: 150000
10
+
11
+ <% for digit in 3..10 %>
12
+ dev_<%= digit %>:
13
+ id: <%= digit %>
14
+ name: fixture_<%= digit %>
15
+ salary: 100000
16
+ <% end %>
17
+
18
+ poor_jamis:
19
+ id: 11
20
+ name: Jamis
21
+ salary: 9000
@@ -0,0 +1,40 @@
1
+ require 'fixtures/topic'
2
+
3
+ class Reply < Topic
4
+ N_("Reply|Topic") # Need to define relation table names explicity
5
+ # if you use it in validations.
6
+
7
+ belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true
8
+ has_many :replies, :class_name => "SillyReply", :dependent => :destroy, :foreign_key => "parent_id"
9
+
10
+ validate :errors_on_empty_content
11
+ validate_on_create :title_is_wrong_create
12
+
13
+ attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read
14
+
15
+ def validate
16
+ errors.add("title", _("Empty")) unless attribute_present? "title"
17
+ end
18
+
19
+ def errors_on_empty_content
20
+ errors.add("content", _("Empty")) unless attribute_present? "content"
21
+ end
22
+
23
+ def validate_on_create
24
+ if attribute_present?("title") && attribute_present?("content") && content == "Mismatch"
25
+ errors.add("title", _("is Content Mismatch"))
26
+ end
27
+ end
28
+
29
+ def title_is_wrong_create
30
+ errors.add("title", _("is Wrong Create")) if attribute_present?("title") && title == "Wrong Create"
31
+ end
32
+
33
+ def validate_on_update
34
+ errors.add("title", _("is Wrong Update")) if attribute_present?("title") && title == "Wrong Update"
35
+ end
36
+ end
37
+
38
+ class SillyReply < Reply
39
+ belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :replies_count
40
+ end
@@ -0,0 +1,23 @@
1
+ class Topic < ActiveRecord::Base
2
+ has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
3
+ serialize :content
4
+
5
+ N_("Topic|Terms of service")
6
+ N_("must be abided")
7
+
8
+ before_create :default_written_on
9
+ before_destroy :destroy_children
10
+
11
+ def parent
12
+ Topic.find(parent_id)
13
+ end
14
+
15
+ protected
16
+ def default_written_on
17
+ self.written_on = Time.now unless attribute_present?("written_on")
18
+ end
19
+
20
+ def destroy_children
21
+ self.class.delete_all "parent_id = #{id}"
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ first:
2
+ id: 1
3
+ title: The First Topic
4
+ author_name: David
5
+ author_email_address: david@loudthinking.com
6
+ written_on: 2003-07-16t15:28:00.00+01:00
7
+ last_read: 2004-04-15
8
+ bonus_time: 2005-01-30t15:28:00.00+01:00
9
+ content: Have a nice day
10
+ approved: false
11
+ replies_count: 0
12
+
13
+ second:
14
+ id: 2
15
+ title: The Second Topic's of the day
16
+ author_name: Mary
17
+ written_on: 2003-07-15t15:28:00.00+01:00
18
+ content: Have a nice day
19
+ approved: true
20
+ replies_count: 2
21
+ parent_id: 1
22
+ type: Reply
data/test/gettext_test.rb CHANGED
@@ -67,7 +67,7 @@ class TestGetText < Test::Unit::TestCase
67
67
  bindtextdomain("test2", "locale")
68
68
  testlib6 = TestLib6.new
69
69
  assert_equal("japanese", testlib6.test)
70
- setlocale("fr")
70
+ set_locale("fr")
71
71
  assert_equal("japanese", testlib6.test) #no influence of previous line
72
72
  assert_equal("ONE IS 1.", testlib6.test_formatted_string)
73
73
  testlib6.setlocale("ja")
@@ -234,7 +234,7 @@ DDD
234
234
  assert_equal(default_locale_dirs, GetText::TextDomain::DEFAULT_LOCALE_PATHS)
235
235
  new_path = "/foo/%{locale}/%{name}.mo"
236
236
  GetText::TextDomain.add_default_locale_path(new_path)
237
- assert_equal(default_locale_dirs << new_path, GetText::TextDomain::DEFAULT_LOCALE_PATHS)
237
+ assert_equal([new_path] + default_locale_dirs, GetText::TextDomain::DEFAULT_LOCALE_PATHS)
238
238
  end
239
239
 
240
240
  def test_setlocale
@@ -0,0 +1,1479 @@
1
+ $KCODE = "U"
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'rubygems'
5
+ require 'gettext'
6
+ require 'gettext/active_record'
7
+ require 'test/unit'
8
+ require 'active_record'
9
+ require 'active_record/fixtures'
10
+ require 'active_support/binding_of_caller'
11
+ require 'active_support/breakpoint'
12
+ require 'fixtures/topic'
13
+ require 'fixtures/reply'
14
+ require 'fixtures/developer'
15
+ require 'logger'
16
+
17
+ puts "This test requires MySQL"
18
+
19
+ ActiveRecord::Base.establish_connection(
20
+ :adapter => "mysql",
21
+ :username => "root",
22
+ :password => "",
23
+ :encoding => "utf8",
24
+ :socket => "/var/lib/mysql/mysql.sock",
25
+ :database => 'activerecord_unittest'
26
+ )
27
+
28
+ class Test::Unit::TestCase #:nodoc:
29
+ include GetText
30
+ bindtextdomain("active_record", "locale")
31
+ textdomain_to(ActiveRecord::Base, "active_record")
32
+ textdomain_to(ActiveRecord::Validations, "active_record")
33
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
34
+ end
35
+
36
+ # The following methods in Topic are used in test_conditional_validation_*
37
+ class Topic
38
+ def condition_is_true
39
+ return true
40
+ end
41
+
42
+ def condition_is_true_but_its_not
43
+ return false
44
+ end
45
+ end
46
+
47
+ class ValidationsTest < Test::Unit::TestCase
48
+ fixtures :topics, :developers
49
+
50
+ def setup
51
+ bindtextdomain_to(Topic, "active_record")
52
+ Topic.write_inheritable_attribute(:validate, nil)
53
+ Topic.write_inheritable_attribute(:validate_on_create, nil)
54
+ Topic.write_inheritable_attribute(:validate_on_update, nil)
55
+ end
56
+
57
+ def test_single_attr_validation_and_error_msg
58
+ GetText.set_locale_all "ja_JP.UTF-8"
59
+ r = Reply.new
60
+ r.title = "There's no content!"
61
+ r.save
62
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
63
+ assert_equal "空です。", r.errors.on("content"), "A reply without content should contain an error"
64
+ assert_equal "内容 空です。", r.errors.full_messages[0], "A reply without content should contain an error"
65
+ assert_equal 1, r.errors.count
66
+
67
+ GetText.set_locale_all "en"
68
+ r = Reply.new
69
+ r.title = "There's no content!"
70
+ r.save
71
+ assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error"
72
+ end
73
+
74
+ def test_double_attr_validation_and_error_msg
75
+ GetText.set_locale_all "ja_JP.UTF-8"
76
+ r = Reply.new
77
+ assert !r.save
78
+
79
+ assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid"
80
+ assert_equal "タイトル 空です。", r.errors.full_messages[0]
81
+ assert_equal "空です。", r.errors.on("title")
82
+
83
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
84
+ assert_equal "内容 空です。", r.errors.full_messages[1]
85
+ assert_equal "空です。", r.errors.on("content")
86
+
87
+ assert_equal 2, r.errors.count
88
+
89
+ GetText.set_locale_all "en"
90
+ r = Reply.new
91
+ assert !r.save
92
+
93
+ assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid"
94
+ assert_equal "Title Empty", r.errors.full_messages[0]
95
+ assert_equal "Empty", r.errors.on("title"), "A reply without title should contain an error"
96
+
97
+ assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid"
98
+ assert_equal "Content Empty", r.errors.full_messages[1]
99
+ assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error"
100
+
101
+ assert_equal 2, r.errors.count
102
+ end
103
+
104
+ def test_error_on_create
105
+ GetText.set_locale_all "ja_JP.UTF-8"
106
+ r = Reply.new
107
+ r.title = "Wrong Create"
108
+ assert !r.save
109
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
110
+ assert_equal "タイトル が不正に生成されました。", r.errors.full_messages[0]
111
+ assert_equal "が不正に生成されました。", r.errors.on("title")
112
+
113
+ GetText.set_locale_all "en"
114
+ r = Reply.new
115
+ r.title = "Wrong Create"
116
+ assert !r.save
117
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
118
+ assert_equal "Title is Wrong Create", r.errors.full_messages[0]
119
+ assert_equal "is Wrong Create", r.errors.on("title")
120
+ end
121
+
122
+
123
+ def test_error_on_update
124
+ GetText.set_locale_all "ja_JP.UTF-8"
125
+ r = Reply.new
126
+ r.title = "Bad"
127
+ r.content = "Good"
128
+ assert r.save, "First save should be successful"
129
+ r.title = "Wrong Update"
130
+ assert !r.save, "Second save should fail"
131
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
132
+ assert_equal "タイトル が不正に更新されました。", r.errors.full_messages[0]
133
+ assert_equal "が不正に更新されました。", r.errors.on("title")
134
+
135
+ GetText.set_locale_all "en"
136
+ r = Reply.new
137
+ r.title = "Bad"
138
+ r.content = "Good"
139
+ assert r.save, "First save should be successful"
140
+
141
+ r.title = "Wrong Update"
142
+ assert !r.save, "Second save should fail"
143
+
144
+ assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid"
145
+ assert_equal "Title is Wrong Update", r.errors.full_messages[0]
146
+ assert_equal "is Wrong Update", r.errors.on("title")
147
+ end
148
+
149
+ def test_single_error_per_attr_iteration
150
+ GetText.set_locale_all "ja_JP.UTF-8"
151
+ r = Reply.new
152
+ r.save
153
+
154
+ errors = []
155
+ r.errors.each { |attr, msg| errors << [attr, msg] }
156
+
157
+ assert errors.include?(["title", "空です。"])
158
+ assert errors.include?(["content", "空です。"])
159
+
160
+ GetText.set_locale_all "en"
161
+ r = Reply.new
162
+ r.save
163
+
164
+ errors = []
165
+ r.errors.each { |attr, msg| errors << [attr, msg] }
166
+
167
+ assert errors.include?(["title", "Empty"])
168
+ assert errors.include?(["content", "Empty"])
169
+ end
170
+
171
+ def test_multiple_errors_per_attr_iteration_with_full_error_composition
172
+ GetText.set_locale_all "ja_JP.UTF-8"
173
+ r = Reply.new
174
+ r.title = "Wrong Create"
175
+ r.content = "Mismatch"
176
+ r.save
177
+
178
+ errors = []
179
+ r.errors.each_full { |error| errors << error }
180
+ assert_equal "タイトル が不正に生成されました。", errors[0]
181
+ assert_equal "タイトル は内容がミスマッチです。", errors[1]
182
+ assert_equal 2, r.errors.count
183
+
184
+ GetText.set_locale_all "en"
185
+ r = Reply.new
186
+ r.title = "Wrong Create"
187
+ r.content = "Mismatch"
188
+ r.save
189
+
190
+ errors = []
191
+ r.errors.each_full { |error| errors << error }
192
+
193
+ assert_equal "Title is Wrong Create", errors[0]
194
+ assert_equal "Title is Content Mismatch", errors[1]
195
+ assert_equal 2, r.errors.count
196
+ end
197
+
198
+ def test_errors_on_base
199
+ GetText.set_locale_all "ja_JP.UTF-8"
200
+ r = Reply.new
201
+ r.content = "Mismatch"
202
+ r.save
203
+ r.errors.add_to_base "リプライはdignifyされてません。"
204
+
205
+ errors = []
206
+ r.errors.each_full { |error| errors << error }
207
+
208
+ assert_equal "リプライはdignifyされてません。", r.errors.on_base
209
+
210
+ assert errors.include?("タイトル 空です。")
211
+ assert errors.include?("リプライはdignifyされてません。")
212
+ assert_equal 2, r.errors.count
213
+
214
+ GetText.set_locale_all "en"
215
+ r = Reply.new
216
+ r.content = "Mismatch"
217
+ r.save
218
+ r.errors.add_to_base "Reply is not dignifying"
219
+
220
+ errors = []
221
+ r.errors.each_full { |error| errors << error }
222
+
223
+ assert_equal "Reply is not dignifying", r.errors.on_base
224
+
225
+ assert errors.include?("Title Empty")
226
+ assert errors.include?("Reply is not dignifying")
227
+ assert_equal 2, r.errors.count
228
+ end
229
+
230
+ def test_validates_each
231
+ perform = true
232
+ hits = 0
233
+ Topic.validates_each(:title, :content, [:title, :content]) do |record, attr|
234
+ if perform
235
+ record.errors.add attr, N_('gotcha')
236
+ hits += 1
237
+ end
238
+ end
239
+
240
+ GetText.set_locale_all "ja_JP.UTF-8"
241
+ t = Topic.new("title" => "valid", "content" => "whatever")
242
+ assert !t.save
243
+ assert_equal 4, hits
244
+ assert_equal ["タイトル ごっちゃ", "タイトル ごっちゃ",
245
+ "内容 ごっちゃ", "内容 ごっちゃ"], t.errors.full_messages
246
+ assert_equal ["ごっちゃ", "ごっちゃ"], t.errors.on(:title)
247
+ assert_equal ["ごっちゃ", "ごっちゃ"], t.errors.on(:content)
248
+
249
+ GetText.set_locale_all "en"
250
+ hits = 0
251
+ t = Topic.new("title" => "valid", "content" => "whatever")
252
+ assert !t.save
253
+ assert_equal 4, hits
254
+ assert_equal ["Title gotcha", "Title gotcha",
255
+ "Content gotcha", "Content gotcha"], t.errors.full_messages
256
+
257
+ assert_equal ["gotcha", "gotcha"], t.errors.on(:title)
258
+ assert_equal ["gotcha", "gotcha"], t.errors.on(:content)
259
+
260
+ ensure
261
+ perform = false
262
+ end
263
+
264
+ def test_errors_on_boundary_breaking
265
+ GetText.set_locale_all "ja_JP.UTF-8"
266
+ developer = Developer.new("name" => "xs")
267
+ assert !developer.save
268
+ assert_equal "開発者名は3文字以上で入力してください。", developer.errors.full_messages[0]
269
+ assert_equal "開発者名は3文字以上で入力してください。", developer.errors.on("name")
270
+
271
+ developer.name = "All too very long for this boundary, it really is"
272
+ assert !developer.save
273
+ assert_equal "開発者名は20文字以内で入力してください。", developer.errors.full_messages[0]
274
+ assert_equal "開発者名は20文字以内で入力してください。", developer.errors.on("name")
275
+
276
+ developer.name = "ちょうどぴったり12"
277
+ assert developer.save
278
+
279
+ GetText.set_locale_all "en"
280
+ developer = Developer.new("name" => "xs")
281
+ assert !developer.save
282
+ assert_equal "Name is too short (minimum is 3 characters)", developer.errors.full_messages[0]
283
+ assert_equal "Name is too short (minimum is 3 characters)", developer.errors.on("name")
284
+
285
+ developer.name = "All too very long for this boundary, it really is"
286
+ assert !developer.save
287
+ assert_equal "Name is too long (maximum is 20 characters)", developer.errors.full_messages[0]
288
+ assert_equal "Name is too long (maximum is 20 characters)", developer.errors.on("name")
289
+
290
+ developer.name = "Just right"
291
+ assert developer.save
292
+ end
293
+
294
+ def test_terms_of_service_agreement
295
+ GetText.set_locale_all "ja_JP.UTF-8"
296
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
297
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
298
+ assert !t.save
299
+ assert_equal "利用規約を受諾してください。", t.errors.full_messages[0]
300
+ assert_equal "利用規約を受諾してください。", t.errors.on(:terms_of_service)
301
+
302
+ t.terms_of_service = "1"
303
+ assert t.save
304
+
305
+ GetText.set_locale_all "en"
306
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
307
+ assert !t.save
308
+ assert_equal "Terms of service must be accepted", t.errors.full_messages[0]
309
+ assert_equal "Terms of service must be accepted", t.errors.on(:terms_of_service)
310
+
311
+ t.terms_of_service = "1"
312
+ assert t.save
313
+ end
314
+
315
+ def test_eula_fn
316
+ GetText.set_locale_all "ja_JP.UTF-8"
317
+ Topic.validates_acceptance_of(:eula, :message => N_("%{fn} must be abided"), :on => :create)
318
+
319
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
320
+ assert !t.save
321
+ assert_equal "Eulaに従ってください。", t.errors.full_messages[0]
322
+ assert_equal "Eulaに従ってください。", t.errors.on(:eula)
323
+
324
+ t.eula = "1"
325
+ assert t.save
326
+
327
+ GetText.set_locale_all "en"
328
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
329
+ assert !t.save
330
+ assert_equal "Eula must be abided", t.errors.full_messages[0]
331
+ assert_equal "Eula must be abided", t.errors.on(:eula)
332
+
333
+ t.eula = "1"
334
+ assert t.save
335
+ end
336
+
337
+ def test_eula
338
+ GetText.set_locale_all "ja_JP.UTF-8"
339
+ Topic.validates_acceptance_of(:eula, :message => N_("must be abided"), :on => :create)
340
+
341
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
342
+ assert !t.save
343
+ assert_equal "Eula に従ってください。", t.errors.full_messages[0]
344
+ assert_equal "に従ってください。", t.errors.on(:eula)
345
+
346
+ t.eula = "1"
347
+ assert t.save
348
+
349
+ GetText.set_locale_all "en"
350
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
351
+ assert !t.save
352
+ assert_equal "Eula must be abided", t.errors.full_messages[0]
353
+ assert_equal "must be abided", t.errors.on(:eula)
354
+
355
+ t.eula = "1"
356
+ assert t.save
357
+ end
358
+
359
+ def test_terms_of_service_agreement_with_accept_value
360
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
361
+
362
+ GetText.set_locale_all "ja_JP.UTF-8"
363
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
364
+ assert !t.save
365
+ assert_equal "利用規約を受諾してください。", t.errors.full_messages[0]
366
+ assert_equal "利用規約を受諾してください。", t.errors.on(:terms_of_service)
367
+
368
+ t.terms_of_service = "I agree."
369
+ assert t.save
370
+
371
+ GetText.set_locale_all "en"
372
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
373
+ assert !t.save
374
+ assert_equal "Terms of service must be accepted", t.errors.full_messages[0]
375
+ assert_equal "Terms of service must be accepted", t.errors.on(:terms_of_service)
376
+
377
+ t.terms_of_service = "I agree."
378
+ assert t.save
379
+ end
380
+
381
+ def test_validate_presences
382
+ Topic.validates_presence_of(:title, :content)
383
+
384
+ GetText.set_locale_all "ja_JP.UTF-8"
385
+ t = Topic.create
386
+ assert !t.save
387
+ assert_equal "タイトルを入力してください。", t.errors.full_messages[0]
388
+ assert_equal "内容を入力してください。", t.errors.full_messages[1]
389
+ assert_equal "タイトルを入力してください。", t.errors.on(:title)
390
+ assert_equal "内容を入力してください。", t.errors.on(:content)
391
+
392
+ t.title = "something"
393
+ t.content = " "
394
+
395
+ assert !t.save
396
+ assert_equal "内容を入力してください。", t.errors.full_messages[0]
397
+ assert_equal "内容を入力してください。", t.errors.on(:content)
398
+
399
+ t.content = "like stuff"
400
+
401
+ assert t.save
402
+
403
+ GetText.set_locale_all "en"
404
+ t = Topic.create
405
+ assert !t.save
406
+ assert_equal "Title can't be blank", t.errors.full_messages[0]
407
+ assert_equal "Content can't be blank", t.errors.full_messages[1]
408
+ assert_equal "Title can't be blank", t.errors.on(:title)
409
+ assert_equal "Content can't be blank", t.errors.on(:content)
410
+
411
+ t.title = "something"
412
+ t.content = " "
413
+
414
+ assert !t.save
415
+ assert_equal "Content can't be blank", t.errors.full_messages[0]
416
+ assert_equal "Content can't be blank", t.errors.on(:content)
417
+
418
+ t.content = "like stuff"
419
+
420
+ assert t.save
421
+ end
422
+
423
+ def test_validate_uniqueness
424
+ Topic.validates_uniqueness_of(:title)
425
+
426
+ GetText.set_locale_all "ja_JP.UTF-8"
427
+ t = Topic.new("title" => "I'm unique!")
428
+ assert t.save, "Should save t as unique"
429
+
430
+ t.content = "Remaining unique"
431
+ assert t.save, "Should still save t as unique"
432
+
433
+ t2 = Topic.new("title" => "I'm unique!")
434
+ assert !t2.valid?, "Shouldn't be valid"
435
+ assert !t2.save, "Shouldn't save t2 as unique"
436
+ assert_equal "タイトルはすでに存在します。", t2.errors.full_messages[0]
437
+ assert_equal "タイトルはすでに存在します。", t2.errors.on(:title)
438
+
439
+ GetText.set_locale_all "en"
440
+ t = Topic.new("title" => "I'm unique2!")
441
+ assert t.save, "Should save t as unique"
442
+
443
+ t.content = "Remaining unique2"
444
+ assert t.save, "Should still save t as unique"
445
+
446
+ t2 = Topic.new("title" => "I'm unique2!")
447
+ assert !t2.valid?, "Shouldn't be valid"
448
+ assert !t2.save, "Shouldn't save t2 as unique"
449
+ assert_equal "Title has already been taken", t2.errors.full_messages[0]
450
+ assert_equal "Title has already been taken", t2.errors.on(:title)
451
+ end
452
+
453
+ def test_validate_format_fn
454
+ Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => N_("%{fn} is bad data"))
455
+
456
+ GetText.set_locale_all "ja_JP.UTF-8"
457
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
458
+ assert !t.valid?, "Shouldn't be valid"
459
+ assert !t.save, "Shouldn't save because it's invalid"
460
+ assert_equal "タイトルは悪いデータです。", t.errors.full_messages[0]
461
+ assert_equal "タイトルは悪いデータです。", t.errors.on(:title)
462
+ assert_nil t.errors.on(:content)
463
+
464
+ GetText.set_locale_all "en"
465
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
466
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
467
+ assert !t.valid?, "Shouldn't be valid"
468
+ assert !t.save, "Shouldn't save because it's invalid"
469
+ assert_equal "Title is bad data", t.errors.full_messages[0]
470
+ assert_equal "Title is bad data", t.errors.on(:title)
471
+ assert_nil t.errors.on(:content)
472
+ end
473
+
474
+ def test_validate_format
475
+ Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => N_("is bad data"))
476
+
477
+ GetText.set_locale_all "ja_JP.UTF-8"
478
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
479
+ assert !t.valid?, "Shouldn't be valid"
480
+ assert !t.save, "Shouldn't save because it's invalid"
481
+ assert_equal "タイトル は悪いデータです。", t.errors.full_messages[0]
482
+ assert_equal "は悪いデータです。", t.errors.on(:title)
483
+ assert_nil t.errors.on(:content)
484
+
485
+ GetText.set_locale_all "en"
486
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
487
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
488
+ assert !t.valid?, "Shouldn't be valid"
489
+ assert !t.save, "Shouldn't save because it's invalid"
490
+ assert_equal "Title is bad data", t.errors.full_messages[0]
491
+ assert_equal "is bad data", t.errors.on(:title)
492
+ assert_nil t.errors.on(:content)
493
+
494
+ t.title = "Validation macros rule!"
495
+
496
+ assert t.save
497
+ assert_nil t.errors.on(:title)
498
+
499
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
500
+ end
501
+
502
+ def test_validates_inclusion_of
503
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
504
+
505
+ GetText.set_locale_all "ja_JP.UTF-8"
506
+ t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
507
+ assert t.valid?
508
+ t.title = "uhoh"
509
+ assert !t.valid?
510
+ assert t.errors.on(:title)
511
+ assert_equal "タイトルは一覧にありません。", t.errors.full_messages[0]
512
+ assert_equal "タイトルは一覧にありません。", t.errors.on(:title)
513
+
514
+ GetText.set_locale_all "en"
515
+ t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
516
+ assert t.valid?
517
+ t.title = "uhoh"
518
+ assert !t.valid?
519
+ assert t.errors.on(:title)
520
+ assert_equal "Title is not included in the list", t.errors.full_messages[0]
521
+ assert_equal "Title is not included in the list", t.errors.on(:title)
522
+ end
523
+
524
+ def test_validates_length_of_using_minimum
525
+ Topic.validates_length_of :title, :minimum => 5
526
+
527
+ GetText.set_locale_all "ja_JP.UTF-8"
528
+ t = Topic.create("title" => "valid", "content" => "whatever")
529
+ assert t.valid?
530
+
531
+ t.title = "not"
532
+ assert !t.valid?
533
+ assert t.errors.on(:title)
534
+ assert_equal "タイトルは5文字以上で入力してください。", t.errors.full_messages[0]
535
+ assert_equal "タイトルは5文字以上で入力してください。", t.errors.on("title")
536
+
537
+ GetText.set_locale_all "en"
538
+ t = Topic.create("title" => "valid", "content" => "whatever")
539
+ assert t.valid?
540
+
541
+ t.title = "not"
542
+ assert !t.valid?
543
+ assert t.errors.on(:title)
544
+ assert_equal "Title is too short (minimum is 5 characters)", t.errors.full_messages[0]
545
+ assert_equal "Title is too short (minimum is 5 characters)", t.errors.on("title")
546
+ end
547
+
548
+ def test_validates_length_of_using_maximum
549
+ Topic.validates_length_of :title, :maximum => 5
550
+ GetText.set_locale_all "ja_JP.UTF-8"
551
+ t = Topic.create("title" => "valid", "content" => "whatever")
552
+ assert t.valid?
553
+
554
+ t.title = "notvalid"
555
+ assert !t.valid?
556
+ assert t.errors.on(:title)
557
+ assert_equal "タイトルは5文字以内で入力してください。", t.errors.full_messages[0]
558
+ assert_equal "タイトルは5文字以内で入力してください。", t.errors.on("title")
559
+
560
+ GetText.set_locale_all "en"
561
+ t = Topic.create("title" => "valid", "content" => "whatever")
562
+ assert t.valid?
563
+
564
+ t.title = "notvalid"
565
+ assert !t.valid?
566
+ assert t.errors.on(:title)
567
+ assert_equal "Title is too long (maximum is 5 characters)", t.errors.full_messages[0]
568
+ assert_equal "Title is too long (maximum is 5 characters)", t.errors.on("title")
569
+ end
570
+
571
+ def test_validates_length_of_using_within
572
+ Topic.validates_length_of(:title, :content, :within => 3..5)
573
+
574
+ GetText.set_locale_all "ja_JP.UTF-8"
575
+ t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
576
+ assert !t.valid?
577
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.full_messages[0]
578
+ assert_equal "内容は5文字以内で入力してください。", t.errors.full_messages[1]
579
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.on(:title)
580
+ assert_equal "内容は5文字以内で入力してください。", t.errors.on(:content)
581
+
582
+ t.title = nil
583
+ t.content = nil
584
+ assert !t.valid?
585
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.full_messages[0]
586
+ assert_equal "内容は3文字以上で入力してください。", t.errors.full_messages[1]
587
+ assert_equal "タイトルは3文字以上で入力してください。", t.errors.on(:title)
588
+ assert_equal "内容は3文字以上で入力してください。", t.errors.on(:content)
589
+
590
+ GetText.set_locale_all "en"
591
+ t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
592
+ assert !t.valid?
593
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.full_messages[0]
594
+ assert_equal "Content is too long (maximum is 5 characters)", t.errors.full_messages[1]
595
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.on(:title)
596
+ assert_equal "Content is too long (maximum is 5 characters)", t.errors.on(:content)
597
+
598
+ t.title = nil
599
+ t.content = nil
600
+ assert !t.valid?
601
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.full_messages[0]
602
+ assert_equal "Content is too short (minimum is 3 characters)", t.errors.full_messages[1]
603
+ assert_equal "Title is too short (minimum is 3 characters)", t.errors.on(:title)
604
+ assert_equal "Content is too short (minimum is 3 characters)", t.errors.on(:content)
605
+ end
606
+
607
+ def test_optionally_validates_length_of_using_within_on_create_fn
608
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => N_("my string(%{fn}) is too long: %d")
609
+
610
+ GetText.set_locale_all "ja_JP.UTF-8"
611
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
612
+ assert !t.save
613
+ assert t.errors.on(:title)
614
+ assert_equal "文字列:タイトルは長すぎ: 10", t.errors.full_messages[0]
615
+ assert_equal "文字列:タイトルは長すぎ: 10", t.errors.on(:title)
616
+
617
+ GetText.set_locale_all "en"
618
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
619
+ assert !t.save
620
+ assert t.errors.on(:title)
621
+ assert_equal "my string(Title) is too long: 10", t.errors.full_messages[0]
622
+ assert_equal "my string(Title) is too long: 10", t.errors.on(:title)
623
+ end
624
+
625
+ def test_optionally_validates_length_of_using_within_on_create
626
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => N_("my string is too long: %d")
627
+
628
+ GetText.set_locale_all "ja_JP.UTF-8"
629
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
630
+ assert !t.save
631
+ assert t.errors.on(:title)
632
+ assert_equal "タイトル 文字列は長すぎ: 10", t.errors.full_messages[0]
633
+ assert_equal "文字列は長すぎ: 10", t.errors.on(:title)
634
+
635
+ GetText.set_locale_all "en"
636
+ t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
637
+ assert !t.save
638
+ assert t.errors.on(:title)
639
+ assert_equal "Title my string is too long: 10", t.errors.full_messages[0]
640
+ assert_equal "my string is too long: 10", t.errors.on(:title)
641
+ end
642
+
643
+ def test_optionally_validates_length_of_using_within_on_update_fn
644
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => N_("my string(%{fn}) is too short: %d")
645
+ GetText.set_locale_all "ja_JP.UTF-8"
646
+ t = Topic.create("title" => "vali", "content" => "whatever")
647
+ assert !t.save
648
+ assert t.errors.on(:title)
649
+
650
+ t.title = "not"
651
+ assert !t.save
652
+ assert t.errors.on(:title)
653
+ assert_equal "文字列:タイトルは短すぎ: 5", t.errors.full_messages[0]
654
+ assert_equal "文字列:タイトルは短すぎ: 5", t.errors.on(:title)
655
+
656
+ GetText.set_locale_all "en"
657
+ t = Topic.create("title" => "vali", "content" => "whatever")
658
+ assert !t.save
659
+ assert t.errors.on(:title)
660
+
661
+ t.title = "not"
662
+ assert !t.save
663
+ assert t.errors.on(:title)
664
+ assert_equal "my string(Title) is too short: 5", t.errors.full_messages[0]
665
+ assert_equal "my string(Title) is too short: 5", t.errors.on(:title)
666
+
667
+ end
668
+
669
+ def test_optionally_validates_length_of_using_within_on_update
670
+ Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => N_("my string is too short: %d")
671
+ GetText.set_locale_all "ja_JP.UTF-8"
672
+ t = Topic.create("title" => "vali", "content" => "whatever")
673
+ assert !t.save
674
+ assert t.errors.on(:title)
675
+
676
+ t.title = "not"
677
+ assert !t.save
678
+ assert t.errors.on(:title)
679
+ assert_equal "タイトル 文字列は短すぎ: 5", t.errors.full_messages[0]
680
+ assert_equal "文字列は短すぎ: 5", t.errors.on(:title)
681
+
682
+ GetText.set_locale_all "en"
683
+ t = Topic.create("title" => "vali", "content" => "whatever")
684
+ assert !t.save
685
+ assert t.errors.on(:title)
686
+
687
+ t.title = "not"
688
+ assert !t.save
689
+ assert t.errors.on(:title)
690
+ assert_equal "Title my string is too short: 5", t.errors.full_messages[0]
691
+ assert_equal "my string is too short: 5", t.errors.on(:title)
692
+
693
+ end
694
+
695
+ def test_validates_length_of_using_is
696
+ Topic.validates_length_of :title, :is => 5
697
+
698
+ GetText.set_locale_all "ja_JP.UTF-8"
699
+ t = Topic.create("title" => "valid", "content" => "whatever")
700
+ assert t.valid?
701
+
702
+ t.title = "notvalid"
703
+ assert !t.valid?
704
+ assert t.errors.on(:title)
705
+ assert_equal "タイトルは5文字で入力してください。", t.errors.full_messages[0]
706
+ assert_equal "タイトルは5文字で入力してください。", t.errors.on("title")
707
+
708
+ GetText.set_locale_all "en"
709
+ t = Topic.create("title" => "valid", "content" => "whatever")
710
+ assert t.valid?
711
+
712
+ t.title = "notvalid"
713
+ assert !t.valid?
714
+ assert t.errors.on(:title)
715
+ assert_equal "Title is the wrong length (should be 5 characters)", t.errors.full_messages[0]
716
+ assert_equal "Title is the wrong length (should be 5 characters)", t.errors.on("title")
717
+
718
+ end
719
+
720
+ def test_validates_length_with_globaly_modified_error_message_fn
721
+ ActiveRecord::Errors.default_error_messages[:too_short] = N_('%{fn} %d dayo')
722
+ Topic.validates_length_of :title, :minimum => 10
723
+
724
+ GetText.set_locale_all "ja_JP.UTF-8"
725
+ t = Topic.create(:title => 'too short')
726
+ assert !t.valid?
727
+ assert_equal 'タイトルは10以上だよ。', t.errors.full_messages[0]
728
+ assert_equal 'タイトルは10以上だよ。', t.errors.on('title')
729
+
730
+ GetText.set_locale_all "en"
731
+ t = Topic.create(:title => 'too short')
732
+ assert !t.valid?
733
+ assert_equal 'Title 10 dayo', t.errors.full_messages[0]
734
+ assert_equal 'Title 10 dayo', t.errors.on('title')
735
+ end
736
+
737
+ def test_validates_length_with_globaly_modified_error_message
738
+ ActiveRecord::Errors.default_error_messages[:too_short] = N_('%d dayo')
739
+ Topic.validates_length_of :title, :minimum => 10
740
+
741
+ GetText.set_locale_all "ja_JP.UTF-8"
742
+ t = Topic.create(:title => 'too short')
743
+ assert !t.valid?
744
+ assert_equal 'タイトル は10以上だよ。', t.errors.full_messages[0]
745
+ assert_equal 'は10以上だよ。', t.errors.on('title')
746
+
747
+ GetText.set_locale_all "en"
748
+ t = Topic.create(:title => 'too short')
749
+ assert !t.valid?
750
+ assert_equal 'Title 10 dayo', t.errors.full_messages[0]
751
+ assert_equal '10 dayo', t.errors.on('title')
752
+ end
753
+
754
+ def test_validates_length_of_custom_errors_for_minimum_with_message_fn
755
+ Topic.validates_length_of( :title, :minimum => 5, :message => N_("%{fn} hoo %d") )
756
+ GetText.set_locale_all "ja_JP.UTF-8"
757
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
758
+ assert !t.valid?
759
+ assert t.errors.on(:title)
760
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
761
+ assert_equal "タイトルふー5", t.errors.on("title")
762
+
763
+ GetText.set_locale_all "en"
764
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
765
+ assert !t.valid?
766
+ assert t.errors.on(:title)
767
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
768
+ assert_equal "Title hoo 5", t.errors.on("title")
769
+ end
770
+
771
+ def test_validates_length_of_custom_errors_for_minimum_with_message
772
+ Topic.validates_length_of( :title, :minimum => 5, :message => N_("hoo %d") )
773
+ GetText.set_locale_all "ja_JP.UTF-8"
774
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
775
+ assert !t.valid?
776
+ assert t.errors.on(:title)
777
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
778
+ assert_equal "ふー5", t.errors.on("title")
779
+
780
+ GetText.set_locale_all "en"
781
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
782
+ assert !t.valid?
783
+ assert t.errors.on(:title)
784
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
785
+ assert_equal "hoo 5", t.errors.on("title")
786
+ end
787
+
788
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short_fn
789
+ Topic.validates_length_of( :title, :minimum=>5, :too_short => N_("%{fn} hoo %d") )
790
+
791
+ GetText.set_locale_all "ja_JP.UTF-8"
792
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
793
+ assert !t.valid?
794
+ assert t.errors.on(:title)
795
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
796
+ assert_equal "タイトルふー5", t.errors.on("title")
797
+
798
+ GetText.set_locale_all "en"
799
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
800
+ assert !t.valid?
801
+ assert t.errors.on(:title)
802
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
803
+ assert_equal "Title hoo 5", t.errors.on("title")
804
+ end
805
+
806
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short
807
+ Topic.validates_length_of( :title, :minimum=>5, :too_short => N_("hoo %d") )
808
+
809
+ GetText.set_locale_all "ja_JP.UTF-8"
810
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
811
+ assert !t.valid?
812
+ assert t.errors.on(:title)
813
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
814
+ assert_equal "ふー5", t.errors.on("title")
815
+
816
+ GetText.set_locale_all "en"
817
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
818
+ assert !t.valid?
819
+ assert t.errors.on(:title)
820
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
821
+ assert_equal "hoo 5", t.errors.on("title")
822
+ end
823
+
824
+ def test_validates_length_of_custom_errors_for_maximum_with_message_fn
825
+ Topic.validates_length_of( :title, :maximum=>5, :message => N_("%{fn} hoo %d"))
826
+
827
+ GetText.set_locale_all "ja_JP.UTF-8"
828
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
829
+ assert !t.valid?
830
+ assert t.errors.on(:title)
831
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
832
+ assert_equal "タイトルふー5", t.errors.on("title")
833
+
834
+ GetText.set_locale_all "en"
835
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
836
+ assert !t.valid?
837
+ assert t.errors.on(:title)
838
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
839
+ assert_equal "Title hoo 5", t.errors.on("title")
840
+ end
841
+
842
+ def test_validates_length_of_custom_errors_for_maximum_with_message
843
+ Topic.validates_length_of( :title, :maximum=>5, :message => N_("hoo %d"))
844
+
845
+ GetText.set_locale_all "ja_JP.UTF-8"
846
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
847
+ assert !t.valid?
848
+ assert t.errors.on(:title)
849
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
850
+ assert_equal "ふー5", t.errors.on("title")
851
+
852
+ GetText.set_locale_all "en"
853
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
854
+ assert !t.valid?
855
+ assert t.errors.on(:title)
856
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
857
+ assert_equal "hoo 5", t.errors.on("title")
858
+ end
859
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long_fn
860
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("%{fn} hoo %d"))
861
+
862
+ GetText.set_locale_all "ja_JP.UTF-8"
863
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
864
+ assert !t.valid?
865
+ assert t.errors.on(:title)
866
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
867
+ assert_equal "タイトルふー5", t.errors.on("title")
868
+
869
+ GetText.set_locale_all "en"
870
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
871
+ assert !t.valid?
872
+ assert t.errors.on(:title)
873
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
874
+ assert_equal "Title hoo 5", t.errors.on("title")
875
+ end
876
+
877
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long
878
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("hoo %d"))
879
+
880
+ GetText.set_locale_all "ja_JP.UTF-8"
881
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
882
+ assert !t.valid?
883
+ assert t.errors.on(:title)
884
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
885
+ assert_equal "ふー5", t.errors.on("title")
886
+
887
+ GetText.set_locale_all "en"
888
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
889
+ assert !t.valid?
890
+ assert t.errors.on(:title)
891
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
892
+ assert_equal "hoo 5", t.errors.on("title")
893
+ end
894
+
895
+ def test_validates_length_of_custom_errors_for_is_with_message_fn
896
+ Topic.validates_length_of( :title, :is=>5, :message=> N_("%{fn} hoo %d") )
897
+ GetText.set_locale_all "ja_JP.UTF-8"
898
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
899
+ assert !t.valid?
900
+ assert t.errors.on(:title)
901
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
902
+ assert_equal "タイトルふー5", t.errors.on("title")
903
+
904
+ GetText.set_locale_all "en"
905
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
906
+ assert !t.valid?
907
+ assert t.errors.on(:title)
908
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
909
+ assert_equal "Title hoo 5", t.errors.on("title")
910
+ end
911
+
912
+ def test_validates_length_of_custom_errors_for_is_with_message
913
+ Topic.validates_length_of( :title, :is=>5, :message=> N_("hoo %d") )
914
+ GetText.set_locale_all "ja_JP.UTF-8"
915
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
916
+ assert !t.valid?
917
+ assert t.errors.on(:title)
918
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
919
+ assert_equal "ふー5", t.errors.on("title")
920
+
921
+ GetText.set_locale_all "en"
922
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
923
+ assert !t.valid?
924
+ assert t.errors.on(:title)
925
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
926
+ assert_equal "hoo 5", t.errors.on("title")
927
+ end
928
+
929
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length_fn
930
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=> N_("hoo %d") )
931
+ GetText.set_locale_all "ja_JP.UTF-8"
932
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
933
+ assert !t.valid?
934
+ assert t.errors.on(:title)
935
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
936
+ assert_equal "ふー5", t.errors.on("title")
937
+
938
+ GetText.set_locale_all "en"
939
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
940
+ assert !t.valid?
941
+ assert t.errors.on(:title)
942
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
943
+ assert_equal "hoo 5", t.errors.on("title")
944
+ end
945
+
946
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length
947
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=> N_("hoo %d") )
948
+ GetText.set_locale_all "ja_JP.UTF-8"
949
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
950
+ assert !t.valid?
951
+ assert t.errors.on(:title)
952
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
953
+ assert_equal "ふー5", t.errors.on("title")
954
+
955
+ GetText.set_locale_all "en"
956
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
957
+ assert !t.valid?
958
+ assert t.errors.on(:title)
959
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
960
+ assert_equal "hoo 5", t.errors.on("title")
961
+ end
962
+
963
+ def test_validate_block_fn
964
+ Topic.validate { |topic| topic.errors.add("title", N_("%{fn} will never be valid")) }
965
+ GetText.set_locale_all "ja_JP.UTF-8"
966
+ t = Topic.create("title" => "Title", "content" => "whatever")
967
+ assert !t.valid?
968
+ assert t.errors.on(:title)
969
+ assert_equal "タイトルは決して正しくならないでしょう。", t.errors.full_messages[0]
970
+ assert_equal "タイトルは決して正しくならないでしょう。", t.errors.on("title")
971
+
972
+ GetText.set_locale_all "en"
973
+ t = Topic.create("title" => "Title", "content" => "whatever")
974
+ assert !t.valid?
975
+ assert t.errors.on(:title)
976
+ assert_equal "Title will never be valid", t.errors.full_messages[0]
977
+ assert_equal "Title will never be valid", t.errors.on("title")
978
+ end
979
+
980
+ def test_validate_block
981
+ Topic.validate { |topic| topic.errors.add("title", N_("will never be valid")) }
982
+ GetText.set_locale_all "ja_JP.UTF-8"
983
+ t = Topic.create("title" => "Title", "content" => "whatever")
984
+ assert !t.valid?
985
+ assert t.errors.on(:title)
986
+ assert_equal "タイトル は決して正しくならないでしょう。", t.errors.full_messages[0]
987
+ assert_equal "は決して正しくならないでしょう。", t.errors.on("title")
988
+
989
+ GetText.set_locale_all "en"
990
+ t = Topic.create("title" => "Title", "content" => "whatever")
991
+ assert !t.valid?
992
+ assert t.errors.on(:title)
993
+ assert_equal "Title will never be valid", t.errors.full_messages[0]
994
+ assert_equal "will never be valid", t.errors.on("title")
995
+ end
996
+
997
+ def test_validates_acceptance_of_with_custom_error_using_quotes_fn
998
+ Developer.validates_acceptance_of :salary, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
999
+
1000
+ GetText.set_locale_all "ja_JP.UTF-8"
1001
+ d = Developer.new
1002
+ d.salary = "0"
1003
+ assert !d.valid?
1004
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1005
+
1006
+ GetText.set_locale_all "en"
1007
+ d = Developer.new
1008
+ d.salary = "0"
1009
+ assert !d.valid?
1010
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1011
+ end
1012
+
1013
+ def test_validates_acceptance_of_with_custom_error_using_quotes
1014
+ Developer.validates_acceptance_of :salary, :message=> N_("This string contains 'single' and \"double\" quotes")
1015
+
1016
+ GetText.set_locale_all "ja_JP.UTF-8"
1017
+ d = Developer.new
1018
+ d.salary = "0"
1019
+ assert !d.valid?
1020
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1021
+
1022
+ GetText.set_locale_all "en"
1023
+ d = Developer.new
1024
+ d.salary = "0"
1025
+ assert !d.valid?
1026
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1027
+ end
1028
+
1029
+ def test_validates_confirmation_of_with_custom_error_using_quotes_fn
1030
+ Developer.validates_confirmation_of :name, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1031
+
1032
+ GetText.set_locale_all "ja_JP.UTF-8"
1033
+ d = Developer.new
1034
+ d.name = "John"
1035
+ d.name_confirmation = "Johnny"
1036
+ assert !d.valid?
1037
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1038
+
1039
+ GetText.set_locale_all "en"
1040
+ d = Developer.new
1041
+ d.name = "John"
1042
+ d.name_confirmation = "Johnny"
1043
+ assert !d.valid?
1044
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1045
+ end
1046
+
1047
+ def test_validates_confirmation_of_with_custom_error_using_quotes
1048
+ Developer.validates_confirmation_of :name, :message=> N_("This string contains 'single' and \"double\" quotes")
1049
+
1050
+ GetText.set_locale_all "ja_JP.UTF-8"
1051
+ d = Developer.new
1052
+ d.name = "John"
1053
+ d.name_confirmation = "Johnny"
1054
+ assert !d.valid?
1055
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name)
1056
+
1057
+ GetText.set_locale_all "en"
1058
+ d = Developer.new
1059
+ d.name = "John"
1060
+ d.name_confirmation = "Johnny"
1061
+ assert !d.valid?
1062
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
1063
+ end
1064
+
1065
+ def test_validates_format_of_with_custom_error_using_quotes_fn
1066
+ Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message => "%{fn} contains 'single' and \"double\" quotes"
1067
+
1068
+ GetText.set_locale_all "ja_JP.UTF-8"
1069
+ d = Developer.new
1070
+ d.name = "John 32"
1071
+ assert !d.valid?
1072
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1073
+
1074
+ GetText.set_locale_all "en"
1075
+ d = Developer.new
1076
+ d.name = "John 32"
1077
+ assert !d.valid?
1078
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1079
+ end
1080
+
1081
+ def test_validates_format_of_with_custom_error_using_quotes
1082
+ Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message => "This string contains 'single' and \"double\" quotes"
1083
+
1084
+ GetText.set_locale_all "ja_JP.UTF-8"
1085
+ d = Developer.new
1086
+ d.name = "John 32"
1087
+ assert !d.valid?
1088
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name)
1089
+
1090
+ GetText.set_locale_all "en"
1091
+ d = Developer.new
1092
+ d.name = "John 32"
1093
+ assert !d.valid?
1094
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name)
1095
+ end
1096
+
1097
+ def test_validates_inclusion_of_with_custom_error_using_quotes_fn
1098
+ Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1099
+
1100
+ GetText.set_locale_all "ja_JP.UTF-8"
1101
+ d = Developer.new
1102
+ d.salary = "90,000"
1103
+ assert !d.valid?
1104
+ assert_equal "給料は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1105
+ GetText.set_locale_all "en"
1106
+ d = Developer.new
1107
+ d.salary = "90,000"
1108
+ assert !d.valid?
1109
+ assert_equal "Salary contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1110
+ end
1111
+
1112
+ def test_validates_inclusion_of_with_custom_error_using_quotes
1113
+ Developer.validates_inclusion_of :salary, :in => 1000..80000, :message=> N_("This string contains 'single' and \"double\" quotes")
1114
+
1115
+ GetText.set_locale_all "ja_JP.UTF-8"
1116
+ d = Developer.new
1117
+ d.salary = "90,000"
1118
+ assert !d.valid?
1119
+
1120
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:salary).first
1121
+
1122
+ GetText.set_locale_all "en"
1123
+ d = Developer.new
1124
+ d.salary = "90,000"
1125
+ assert !d.valid?
1126
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).first
1127
+ end
1128
+
1129
+ def test_validates_length_of_with_custom_too_long_using_quotes_fn
1130
+ Developer.validates_length_of :name, :maximum => 4, :too_long=> N_("%{fn} contains 'single' and \"double\" quotes")
1131
+
1132
+ GetText.set_locale_all "ja_JP.UTF-8"
1133
+ d = Developer.new
1134
+ d.name = "Jeffrey"
1135
+ assert !d.valid?
1136
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1137
+
1138
+ GetText.set_locale_all "en"
1139
+ d = Developer.new
1140
+ d.name = "Jeffrey"
1141
+ assert !d.valid?
1142
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1143
+ end
1144
+
1145
+ def test_validates_length_of_with_custom_too_long_using_quotes
1146
+ Developer.validates_length_of :name, :maximum => 4, :too_long=> N_("This string contains 'single' and \"double\" quotes")
1147
+
1148
+ GetText.set_locale_all "ja_JP.UTF-8"
1149
+ d = Developer.new
1150
+ d.name = "Jeffrey"
1151
+ assert !d.valid?
1152
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1153
+
1154
+ GetText.set_locale_all "en"
1155
+ d = Developer.new
1156
+ d.name = "Jeffrey"
1157
+ assert !d.valid?
1158
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first
1159
+ end
1160
+
1161
+ def test_validates_length_of_with_custom_too_short_using_quotes_fn
1162
+ Developer.validates_length_of :name, :minimum => 4, :too_short=> N_("%{fn} contains 'single' and \"double\" quotes")
1163
+ GetText.set_locale_all "ja_JP.UTF-8"
1164
+ d = Developer.new
1165
+ d.name = "Joe"
1166
+ assert !d.valid?
1167
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1168
+
1169
+ GetText.set_locale_all "en"
1170
+ d = Developer.new
1171
+ d.name = "Joe"
1172
+ assert !d.valid?
1173
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1174
+ end
1175
+
1176
+ def test_validates_length_of_with_custom_too_short_using_quotes
1177
+ Developer.validates_length_of :name, :minimum => 4, :too_short=> N_("This string contains 'single' and \"double\" quotes")
1178
+ GetText.set_locale_all "ja_JP.UTF-8"
1179
+ d = Developer.new
1180
+ d.name = "Joe"
1181
+ assert !d.valid?
1182
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1183
+
1184
+ GetText.set_locale_all "en"
1185
+ d = Developer.new
1186
+ d.name = "Joe"
1187
+ assert !d.valid?
1188
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first
1189
+ end
1190
+
1191
+ def test_validates_length_of_with_custom_message_using_quotes_fn
1192
+ GetText.set_locale_all "ja_JP.UTF-8"
1193
+ Developer.validates_length_of :name, :minimum => 4, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1194
+ d = Developer.new
1195
+ d.name = "Joe"
1196
+ assert !d.valid?
1197
+ assert_equal "開発者名は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1198
+
1199
+ GetText.set_locale_all "en"
1200
+ d = Developer.new
1201
+ d.name = "Joe"
1202
+ assert !d.valid?
1203
+ assert_equal "Name contains 'single' and \"double\" quotes", d.errors.on(:name).first
1204
+ end
1205
+
1206
+ def test_validates_length_of_with_custom_message_using_quotes
1207
+ GetText.set_locale_all "ja_JP.UTF-8"
1208
+ Developer.validates_length_of :name, :minimum => 4, :message=> N_("This string contains 'single' and \"double\" quotes")
1209
+ d = Developer.new
1210
+ d.name = "Joe"
1211
+ assert !d.valid?
1212
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:name).first
1213
+
1214
+ GetText.set_locale_all "en"
1215
+ d = Developer.new
1216
+ d.name = "Joe"
1217
+ assert !d.valid?
1218
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first
1219
+ end
1220
+
1221
+ def test_validates_presence_of_with_custom_message_using_quotes_fn
1222
+ # This test depends on test_validates_presence_of_with_custom_message_using_quotes
1223
+ GetText.set_locale_all "ja_JP.UTF-8"
1224
+ Developer.validates_presence_of :non_existent, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1225
+ d = Developer.new
1226
+ d.name = "Joe"
1227
+ assert !d.valid?
1228
+
1229
+ assert_equal "存在しないは'シングル' \"ダブル\"クオートを含む。", d.errors.on(:non_existent).first
1230
+
1231
+ GetText.set_locale_all "en"
1232
+ d = Developer.new
1233
+ d.name = "Joe"
1234
+ assert !d.valid?
1235
+ assert_equal "Non existent contains 'single' and \"double\" quotes", d.errors.on(:non_existent).first
1236
+ end
1237
+
1238
+ def test_validates_presence_of_with_custom_message_using_quotes
1239
+ GetText.set_locale_all "ja_JP.UTF-8"
1240
+ Developer.validates_presence_of :non_existent, :message=> N_("This string contains 'single' and \"double\" quotes")
1241
+ d = Developer.new
1242
+ d.name = "Joe"
1243
+ assert !d.valid?
1244
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", d.errors.on(:non_existent)
1245
+
1246
+ GetText.set_locale_all "en"
1247
+ d = Developer.new
1248
+ d.name = "Joe"
1249
+ assert !d.valid?
1250
+ assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent)
1251
+ end
1252
+
1253
+ def test_validates_uniqueness_of_with_custom_message_using_quotes_fn
1254
+ Developer.validates_uniqueness_of :name, :message=> N_("%{fn} contains 'single' and \"double\" quotes")
1255
+
1256
+ GetText.set_locale_all "ja_JP.UTF-8"
1257
+ d = Developer.new
1258
+ d.name = "David"
1259
+ assert !d.valid?
1260
+ assert_equal d.errors.on(:name).first, "開発者名は'シングル' \"ダブル\"クオートを含む。"
1261
+
1262
+ GetText.set_locale_all "en"
1263
+ d = Developer.new
1264
+ d.name = "David"
1265
+ assert !d.valid?
1266
+ assert_equal d.errors.on(:name).first, "Name contains 'single' and \"double\" quotes"
1267
+ end
1268
+
1269
+ def test_validates_uniqueness_of_with_custom_message_using_quotes
1270
+ Developer.validates_uniqueness_of :name, :message=> N_("This string contains 'single' and \"double\" quotes")
1271
+
1272
+ GetText.set_locale_all "ja_JP.UTF-8"
1273
+ d = Developer.new
1274
+ d.name = "David"
1275
+ assert !d.valid?
1276
+ assert_equal d.errors.on(:name).first, "この文字列は'シングル' \"ダブル\"クオートを含む。"
1277
+
1278
+ GetText.set_locale_all "en"
1279
+ d = Developer.new
1280
+ d.name = "David"
1281
+ assert !d.valid?
1282
+ assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes"
1283
+ end
1284
+
1285
+ def test_validates_associated_with_custom_message_using_quotes_fn
1286
+ Reply.validates_associated( :topic )
1287
+ Topic.validates_presence_of( :content )
1288
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1289
+ r.topic = Topic.create("title" => "uhohuhoh")
1290
+ assert !r.valid?
1291
+ assert r.errors.on(:topic)
1292
+ r.topic.content = "non-empty"
1293
+ assert r.valid?
1294
+
1295
+ GetText.set_locale_all "ja_JP.UTF-8"
1296
+ Reply.validates_associated :topic, :message => N_("%{fn} contains 'single' and \"double\" quotes")
1297
+ Topic.validates_presence_of :content
1298
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1299
+ r.topic = Topic.create("title" => "uhohuhoh")
1300
+ assert !r.valid?
1301
+ assert_equal "トピックは'シングル' \"ダブル\"クオートを含む。", r.errors.full_messages[0]
1302
+ assert_equal "トピックは'シングル' \"ダブル\"クオートを含む。", r.errors.on(:topic).first
1303
+
1304
+ GetText.set_locale_all "en"
1305
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1306
+ r.topic = Topic.create("title" => "uhohuhoh")
1307
+ assert !r.valid?
1308
+ assert_equal "Topic contains 'single' and \"double\" quotes", r.errors.full_messages[0]
1309
+ assert_equal "Topic contains 'single' and \"double\" quotes", r.errors.on(:topic).first
1310
+ end
1311
+
1312
+ def test_validates_associated_with_custom_message_using_quotes
1313
+ Reply.validates_associated( :topic )
1314
+ Topic.validates_presence_of( :content )
1315
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1316
+ r.topic = Topic.create("title" => "uhohuhoh")
1317
+ assert !r.valid?
1318
+ assert r.errors.on(:topic)
1319
+ r.topic.content = "non-empty"
1320
+ assert r.valid?
1321
+
1322
+ GetText.set_locale_all "ja_JP.UTF-8"
1323
+ Reply.validates_associated :topic, :message => N_("This string contains 'single' and \"double\" quotes")
1324
+ Topic.validates_presence_of :content
1325
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1326
+ r.topic = Topic.create("title" => "uhohuhoh")
1327
+ assert !r.valid?
1328
+ assert_equal "トピック この文字列は'シングル' \"ダブル\"クオートを含む。", r.errors.full_messages[0]
1329
+ assert_equal "この文字列は'シングル' \"ダブル\"クオートを含む。", r.errors.on(:topic).first
1330
+
1331
+ GetText.set_locale_all "en"
1332
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1333
+ r.topic = Topic.create("title" => "uhohuhoh")
1334
+ assert !r.valid?
1335
+ assert_equal "Topic This string contains 'single' and \"double\" quotes", r.errors.full_messages[0]
1336
+ assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).first
1337
+ end
1338
+
1339
+ def test_conditional_validation_using_method_true_fn
1340
+ # When the method returns true
1341
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("%{fn} hoo %d"), :if => :condition_is_true )
1342
+ GetText.set_locale_all "ja_JP.UTF-8"
1343
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1344
+ assert !t.valid?
1345
+ assert t.errors.on(:title)
1346
+ assert_equal "タイトルふー5", t.errors.on("title")
1347
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1348
+
1349
+ GetText.set_locale_all "en"
1350
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1351
+ assert !t.valid?
1352
+ assert t.errors.on(:title)
1353
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1354
+ assert_equal "Title hoo 5", t.errors.on("title")
1355
+ end
1356
+
1357
+ def test_conditional_validation_using_method_true
1358
+ # When the method returns true
1359
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("hoo %d"), :if => :condition_is_true )
1360
+ GetText.set_locale_all "ja_JP.UTF-8"
1361
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1362
+ assert !t.valid?
1363
+ assert t.errors.on(:title)
1364
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1365
+ assert_equal "ふー5", t.errors.on("title")
1366
+
1367
+ GetText.set_locale_all "en"
1368
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1369
+ assert !t.valid?
1370
+ assert t.errors.on(:title)
1371
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1372
+ assert_equal "hoo 5", t.errors.on("title")
1373
+ end
1374
+
1375
+ def test_conditional_validation_using_string_true_fn
1376
+ # When the evaluated string returns true
1377
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("%{fn} hoo %d"), :if => "a = 1; a == 1" )
1378
+
1379
+ GetText.set_locale_all "ja_JP.UTF-8"
1380
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1381
+ assert !t.valid?
1382
+ assert t.errors.on(:title)
1383
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1384
+ assert_equal "タイトルふー5", t.errors.on("title")
1385
+
1386
+ GetText.set_locale_all "en"
1387
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1388
+ assert !t.valid?
1389
+ assert t.errors.on(:title)
1390
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1391
+ assert_equal "Title hoo 5", t.errors.on("title")
1392
+ end
1393
+
1394
+ def test_conditional_validation_using_string_true
1395
+ # When the evaluated string returns true
1396
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=> N_("hoo %d"), :if => "a = 1; a == 1" )
1397
+
1398
+ GetText.set_locale_all "ja_JP.UTF-8"
1399
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1400
+ assert !t.valid?
1401
+ assert t.errors.on(:title)
1402
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1403
+ assert_equal "ふー5", t.errors.on("title")
1404
+
1405
+ GetText.set_locale_all "en"
1406
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1407
+ assert !t.valid?
1408
+ assert t.errors.on(:title)
1409
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1410
+ assert_equal "hoo 5", t.errors.on("title")
1411
+ end
1412
+
1413
+ def test_conditional_validation_using_block_true_fn
1414
+ # When the block returns true
1415
+ GetText.set_locale_all "ja_JP.UTF-8"
1416
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("%{fn} hoo %d"),
1417
+ :if => Proc.new { |r| r.content.size > 4 } )
1418
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1419
+ assert !t.valid?
1420
+ assert t.errors.on(:title)
1421
+ assert_equal "タイトルふー5", t.errors.full_messages[0]
1422
+ assert_equal "タイトルふー5", t.errors.on("title")
1423
+
1424
+ GetText.set_locale_all "en"
1425
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1426
+ assert !t.valid?
1427
+ assert t.errors.on(:title)
1428
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1429
+ assert_equal "Title hoo 5", t.errors.on("title")
1430
+
1431
+ end
1432
+
1433
+ def test_conditional_validation_using_block_true
1434
+ # When the block returns true
1435
+ GetText.set_locale_all "ja_JP.UTF-8"
1436
+ Topic.validates_length_of( :title, :maximum=>5, :too_long => N_("hoo %d"),
1437
+ :if => Proc.new { |r| r.content.size > 4 } )
1438
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1439
+ assert !t.valid?
1440
+ assert t.errors.on(:title)
1441
+ assert_equal "タイトル ふー5", t.errors.full_messages[0]
1442
+ assert_equal "ふー5", t.errors.on("title")
1443
+
1444
+ GetText.set_locale_all "en"
1445
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
1446
+ assert !t.valid?
1447
+ assert t.errors.on(:title)
1448
+ assert_equal "Title hoo 5", t.errors.full_messages[0]
1449
+ assert_equal "hoo 5", t.errors.on("title")
1450
+
1451
+ end
1452
+
1453
+ def test_validates_associated_missing
1454
+ GetText.set_locale_all "ja_JP.UTF-8"
1455
+ Reply.validates_presence_of(:topic)
1456
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1457
+ # In this case, rgettext doesn't pick up the names of relations as msgid,
1458
+ # so you need to define N_() in the model class
1459
+ assert_equal "トピックを入力してください。", r.errors.on(:topic)
1460
+
1461
+ GetText.set_locale_all "en"
1462
+ r = Reply.create("title" => "A reply", "content" => "with content!")
1463
+ assert_equal "Topic can't be blank", r.errors.on(:topic)
1464
+ assert_equal "Topic can't be blank", r.errors.full_messages[0]
1465
+ end
1466
+
1467
+ def test_default_validates_numericality_of
1468
+ GetText.set_locale_all "ja_JP.UTF-8"
1469
+ Topic.validates_numericality_of :approved
1470
+ topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => "aaa")
1471
+ assert_equal "承認は数値で入力してください。", topic.errors.on(:approved)
1472
+
1473
+ GetText.set_locale_all "en"
1474
+ topic = Topic.create("title" => "numeric test", "content" => "whatever", "approved" => "aaa")
1475
+ assert_equal "Approved is not a number", topic.errors.on(:approved)
1476
+ assert_equal "Approved is not a number", topic.errors.full_messages[0]
1477
+ end
1478
+ end
1479
+