2006-07-09 02:14:49 +08:00
|
|
|
require File.dirname(__FILE__) + '/abstract_unit'
|
2004-12-30 05:03:21 +08:00
|
|
|
|
2005-02-15 23:02:43 +08:00
|
|
|
module Ace
|
|
|
|
module Base
|
|
|
|
class Case
|
2005-02-15 09:45:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
class InflectorTest < Test::Unit::TestCase
|
|
|
|
SingularToPlural = {
|
|
|
|
"search" => "searches",
|
|
|
|
"switch" => "switches",
|
|
|
|
"fix" => "fixes",
|
|
|
|
"box" => "boxes",
|
|
|
|
"process" => "processes",
|
|
|
|
"address" => "addresses",
|
|
|
|
"case" => "cases",
|
2005-03-26 21:20:47 +08:00
|
|
|
"stack" => "stacks",
|
|
|
|
"wish" => "wishes",
|
|
|
|
"fish" => "fish",
|
2004-12-30 05:03:21 +08:00
|
|
|
|
|
|
|
"category" => "categories",
|
|
|
|
"query" => "queries",
|
|
|
|
"ability" => "abilities",
|
|
|
|
"agency" => "agencies",
|
|
|
|
"movie" => "movies",
|
|
|
|
|
2005-07-03 03:05:47 +08:00
|
|
|
"archive" => "archives",
|
|
|
|
|
2005-07-22 14:52:10 +08:00
|
|
|
"index" => "indices",
|
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
"wife" => "wives",
|
|
|
|
"safe" => "saves",
|
|
|
|
"half" => "halves",
|
|
|
|
|
2005-09-12 13:29:47 +08:00
|
|
|
"move" => "moves",
|
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
"salesperson" => "salespeople",
|
|
|
|
"person" => "people",
|
|
|
|
|
|
|
|
"spokesman" => "spokesmen",
|
|
|
|
"man" => "men",
|
|
|
|
"woman" => "women",
|
|
|
|
|
|
|
|
"basis" => "bases",
|
|
|
|
"diagnosis" => "diagnoses",
|
|
|
|
|
|
|
|
"datum" => "data",
|
|
|
|
"medium" => "media",
|
2005-05-20 01:10:20 +08:00
|
|
|
"analysis" => "analyses",
|
2004-12-30 05:03:21 +08:00
|
|
|
|
|
|
|
"node_child" => "node_children",
|
|
|
|
"child" => "children",
|
|
|
|
|
|
|
|
"experience" => "experiences",
|
|
|
|
"day" => "days",
|
|
|
|
|
|
|
|
"comment" => "comments",
|
2005-02-15 10:02:20 +08:00
|
|
|
"foobar" => "foobars",
|
|
|
|
"newsletter" => "newsletters",
|
|
|
|
|
|
|
|
"old_news" => "old_news",
|
|
|
|
"news" => "news",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-04-07 14:19:10 +08:00
|
|
|
"series" => "series",
|
2005-07-05 15:44:46 +08:00
|
|
|
"species" => "species",
|
2005-04-07 14:19:10 +08:00
|
|
|
|
2005-09-12 13:29:47 +08:00
|
|
|
"quiz" => "quizzes",
|
|
|
|
|
2005-07-02 00:57:58 +08:00
|
|
|
"perspective" => "perspectives",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-07-02 00:57:58 +08:00
|
|
|
"ox" => "oxen",
|
2005-07-03 02:59:01 +08:00
|
|
|
"photo" => "photos",
|
2005-07-02 00:57:58 +08:00
|
|
|
"buffalo" => "buffaloes",
|
|
|
|
"tomato" => "tomatoes",
|
|
|
|
"dwarf" => "dwarves",
|
|
|
|
"elf" => "elves",
|
|
|
|
"information" => "information",
|
2005-07-04 23:15:13 +08:00
|
|
|
"equipment" => "equipment",
|
2006-07-09 02:14:49 +08:00
|
|
|
"bus" => "buses",
|
2005-09-12 13:29:47 +08:00
|
|
|
"status" => "statuses",
|
2005-11-07 09:20:54 +08:00
|
|
|
"status_code" => "status_codes",
|
2005-07-02 00:57:58 +08:00
|
|
|
"mouse" => "mice",
|
2005-07-05 15:44:46 +08:00
|
|
|
|
2005-07-04 23:15:13 +08:00
|
|
|
"louse" => "lice",
|
|
|
|
"house" => "houses",
|
|
|
|
"octopus" => "octopi",
|
|
|
|
"virus" => "viri",
|
|
|
|
"alias" => "aliases",
|
2005-07-05 15:44:46 +08:00
|
|
|
"portfolio" => "portfolios",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-07-05 15:44:46 +08:00
|
|
|
"vertex" => "vertices",
|
|
|
|
"matrix" => "matrices",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-07-05 15:44:46 +08:00
|
|
|
"axis" => "axes",
|
|
|
|
"testis" => "testes",
|
|
|
|
"crisis" => "crises",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-07-05 15:44:46 +08:00
|
|
|
"rice" => "rice",
|
|
|
|
"shoe" => "shoes",
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-07-05 15:44:46 +08:00
|
|
|
"horse" => "horses",
|
|
|
|
"prize" => "prizes",
|
|
|
|
"edge" => "edges"
|
2004-12-30 05:03:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CamelToUnderscore = {
|
2005-04-07 14:19:10 +08:00
|
|
|
"Product" => "product",
|
|
|
|
"SpecialGuest" => "special_guest",
|
2005-05-20 03:15:30 +08:00
|
|
|
"ApplicationController" => "application_controller",
|
2006-03-20 11:32:28 +08:00
|
|
|
"Area51Controller" => "area51_controller"
|
|
|
|
}
|
|
|
|
|
|
|
|
UnderscoreToLowerCamel = {
|
|
|
|
"product" => "product",
|
|
|
|
"special_guest" => "specialGuest",
|
|
|
|
"application_controller" => "applicationController",
|
2006-07-09 02:14:49 +08:00
|
|
|
"area51_controller" => "area51Controller"
|
2005-09-13 15:44:20 +08:00
|
|
|
}
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-09-13 15:44:20 +08:00
|
|
|
CamelToUnderscoreWithoutReverse = {
|
|
|
|
"HTMLTidy" => "html_tidy",
|
|
|
|
"HTMLTidyGenerator" => "html_tidy_generator",
|
|
|
|
"FreeBSD" => "free_bsd",
|
|
|
|
"HTML" => "html",
|
2004-12-30 05:03:21 +08:00
|
|
|
}
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-02-15 09:45:35 +08:00
|
|
|
CamelWithModuleToUnderscoreWithSlash = {
|
|
|
|
"Admin::Product" => "admin/product",
|
|
|
|
"Users::Commission::Department" => "users/commission/department",
|
|
|
|
"UsersSection::CommissionDepartment" => "users_section/commission_department",
|
|
|
|
}
|
2004-12-30 05:03:21 +08:00
|
|
|
|
|
|
|
ClassNameToForeignKeyWithUnderscore = {
|
|
|
|
"Person" => "person_id",
|
|
|
|
"MyApplication::Billing::Account" => "account_id"
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassNameToForeignKeyWithoutUnderscore = {
|
|
|
|
"Person" => "personid",
|
|
|
|
"MyApplication::Billing::Account" => "accountid"
|
|
|
|
}
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
ClassNameToTableName = {
|
|
|
|
"PrimarySpokesman" => "primary_spokesmen",
|
|
|
|
"NodeChild" => "node_children"
|
|
|
|
}
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-01-18 03:53:42 +08:00
|
|
|
UnderscoreToHuman = {
|
|
|
|
"employee_salary" => "Employee salary",
|
2005-07-17 18:18:36 +08:00
|
|
|
"employee_id" => "Employee",
|
2005-01-18 03:53:42 +08:00
|
|
|
"underground" => "Underground"
|
|
|
|
}
|
2004-12-30 05:03:21 +08:00
|
|
|
|
2005-10-20 04:20:11 +08:00
|
|
|
MixtureToTitleCase = {
|
|
|
|
'active_record' => 'Active Record',
|
|
|
|
'ActiveRecord' => 'Active Record',
|
|
|
|
'action web service' => 'Action Web Service',
|
|
|
|
'Action Web Service' => 'Action Web Service',
|
|
|
|
'Action web service' => 'Action Web Service',
|
|
|
|
'actionwebservice' => 'Actionwebservice',
|
|
|
|
'Actionwebservice' => 'Actionwebservice'
|
|
|
|
}
|
|
|
|
|
2005-07-17 18:02:23 +08:00
|
|
|
OrdinalNumbers = {
|
|
|
|
"0" => "0th",
|
|
|
|
"1" => "1st",
|
|
|
|
"2" => "2nd",
|
|
|
|
"3" => "3rd",
|
|
|
|
"4" => "4th",
|
|
|
|
"5" => "5th",
|
|
|
|
"6" => "6th",
|
|
|
|
"7" => "7th",
|
|
|
|
"8" => "8th",
|
|
|
|
"9" => "9th",
|
|
|
|
"10" => "10th",
|
|
|
|
"11" => "11th",
|
|
|
|
"12" => "12th",
|
|
|
|
"13" => "13th",
|
|
|
|
"14" => "14th",
|
|
|
|
"20" => "20th",
|
|
|
|
"21" => "21st",
|
|
|
|
"22" => "22nd",
|
|
|
|
"23" => "23rd",
|
|
|
|
"24" => "24th",
|
|
|
|
"100" => "100th",
|
|
|
|
"101" => "101st",
|
|
|
|
"102" => "102nd",
|
|
|
|
"103" => "103rd",
|
|
|
|
"104" => "104th",
|
|
|
|
"110" => "110th",
|
|
|
|
"1000" => "1000th",
|
|
|
|
"1001" => "1001st"
|
|
|
|
}
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2006-03-08 10:56:25 +08:00
|
|
|
UnderscoresToDashes = {
|
|
|
|
"street" => "street",
|
|
|
|
"street_address" => "street-address",
|
|
|
|
"person_street_address" => "person-street-address"
|
|
|
|
}
|
2005-07-17 18:02:23 +08:00
|
|
|
|
2005-03-26 21:20:47 +08:00
|
|
|
def test_pluralize_plurals
|
|
|
|
assert_equal "plurals", Inflector.pluralize("plurals")
|
|
|
|
assert_equal "Plurals", Inflector.pluralize("Plurals")
|
|
|
|
end
|
|
|
|
|
|
|
|
SingularToPlural.each do |singular, plural|
|
|
|
|
define_method "test_pluralize_#{singular}" do
|
2004-12-30 05:03:21 +08:00
|
|
|
assert_equal(plural, Inflector.pluralize(singular))
|
2005-03-26 21:20:47 +08:00
|
|
|
assert_equal(plural.capitalize, Inflector.pluralize(singular.capitalize))
|
2004-12-30 05:03:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-03-26 21:20:47 +08:00
|
|
|
SingularToPlural.each do |singular, plural|
|
|
|
|
define_method "test_singularize_#{plural}" do
|
2004-12-30 05:03:21 +08:00
|
|
|
assert_equal(singular, Inflector.singularize(plural))
|
2005-03-26 21:20:47 +08:00
|
|
|
assert_equal(singular.capitalize, Inflector.singularize(plural.capitalize))
|
2004-12-30 05:03:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-20 04:20:11 +08:00
|
|
|
MixtureToTitleCase.each do |before, title_cased|
|
|
|
|
define_method 'test_titlecase' do
|
|
|
|
assert_equal(title_cased, Inflector.titleize(before))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
def test_camelize
|
|
|
|
CamelToUnderscore.each do |camel, underscore|
|
|
|
|
assert_equal(camel, Inflector.camelize(underscore))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_underscore
|
|
|
|
CamelToUnderscore.each do |camel, underscore|
|
|
|
|
assert_equal(underscore, Inflector.underscore(camel))
|
|
|
|
end
|
2005-09-13 15:44:20 +08:00
|
|
|
CamelToUnderscoreWithoutReverse.each do |camel, underscore|
|
|
|
|
assert_equal(underscore, Inflector.underscore(camel))
|
|
|
|
end
|
2004-12-30 05:03:21 +08:00
|
|
|
end
|
|
|
|
|
2005-02-15 09:45:35 +08:00
|
|
|
def test_camelize_with_module
|
|
|
|
CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
|
|
|
assert_equal(camel, Inflector.camelize(underscore))
|
|
|
|
end
|
|
|
|
end
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-02-15 09:45:35 +08:00
|
|
|
def test_underscore_with_slashes
|
|
|
|
CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
|
|
|
|
assert_equal(underscore, Inflector.underscore(camel))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-12-30 05:03:21 +08:00
|
|
|
def test_demodulize
|
|
|
|
assert_equal "Account", Inflector.demodulize("MyApplication::Billing::Account")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_foreign_key
|
|
|
|
ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
|
|
|
|
assert_equal(foreign_key, Inflector.foreign_key(klass))
|
|
|
|
end
|
|
|
|
|
|
|
|
ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
|
|
|
|
assert_equal(foreign_key, Inflector.foreign_key(klass, false))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tableize
|
|
|
|
ClassNameToTableName.each do |class_name, table_name|
|
|
|
|
assert_equal(table_name, Inflector.tableize(class_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_classify
|
|
|
|
ClassNameToTableName.each do |class_name, table_name|
|
|
|
|
assert_equal(class_name, Inflector.classify(table_name))
|
2007-01-23 13:11:34 +08:00
|
|
|
assert_equal(class_name, Inflector.classify("table_prefix." + table_name))
|
2004-12-30 05:03:21 +08:00
|
|
|
end
|
|
|
|
end
|
2006-05-22 23:17:45 +08:00
|
|
|
|
|
|
|
def test_classify_with_symbol
|
|
|
|
assert_nothing_raised do
|
2007-01-23 13:11:34 +08:00
|
|
|
assert_equal 'FooBar', Inflector.classify(:foo_bars)
|
2006-05-22 23:17:45 +08:00
|
|
|
end
|
|
|
|
end
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-01-18 03:53:42 +08:00
|
|
|
def test_humanize
|
|
|
|
UnderscoreToHuman.each do |underscore, human|
|
|
|
|
assert_equal(human, Inflector.humanize(underscore))
|
|
|
|
end
|
|
|
|
end
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-02-15 09:45:35 +08:00
|
|
|
def test_constantize
|
2005-02-15 23:02:43 +08:00
|
|
|
assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case")
|
2006-02-12 02:41:59 +08:00
|
|
|
assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case")
|
2005-02-15 09:45:35 +08:00
|
|
|
assert_equal InflectorTest, Inflector.constantize("InflectorTest")
|
2006-02-12 02:41:59 +08:00
|
|
|
assert_equal InflectorTest, Inflector.constantize("::InflectorTest")
|
2005-02-15 09:45:35 +08:00
|
|
|
assert_raises(NameError) { Inflector.constantize("UnknownClass") }
|
2005-11-16 08:56:16 +08:00
|
|
|
assert_raises(NameError) { Inflector.constantize("An invalid string") }
|
|
|
|
end
|
2006-07-09 02:14:49 +08:00
|
|
|
|
2005-11-16 08:56:16 +08:00
|
|
|
def test_constantize_doesnt_look_in_parent
|
|
|
|
assert_raises(NameError) { Inflector.constantize("Ace::Base::InflectorTest") }
|
2005-02-15 09:45:35 +08:00
|
|
|
end
|
2005-07-17 18:02:23 +08:00
|
|
|
|
|
|
|
def test_ordinal
|
|
|
|
OrdinalNumbers.each do |number, ordinalized|
|
|
|
|
assert_equal(ordinalized, Inflector.ordinalize(number))
|
|
|
|
end
|
|
|
|
end
|
2006-03-08 10:56:25 +08:00
|
|
|
|
|
|
|
def test_dasherize
|
|
|
|
UnderscoresToDashes.each do |underscored, dasherized|
|
|
|
|
assert_equal(dasherized, Inflector.dasherize(underscored))
|
|
|
|
end
|
|
|
|
end
|
2006-03-16 05:05:10 +08:00
|
|
|
|
|
|
|
def test_underscore_as_reverse_of_dasherize
|
|
|
|
UnderscoresToDashes.each do |underscored, dasherized|
|
|
|
|
assert_equal(underscored, Inflector.underscore(Inflector.dasherize(underscored)))
|
|
|
|
end
|
|
|
|
end
|
2006-03-20 11:32:28 +08:00
|
|
|
|
|
|
|
def test_underscore_to_lower_camel
|
|
|
|
UnderscoreToLowerCamel.each do |underscored, lower_camel|
|
|
|
|
assert_equal(lower_camel, Inflector.camelize(underscored, false))
|
|
|
|
end
|
|
|
|
end
|
2007-01-23 13:32:08 +08:00
|
|
|
|
|
|
|
%w{plurals singulars uncountables}.each do |inflection_type|
|
|
|
|
class_eval "
|
|
|
|
def test_clear_#{inflection_type}
|
|
|
|
cached_values = Inflector.inflections.#{inflection_type}
|
|
|
|
Inflector.inflections.clear :#{inflection_type}
|
|
|
|
assert Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
|
|
|
|
Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values
|
|
|
|
end
|
|
|
|
"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_all
|
|
|
|
cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables
|
|
|
|
Inflector.inflections.clear :all
|
|
|
|
assert Inflector.inflections.plurals.empty?
|
|
|
|
assert Inflector.inflections.singulars.empty?
|
|
|
|
assert Inflector.inflections.uncountables.empty?
|
|
|
|
Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
|
|
|
|
Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
|
|
|
|
Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_with_default
|
|
|
|
cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables
|
|
|
|
Inflector.inflections.clear
|
|
|
|
assert Inflector.inflections.plurals.empty?
|
|
|
|
assert Inflector.inflections.singulars.empty?
|
|
|
|
assert Inflector.inflections.uncountables.empty?
|
|
|
|
Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
|
|
|
|
Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
|
|
|
|
Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
|
|
|
|
end
|
2005-07-17 18:02:23 +08:00
|
|
|
end
|