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

/activesupport/test/inflector_test.rb

https://github.com/awscloudtest/rails
Ruby | 316 lines | 260 code | 50 blank | 6 comment | 2 complexity | b8a0c4c0e2cec7e4bda8348c37c45d0c 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. SingularToPlural.each do |singular, plural|
  20. define_method "test_pluralize_#{singular}" do
  21. assert_equal(plural, ActiveSupport::Inflector.pluralize(singular))
  22. assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize))
  23. end
  24. end
  25. SingularToPlural.each do |singular, plural|
  26. define_method "test_singularize_#{plural}" do
  27. assert_equal(singular, ActiveSupport::Inflector.singularize(plural))
  28. assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize))
  29. end
  30. end
  31. def test_overwrite_previous_inflectors
  32. assert_equal("series", ActiveSupport::Inflector.singularize("series"))
  33. ActiveSupport::Inflector.inflections.singular "series", "serie"
  34. assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
  35. ActiveSupport::Inflector.inflections.uncountable "series" # Return to normal
  36. end
  37. MixtureToTitleCase.each do |before, titleized|
  38. define_method "test_titleize_#{before}" do
  39. assert_equal(titleized, ActiveSupport::Inflector.titleize(before))
  40. end
  41. end
  42. def test_camelize
  43. CamelToUnderscore.each do |camel, underscore|
  44. assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  45. end
  46. end
  47. def test_camelize_with_lower_downcases_the_first_letter
  48. assert_equal('capital', ActiveSupport::Inflector.camelize('Capital', false))
  49. end
  50. def test_underscore
  51. CamelToUnderscore.each do |camel, underscore|
  52. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  53. end
  54. CamelToUnderscoreWithoutReverse.each do |camel, underscore|
  55. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  56. end
  57. end
  58. def test_camelize_with_module
  59. CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
  60. assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  61. end
  62. end
  63. def test_underscore_with_slashes
  64. CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
  65. assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  66. end
  67. end
  68. def test_demodulize
  69. assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
  70. end
  71. def test_foreign_key
  72. ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
  73. assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
  74. end
  75. ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
  76. assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
  77. end
  78. end
  79. def test_tableize
  80. ClassNameToTableName.each do |class_name, table_name|
  81. assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
  82. end
  83. end
  84. def test_parameterize
  85. StringToParameterized.each do |some_string, parameterized_string|
  86. assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  87. end
  88. end
  89. def test_parameterize_and_normalize
  90. StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
  91. assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  92. end
  93. end
  94. def test_parameterize_with_custom_separator
  95. StringToParameterized.each do |some_string, parameterized_string|
  96. assert_equal(parameterized_string.gsub('-', '_'), ActiveSupport::Inflector.parameterize(some_string, '_'))
  97. end
  98. end
  99. def test_parameterize_with_multi_character_separator
  100. StringToParameterized.each do |some_string, parameterized_string|
  101. assert_equal(parameterized_string.gsub('-', '__sep__'), ActiveSupport::Inflector.parameterize(some_string, '__sep__'))
  102. end
  103. end
  104. def test_classify
  105. ClassNameToTableName.each do |class_name, table_name|
  106. assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
  107. assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
  108. end
  109. end
  110. def test_classify_with_symbol
  111. assert_nothing_raised do
  112. assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars)
  113. end
  114. end
  115. def test_classify_with_leading_schema_name
  116. assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar')
  117. end
  118. def test_humanize
  119. UnderscoreToHuman.each do |underscore, human|
  120. assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
  121. end
  122. end
  123. def test_humanize_by_rule
  124. ActiveSupport::Inflector.inflections do |inflect|
  125. inflect.human(/_cnt$/i, '\1_count')
  126. inflect.human(/^prefx_/i, '\1')
  127. end
  128. assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
  129. assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
  130. end
  131. def test_humanize_by_string
  132. ActiveSupport::Inflector.inflections do |inflect|
  133. inflect.human("col_rpted_bugs", "Reported bugs")
  134. end
  135. assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
  136. assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
  137. end
  138. def test_constantize
  139. assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") }
  140. assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
  141. assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") }
  142. assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") }
  143. assert_raise(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") }
  144. assert_raise(NameError) { ActiveSupport::Inflector.constantize("An invalid string") }
  145. assert_raise(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") }
  146. end
  147. def test_constantize_does_lexical_lookup
  148. assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
  149. end
  150. def test_ordinal
  151. OrdinalNumbers.each do |number, ordinalized|
  152. assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
  153. end
  154. end
  155. def test_dasherize
  156. UnderscoresToDashes.each do |underscored, dasherized|
  157. assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
  158. end
  159. end
  160. def test_underscore_as_reverse_of_dasherize
  161. UnderscoresToDashes.each do |underscored, dasherized|
  162. assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
  163. end
  164. end
  165. def test_underscore_to_lower_camel
  166. UnderscoreToLowerCamel.each do |underscored, lower_camel|
  167. assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
  168. end
  169. end
  170. def test_symbol_to_lower_camel
  171. SymbolToLowerCamel.each do |symbol, lower_camel|
  172. assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
  173. end
  174. end
  175. %w{plurals singulars uncountables humans}.each do |inflection_type|
  176. class_eval "
  177. def test_clear_#{inflection_type}
  178. cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
  179. ActiveSupport::Inflector.inflections.clear :#{inflection_type}
  180. assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
  181. ActiveSupport::Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values
  182. end
  183. "
  184. end
  185. def test_clear_all
  186. cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
  187. ActiveSupport::Inflector.inflections.clear :all
  188. assert ActiveSupport::Inflector.inflections.plurals.empty?
  189. assert ActiveSupport::Inflector.inflections.singulars.empty?
  190. assert ActiveSupport::Inflector.inflections.uncountables.empty?
  191. assert ActiveSupport::Inflector.inflections.humans.empty?
  192. ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
  193. ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
  194. ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
  195. ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
  196. end
  197. def test_clear_with_default
  198. cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables, ActiveSupport::Inflector.inflections.humans
  199. ActiveSupport::Inflector.inflections.clear
  200. assert ActiveSupport::Inflector.inflections.plurals.empty?
  201. assert ActiveSupport::Inflector.inflections.singulars.empty?
  202. assert ActiveSupport::Inflector.inflections.uncountables.empty?
  203. assert ActiveSupport::Inflector.inflections.humans.empty?
  204. ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
  205. ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
  206. ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
  207. ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
  208. end
  209. Irregularities.each do |irregularity|
  210. singular, plural = *irregularity
  211. ActiveSupport::Inflector.inflections do |inflect|
  212. define_method("test_irregularity_between_#{singular}_and_#{plural}") do
  213. inflect.irregular(singular, plural)
  214. assert_equal singular, ActiveSupport::Inflector.singularize(plural)
  215. assert_equal plural, ActiveSupport::Inflector.pluralize(singular)
  216. end
  217. end
  218. end
  219. Irregularities.each do |irregularity|
  220. singular, plural = *irregularity
  221. ActiveSupport::Inflector.inflections do |inflect|
  222. define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
  223. inflect.irregular(singular, plural)
  224. assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
  225. end
  226. end
  227. end
  228. [ :all, [] ].each do |scope|
  229. ActiveSupport::Inflector.inflections do |inflect|
  230. define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do
  231. # save all the inflections
  232. singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables
  233. # clear all the inflections
  234. inflect.clear(*scope)
  235. assert_equal [], inflect.singulars
  236. assert_equal [], inflect.plurals
  237. assert_equal [], inflect.uncountables
  238. # restore all the inflections
  239. singulars.reverse.each { |singular| inflect.singular(*singular) }
  240. plurals.reverse.each { |plural| inflect.plural(*plural) }
  241. inflect.uncountable(uncountables)
  242. assert_equal singulars, inflect.singulars
  243. assert_equal plurals, inflect.plurals
  244. assert_equal uncountables, inflect.uncountables
  245. end
  246. end
  247. end
  248. { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable, :humans => :human }.each do |scope, method|
  249. ActiveSupport::Inflector.inflections do |inflect|
  250. define_method("test_clear_inflections_with_#{scope}") do
  251. # save the inflections
  252. values = inflect.send(scope)
  253. # clear the inflections
  254. inflect.clear(scope)
  255. assert_equal [], inflect.send(scope)
  256. # restore the inflections
  257. if scope == :uncountables
  258. inflect.send(method, values)
  259. else
  260. values.reverse.each { |value| inflect.send(method, *value) }
  261. end
  262. assert_equal values, inflect.send(scope)
  263. end
  264. end
  265. end
  266. end