motion_support 0.0.1 → 0.0.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.
@@ -0,0 +1,535 @@
1
+ describe "Inflector" do
2
+
3
+ it "should pluarlize 'pluarls'" do
4
+ MotionSupport::Inflector.pluralize("plurals").should.equal("plurals")
5
+ MotionSupport::Inflector.pluralize("Plurals").should.equal("Plurals")
6
+ end
7
+
8
+ it "should return empty string with empty string" do
9
+ MotionSupport::Inflector.pluralize("").should.equal("")
10
+ end
11
+
12
+ MotionSupport::Inflector.inflections.uncountable.each do |word|
13
+ it "should not puralize #{word}" do
14
+ MotionSupport::Inflector.singularize(word).should.equal(word)
15
+ MotionSupport::Inflector.pluralize(word).should.equal(word)
16
+ MotionSupport::Inflector.singularize(word).should.equal(MotionSupport::Inflector.pluralize(word))
17
+ end
18
+ end
19
+
20
+ it "uncountables should not be gready" do
21
+ with_dup do
22
+ uncountable_word = "ors"
23
+ countable_word = "sponsor"
24
+
25
+ MotionSupport::Inflector.inflections.uncountable << uncountable_word
26
+
27
+ MotionSupport::Inflector.singularize(uncountable_word).should.equal(uncountable_word)
28
+ MotionSupport::Inflector.pluralize(uncountable_word).should.equal(uncountable_word)
29
+ MotionSupport::Inflector.singularize(uncountable_word).should.equal(MotionSupport::Inflector.pluralize(uncountable_word))
30
+ MotionSupport::Inflector.singularize(countable_word).should.equal "sponsor"
31
+ MotionSupport::Inflector.pluralize(countable_word).should.equal("sponsors")
32
+ MotionSupport::Inflector.singularize(MotionSupport::Inflector.pluralize(countable_word)).should.equal("sponsor")
33
+ end
34
+ end
35
+
36
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
37
+ it "Should pluralize singular #{singular}" do
38
+ MotionSupport::Inflector.pluralize(singular).should.equal(plural)
39
+ MotionSupport::Inflector.pluralize(singular.capitalize).should.equal(plural.capitalize)
40
+ end
41
+ end
42
+
43
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
44
+ it "Should singularize plural #{plural}" do
45
+ MotionSupport::Inflector.singularize(plural).should.equal(singular)
46
+ MotionSupport::Inflector.singularize(plural.capitalize).should.equal(singular.capitalize)
47
+ end
48
+ end
49
+
50
+
51
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
52
+ it "Should singularize singular #{singular}" do
53
+ MotionSupport::Inflector.singularize(singular).should.equal(singular)
54
+ MotionSupport::Inflector.singularize(singular.capitalize).should.equal(singular.capitalize)
55
+ end
56
+ end
57
+
58
+ it "should overwite previous inflections" do
59
+ MotionSupport::Inflector.singularize("series").should.equal("series")
60
+ MotionSupport::Inflector.inflections.singular "series", "serie"
61
+ MotionSupport::Inflector.singularize("series").should.equal("serie")
62
+ MotionSupport::Inflector.inflections.uncountable "series" # return to normal
63
+ end
64
+
65
+
66
+ InflectorTestCases::MixtureToTitleCase.each do |before, titleized|
67
+ it "should titelize_#{before}" do
68
+ MotionSupport::Inflector.titleize(before).should.equal(titleized)
69
+ end
70
+ end
71
+
72
+ describe ".camelize" do
73
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
74
+ it "should camelize #{camel}" do
75
+ MotionSupport::Inflector.camelize(underscore).should.equal(camel)
76
+ end
77
+ end
78
+
79
+ it "should camelize with underscores" do
80
+ MotionSupport::Inflector.camelize('Camel_Case').should.equal("CamelCase")
81
+ end
82
+
83
+ it "shoudl camelize with lower downcases for the first letter" do
84
+ MotionSupport::Inflector.camelize('Capital', false).should.equal("capital")
85
+ end
86
+ end
87
+
88
+ describe "acronyms" do
89
+ MotionSupport::Inflector.inflections do |inflect|
90
+ inflect.acronym("API")
91
+ inflect.acronym("HTML")
92
+ inflect.acronym("HTTP")
93
+ inflect.acronym("RESTful")
94
+ inflect.acronym("W3C")
95
+ inflect.acronym("PhD")
96
+ inflect.acronym("RoR")
97
+ inflect.acronym("SSL")
98
+ ## camelize underscore humanize titleize
99
+ [
100
+ ["API", "api", "API", "API"],
101
+ ["APIController", "api_controller", "API controller", "API Controller"],
102
+ ["Nokogiri::HTML", "nokogiri/html", "Nokogiri/HTML", "Nokogiri/HTML"],
103
+ ["HTTPAPI", "http_api", "HTTP API", "HTTP API"],
104
+ ["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
105
+ ["SSLError", "ssl_error", "SSL error", "SSL Error"],
106
+ ["RESTful", "restful", "RESTful", "RESTful"],
107
+ ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
108
+ ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
109
+ ["PhDRequired", "phd_required", "PhD required", "PhD Required"],
110
+ ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
111
+ ["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
112
+
113
+ # misdirection
114
+ ["Capistrano", "capistrano", "Capistrano", "Capistrano"],
115
+ ["CapiController", "capi_controller", "Capi controller", "Capi Controller"],
116
+ ["HttpsApis", "https_apis", "Https apis", "Https Apis"],
117
+ ["Html5", "html5", "Html5", "Html5"],
118
+ ["Restfully", "restfully", "Restfully", "Restfully"],
119
+ ["RoRails", "ro_rails", "Ro rails", "Ro Rails"]
120
+ ].each do |camel, under, human, title|
121
+ it "should camelize #{under} to equal #{camel}" do
122
+ MotionSupport::Inflector.camelize(under).should.equal(camel)
123
+ end
124
+ it "should camelize #{camel} to equal #{camel}" do
125
+ MotionSupport::Inflector.camelize(camel).should.equal(camel)
126
+ end
127
+ it "should underscore #{under} to equal #{under}" do
128
+ MotionSupport::Inflector.underscore(under).should.equal(under)
129
+ end
130
+ it "should underscore #{camel} to equal #{under}" do
131
+ MotionSupport::Inflector.underscore(camel).should.equal(under)
132
+ end
133
+ it "should titleize #{under} to equal #{title}" do
134
+ MotionSupport::Inflector.titleize(under).should.equal(title)
135
+ end
136
+ it "should titleize #{camel} to equal #{title}" do
137
+ MotionSupport::Inflector.titleize(camel).should.equal(title)
138
+ end
139
+ it "should humanize #{under} to equal #{human}" do
140
+ MotionSupport::Inflector.humanize(under).should.equal(human)
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ describe "acronym override" do
147
+ MotionSupport::Inflector.inflections do |inflect|
148
+ inflect.acronym("API")
149
+ inflect.acronym("LegacyApi")
150
+ end
151
+
152
+ it "should camelize 'legacyapi' to be 'LegacyApi'" do
153
+ MotionSupport::Inflector.camelize("legacyapi").should.equal("LegacyApi")
154
+ end
155
+
156
+ it "should camelize 'legacy_api' to be 'LegacyAPI'" do
157
+ MotionSupport::Inflector.camelize("legacy_api").should.equal("LegacyAPI")
158
+ end
159
+
160
+ it "should camelize 'legacy_api' to be 'LegacyAPI'" do
161
+ MotionSupport::Inflector.camelize("some_legacyapi").should.equal("SomeLegacyApi")
162
+ end
163
+
164
+ it "should camelize 'legacy_api' to be 'LegacyAPI'" do
165
+ MotionSupport::Inflector.camelize("nonlegacyapi").should.equal("Nonlegacyapi")
166
+ end
167
+ end
168
+
169
+ describe "acronyms camelize lower" do
170
+ MotionSupport::Inflector.inflections do |inflect|
171
+ inflect.acronym("API")
172
+ inflect.acronym("HTML")
173
+ end
174
+
175
+ it "should camelize html_api with false to be 'htmlAPI'" do
176
+ MotionSupport::Inflector.camelize("html_api", false).should.equal("htmlAPI")
177
+ end
178
+
179
+ it "should camelize html_api with false to be 'htmlAPI'" do
180
+ MotionSupport::Inflector.camelize("htmlAPI", false).should.equal("htmlAPI")
181
+ end
182
+
183
+ it "should camelize html_api with false to be 'htmlAPI'" do
184
+ MotionSupport::Inflector.camelize("HTMLAPI", false).should.equal("htmlAPI")
185
+ end
186
+ end
187
+
188
+ describe "underscore acronym sequence" do
189
+ before do
190
+ with_dup do
191
+ MotionSupport::Inflector.inflections do |inflect|
192
+ inflect.acronym("API")
193
+ inflect.acronym("HTML5")
194
+ inflect.acronym("HTML")
195
+ end
196
+ end
197
+ end
198
+
199
+ it "should underscore 'HTML5HTMLAPI' to equal 'html5_html_api'" do
200
+ MotionSupport::Inflector.underscore("HTML5HTMLAPI").should.equal("html5_html_api")
201
+ end
202
+ end
203
+
204
+ describe "underscore" do
205
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
206
+ it "should underscore #{camel} to equal #{underscore}" do
207
+ MotionSupport::Inflector.underscore(camel).should.equal(underscore)
208
+ end
209
+ end
210
+ end
211
+
212
+ describe "camelize with module" do
213
+ InflectorTestCases::CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
214
+ it "should camelize #{underscore} to equal #{camel}" do
215
+ MotionSupport::Inflector.camelize(underscore).should.equal(camel)
216
+ end
217
+ end
218
+ end
219
+
220
+ describe "underscore with slashes" do
221
+ InflectorTestCases::CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
222
+ it "should underscore #{underscore} to equal #{camel}" do
223
+ MotionSupport::Inflector.camelize(underscore).should.equal(camel)
224
+ end
225
+ end
226
+ end
227
+
228
+ describe "demodulize" do
229
+ it "should demodulize 'MyApplication::Billing::Account'" do
230
+ MotionSupport::Inflector.demodulize("MyApplication::Billing::Account").should.equal("Account")
231
+ end
232
+
233
+ it "should demodulize 'Account' to 'Account'" do
234
+ MotionSupport::Inflector.demodulize("Account").should.equal("Account")
235
+ end
236
+
237
+ it "should demodulize to empty string with empty string" do
238
+ MotionSupport::Inflector.demodulize("").should.equal("")
239
+ end
240
+ end
241
+
242
+ describe "deconstantize" do
243
+ it "should deconstantize 'MyApplication::Billing::Account' to equal 'MyApplication::Billing'" do
244
+ MotionSupport::Inflector.deconstantize("MyApplication::Billing::Account").should.equal("MyApplication::Billing")
245
+ end
246
+
247
+ it "should deconstantize '::MyApplication::Billing::Account' to equal '::MyApplication::Billing'" do
248
+ MotionSupport::Inflector.deconstantize("::MyApplication::Billing::Account").should.equal("::MyApplication::Billing")
249
+ end
250
+
251
+ it "should deconstantize 'MyApplication::Billing' to equal 'MyApplication'" do
252
+ MotionSupport::Inflector.deconstantize("MyApplication::Billing").should.equal("MyApplication")
253
+ end
254
+
255
+ it "should deconstantize '::MyApplication::Billing' to equal '::MyApplication'" do
256
+ MotionSupport::Inflector.deconstantize("::MyApplication::Billing").should.equal("::MyApplication")
257
+ end
258
+
259
+ it "should deconstantize 'Account' to equal ''" do
260
+ MotionSupport::Inflector.deconstantize("Account").should.equal("")
261
+ end
262
+
263
+ it "should deconstantize '::Account' to equal ''" do
264
+ MotionSupport::Inflector.deconstantize("::Account").should.equal("")
265
+ end
266
+
267
+ it "should deconstantize '' to equal ''" do
268
+ MotionSupport::Inflector.deconstantize("").should.equal("")
269
+ end
270
+ end
271
+
272
+ describe ".foreign_key" do
273
+ InflectorTestCases::ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
274
+ it "should give #{foreign_key} with given klass #{klass}" do
275
+ MotionSupport::Inflector.foreign_key(klass).should.equal(foreign_key)
276
+ end
277
+ end
278
+
279
+ InflectorTestCases::ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
280
+ it "should give foreign_key for #{klass} as #{foreign_key}" do
281
+ MotionSupport::Inflector.foreign_key(klass, false).should.equal(foreign_key)
282
+ end
283
+ end
284
+ end
285
+
286
+ describe "tableize" do
287
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
288
+ it "should tableize #{class_name} to #{table_name}" do
289
+ MotionSupport::Inflector.tableize(class_name).should.equal(table_name)
290
+ end
291
+ end
292
+ end
293
+
294
+ describe "classify" do
295
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
296
+ it "should classify #{table_name} to equal #{class_name}" do
297
+ MotionSupport::Inflector.classify(table_name).should.equal(class_name)
298
+ end
299
+
300
+ it "should classify table_prefix.#{table_name} to equal #{class_name}" do
301
+ MotionSupport::Inflector.classify("table_prefix." + table_name).should.equal(class_name)
302
+ end
303
+ end
304
+ end
305
+
306
+ describe "classify with symbol" do
307
+ it "should not raise nothing with :foo_bars" do
308
+ lambda{MotionSupport::Inflector.classify(:foo_bars)}.should.not.raise(Exception)
309
+ end
310
+
311
+ it "should give 'FooBar' given 'foo_bars'" do
312
+ MotionSupport::Inflector.classify(:foo_bars).should.equal("FooBar")
313
+ end
314
+ end
315
+
316
+ describe "classify with leading schema name" do
317
+ it "should classify 'schema.foo_bar' to equal 'FooBar'" do
318
+ MotionSupport::Inflector.classify('schema.foo_bar').should.equal("FooBar")
319
+ end
320
+ end
321
+
322
+ describe "hamanize" do
323
+ InflectorTestCases::UnderscoreToHuman.each do |underscore, human|
324
+ it "should humanize #{underscore} to equal #{human}" do
325
+ MotionSupport::Inflector.humanize(underscore).should.equal(human)
326
+ end
327
+ end
328
+ end
329
+
330
+ describe "humanize by rule" do
331
+ before do
332
+ with_dup do
333
+ MotionSupport::Inflector.inflections do |inflect|
334
+ inflect.human(/_cnt$/i, '\1_count')
335
+ inflect.human(/^prefx_/i, '\1')
336
+ end
337
+ end
338
+ end
339
+
340
+ it "should humanize 'jargon_cnt' to equal 'Jargon count'" do
341
+ MotionSupport::Inflector.humanize("jargon_cnt").should.equal("Jargon count")
342
+ end
343
+
344
+ it "should humanize 'prefx_request' to equal 'Request'" do
345
+ MotionSupport::Inflector.humanize("prefx_request").should.equal("Request")
346
+ end
347
+ end
348
+
349
+ describe "humanize by string" do
350
+ before do
351
+ with_dup do
352
+ MotionSupport::Inflector.inflections do |inflect|
353
+ inflect.human("col_rpted_bugs", "Reported bugs")
354
+ end
355
+ end
356
+ end
357
+
358
+ it "should humanize 'col_rpted_bugs' to equal 'Reported bugs'" do
359
+ MotionSupport::Inflector.humanize("col_rpted_bugs").should.equal("Reported bugs")
360
+ end
361
+ it "should humaize 'COL_rpted_bugs' to equal 'Col rpted bugs'" do
362
+ MotionSupport::Inflector.humanize("COL_rpted_bugs").should.equal('Col rpted bugs')
363
+ end
364
+ end
365
+
366
+ describe "constantize" do
367
+ #def test_constantize
368
+ #run_constantize_tests_on do |string|
369
+ #ActiveSupport::Inflector.constantize(string)
370
+ #end
371
+ #end
372
+
373
+ #def test_safe_constantize
374
+ #run_safe_constantize_tests_on do |string|
375
+ #ActiveSupport::Inflector.safe_constantize(string)
376
+ #end
377
+ #end
378
+ end
379
+
380
+ describe "ordinal" do
381
+ InflectorTestCases::OrdinalNumbers.each do |number, ordinalized|
382
+ it "should ordinal #{number} to equal #{ordinalized}" do
383
+ (number + MotionSupport::Inflector.ordinal(number)).should.equal(ordinalized)
384
+ end
385
+ end
386
+ end
387
+
388
+ describe "ordinalize" do
389
+ InflectorTestCases::OrdinalNumbers.each do |number, ordinalized|
390
+ it "should ordinalized #{number} to equal #{ordinalized}" do
391
+ MotionSupport::Inflector.ordinalize(number).should.equal(ordinalized)
392
+ end
393
+ end
394
+ end
395
+
396
+ describe "dasherize" do
397
+ InflectorTestCases::UnderscoresToDashes.each do |underscored, dasherized|
398
+ it "should dasherize '#{underscored}' to equal '#{dasherized}'" do
399
+ MotionSupport::Inflector.dasherize(underscored).should.equal(dasherized)
400
+ end
401
+ end
402
+ end
403
+
404
+ describe "reverse of dasherize" do
405
+ InflectorTestCases::UnderscoresToDashes.each do |underscored, dasherized|
406
+ it "should underscore dasherized '#{underscored}' to equal '#{underscored}'" do
407
+ MotionSupport::Inflector.underscore(MotionSupport::Inflector.dasherize(underscored)).should.equal(underscored)
408
+ end
409
+ end
410
+ end
411
+
412
+ describe "camelize with lower" do
413
+ InflectorTestCases::UnderscoreToLowerCamel.each do |underscored, lower_camel|
414
+ it "should lower camelized '#{underscored}' to equal '#{lower_camel}'" do
415
+ MotionSupport::Inflector.camelize(underscored, false).should.equal(lower_camel)
416
+ end
417
+ end
418
+ end
419
+
420
+ describe "symbol_to_lower_camel" do
421
+ InflectorTestCases::SymbolToLowerCamel.each do |symbol, lower_camel|
422
+ it "should camelize symbol '#{symbol}' to equal '#{lower_camel}'" do
423
+ MotionSupport::Inflector.camelize(symbol, false).should.equal(lower_camel)
424
+ end
425
+ end
426
+ end
427
+
428
+ %w{plurals singulars uncountables humans}.each do |inflection_type|
429
+ describe "clearing #{inflection_type} inflection type" do
430
+ before do
431
+ MotionSupport::Inflector.inflections.clear(inflection_type)
432
+ end
433
+ it "should have empty #{inflection_type}" do
434
+ MotionSupport::Inflector.inflections.send(inflection_type).empty?.should.equal(true)
435
+ end
436
+ end
437
+ end
438
+
439
+ describe "clearing all" do
440
+ before do
441
+ MotionSupport::Inflector.inflections do |inflect|
442
+ ## ensure any data is present
443
+ inflect.plural(/(quiz)$/i, '\1zes')
444
+ inflect.singular(/(database)s$/i, '\1')
445
+ inflect.uncountable('series')
446
+ inflect.human("col_rpted_bugs", "Reported bugs")
447
+
448
+ inflect.clear :all
449
+ end
450
+ end
451
+
452
+ it "should have empty pluarals" do
453
+ MotionSupport::Inflector.inflections.plurals.empty?.should.equal(true)
454
+ end
455
+ it "should have empty singulars" do
456
+ MotionSupport::Inflector.inflections.singulars.empty?.should.equal(true)
457
+ end
458
+ it "should have empty uncountables" do
459
+ MotionSupport::Inflector.inflections.uncountables.empty?.should.equal(true)
460
+ end
461
+ it "should have empty humans" do
462
+ MotionSupport::Inflector.inflections.humans.empty?.should.equal(true)
463
+ end
464
+ end
465
+
466
+ describe "Irregularities" do
467
+ InflectorTestCases::Irregularities.each do |irregularity|
468
+ singular, plural = *irregularity
469
+
470
+ MotionSupport::Inflector.inflections do |inflect|
471
+ it "should singularize #{plural} to equal #{singular}" do
472
+ inflect.irregular(singular, plural)
473
+ MotionSupport::Inflector.singularize(plural).should.equal(singular)
474
+ end
475
+
476
+ it "should pluralize #{singular} to equal #{plural}" do
477
+ inflect.irregular(singular, plural)
478
+ MotionSupport::Inflector.pluralize(singular).should.equal(plural)
479
+ end
480
+ end
481
+ end
482
+
483
+ InflectorTestCases::Irregularities.each do |irregularity|
484
+ singular, plural = *irregularity
485
+
486
+ MotionSupport::Inflector.inflections do |inflect|
487
+ it "should singularize #{plural} to equal #{singular}" do
488
+ inflect.irregular(singular, plural)
489
+ MotionSupport::Inflector.singularize(plural).should.equal(singular)
490
+ end
491
+
492
+ it "should pluralize #{singular} to equal #{plural}" do
493
+ inflect.irregular(singular, plural)
494
+ MotionSupport::Inflector.pluralize(singular).should.equal(plural)
495
+ end
496
+ end
497
+ end
498
+ end
499
+
500
+
501
+ [ :all, [] ].each do |scope|
502
+ MotionSupport::Inflector.inflections do |inflect|
503
+ describe "clearing inflections with #{scope.kind_of?(Array) ? "no_arguments" : scope}" do
504
+ before do
505
+ @singulars, @plurals, @uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
506
+ inflect.clear(*scope)
507
+ end
508
+
509
+ it "should have empty singular" do
510
+ inflect.singulars.should.equal([])
511
+ end
512
+
513
+ it "should have empty plurals" do
514
+ inflect.plurals.should.equal([])
515
+ end
516
+
517
+ it "should have empty uncountables" do
518
+ inflect.uncountables.should.equal([])
519
+ end
520
+
521
+ describe "restore all the inflections" do
522
+ before do
523
+ @singulars.reverse.each { |singular| inflect.singular(*singular) }
524
+ @plurals.reverse.each { |plural| inflect.plural(*plural) }
525
+ inflect.uncountable(@uncountables)
526
+ end
527
+
528
+ it "should restore singulars" do
529
+ inflect.singulars.should.equal(@singulars)
530
+ end
531
+ end
532
+ end
533
+ end
534
+ end
535
+ end