PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/activesupport/test/inflector_test.rb

https://github.com/ghar/rails
Ruby | 451 lines | 369 code | 72 blank | 10 comment | 2 complexity | d1a950faa5212e3982a26bba9bb8b2d5 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/inflector'
  3. require 'inflector_test_cases'
  4. module Ace
  5. module Base
  6. class Case
  7. end
  8. end
  9. end
  10. class InflectorTest < Test::Unit::TestCase
  11. include InflectorTestCases
  12. def test_pluralize_plurals
  13. assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
  14. assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
  15. end
  16. def test_pluralize_empty_string
  17. assert_equal "", ActiveSupport::Inflector.pluralize("")
  18. end
  19. ActiveSupport::Inflector.inflections.uncountable.each do |word|
  20. define_method "test_uncountability_of_#{word}" do
  21. assert_equal word, ActiveSupport::Inflector.singularize(word)
  22. assert_equal word, ActiveSupport::Inflector.pluralize(word)
  23. assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
  24. end
  25. end
  26. def test_uncountable_word_is_not_greedy
  27. uncountable_word = "ors"
  28. countable_word = "sponsor"
  29. cached_uncountables = ActiveSupport::Inflector.inflections.uncountables
  30. ActiveSupport::Inflector.inflections.uncountable << uncountable_word
  31. assert_equal uncountable_word, ActiveSupport::Inflector.singularize(uncountable_word)
  32. assert_equal uncountable_word, ActiveSupport::Inflector.pluralize(uncountable_word)
  33. assert_equal ActiveSupport::Inflector.pluralize(uncountable_word), ActiveSupport::Inflector.singularize(uncountable_word)
  34. assert_equal "sponsor", ActiveSupport::Inflector.singularize(countable_word)
  35. assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
  36. assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
  37. ensure
  38. ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_uncountables
  39. end
  40. SingularToPlural.each do |singular, plural|
  41. define_method "test_pluralize_singular_#{singular}" do
  42. assert_equal(plural, ActiveSupport::Inflector.pluralize(singular))
  43. assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize))
  44. end
  45. end
  46. SingularToPlural.each do |singular, plural|
  47. define_method "test_singularize_plural_#{plural}" do
  48. assert_equal(singular, ActiveSupport::Inflector.singularize(plural))
  49. assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize))
  50. end
  51. end
  52. SingularToPlural.each do |singular, plural|
  53. define_method "test_pluralize_plural_#{plural}" do
  54. assert_equal(plural, ActiveSupport::Inflector.pluralize(plural))
  55. assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize))
  56. end
  57. end
  58. def test_overwrite_previous_inflectors
  59. assert_equal("series", ActiveSupport::Inflector.singularize("series"))
  60. ActiveSupport::Inflector.inflections.singular "series", "serie"
  61. assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
  62. ActiveSupport::Inflector.inflections.uncountable "series" # Return to normal
  63. end
  64. MixtureToTitleCase.each do |before, titleized|
  65. define_method "test_titleize_#{before}" do
  66. assert_equal(titleized, ActiveSupport::Inflector.titleize(before))
  67. end
  68. end
  69. def test_camelize
  70. CamelToUnderscore.each do |camel, underscore|
  71. assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  72. end
  73. end
  74. def test_camelize_with_lower_downcases_the_first_letter
  75. assert_equal('capital', ActiveSupport::Inflector.camelize('Capital', false))
  76. end
  77. def test_camelize_with_underscores
  78. assert_equal("CamelCase", ActiveSupport::Inflector.camelize('Camel_Case'))
  79. end
  80. def test_acronyms
  81. ActiveSupport::Inflector.inflections do |inflect|
  82. inflect.acronym("API")
  83. inflect.acronym("HTML")
  84. inflect.acronym("HTTP")
  85. inflect.acronym("RESTful")
  86. inflect.acronym("W3C")
  87. inflect.acronym("PhD")
  88. inflect.acronym("RoR")
  89. inflect.acronym("SSL")
  90. end
  91. # camelize underscore humanize titleize
  92. [
  93. ["API", "api", "API", "API"],
  94. ["APIController", "api_controller", "API controller", "API Controller"],
  95. ["Nokogiri::HTML", "nokogiri/html", "Nokogiri/HTML", "Nokogiri/HTML"],
  96. ["HTTPAPI", "http_api", "HTTP API", "HTTP API"],
  97. ["HTTP::Get", "http/get", "HTTP/get", "HTTP/Get"],
  98. ["SSLError", "ssl_error", "SSL error", "SSL Error"],
  99. ["RESTful", "restful", "RESTful", "RESTful"],
  100. ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
  101. ["IHeartW3C", "i_heart_w3c", "I heart W3C", "I Heart W3C"],
  102. ["PhDRequired", "phd_required", "PhD required", "PhD Required"],
  103. ["IRoRU", "i_ror_u", "I RoR u", "I RoR U"],
  104. ["RESTfulHTTPAPI", "restful_http_api", "RESTful HTTP API", "RESTful HTTP API"],
  105. # misdirection
  106. ["Capistrano", "capistrano", "Capistrano", "Capistrano"],
  107. ["CapiController", "capi_controller", "Capi controller", "Capi Controller"],
  108. ["HttpsApis", "https_apis", "Https apis", "Https Apis"],
  109. ["Html5", "html5", "Html5", "Html5"],
  110. ["Restfully", "restfully", "Restfully", "Restfully"],
  111. ["RoRails", "ro_rails", "Ro rails", "Ro Rails"]
  112. ].each do |camel, under, human, title|
  113. assert_equal(camel, ActiveSupport::Inflector.camelize(under))
  114. assert_equal(camel, ActiveSupport::Inflector.camelize(camel))
  115. assert_equal(under, ActiveSupport::Inflector.underscore(under))
  116. assert_equal(under, ActiveSupport::Inflector.underscore(camel))
  117. assert_equal(title, ActiveSupport::Inflector.titleize(under))
  118. assert_equal(title, ActiveSupport::Inflector.titleize(camel))
  119. assert_equal(human, ActiveSupport::Inflector.humanize(under))
  120. end
  121. end
  122. def test_acronym_override
  123. ActiveSupport::Inflector.inflections do |inflect|
  124. inflect.acronym("API")
  125. inflect.acronym("LegacyApi")
  126. end
  127. assert_equal("LegacyApi", ActiveSupport::Inflector.camelize("legacyapi"))
  128. assert_equal("LegacyAPI", ActiveSupport::Inflector.camelize("legacy_api"))
  129. assert_equal("SomeLegacyApi", ActiveSupport::Inflector.camelize("some_legacyapi"))
  130. assert_equal("Nonlegacyapi", ActiveSupport::Inflector.camelize("nonlegacyapi"))
  131. end
  132. def test_acronyms_camelize_lower
  133. ActiveSupport::Inflector.inflections do |inflect|
  134. inflect.acronym("API")
  135. inflect.acronym("HTML")
  136. end
  137. assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("html_api", false))
  138. assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("htmlAPI", false))
  139. assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("HTMLAPI", false))
  140. end
  141. def test_underscore_acronym_sequence
  142. ActiveSupport::Inflector.inflections do |inflect|
  143. inflect.acronym("API")
  144. inflect.acronym("HTML5")
  145. inflect.acronym("HTML")
  146. end
  147. assert_equal("html5_html_api", ActiveSupport::Inflector.underscore("HTML5HTMLAPI"))
  148. end
  149. def test_underscore
  150. CamelToUnderscore.each do |camel, underscore|
  151. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  152. end
  153. CamelToUnderscoreWithoutReverse.each do |camel, underscore|
  154. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  155. end
  156. end
  157. def test_camelize_with_module
  158. CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
  159. assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  160. end
  161. end
  162. def test_underscore_with_slashes
  163. CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
  164. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  165. end
  166. end
  167. def test_demodulize
  168. assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
  169. end
  170. def test_foreign_key
  171. ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
  172. assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
  173. end
  174. ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
  175. assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
  176. end
  177. end
  178. def test_tableize
  179. ClassNameToTableName.each do |class_name, table_name|
  180. assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
  181. end
  182. end
  183. def test_parameterize
  184. StringToParameterized.each do |some_string, parameterized_string|
  185. assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  186. end
  187. end
  188. def test_parameterize_and_normalize
  189. StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
  190. assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  191. end
  192. end
  193. def test_parameterize_with_custom_separator
  194. StringToParameterizeWithUnderscore.each do |some_string, parameterized_string|
  195. assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string, '_'))
  196. end
  197. end
  198. def test_parameterize_with_multi_character_separator
  199. StringToParameterized.each do |some_string, parameterized_string|
  200. assert_equal(parameterized_string.gsub('-', '__sep__'), ActiveSupport::Inflector.parameterize(some_string, '__sep__'))
  201. end
  202. end
  203. def test_classify
  204. ClassNameToTableName.each do |class_name, table_name|
  205. assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
  206. assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
  207. end
  208. end
  209. def test_classify_with_symbol
  210. assert_nothing_raised do
  211. assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars)
  212. end
  213. end
  214. def test_classify_with_leading_schema_name
  215. assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar')
  216. end
  217. def test_humanize
  218. UnderscoreToHuman.each do |underscore, human|
  219. assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
  220. end
  221. end
  222. def test_humanize_by_rule
  223. ActiveSupport::Inflector.inflections do |inflect|
  224. inflect.human(/_cnt$/i, '\1_count')
  225. inflect.human(/^prefx_/i, '\1')
  226. end
  227. assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
  228. assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
  229. end
  230. def test_humanize_by_string
  231. ActiveSupport::Inflector.inflections do |inflect|
  232. inflect.human("col_rpted_bugs", "Reported bugs")
  233. end
  234. assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
  235. assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
  236. end
  237. def test_constantize
  238. assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") }
  239. assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
  240. assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") }
  241. assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") }
  242. assert_raise(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") }
  243. assert_raise(NameError) { ActiveSupport::Inflector.constantize("An invalid string") }
  244. assert_raise(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") }
  245. end
  246. def test_constantize_does_lexical_lookup
  247. assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
  248. end
  249. def test_ordinal
  250. OrdinalNumbers.each do |number, ordinalized|
  251. assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
  252. end
  253. end
  254. def test_dasherize
  255. UnderscoresToDashes.each do |underscored, dasherized|
  256. assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
  257. end
  258. end
  259. def test_underscore_as_reverse_of_dasherize
  260. UnderscoresToDashes.each do |underscored, dasherized|
  261. assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
  262. end
  263. end
  264. def test_underscore_to_lower_camel
  265. UnderscoreToLowerCamel.each do |underscored, lower_camel|
  266. assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
  267. end
  268. end
  269. def test_symbol_to_lower_camel
  270. SymbolToLowerCamel.each do |symbol, lower_camel|
  271. assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
  272. end
  273. end
  274. %w{plurals singulars uncountables humans}.each do |inflection_type|
  275. class_eval <<-RUBY, __FILE__, __LINE__ + 1
  276. def test_clear_#{inflection_type}
  277. cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
  278. ActiveSupport::Inflector.inflections.clear :#{inflection_type}
  279. assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
  280. ActiveSupport::Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values
  281. end
  282. RUBY
  283. end
  284. def test_clear_all
  285. cached_values = ActiveSupport::Inflector.inflections.plurals.dup, ActiveSupport::Inflector.inflections.singulars.dup, ActiveSupport::Inflector.inflections.uncountables.dup, ActiveSupport::Inflector.inflections.humans.dup
  286. ActiveSupport::Inflector.inflections do |inflect|
  287. # ensure any data is present
  288. inflect.plural(/(quiz)$/i, '\1zes')
  289. inflect.singular(/(database)s$/i, '\1')
  290. inflect.uncountable('series')
  291. inflect.human("col_rpted_bugs", "Reported bugs")
  292. inflect.clear :all
  293. assert inflect.plurals.empty?
  294. assert inflect.singulars.empty?
  295. assert inflect.uncountables.empty?
  296. assert inflect.humans.empty?
  297. end
  298. ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
  299. ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
  300. ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
  301. ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
  302. end
  303. def test_clear_with_default
  304. cached_values = ActiveSupport::Inflector.inflections.plurals.dup, ActiveSupport::Inflector.inflections.singulars.dup, ActiveSupport::Inflector.inflections.uncountables.dup, ActiveSupport::Inflector.inflections.humans.dup
  305. ActiveSupport::Inflector.inflections do |inflect|
  306. # ensure any data is present
  307. inflect.plural(/(quiz)$/i, '\1zes')
  308. inflect.singular(/(database)s$/i, '\1')
  309. inflect.uncountable('series')
  310. inflect.human("col_rpted_bugs", "Reported bugs")
  311. inflect.clear
  312. assert inflect.plurals.empty?
  313. assert inflect.singulars.empty?
  314. assert inflect.uncountables.empty?
  315. assert inflect.humans.empty?
  316. end
  317. ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
  318. ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
  319. ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
  320. ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
  321. end
  322. Irregularities.each do |irregularity|
  323. singular, plural = *irregularity
  324. ActiveSupport::Inflector.inflections do |inflect|
  325. define_method("test_irregularity_between_#{singular}_and_#{plural}") do
  326. inflect.irregular(singular, plural)
  327. assert_equal singular, ActiveSupport::Inflector.singularize(plural)
  328. assert_equal plural, ActiveSupport::Inflector.pluralize(singular)
  329. end
  330. end
  331. end
  332. Irregularities.each do |irregularity|
  333. singular, plural = *irregularity
  334. ActiveSupport::Inflector.inflections do |inflect|
  335. define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
  336. inflect.irregular(singular, plural)
  337. assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
  338. end
  339. end
  340. end
  341. [ :all, [] ].each do |scope|
  342. ActiveSupport::Inflector.inflections do |inflect|
  343. define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do
  344. # save all the inflections
  345. singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
  346. # clear all the inflections
  347. inflect.clear(*scope)
  348. assert_equal [], inflect.singulars
  349. assert_equal [], inflect.plurals
  350. assert_equal [], inflect.uncountables
  351. # restore all the inflections
  352. singulars.reverse.each { |singular| inflect.singular(*singular) }
  353. plurals.reverse.each { |plural| inflect.plural(*plural) }
  354. inflect.uncountable(uncountables)
  355. assert_equal singulars, inflect.singulars
  356. assert_equal plurals, inflect.plurals
  357. assert_equal uncountables, inflect.uncountables
  358. end
  359. end
  360. end
  361. { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable, :humans => :human }.each do |scope, method|
  362. ActiveSupport::Inflector.inflections do |inflect|
  363. define_method("test_clear_inflections_with_#{scope}") do
  364. # save the inflections
  365. values = inflect.send(scope)
  366. # clear the inflections
  367. inflect.clear(scope)
  368. assert_equal [], inflect.send(scope)
  369. # restore the inflections
  370. if scope == :uncountables
  371. inflect.send(method, values)
  372. else
  373. values.reverse.each { |value| inflect.send(method, *value) }
  374. end
  375. assert_equal values, inflect.send(scope)
  376. end
  377. end
  378. end
  379. end