Permalink
Browse files

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...
1 parent 269ad97 commit b0ed5057e01b78f2f7e0e078c0534647b425c290 @jeremy jeremy committed Jan 28, 2007
View
2 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'
View
5 activesupport/lib/active_support/inflector.rb
@@ -243,12 +243,11 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
# "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
View
20 activesupport/test/core_ext/string_ext_test.rb
@@ -16,6 +16,12 @@ def test_singularize
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 @@ def test_underscore
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 @@ def test_classify
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)
View
141 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 @@ def test_pluralize_plurals
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,19 +296,24 @@ def test_classify_with_symbol
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))
end
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 @@ def test_clear_with_default
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

0 comments on commit b0ed505

Please sign in to comment.