Skip to content
This repository
Browse code

Full test coverage for Inflector. Closes #7228.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6075 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit b0ed5057e01b78f2f7e0e078c0534647b425c290 1 parent 269ad97
Jeremy Kemper jeremy authored
2  activesupport/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Full test coverage for Inflector. #7228 [Dan Kubb]
  4 +
3 5 * :db format for Date#to_s [Jeremy Kemper]
4 6 Date.new(2007, 1, 27).to_s(:db) # => '2007-01-27'
5 7
5 activesupport/lib/active_support/inflector.rb
@@ -243,12 +243,11 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
243 243 # "Module".constantize #=> Module
244 244 # "Class".constantize #=> Class
245 245 def constantize(camel_cased_word)
246   - unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word
  246 + unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
247 247 raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
248 248 end
249 249
250   - camel_cased_word = "::#{camel_cased_word}" unless $1
251   - Object.module_eval(camel_cased_word, __FILE__, __LINE__)
  250 + Object.module_eval("::#{$1}", __FILE__, __LINE__)
252 251 end
253 252
254 253 # Ordinalize turns a number into an ordinal string used to denote the
20 activesupport/test/core_ext/string_ext_test.rb
@@ -16,6 +16,12 @@ def test_singularize
16 16 end
17 17 end
18 18
  19 + def test_titleize
  20 + InflectorTest::MixtureToTitleCase.each do |before, titleized|
  21 + assert_equal(titleized, before.titleize)
  22 + end
  23 + end
  24 +
19 25 def test_camelize
20 26 InflectorTest::CamelToUnderscore.each do |camel, underscore|
21 27 assert_equal(camel, underscore.camelize)
@@ -31,8 +37,14 @@ def test_underscore
31 37 assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
32 38 end
33 39
  40 + def test_underscore_to_lower_camel
  41 + InflectorTest::UnderscoreToLowerCamel.each do |underscored, lower_camel|
  42 + assert_equal(lower_camel, underscored.camelize(:lower))
  43 + end
  44 + end
  45 +
34 46 def test_demodulize
35   - assert_equal "Account", Inflector.demodulize("MyApplication::Billing::Account")
  47 + assert_equal "Account", "MyApplication::Billing::Account".demodulize
36 48 end
37 49
38 50 def test_foreign_key
@@ -57,6 +69,12 @@ def test_classify
57 69 end
58 70 end
59 71
  72 + def test_humanize
  73 + InflectorTest::UnderscoreToHuman.each do |underscore, human|
  74 + assert_equal(human, underscore.humanize)
  75 + end
  76 + end
  77 +
60 78 def test_string_to_time
61 79 assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
62 80 assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
141 activesupport/test/inflector_test.rb
@@ -70,39 +70,39 @@ class InflectorTest < Test::Unit::TestCase
70 70
71 71 "perspective" => "perspectives",
72 72
73   - "ox" => "oxen",
74   - "photo" => "photos",
75   - "buffalo" => "buffaloes",
76   - "tomato" => "tomatoes",
77   - "dwarf" => "dwarves",
78   - "elf" => "elves",
  73 + "ox" => "oxen",
  74 + "photo" => "photos",
  75 + "buffalo" => "buffaloes",
  76 + "tomato" => "tomatoes",
  77 + "dwarf" => "dwarves",
  78 + "elf" => "elves",
79 79 "information" => "information",
80   - "equipment" => "equipment",
81   - "bus" => "buses",
82   - "status" => "statuses",
  80 + "equipment" => "equipment",
  81 + "bus" => "buses",
  82 + "status" => "statuses",
83 83 "status_code" => "status_codes",
84   - "mouse" => "mice",
  84 + "mouse" => "mice",
85 85
86   - "louse" => "lice",
87   - "house" => "houses",
88   - "octopus" => "octopi",
89   - "virus" => "viri",
90   - "alias" => "aliases",
91   - "portfolio" => "portfolios",
  86 + "louse" => "lice",
  87 + "house" => "houses",
  88 + "octopus" => "octopi",
  89 + "virus" => "viri",
  90 + "alias" => "aliases",
  91 + "portfolio" => "portfolios",
92 92
93   - "vertex" => "vertices",
94   - "matrix" => "matrices",
  93 + "vertex" => "vertices",
  94 + "matrix" => "matrices",
95 95
96   - "axis" => "axes",
97   - "testis" => "testes",
98   - "crisis" => "crises",
  96 + "axis" => "axes",
  97 + "testis" => "testes",
  98 + "crisis" => "crises",
99 99
100   - "rice" => "rice",
101   - "shoe" => "shoes",
  100 + "rice" => "rice",
  101 + "shoe" => "shoes",
102 102
103   - "horse" => "horses",
104   - "prize" => "prizes",
105   - "edge" => "edges"
  103 + "horse" => "horses",
  104 + "prize" => "prizes",
  105 + "edge" => "edges"
106 106 }
107 107
108 108 CamelToUnderscore = {
@@ -190,6 +190,9 @@ class InflectorTest < Test::Unit::TestCase
190 190 "103" => "103rd",
191 191 "104" => "104th",
192 192 "110" => "110th",
  193 + "111" => "111th",
  194 + "112" => "112th",
  195 + "113" => "113th",
193 196 "1000" => "1000th",
194 197 "1001" => "1001st"
195 198 }
@@ -200,6 +203,14 @@ class InflectorTest < Test::Unit::TestCase
200 203 "person_street_address" => "person-street-address"
201 204 }
202 205
  206 + Irregularities = {
  207 + 'person' => 'people',
  208 + 'man' => 'men',
  209 + 'child' => 'children',
  210 + 'sex' => 'sexes',
  211 + 'move' => 'moves',
  212 + }
  213 +
203 214 def test_pluralize_plurals
204 215 assert_equal "plurals", Inflector.pluralize("plurals")
205 216 assert_equal "Plurals", Inflector.pluralize("Plurals")
@@ -219,9 +230,9 @@ def test_pluralize_plurals
219 230 end
220 231 end
221 232
222   - MixtureToTitleCase.each do |before, title_cased|
223   - define_method 'test_titlecase' do
224   - assert_equal(title_cased, Inflector.titleize(before))
  233 + MixtureToTitleCase.each do |before, titleized|
  234 + define_method "test_titleize_#{before}" do
  235 + assert_equal(titleized, Inflector.titleize(before))
225 236 end
226 237 end
227 238
@@ -285,6 +296,10 @@ def test_classify_with_symbol
285 296 end
286 297 end
287 298
  299 + def test_classify_with_leading_schema_name
  300 + assert_equal 'FooBar', Inflector.classify('schema.foo_bar')
  301 + end
  302 +
288 303 def test_humanize
289 304 UnderscoreToHuman.each do |underscore, human|
290 305 assert_equal(human, Inflector.humanize(underscore))
@@ -292,12 +307,13 @@ def test_humanize
292 307 end
293 308
294 309 def test_constantize
295   - assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case")
296   - assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case")
297   - assert_equal InflectorTest, Inflector.constantize("InflectorTest")
298   - assert_equal InflectorTest, Inflector.constantize("::InflectorTest")
  310 + assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") }
  311 + assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") }
  312 + assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("InflectorTest") }
  313 + assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("::InflectorTest") }
299 314 assert_raises(NameError) { Inflector.constantize("UnknownClass") }
300 315 assert_raises(NameError) { Inflector.constantize("An invalid string") }
  316 + assert_raises(NameError) { Inflector.constantize("InvalidClass\n") }
301 317 end
302 318
303 319 def test_constantize_doesnt_look_in_parent
@@ -360,4 +376,63 @@ def test_clear_with_default
360 376 Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
361 377 Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
362 378 end
  379 +
  380 + Irregularities.each do |irregularity|
  381 + singular, plural = *irregularity
  382 + Inflector.inflections do |inflect|
  383 + define_method("test_irregularity_between_#{singular}_and_#{plural}") do
  384 + inflect.irregular(singular, plural)
  385 + assert_equal singular, Inflector.singularize(plural)
  386 + assert_equal plural, Inflector.pluralize(singular)
  387 + end
  388 + end
  389 + end
  390 +
  391 + [ :all, [] ].each do |scope|
  392 + Inflector.inflections do |inflect|
  393 + define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do
  394 + # save all the inflections
  395 + singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
  396 +
  397 + # clear all the inflections
  398 + inflect.clear(*scope)
  399 +
  400 + assert_equal [], inflect.singulars
  401 + assert_equal [], inflect.plurals
  402 + assert_equal [], inflect.uncountables
  403 +
  404 + # restore all the inflections
  405 + singulars.reverse.each { |singular| inflect.singular(*singular) }
  406 + plurals.reverse.each { |plural| inflect.plural(*plural) }
  407 + inflect.uncountable(uncountables)
  408 +
  409 + assert_equal singulars, inflect.singulars
  410 + assert_equal plurals, inflect.plurals
  411 + assert_equal uncountables, inflect.uncountables
  412 + end
  413 + end
  414 + end
  415 +
  416 + { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable }.each do |scope, method|
  417 + Inflector.inflections do |inflect|
  418 + define_method("test_clear_inflections_with_#{scope}") do
  419 + # save the inflections
  420 + values = inflect.send(scope)
  421 +
  422 + # clear the inflections
  423 + inflect.clear(scope)
  424 +
  425 + assert_equal [], inflect.send(scope)
  426 +
  427 + # restore the inflections
  428 + if scope == :uncountables
  429 + inflect.send(method, values)
  430 + else
  431 + values.reverse.each { |value| inflect.send(method, *value) }
  432 + end
  433 +
  434 + assert_equal values, inflect.send(scope)
  435 + end
  436 + end
  437 + end
363 438 end

0 comments on commit b0ed505

Please sign in to comment.
Something went wrong with that request. Please try again.