Changeset 6075
- Timestamp:
- 01/28/07 15:52:45 (2 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/inflector.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/string_ext_test.rb (modified) (3 diffs)
- trunk/activesupport/test/inflector_test.rb (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r6060 r6075 1 1 *SVN* 2 3 * Full test coverage for Inflector. #7228 [Dan Kubb] 2 4 3 5 * :db format for Date#to_s [Jeremy Kemper] trunk/activesupport/lib/active_support/inflector.rb
r5924 r6075 244 244 # "Class".constantize #=> Class 245 245 def constantize(camel_cased_word) 246 unless / ^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word246 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 trunk/activesupport/test/core_ext/string_ext_test.rb
r5299 r6075 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| … … 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 … … 55 67 InflectorTest::ClassNameToTableName.each do |class_name, table_name| 56 68 assert_equal(class_name, table_name.classify) 69 end 70 end 71 72 def test_humanize 73 InflectorTest::UnderscoreToHuman.each do |underscore, human| 74 assert_equal(human, underscore.humanize) 57 75 end 58 76 end trunk/activesupport/test/inflector_test.rb
r6022 r6075 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",85 86 "louse" => "lice",87 "house" => "houses",88 "octopus" => "octopi",89 "virus" => "viri",90 "alias" => "aliases",91 "portfolio" => "portfolios",92 93 "vertex" => "vertices",94 "matrix" => "matrices",95 96 "axis" => "axes",97 "testis" => "testes",98 "crisis" => "crises",99 100 "rice" => "rice",101 "shoe" => "shoes",102 103 "horse" => "horses",104 "prize" => "prizes",105 "edge" => "edges"84 "mouse" => "mice", 85 86 "louse" => "lice", 87 "house" => "houses", 88 "octopus" => "octopi", 89 "virus" => "viri", 90 "alias" => "aliases", 91 "portfolio" => "portfolios", 92 93 "vertex" => "vertices", 94 "matrix" => "matrices", 95 96 "axis" => "axes", 97 "testis" => "testes", 98 "crisis" => "crises", 99 100 "rice" => "rice", 101 "shoe" => "shoes", 102 103 "horse" => "horses", 104 "prize" => "prizes", 105 "edge" => "edges" 106 106 } 107 107 … … 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" … … 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") … … 220 231 end 221 232 222 MixtureToTitleCase.each do |before, title _cased|223 define_method 'test_titlecase'do224 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 … … 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| … … 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 … … 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