Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6075

Show
Ignore:
Timestamp:
01/28/07 15:52:45 (2 years ago)
Author:
bitsweat
Message:

Full test coverage for Inflector. Closes #7228.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r6060 r6075  
    11*SVN* 
     2 
     3* Full test coverage for Inflector.  #7228 [Dan Kubb] 
    24 
    35* :db format for Date#to_s [Jeremy Kemper] 
  • trunk/activesupport/lib/active_support/inflector.rb

    r5924 r6075  
    244244  #   "Class".constantize #=> Class 
    245245  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 
    247247      raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" 
    248248    end 
    249249 
    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__) 
    252251  end 
    253252 
  • trunk/activesupport/test/core_ext/string_ext_test.rb

    r5299 r6075  
    1717  end 
    1818 
     19  def test_titleize 
     20    InflectorTest::MixtureToTitleCase.each do |before, titleized| 
     21      assert_equal(titleized, before.titleize) 
     22    end 
     23  end 
     24 
    1925  def test_camelize 
    2026    InflectorTest::CamelToUnderscore.each do |camel, underscore| 
     
    3238  end 
    3339 
     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 
    3446  def test_demodulize 
    35     assert_equal "Account", Inflector.demodulize("MyApplication::Billing::Account") 
     47    assert_equal "Account", "MyApplication::Billing::Account".demodulize 
    3648  end 
    3749 
     
    5567    InflectorTest::ClassNameToTableName.each do |class_name, table_name| 
    5668      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) 
    5775    end 
    5876  end 
  • trunk/activesupport/test/inflector_test.rb

    r6022 r6075  
    7171    "perspective" => "perspectives", 
    7272 
    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", 
    7979    "information" => "information", 
    80     "equipment" => "equipment", 
    81     "bus" => "buses", 
    82     "status" => "statuses", 
     80    "equipment"   => "equipment", 
     81    "bus"         => "buses", 
     82    "status"      => "statuses", 
    8383    "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" 
    106106  } 
    107107 
     
    191191    "104" => "104th", 
    192192    "110" => "110th", 
     193    "111" => "111th", 
     194    "112" => "112th", 
     195    "113" => "113th", 
    193196    "1000" => "1000th", 
    194197    "1001" => "1001st" 
     
    201204  } 
    202205 
     206  Irregularities = { 
     207    'person' => 'people', 
     208    'man'    => 'men', 
     209    'child'  => 'children', 
     210    'sex'    => 'sexes', 
     211    'move'   => 'moves', 
     212  } 
     213 
    203214  def test_pluralize_plurals 
    204215    assert_equal "plurals", Inflector.pluralize("plurals") 
     
    220231  end 
    221232 
    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)) 
    225236    end 
    226237  end 
     
    286297  end 
    287298 
     299  def test_classify_with_leading_schema_name 
     300    assert_equal 'FooBar', Inflector.classify('schema.foo_bar') 
     301  end 
     302 
    288303  def test_humanize 
    289304    UnderscoreToHuman.each do |underscore, human| 
     
    293308 
    294309  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") } 
    299314    assert_raises(NameError) { Inflector.constantize("UnknownClass") } 
    300315    assert_raises(NameError) { Inflector.constantize("An invalid string") } 
     316    assert_raises(NameError) { Inflector.constantize("InvalidClass\n") } 
    301317  end 
    302318 
     
    361377    Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] 
    362378  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 
    363438end