opal-activesupport 0.3.1 → 0.3.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/README.md +4 -1
- data/Rakefile +22 -6
- data/lib/opal/activesupport/version.rb +1 -1
- data/opal-activesupport.gemspec +1 -0
- data/opal/active_support/concern.rb +152 -0
- data/opal/active_support/core_ext/string.rb +1 -38
- data/opal/active_support/core_ext/string/filters.rb +146 -0
- data/opal/active_support/core_ext/string/inflections.rb +66 -4
- data/opal/active_support/inflections.rb +2 -0
- data/opal/active_support/inflector.rb +5 -2
- data/opal/active_support/inflector/inflections.rb +64 -66
- data/opal/active_support/inflector/methods.rb +198 -0
- data/test/abstract_unit.rb +22 -5
- data/test/concern_test.rb +144 -0
- data/test/constantize_test_cases.rb +121 -0
- data/test/core_ext/numeric_ext_test.rb +1 -1
- data/test/core_ext/string_ext_test.rb +675 -304
- data/test/inflector_test.rb +578 -0
- data/test/inflector_test_cases.rb +91 -31
- metadata +27 -4
@@ -0,0 +1,578 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "abstract_unit"
|
4
|
+
require "active_support/inflector"
|
5
|
+
|
6
|
+
require "inflector_test_cases"
|
7
|
+
require "constantize_test_cases"
|
8
|
+
|
9
|
+
class InflectorTest < ActiveSupport::TestCase
|
10
|
+
include InflectorTestCases
|
11
|
+
include ConstantizeTestCases
|
12
|
+
|
13
|
+
def setup
|
14
|
+
# Dups the singleton before each test, restoring the original inflections later.
|
15
|
+
#
|
16
|
+
# This helper is implemented by setting @__instance__ because in some tests
|
17
|
+
# there are module functions that access ActiveSupport::Inflector.inflections,
|
18
|
+
# so we need to replace the singleton itself.
|
19
|
+
@original_inflections = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
|
20
|
+
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections.dup)
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_pluralize_plurals
|
28
|
+
assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
|
29
|
+
assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_pluralize_empty_string
|
33
|
+
assert_equal "", ActiveSupport::Inflector.pluralize("")
|
34
|
+
end
|
35
|
+
|
36
|
+
test "uncountability of ascii word" do
|
37
|
+
word = "HTTP"
|
38
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
39
|
+
inflect.uncountable word
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal word, ActiveSupport::Inflector.pluralize(word)
|
43
|
+
assert_equal word, ActiveSupport::Inflector.singularize(word)
|
44
|
+
assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
|
45
|
+
|
46
|
+
ActiveSupport::Inflector.inflections.uncountables.pop
|
47
|
+
end
|
48
|
+
|
49
|
+
test "uncountability of non-ascii word" do
|
50
|
+
word = "猫"
|
51
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
52
|
+
inflect.uncountable word
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_equal word, ActiveSupport::Inflector.pluralize(word)
|
56
|
+
assert_equal word, ActiveSupport::Inflector.singularize(word)
|
57
|
+
assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
|
58
|
+
|
59
|
+
ActiveSupport::Inflector.inflections.uncountables.pop
|
60
|
+
end
|
61
|
+
|
62
|
+
ActiveSupport::Inflector.inflections.uncountable.each do |word|
|
63
|
+
define_method "test_uncountability_of_#{word}" do
|
64
|
+
assert_equal word, ActiveSupport::Inflector.singularize(word)
|
65
|
+
assert_equal word, ActiveSupport::Inflector.pluralize(word)
|
66
|
+
assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_uncountable_word_is_not_greedy
|
71
|
+
uncountable_word = "ors"
|
72
|
+
countable_word = "sponsor"
|
73
|
+
|
74
|
+
ActiveSupport::Inflector.inflections.uncountable << uncountable_word
|
75
|
+
|
76
|
+
assert_equal uncountable_word, ActiveSupport::Inflector.singularize(uncountable_word)
|
77
|
+
assert_equal uncountable_word, ActiveSupport::Inflector.pluralize(uncountable_word)
|
78
|
+
assert_equal ActiveSupport::Inflector.pluralize(uncountable_word), ActiveSupport::Inflector.singularize(uncountable_word)
|
79
|
+
|
80
|
+
assert_equal "sponsor", ActiveSupport::Inflector.singularize(countable_word)
|
81
|
+
assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
|
82
|
+
assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
|
83
|
+
end
|
84
|
+
|
85
|
+
SingularToPlural.each do |singular, plural|
|
86
|
+
define_method "test_pluralize_singular_#{singular}" do
|
87
|
+
assert_equal(plural, ActiveSupport::Inflector.pluralize(singular))
|
88
|
+
assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
SingularToPlural.each do |singular, plural|
|
93
|
+
define_method "test_singularize_plural_#{plural}" do
|
94
|
+
assert_equal(singular, ActiveSupport::Inflector.singularize(plural))
|
95
|
+
assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
SingularToPlural.each do |singular, plural|
|
100
|
+
define_method "test_pluralize_plural_#{plural}" do
|
101
|
+
assert_equal(plural, ActiveSupport::Inflector.pluralize(plural))
|
102
|
+
assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize))
|
103
|
+
end
|
104
|
+
|
105
|
+
define_method "test_singularize_singular_#{singular}" do
|
106
|
+
assert_equal(singular, ActiveSupport::Inflector.singularize(singular))
|
107
|
+
assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(singular.capitalize))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_overwrite_previous_inflectors
|
112
|
+
assert_equal("series", ActiveSupport::Inflector.singularize("series"))
|
113
|
+
ActiveSupport::Inflector.inflections.singular "series", "serie"
|
114
|
+
assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
|
115
|
+
end
|
116
|
+
|
117
|
+
MixtureToTitleCase.each_with_index do |(before, titleized), index|
|
118
|
+
define_method "test_titleize_mixture_to_title_case_#{index}" do
|
119
|
+
assert_equal(titleized, ActiveSupport::Inflector.titleize(before), "mixture \
|
120
|
+
to TitleCase failed for #{before}")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
MixtureToTitleCaseWithKeepIdSuffix.each_with_index do |(before, titleized), index|
|
125
|
+
define_method "test_titleize_with_keep_id_suffix_mixture_to_title_case_#{index}" do
|
126
|
+
assert_equal(titleized, ActiveSupport::Inflector.titleize(before, keep_id_suffix: true),
|
127
|
+
"mixture to TitleCase with keep_id_suffix failed for #{before}")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_camelize
|
132
|
+
CamelToUnderscore.each do |camel, underscore|
|
133
|
+
assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Broken: to pass this test we need a better regexps suport
|
138
|
+
# def test_camelize_with_lower_downcases_the_first_letter
|
139
|
+
# assert_equal("capital", ActiveSupport::Inflector.camelize("Capital", false))
|
140
|
+
# end
|
141
|
+
|
142
|
+
def test_camelize_with_underscores
|
143
|
+
assert_equal("CamelCase", ActiveSupport::Inflector.camelize("Camel_Case"))
|
144
|
+
end
|
145
|
+
|
146
|
+
# Broken: this method is not implemented
|
147
|
+
# def test_acronyms
|
148
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
149
|
+
# inflect.acronym("API")
|
150
|
+
# inflect.acronym("HTML")
|
151
|
+
# inflect.acronym("HTTP")
|
152
|
+
# inflect.acronym("RESTful")
|
153
|
+
# inflect.acronym("W3C")
|
154
|
+
# inflect.acronym("PhD")
|
155
|
+
# inflect.acronym("RoR")
|
156
|
+
# inflect.acronym("SSL")
|
157
|
+
# end
|
158
|
+
|
159
|
+
# # camelize underscore humanize titleize
|
160
|
+
# [
|
161
|
+
# ["API", "api", "API", "API"],
|
162
|
+
# ["APIController", "api_controller", "API controller", "API Controller"],
|
163
|
+
# ["Nokogiri::HTML", "nokogiri/html", "Nokogiri/HTML", "Nokogiri/HTML"],
|
164
|
+
# ["HTTPAPI", "http_api", "HTTP API", "HTTP API"],
|
165
|
+
# ["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
|
166
|
+
# ["SSLError", "ssl_error", "SSL error", "SSL Error"],
|
167
|
+
# ["RESTful", "restful", "RESTful", "RESTful"],
|
168
|
+
# ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
|
169
|
+
# ["Nested::RESTful", "nested/restful", "Nested/RESTful", "Nested/RESTful"],
|
170
|
+
# ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
|
171
|
+
# ["PhDRequired", "phd_required", "PhD required", "PhD Required"],
|
172
|
+
# ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
|
173
|
+
# ["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
|
174
|
+
# ["HTTP::RESTful", "http/restful", "HTTP/RESTful", "HTTP/RESTful"],
|
175
|
+
# ["HTTP::RESTfulAPI", "http/restful_api", "HTTP/RESTful API", "HTTP/RESTful API"],
|
176
|
+
# ["APIRESTful", "api_restful", "API RESTful", "API RESTful"],
|
177
|
+
|
178
|
+
# # misdirection
|
179
|
+
# ["Capistrano", "capistrano", "Capistrano", "Capistrano"],
|
180
|
+
# ["CapiController", "capi_controller", "Capi controller", "Capi Controller"],
|
181
|
+
# ["HttpsApis", "https_apis", "Https apis", "Https Apis"],
|
182
|
+
# ["Html5", "html5", "Html5", "Html5"],
|
183
|
+
# ["Restfully", "restfully", "Restfully", "Restfully"],
|
184
|
+
# ["RoRails", "ro_rails", "Ro rails", "Ro Rails"]
|
185
|
+
# ].each do |camel, under, human, title|
|
186
|
+
# assert_equal(camel, ActiveSupport::Inflector.camelize(under))
|
187
|
+
# assert_equal(camel, ActiveSupport::Inflector.camelize(camel))
|
188
|
+
# assert_equal(under, ActiveSupport::Inflector.underscore(under))
|
189
|
+
# assert_equal(under, ActiveSupport::Inflector.underscore(camel))
|
190
|
+
# assert_equal(title, ActiveSupport::Inflector.titleize(under))
|
191
|
+
# assert_equal(title, ActiveSupport::Inflector.titleize(camel))
|
192
|
+
# assert_equal(human, ActiveSupport::Inflector.humanize(under))
|
193
|
+
# end
|
194
|
+
# end
|
195
|
+
|
196
|
+
# Broken: this method is not implemented
|
197
|
+
# def test_acronym_override
|
198
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
199
|
+
# inflect.acronym("API")
|
200
|
+
# inflect.acronym("LegacyApi")
|
201
|
+
# end
|
202
|
+
|
203
|
+
# assert_equal("LegacyApi", ActiveSupport::Inflector.camelize("legacyapi"))
|
204
|
+
# assert_equal("LegacyAPI", ActiveSupport::Inflector.camelize("legacy_api"))
|
205
|
+
# assert_equal("SomeLegacyApi", ActiveSupport::Inflector.camelize("some_legacyapi"))
|
206
|
+
# assert_equal("Nonlegacyapi", ActiveSupport::Inflector.camelize("nonlegacyapi"))
|
207
|
+
# end
|
208
|
+
|
209
|
+
# Broken: this method is not implemented
|
210
|
+
# def test_acronyms_camelize_lower
|
211
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
212
|
+
# inflect.acronym("API")
|
213
|
+
# inflect.acronym("HTML")
|
214
|
+
# end
|
215
|
+
|
216
|
+
# assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("html_api", false))
|
217
|
+
# assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("htmlAPI", false))
|
218
|
+
# assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("HTMLAPI", false))
|
219
|
+
# end
|
220
|
+
|
221
|
+
# Broken: this method is not implemented
|
222
|
+
# def test_underscore_acronym_sequence
|
223
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
224
|
+
# inflect.acronym("API")
|
225
|
+
# inflect.acronym("JSON")
|
226
|
+
# inflect.acronym("HTML")
|
227
|
+
# end
|
228
|
+
|
229
|
+
# assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI"))
|
230
|
+
# end
|
231
|
+
|
232
|
+
# Broken: this method is not implemented
|
233
|
+
# def test_acronym_regexp_is_deprecated
|
234
|
+
# assert_deprecated do
|
235
|
+
# ActiveSupport::Inflector.inflections.acronym_regex
|
236
|
+
# end
|
237
|
+
# end
|
238
|
+
|
239
|
+
def test_underscore
|
240
|
+
CamelToUnderscore.each do |camel, underscore|
|
241
|
+
assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
|
242
|
+
end
|
243
|
+
CamelToUnderscoreWithoutReverse.each do |camel, underscore|
|
244
|
+
assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_camelize_with_module
|
249
|
+
CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
250
|
+
assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_underscore_with_slashes
|
255
|
+
CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
256
|
+
assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_demodulize
|
261
|
+
assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
|
262
|
+
assert_equal "Account", ActiveSupport::Inflector.demodulize("Account")
|
263
|
+
assert_equal "Account", ActiveSupport::Inflector.demodulize("::Account")
|
264
|
+
assert_equal "", ActiveSupport::Inflector.demodulize("")
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_deconstantize
|
268
|
+
assert_equal "MyApplication::Billing", ActiveSupport::Inflector.deconstantize("MyApplication::Billing::Account")
|
269
|
+
assert_equal "::MyApplication::Billing", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing::Account")
|
270
|
+
|
271
|
+
assert_equal "MyApplication", ActiveSupport::Inflector.deconstantize("MyApplication::Billing")
|
272
|
+
assert_equal "::MyApplication", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing")
|
273
|
+
|
274
|
+
assert_equal "", ActiveSupport::Inflector.deconstantize("Account")
|
275
|
+
assert_equal "", ActiveSupport::Inflector.deconstantize("::Account")
|
276
|
+
assert_equal "", ActiveSupport::Inflector.deconstantize("")
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_foreign_key
|
280
|
+
ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
|
281
|
+
assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
|
282
|
+
end
|
283
|
+
|
284
|
+
ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
|
285
|
+
assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_tableize
|
290
|
+
ClassNameToTableName.each do |class_name, table_name|
|
291
|
+
assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# Broken: this method is not implemented
|
296
|
+
# def test_parameterize
|
297
|
+
# StringToParameterized.each do |some_string, parameterized_string|
|
298
|
+
# assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
|
299
|
+
# end
|
300
|
+
# end
|
301
|
+
|
302
|
+
# Broken: this method is not implemented
|
303
|
+
# def test_parameterize_and_normalize
|
304
|
+
# StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
|
305
|
+
# assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
|
306
|
+
# end
|
307
|
+
# end
|
308
|
+
|
309
|
+
# Broken: this method is not implemented
|
310
|
+
# def test_parameterize_with_custom_separator
|
311
|
+
# StringToParameterizeWithUnderscore.each do |some_string, parameterized_string|
|
312
|
+
# assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string, separator: "_"))
|
313
|
+
# end
|
314
|
+
# end
|
315
|
+
|
316
|
+
# Broken: this method is not implemented
|
317
|
+
# def test_parameterize_with_multi_character_separator
|
318
|
+
# StringToParameterized.each do |some_string, parameterized_string|
|
319
|
+
# assert_equal(parameterized_string.gsub("-", "__sep__"), ActiveSupport::Inflector.parameterize(some_string, separator: "__sep__"))
|
320
|
+
# end
|
321
|
+
# end
|
322
|
+
|
323
|
+
def test_classify
|
324
|
+
ClassNameToTableName.each do |class_name, table_name|
|
325
|
+
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
|
326
|
+
assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_classify_with_symbol
|
331
|
+
assert_nothing_raised do
|
332
|
+
assert_equal "FooBar", ActiveSupport::Inflector.classify(:foo_bars)
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_classify_with_leading_schema_name
|
337
|
+
assert_equal "FooBar", ActiveSupport::Inflector.classify("schema.foo_bar")
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_humanize
|
341
|
+
UnderscoreToHuman.each do |underscore, human|
|
342
|
+
assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_humanize_without_capitalize
|
347
|
+
UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
|
348
|
+
assert_equal(human, ActiveSupport::Inflector.humanize(underscore, capitalize: false))
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_humanize_with_keep_id_suffix
|
353
|
+
UnderscoreToHumanWithKeepIdSuffix.each do |underscore, human|
|
354
|
+
assert_equal(human, ActiveSupport::Inflector.humanize(underscore, keep_id_suffix: true))
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_humanize_by_rule
|
359
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
360
|
+
inflect.human(/_cnt$/i, '\1_count')
|
361
|
+
inflect.human(/^prefx_/i, '\1')
|
362
|
+
end
|
363
|
+
assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
|
364
|
+
assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_humanize_by_string
|
368
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
369
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
370
|
+
end
|
371
|
+
assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
|
372
|
+
assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
|
373
|
+
end
|
374
|
+
|
375
|
+
# Broken: .acronym is not implemented
|
376
|
+
# def test_humanize_with_acronyms
|
377
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
378
|
+
# inflect.acronym "LAX"
|
379
|
+
# inflect.acronym "SFO"
|
380
|
+
# end
|
381
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("LAX ROUNDTRIP TO SFO"))
|
382
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("LAX ROUNDTRIP TO SFO", capitalize: false))
|
383
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("lax roundtrip to sfo"))
|
384
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("lax roundtrip to sfo", capitalize: false))
|
385
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("Lax Roundtrip To Sfo"))
|
386
|
+
# assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("Lax Roundtrip To Sfo", capitalize: false))
|
387
|
+
# end
|
388
|
+
|
389
|
+
def test_constantize
|
390
|
+
run_constantize_tests_on do |string|
|
391
|
+
ActiveSupport::Inflector.constantize(string)
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_safe_constantize
|
396
|
+
run_safe_constantize_tests_on do |string|
|
397
|
+
ActiveSupport::Inflector.safe_constantize(string)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
def test_ordinal
|
402
|
+
OrdinalNumbers.each do |number, ordinalized|
|
403
|
+
assert_equal(ordinalized, number + ActiveSupport::Inflector.ordinal(number))
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_ordinalize
|
408
|
+
OrdinalNumbers.each do |number, ordinalized|
|
409
|
+
assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
def test_dasherize
|
414
|
+
UnderscoresToDashes.each do |underscored, dasherized|
|
415
|
+
assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_underscore_as_reverse_of_dasherize
|
420
|
+
UnderscoresToDashes.each_key do |underscored|
|
421
|
+
assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_underscore_to_lower_camel
|
426
|
+
UnderscoreToLowerCamel.each do |underscored, lower_camel|
|
427
|
+
assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_symbol_to_lower_camel
|
432
|
+
SymbolToLowerCamel.each do |symbol, lower_camel|
|
433
|
+
assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
# %w{plurals singulars uncountables humans}.each do |inflection_type|
|
438
|
+
# class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
439
|
+
# def test_clear_#{inflection_type}
|
440
|
+
# ActiveSupport::Inflector.inflections.clear :#{inflection_type}
|
441
|
+
# assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
|
442
|
+
# end
|
443
|
+
# RUBY
|
444
|
+
# end
|
445
|
+
|
446
|
+
def test_inflector_locality
|
447
|
+
ActiveSupport::Inflector.inflections(:es) do |inflect|
|
448
|
+
inflect.plural(/$/, "s")
|
449
|
+
inflect.plural(/z$/i, "ces")
|
450
|
+
|
451
|
+
inflect.singular(/s$/, "")
|
452
|
+
inflect.singular(/es$/, "")
|
453
|
+
|
454
|
+
inflect.irregular("el", "los")
|
455
|
+
|
456
|
+
inflect.uncountable("agua")
|
457
|
+
end
|
458
|
+
|
459
|
+
assert_equal("hijos", "hijo".pluralize(:es))
|
460
|
+
assert_equal("luces", "luz".pluralize(:es))
|
461
|
+
assert_equal("luzs", "luz".pluralize)
|
462
|
+
|
463
|
+
assert_equal("sociedad", "sociedades".singularize(:es))
|
464
|
+
assert_equal("sociedade", "sociedades".singularize)
|
465
|
+
|
466
|
+
assert_equal("los", "el".pluralize(:es))
|
467
|
+
assert_equal("els", "el".pluralize)
|
468
|
+
|
469
|
+
assert_equal("agua", "agua".pluralize(:es))
|
470
|
+
assert_equal("aguas", "agua".pluralize)
|
471
|
+
|
472
|
+
ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }
|
473
|
+
|
474
|
+
assert_empty ActiveSupport::Inflector.inflections(:es).plurals
|
475
|
+
assert_empty ActiveSupport::Inflector.inflections(:es).singulars
|
476
|
+
assert_empty ActiveSupport::Inflector.inflections(:es).uncountables
|
477
|
+
assert_not_empty ActiveSupport::Inflector.inflections.plurals
|
478
|
+
assert_not_empty ActiveSupport::Inflector.inflections.singulars
|
479
|
+
assert_not_empty ActiveSupport::Inflector.inflections.uncountables
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_clear_all
|
483
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
484
|
+
# ensure any data is present
|
485
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
486
|
+
inflect.singular(/(database)s$/i, '\1')
|
487
|
+
inflect.uncountable("series")
|
488
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
489
|
+
|
490
|
+
inflect.clear :all
|
491
|
+
|
492
|
+
assert_empty inflect.plurals
|
493
|
+
assert_empty inflect.singulars
|
494
|
+
assert_empty inflect.uncountables
|
495
|
+
assert_empty inflect.humans
|
496
|
+
end
|
497
|
+
end
|
498
|
+
|
499
|
+
def test_clear_with_default
|
500
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
501
|
+
# ensure any data is present
|
502
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
503
|
+
inflect.singular(/(database)s$/i, '\1')
|
504
|
+
inflect.uncountable("series")
|
505
|
+
inflect.human("col_rpted_bugs", "Reported bugs")
|
506
|
+
|
507
|
+
inflect.clear
|
508
|
+
|
509
|
+
assert_empty inflect.plurals
|
510
|
+
assert_empty inflect.singulars
|
511
|
+
assert_empty inflect.uncountables
|
512
|
+
assert_empty inflect.humans
|
513
|
+
end
|
514
|
+
end
|
515
|
+
|
516
|
+
Irregularities.each do |singular, plural|
|
517
|
+
define_method("test_irregularity_between_#{singular}_and_#{plural}") do
|
518
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
519
|
+
inflect.irregular(singular, plural)
|
520
|
+
assert_equal singular, ActiveSupport::Inflector.singularize(plural)
|
521
|
+
assert_equal plural, ActiveSupport::Inflector.pluralize(singular)
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
Irregularities.each do |singular, plural|
|
527
|
+
define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
|
528
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
529
|
+
inflect.irregular(singular, plural)
|
530
|
+
assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
|
531
|
+
end
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
Irregularities.each do |singular, plural|
|
536
|
+
define_method("test_singularize_of_irregularity_#{singular}_should_be_the_same") do
|
537
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
538
|
+
inflect.irregular(singular, plural)
|
539
|
+
assert_equal singular, ActiveSupport::Inflector.singularize(singular)
|
540
|
+
end
|
541
|
+
end
|
542
|
+
end
|
543
|
+
|
544
|
+
[ :all, [] ].each do |scope|
|
545
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
546
|
+
define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do
|
547
|
+
# save all the inflections
|
548
|
+
singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
|
549
|
+
|
550
|
+
# clear all the inflections
|
551
|
+
inflect.clear(*scope)
|
552
|
+
|
553
|
+
assert_equal [], inflect.singulars
|
554
|
+
assert_equal [], inflect.plurals
|
555
|
+
assert_equal [], inflect.uncountables
|
556
|
+
|
557
|
+
# restore all the inflections
|
558
|
+
singulars.reverse_each { |singular| inflect.singular(*singular) }
|
559
|
+
plurals.reverse_each { |plural| inflect.plural(*plural) }
|
560
|
+
inflect.uncountable(uncountables)
|
561
|
+
|
562
|
+
assert_equal singulars, inflect.singulars
|
563
|
+
assert_equal plurals, inflect.plurals
|
564
|
+
assert_equal uncountables, inflect.uncountables
|
565
|
+
end
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
%w(plurals singulars uncountables humans).each do |scope|
|
570
|
+
define_method("test_clear_inflections_with_#{scope}") do
|
571
|
+
# clear the inflections
|
572
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
573
|
+
inflect.clear(scope)
|
574
|
+
assert_equal [], inflect.send(scope)
|
575
|
+
end
|
576
|
+
end
|
577
|
+
end
|
578
|
+
end
|