diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-28 15:52:45 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-28 15:52:45 +0000 |
commit | b0ed5057e01b78f2f7e0e078c0534647b425c290 (patch) | |
tree | 5f95376c0507f8decd83b76c17de93ed8dfc48e2 /activesupport | |
parent | 269ad9711ddc179e72cc7da3176893a1dcca1b26 (diff) | |
download | rails-b0ed5057e01b78f2f7e0e078c0534647b425c290.tar.gz rails-b0ed5057e01b78f2f7e0e078c0534647b425c290.tar.bz2 rails-b0ed5057e01b78f2f7e0e078c0534647b425c290.zip |
Full test coverage for Inflector. Closes #7228.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6075 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 5 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 20 | ||||
-rw-r--r-- | activesupport/test/inflector_test.rb | 141 |
4 files changed, 131 insertions, 37 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index df9474bb3f..9847131e13 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Full test coverage for Inflector. #7228 [Dan Kubb] + * :db format for Date#to_s [Jeremy Kemper] Date.new(2007, 1, 27).to_s(:db) # => '2007-01-27' diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 8f71b71237..084c58d389 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -243,12 +243,11 @@ module Inflector # "Module".constantize #=> Module # "Class".constantize #=> Class def constantize(camel_cased_word) - unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word + unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end - camel_cased_word = "::#{camel_cased_word}" unless $1 - Object.module_eval(camel_cased_word, __FILE__, __LINE__) + Object.module_eval("::#{$1}", __FILE__, __LINE__) end # Ordinalize turns a number into an ordinal string used to denote the diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 7eff505a22..cd2d12c3b1 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -16,6 +16,12 @@ class StringInflectionsTest < Test::Unit::TestCase end end + def test_titleize + InflectorTest::MixtureToTitleCase.each do |before, titleized| + assert_equal(titleized, before.titleize) + end + end + def test_camelize InflectorTest::CamelToUnderscore.each do |camel, underscore| assert_equal(camel, underscore.camelize) @@ -31,8 +37,14 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore end + def test_underscore_to_lower_camel + InflectorTest::UnderscoreToLowerCamel.each do |underscored, lower_camel| + assert_equal(lower_camel, underscored.camelize(:lower)) + end + end + def test_demodulize - assert_equal "Account", Inflector.demodulize("MyApplication::Billing::Account") + assert_equal "Account", "MyApplication::Billing::Account".demodulize end def test_foreign_key @@ -57,6 +69,12 @@ class StringInflectionsTest < Test::Unit::TestCase end end + def test_humanize + InflectorTest::UnderscoreToHuman.each do |underscore, human| + assert_equal(human, underscore.humanize) + end + end + def test_string_to_time assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index d068a9892f..3aff541646 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -70,39 +70,39 @@ class InflectorTest < Test::Unit::TestCase "perspective" => "perspectives", - "ox" => "oxen", - "photo" => "photos", - "buffalo" => "buffaloes", - "tomato" => "tomatoes", - "dwarf" => "dwarves", - "elf" => "elves", + "ox" => "oxen", + "photo" => "photos", + "buffalo" => "buffaloes", + "tomato" => "tomatoes", + "dwarf" => "dwarves", + "elf" => "elves", "information" => "information", - "equipment" => "equipment", - "bus" => "buses", - "status" => "statuses", + "equipment" => "equipment", + "bus" => "buses", + "status" => "statuses", "status_code" => "status_codes", - "mouse" => "mice", + "mouse" => "mice", - "louse" => "lice", - "house" => "houses", - "octopus" => "octopi", - "virus" => "viri", - "alias" => "aliases", - "portfolio" => "portfolios", + "louse" => "lice", + "house" => "houses", + "octopus" => "octopi", + "virus" => "viri", + "alias" => "aliases", + "portfolio" => "portfolios", - "vertex" => "vertices", - "matrix" => "matrices", + "vertex" => "vertices", + "matrix" => "matrices", - "axis" => "axes", - "testis" => "testes", - "crisis" => "crises", + "axis" => "axes", + "testis" => "testes", + "crisis" => "crises", - "rice" => "rice", - "shoe" => "shoes", + "rice" => "rice", + "shoe" => "shoes", - "horse" => "horses", - "prize" => "prizes", - "edge" => "edges" + "horse" => "horses", + "prize" => "prizes", + "edge" => "edges" } CamelToUnderscore = { @@ -190,6 +190,9 @@ class InflectorTest < Test::Unit::TestCase "103" => "103rd", "104" => "104th", "110" => "110th", + "111" => "111th", + "112" => "112th", + "113" => "113th", "1000" => "1000th", "1001" => "1001st" } @@ -200,6 +203,14 @@ class InflectorTest < Test::Unit::TestCase "person_street_address" => "person-street-address" } + Irregularities = { + 'person' => 'people', + 'man' => 'men', + 'child' => 'children', + 'sex' => 'sexes', + 'move' => 'moves', + } + def test_pluralize_plurals assert_equal "plurals", Inflector.pluralize("plurals") assert_equal "Plurals", Inflector.pluralize("Plurals") @@ -219,9 +230,9 @@ class InflectorTest < Test::Unit::TestCase end end - MixtureToTitleCase.each do |before, title_cased| - define_method 'test_titlecase' do - assert_equal(title_cased, Inflector.titleize(before)) + MixtureToTitleCase.each do |before, titleized| + define_method "test_titleize_#{before}" do + assert_equal(titleized, Inflector.titleize(before)) end end @@ -285,6 +296,10 @@ class InflectorTest < Test::Unit::TestCase end end + def test_classify_with_leading_schema_name + assert_equal 'FooBar', Inflector.classify('schema.foo_bar') + end + def test_humanize UnderscoreToHuman.each do |underscore, human| assert_equal(human, Inflector.humanize(underscore)) @@ -292,12 +307,13 @@ class InflectorTest < Test::Unit::TestCase end def test_constantize - assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") - assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") - assert_equal InflectorTest, Inflector.constantize("InflectorTest") - assert_equal InflectorTest, Inflector.constantize("::InflectorTest") + assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") } + assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") } + assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("InflectorTest") } + assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("::InflectorTest") } assert_raises(NameError) { Inflector.constantize("UnknownClass") } assert_raises(NameError) { Inflector.constantize("An invalid string") } + assert_raises(NameError) { Inflector.constantize("InvalidClass\n") } end def test_constantize_doesnt_look_in_parent @@ -360,4 +376,63 @@ class InflectorTest < Test::Unit::TestCase Inflector.inflections.instance_variable_set :@singulars, cached_values[1] Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] end + + Irregularities.each do |irregularity| + singular, plural = *irregularity + Inflector.inflections do |inflect| + define_method("test_irregularity_between_#{singular}_and_#{plural}") do + inflect.irregular(singular, plural) + assert_equal singular, Inflector.singularize(plural) + assert_equal plural, Inflector.pluralize(singular) + end + end + end + + [ :all, [] ].each do |scope| + Inflector.inflections do |inflect| + define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do + # save all the inflections + singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables + + # clear all the inflections + inflect.clear(*scope) + + assert_equal [], inflect.singulars + assert_equal [], inflect.plurals + assert_equal [], inflect.uncountables + + # restore all the inflections + singulars.reverse.each { |singular| inflect.singular(*singular) } + plurals.reverse.each { |plural| inflect.plural(*plural) } + inflect.uncountable(uncountables) + + assert_equal singulars, inflect.singulars + assert_equal plurals, inflect.plurals + assert_equal uncountables, inflect.uncountables + end + end + end + + { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable }.each do |scope, method| + Inflector.inflections do |inflect| + define_method("test_clear_inflections_with_#{scope}") do + # save the inflections + values = inflect.send(scope) + + # clear the inflections + inflect.clear(scope) + + assert_equal [], inflect.send(scope) + + # restore the inflections + if scope == :uncountables + inflect.send(method, values) + else + values.reverse.each { |value| inflect.send(method, *value) } + end + + assert_equal values, inflect.send(scope) + end + end + end end |