inflector_test.rb 18.5 KB
Newer Older
1
require 'abstract_unit'
2 3
require 'active_support/inflector'

4
require 'inflector_test_cases'
5
require 'constantize_test_cases'
6

7
class InflectorTest < ActiveSupport::TestCase
8
  include InflectorTestCases
9
  include ConstantizeTestCases
10

11
  def test_pluralize_plurals
12 13
    assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
    assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
14 15
  end

16
  def test_pluralize_empty_string
17
    assert_equal "", ActiveSupport::Inflector.pluralize("")
18
  end
19

20 21 22 23 24 25 26
  ActiveSupport::Inflector.inflections.uncountable.each do |word|
    define_method "test_uncountability_of_#{word}" do
      assert_equal word, ActiveSupport::Inflector.singularize(word)
      assert_equal word, ActiveSupport::Inflector.pluralize(word)
      assert_equal ActiveSupport::Inflector.pluralize(word), ActiveSupport::Inflector.singularize(word)
    end
  end
27

28
  def test_uncountable_word_is_not_greedy
29 30 31
    with_dup do
      uncountable_word = "ors"
      countable_word = "sponsor"
32

33
      ActiveSupport::Inflector.inflections.uncountable << uncountable_word
34

35 36 37
      assert_equal uncountable_word, ActiveSupport::Inflector.singularize(uncountable_word)
      assert_equal uncountable_word, ActiveSupport::Inflector.pluralize(uncountable_word)
      assert_equal ActiveSupport::Inflector.pluralize(uncountable_word), ActiveSupport::Inflector.singularize(uncountable_word)
38

39 40 41 42
      assert_equal "sponsor", ActiveSupport::Inflector.singularize(countable_word)
      assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
      assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
    end
43
  end
44

45
  SingularToPlural.each do |singular, plural|
46
    define_method "test_pluralize_singular_#{singular}" do
47 48
      assert_equal(plural, ActiveSupport::Inflector.pluralize(singular))
      assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize))
49 50 51
    end
  end

52
  SingularToPlural.each do |singular, plural|
53
    define_method "test_singularize_plural_#{plural}" do
54 55
      assert_equal(singular, ActiveSupport::Inflector.singularize(plural))
      assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize))
56 57
    end
  end
58

59
  SingularToPlural.each do |singular, plural|
60
    define_method "test_pluralize_plural_#{plural}" do
61 62 63
      assert_equal(plural, ActiveSupport::Inflector.pluralize(plural))
      assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize))
    end
64

65 66 67 68 69
    define_method "test_singularize_singular_#{singular}" do
      assert_equal(singular, ActiveSupport::Inflector.singularize(singular))
      assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(singular.capitalize))
    end
  end
70

71

72 73 74 75 76 77 78
  def test_overwrite_previous_inflectors
    assert_equal("series", ActiveSupport::Inflector.singularize("series"))
    ActiveSupport::Inflector.inflections.singular "series", "serie"
    assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
    ActiveSupport::Inflector.inflections.uncountable "series" # Return to normal
  end

79 80
  MixtureToTitleCase.each do |before, titleized|
    define_method "test_titleize_#{before}" do
81
      assert_equal(titleized, ActiveSupport::Inflector.titleize(before))
82 83 84
    end
  end

85 86
  def test_camelize
    CamelToUnderscore.each do |camel, underscore|
87
      assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
88 89 90
    end
  end

91 92 93 94
  def test_camelize_with_lower_downcases_the_first_letter
    assert_equal('capital', ActiveSupport::Inflector.camelize('Capital', false))
  end

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  def test_camelize_with_underscores
    assert_equal("CamelCase", ActiveSupport::Inflector.camelize('Camel_Case'))
  end

  def test_acronyms
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.acronym("API")
      inflect.acronym("HTML")
      inflect.acronym("HTTP")
      inflect.acronym("RESTful")
      inflect.acronym("W3C")
      inflect.acronym("PhD")
      inflect.acronym("RoR")
      inflect.acronym("SSL")
    end

    #  camelize             underscore            humanize              titleize
    [
      ["API",               "api",                "API",                "API"],
      ["APIController",     "api_controller",     "API controller",     "API Controller"],
      ["Nokogiri::HTML",    "nokogiri/html",      "Nokogiri/HTML",      "Nokogiri/HTML"],
      ["HTTPAPI",           "http_api",           "HTTP API",           "HTTP API"],
      ["HTTP::Get",         "http/get",           "HTTP/get",           "HTTP/Get"],
      ["SSLError",          "ssl_error",          "SSL error",          "SSL Error"],
      ["RESTful",           "restful",            "RESTful",            "RESTful"],
      ["RESTfulController", "restful_controller", "RESTful controller", "RESTful Controller"],
      ["IHeartW3C",         "i_heart_w3c",        "I heart W3C",        "I Heart W3C"],
      ["PhDRequired",       "phd_required",       "PhD required",       "PhD Required"],
      ["IRoRU",             "i_ror_u",            "I RoR u",            "I RoR U"],
      ["RESTfulHTTPAPI",    "restful_http_api",   "RESTful HTTP API",   "RESTful HTTP API"],

      # misdirection
      ["Capistrano",        "capistrano",         "Capistrano",       "Capistrano"],
      ["CapiController",    "capi_controller",    "Capi controller",  "Capi Controller"],
      ["HttpsApis",         "https_apis",         "Https apis",       "Https Apis"],
      ["Html5",             "html5",              "Html5",            "Html5"],
      ["Restfully",         "restfully",          "Restfully",        "Restfully"],
      ["RoRails",           "ro_rails",           "Ro rails",         "Ro Rails"]
    ].each do |camel, under, human, title|
      assert_equal(camel, ActiveSupport::Inflector.camelize(under))
      assert_equal(camel, ActiveSupport::Inflector.camelize(camel))
      assert_equal(under, ActiveSupport::Inflector.underscore(under))
      assert_equal(under, ActiveSupport::Inflector.underscore(camel))
      assert_equal(title, ActiveSupport::Inflector.titleize(under))
      assert_equal(title, ActiveSupport::Inflector.titleize(camel))
      assert_equal(human, ActiveSupport::Inflector.humanize(under))
    end
  end

  def test_acronym_override
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.acronym("API")
      inflect.acronym("LegacyApi")
    end

    assert_equal("LegacyApi", ActiveSupport::Inflector.camelize("legacyapi"))
    assert_equal("LegacyAPI", ActiveSupport::Inflector.camelize("legacy_api"))
    assert_equal("SomeLegacyApi", ActiveSupport::Inflector.camelize("some_legacyapi"))
    assert_equal("Nonlegacyapi", ActiveSupport::Inflector.camelize("nonlegacyapi"))
  end

  def test_acronyms_camelize_lower
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.acronym("API")
      inflect.acronym("HTML")
    end

    assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("html_api", false))
    assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("htmlAPI", false))
    assert_equal("htmlAPI", ActiveSupport::Inflector.camelize("HTMLAPI", false))
  end

  def test_underscore_acronym_sequence
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.acronym("API")
170
      inflect.acronym("JSON")
171 172 173
      inflect.acronym("HTML")
    end

174
    assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI"))
175 176
  end

177 178
  def test_underscore
    CamelToUnderscore.each do |camel, underscore|
179
      assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
180
    end
181
    CamelToUnderscoreWithoutReverse.each do |camel, underscore|
182
      assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
183
    end
184 185
  end

186 187
  def test_camelize_with_module
    CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
188
      assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
189 190
    end
  end
191

192 193
  def test_underscore_with_slashes
    CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
194
      assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
195 196 197
    end
  end

198
  def test_demodulize
199
    assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
200 201
    assert_equal "Account", ActiveSupport::Inflector.demodulize("Account")
    assert_equal "", ActiveSupport::Inflector.demodulize("")
202 203
  end

204 205 206 207 208 209 210 211 212 213 214 215
  def test_deconstantize
    assert_equal "MyApplication::Billing", ActiveSupport::Inflector.deconstantize("MyApplication::Billing::Account")
    assert_equal "::MyApplication::Billing", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing::Account")

    assert_equal "MyApplication", ActiveSupport::Inflector.deconstantize("MyApplication::Billing")
    assert_equal "::MyApplication", ActiveSupport::Inflector.deconstantize("::MyApplication::Billing")

    assert_equal "", ActiveSupport::Inflector.deconstantize("Account")
    assert_equal "", ActiveSupport::Inflector.deconstantize("::Account")
    assert_equal "", ActiveSupport::Inflector.deconstantize("")
  end

216 217
  def test_foreign_key
    ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
218
      assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
219 220 221
    end

    ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
222
      assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
223 224 225 226 227
    end
  end

  def test_tableize
    ClassNameToTableName.each do |class_name, table_name|
228
      assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
229 230 231
    end
  end

232 233 234 235 236 237
  def test_parameterize
    StringToParameterized.each do |some_string, parameterized_string|
      assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
    end
  end

238 239 240 241 242 243
  def test_parameterize_and_normalize
    StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
      assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
    end
  end

244
  def test_parameterize_with_custom_separator
245 246
    StringToParameterizeWithUnderscore.each do |some_string, parameterized_string|
      assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string, '_'))
247 248 249
    end
  end

250 251 252 253 254 255
  def test_parameterize_with_multi_character_separator
    StringToParameterized.each do |some_string, parameterized_string|
      assert_equal(parameterized_string.gsub('-', '__sep__'), ActiveSupport::Inflector.parameterize(some_string, '__sep__'))
    end
  end

256 257
  def test_classify
    ClassNameToTableName.each do |class_name, table_name|
258 259
      assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
      assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
260 261
    end
  end
262

263 264 265 266 267 268
  def test_classify_with_symbol
    assert_nothing_raised do
      assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars)
    end
  end

269
  def test_classify_with_leading_schema_name
270
    assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar')
271 272
  end

273 274
  def test_humanize
    UnderscoreToHuman.each do |underscore, human|
275
      assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
276 277
    end
  end
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  def test_humanize_by_rule
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.human(/_cnt$/i, '\1_count')
      inflect.human(/^prefx_/i, '\1')
    end
    assert_equal("Jargon count", ActiveSupport::Inflector.humanize("jargon_cnt"))
    assert_equal("Request", ActiveSupport::Inflector.humanize("prefx_request"))
  end

  def test_humanize_by_string
    ActiveSupport::Inflector.inflections do |inflect|
      inflect.human("col_rpted_bugs", "Reported bugs")
    end
    assert_equal("Reported bugs", ActiveSupport::Inflector.humanize("col_rpted_bugs"))
    assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
  end

296
  def test_constantize
297 298 299
    run_constantize_tests_on do |string|
      ActiveSupport::Inflector.constantize(string)
    end
300
  end
301

302 303 304 305
  def test_safe_constantize
    run_safe_constantize_tests_on do |string|
      ActiveSupport::Inflector.safe_constantize(string)
    end
306
  end
307 308

  def test_ordinal
309 310 311 312 313 314
    OrdinalNumbers.each do |number, ordinalized|
      assert_equal(ordinalized, number + ActiveSupport::Inflector.ordinal(number))
    end
  end

  def test_ordinalize
315
    OrdinalNumbers.each do |number, ordinalized|
316
      assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
317 318
    end
  end
319 320 321

  def test_dasherize
    UnderscoresToDashes.each do |underscored, dasherized|
322
      assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
323 324
    end
  end
325 326

  def test_underscore_as_reverse_of_dasherize
327
    UnderscoresToDashes.each_key do |underscored|
328
      assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
329 330
    end
  end
331 332 333

  def test_underscore_to_lower_camel
    UnderscoreToLowerCamel.each do |underscored, lower_camel|
334
      assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
335 336
    end
  end
337

338 339 340 341 342 343
  def test_symbol_to_lower_camel
    SymbolToLowerCamel.each do |symbol, lower_camel|
      assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
    end
  end

344
  %w{plurals singulars uncountables humans}.each do |inflection_type|
345
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
346
      def test_clear_#{inflection_type}
347 348 349 350
        with_dup do
          ActiveSupport::Inflector.inflections.clear :#{inflection_type}
          assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
        end
351
      end
352
    RUBY
353
  end
354

355 356 357 358 359 360 361
  def test_inflector_locality
    ActiveSupport::Inflector.inflections(:es) do |inflect|
      inflect.plural(/$/, 's')
      inflect.plural(/z$/i, 'ces')

      inflect.singular(/s$/, '')
      inflect.singular(/es$/, '')
362

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
      inflect.irregular('el', 'los')
    end

    assert_equal('hijos', 'hijo'.pluralize(:es))
    assert_equal('luces', 'luz'.pluralize(:es))
    assert_equal('luzs', 'luz'.pluralize)

    assert_equal('sociedad', 'sociedades'.singularize(:es))
    assert_equal('sociedade', 'sociedades'.singularize)

    assert_equal('los', 'el'.pluralize(:es))
    assert_equal('els', 'el'.pluralize)

    ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear }

    assert ActiveSupport::Inflector.inflections(:es).plurals.empty?
    assert ActiveSupport::Inflector.inflections(:es).singulars.empty?
    assert !ActiveSupport::Inflector.inflections.plurals.empty?
    assert !ActiveSupport::Inflector.inflections.singulars.empty?
  end

384
  def test_clear_all
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    with_dup do
      ActiveSupport::Inflector.inflections do |inflect|
        # ensure any data is present
        inflect.plural(/(quiz)$/i, '\1zes')
        inflect.singular(/(database)s$/i, '\1')
        inflect.uncountable('series')
        inflect.human("col_rpted_bugs", "Reported bugs")

        inflect.clear :all

        assert inflect.plurals.empty?
        assert inflect.singulars.empty?
        assert inflect.uncountables.empty?
        assert inflect.humans.empty?
      end
S
Stefan Huber 已提交
400
    end
401 402
  end

403
  def test_clear_with_default
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    with_dup do
      ActiveSupport::Inflector.inflections do |inflect|
        # ensure any data is present
        inflect.plural(/(quiz)$/i, '\1zes')
        inflect.singular(/(database)s$/i, '\1')
        inflect.uncountable('series')
        inflect.human("col_rpted_bugs", "Reported bugs")

        inflect.clear

        assert inflect.plurals.empty?
        assert inflect.singulars.empty?
        assert inflect.uncountables.empty?
        assert inflect.humans.empty?
      end
S
Stefan Huber 已提交
419
    end
420
  end
421 422 423

  Irregularities.each do |irregularity|
    singular, plural = *irregularity
424
    ActiveSupport::Inflector.inflections do |inflect|
425 426
      define_method("test_irregularity_between_#{singular}_and_#{plural}") do
        inflect.irregular(singular, plural)
427 428
        assert_equal singular, ActiveSupport::Inflector.singularize(plural)
        assert_equal plural, ActiveSupport::Inflector.pluralize(singular)
429 430 431 432
      end
    end
  end

433 434 435 436 437 438 439 440 441 442
  Irregularities.each do |irregularity|
    singular, plural = *irregularity
    ActiveSupport::Inflector.inflections do |inflect|
      define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
        inflect.irregular(singular, plural)
        assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
      end
    end
  end

443 444 445 446 447 448 449 450 451 452
  Irregularities.each do |irregularity|
    singular, plural = *irregularity
    ActiveSupport::Inflector.inflections do |inflect|
      define_method("test_singularize_of_irregularity_#{singular}_should_be_the_same") do
        inflect.irregular(singular, plural)
        assert_equal singular, ActiveSupport::Inflector.singularize(singular)
      end
    end
  end

453
  [ :all, [] ].each do |scope|
454
    ActiveSupport::Inflector.inflections do |inflect|
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
      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

478
  %w(plurals singulars uncountables humans acronyms).each do |scope|
479
    ActiveSupport::Inflector.inflections do |inflect|
480
      define_method("test_clear_inflections_with_#{scope}") do
481 482 483 484
        with_dup do
          # clear the inflections
          inflect.clear(scope)
          assert_equal [], inflect.send(scope)
485 486 487 488
        end
      end
    end
  end
489 490 491 492 493 494 495 496

  # Dups the singleton and yields, restoring the original inflections later.
  # Use this in tests what modify the state of the singleton.
  #
  # This helper is implemented by setting @__instance__ because in some tests
  # there are module functions that access ActiveSupport::Inflector.inflections,
  # so we need to replace the singleton itself.
  def with_dup
497
    original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)
498 499 500 501
    ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original.dup)
  ensure
    ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original)
  end
502
end