class InflectorTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/inflector_test.rb, line 13
def setup
  # Dups the singleton before each test, restoring the original inflections later.
  #
  # 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.
  @original_inflections = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
  ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections.dup)
end
teardown() click to toggle source
# File activesupport/test/inflector_test.rb, line 23
def teardown
  ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections)
end
test_acronym_override() click to toggle source
# File activesupport/test/inflector_test.rb, line 194
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
test_acronyms() click to toggle source
# File activesupport/test/inflector_test.rb, line 145
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"],
    ["Nested::RESTful",   "nested/restful",     "Nested/RESTful",     "Nested/RESTful"],
    ["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"],
    ["HTTP::RESTful",     "http/restful",       "HTTP/RESTful",       "HTTP/RESTful"],
    ["HTTP::RESTfulAPI",  "http/restful_api",   "HTTP/RESTful API",   "HTTP/RESTful API"],
    ["APIRESTful",        "api_restful",        "API RESTful",        "API RESTful"],

    # 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"],
    ["RoQuails",           "ro_quails",           "Ro quails",         "Ro Quails"]
  ].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
test_acronyms_camelize_lower() click to toggle source
# File activesupport/test/inflector_test.rb, line 206
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
test_camelize() click to toggle source
# File activesupport/test/inflector_test.rb, line 131
def test_camelize
  CamelToUnderscore.each do |camel, underscore|
    assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  end
end
test_camelize_with_lower_downcases_the_first_letter() click to toggle source
# File activesupport/test/inflector_test.rb, line 137
def test_camelize_with_lower_downcases_the_first_letter
  assert_equal("capital", ActiveSupport::Inflector.camelize("Capital", false))
end
test_camelize_with_module() click to toggle source
# File activesupport/test/inflector_test.rb, line 236
def test_camelize_with_module
  CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
    assert_equal(camel, ActiveSupport::Inflector.camelize(underscore))
  end
end
test_camelize_with_underscores() click to toggle source
# File activesupport/test/inflector_test.rb, line 141
def test_camelize_with_underscores
  assert_equal("CamelCase", ActiveSupport::Inflector.camelize("Camel_Case"))
end
test_classify() click to toggle source
# File activesupport/test/inflector_test.rb, line 307
def test_classify
  ClassNameToTableName.each do |class_name, table_name|
    assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
    assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name))
  end
end
test_classify_with_leading_schema_name() click to toggle source
# File activesupport/test/inflector_test.rb, line 320
def test_classify_with_leading_schema_name
  assert_equal "FooBar", ActiveSupport::Inflector.classify("schema.foo_bar")
end
test_classify_with_symbol() click to toggle source
# File activesupport/test/inflector_test.rb, line 314
def test_classify_with_symbol
  assert_nothing_raised do
    assert_equal "FooBar", ActiveSupport::Inflector.classify(:foo_bars)
  end
end
test_clear_all() click to toggle source
# File activesupport/test/inflector_test.rb, line 452
def test_clear_all
  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
end
test_clear_with_default() click to toggle source
# File activesupport/test/inflector_test.rb, line 469
def test_clear_with_default
  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
end
test_constantize() click to toggle source
# File activesupport/test/inflector_test.rb, line 359
def test_constantize
  run_constantize_tests_on do |string|
    ActiveSupport::Inflector.constantize(string)
  end
end
test_dasherize() click to toggle source
# File activesupport/test/inflector_test.rb, line 383
def test_dasherize
  UnderscoresToDashes.each do |underscored, dasherized|
    assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored))
  end
end
test_deconstantize() click to toggle source
# File activesupport/test/inflector_test.rb, line 255
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
test_demodulize() click to toggle source
# File activesupport/test/inflector_test.rb, line 248
def test_demodulize
  assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account")
  assert_equal "Account", ActiveSupport::Inflector.demodulize("Account")
  assert_equal "Account", ActiveSupport::Inflector.demodulize("::Account")
  assert_equal "", ActiveSupport::Inflector.demodulize("")
end
test_foreign_key() click to toggle source
# File activesupport/test/inflector_test.rb, line 267
def test_foreign_key
  ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
    assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass))
  end

  ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
    assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false))
  end
end
test_humanize() click to toggle source
# File activesupport/test/inflector_test.rb, line 324
def test_humanize
  UnderscoreToHuman.each do |underscore, human|
    assert_equal(human, ActiveSupport::Inflector.humanize(underscore))
  end
end
test_humanize_by_rule() click to toggle source
# File activesupport/test/inflector_test.rb, line 342
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
test_humanize_by_string() click to toggle source
# File activesupport/test/inflector_test.rb, line 351
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
test_humanize_with_keep_id_suffix() click to toggle source
# File activesupport/test/inflector_test.rb, line 336
def test_humanize_with_keep_id_suffix
  UnderscoreToHumanWithKeepIdSuffix.each do |underscore, human|
    assert_equal(human, ActiveSupport::Inflector.humanize(underscore, keep_id_suffix: true))
  end
end
test_humanize_without_capitalize() click to toggle source
# File activesupport/test/inflector_test.rb, line 330
def test_humanize_without_capitalize
  UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
    assert_equal(human, ActiveSupport::Inflector.humanize(underscore, capitalize: false))
  end
end
test_inflector_locality() click to toggle source
# File activesupport/test/inflector_test.rb, line 416
def test_inflector_locality
  ActiveSupport::Inflector.inflections(:es) do |inflect|
    inflect.plural(/$/, "s")
    inflect.plural(/z$/i, "ces")

    inflect.singular(/s$/, "")
    inflect.singular(/es$/, "")

    inflect.irregular("el", "los")

    inflect.uncountable("agua")
  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)

  assert_equal("agua", "agua".pluralize(:es))
  assert_equal("aguas", "agua".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(:es).uncountables.empty?
  assert !ActiveSupport::Inflector.inflections.plurals.empty?
  assert !ActiveSupport::Inflector.inflections.singulars.empty?
  assert !ActiveSupport::Inflector.inflections.uncountables.empty?
end
test_ordinal() click to toggle source
# File activesupport/test/inflector_test.rb, line 371
def test_ordinal
  OrdinalNumbers.each do |number, ordinalized|
    assert_equal(ordinalized, number + ActiveSupport::Inflector.ordinal(number))
  end
end
test_ordinalize() click to toggle source
# File activesupport/test/inflector_test.rb, line 377
def test_ordinalize
  OrdinalNumbers.each do |number, ordinalized|
    assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
  end
end
test_overwrite_previous_inflectors() click to toggle source
# File activesupport/test/inflector_test.rb, line 111
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"))
end
test_parameterize() click to toggle source
# File activesupport/test/inflector_test.rb, line 283
def test_parameterize
  StringToParameterized.each do |some_string, parameterized_string|
    assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  end
end
test_parameterize_and_normalize() click to toggle source
# File activesupport/test/inflector_test.rb, line 289
def test_parameterize_and_normalize
  StringToParameterizedAndNormalized.each do |some_string, parameterized_string|
    assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
  end
end
test_parameterize_with_custom_separator() click to toggle source
# File activesupport/test/inflector_test.rb, line 295
def test_parameterize_with_custom_separator
  StringToParameterizeWithUnderscore.each do |some_string, parameterized_string|
    assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string, separator: "_"))
  end
end
test_parameterize_with_multi_character_separator() click to toggle source
# File activesupport/test/inflector_test.rb, line 301
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, separator: "__sep__"))
  end
end
test_pluralize_empty_string() click to toggle source
# File activesupport/test/inflector_test.rb, line 32
def test_pluralize_empty_string
  assert_equal "", ActiveSupport::Inflector.pluralize("")
end
test_pluralize_plurals() click to toggle source
# File activesupport/test/inflector_test.rb, line 27
def test_pluralize_plurals
  assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
  assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
end
test_safe_constantize() click to toggle source
# File activesupport/test/inflector_test.rb, line 365
def test_safe_constantize
  run_safe_constantize_tests_on do |string|
    ActiveSupport::Inflector.safe_constantize(string)
  end
end
test_symbol_to_lower_camel() click to toggle source
# File activesupport/test/inflector_test.rb, line 401
def test_symbol_to_lower_camel
  SymbolToLowerCamel.each do |symbol, lower_camel|
    assert_equal(lower_camel, ActiveSupport::Inflector.camelize(symbol, false))
  end
end
test_tableize() click to toggle source
# File activesupport/test/inflector_test.rb, line 277
def test_tableize
  ClassNameToTableName.each do |class_name, table_name|
    assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name))
  end
end
test_uncountable_word_is_not_greedy() click to toggle source
# File activesupport/test/inflector_test.rb, line 70
def test_uncountable_word_is_not_greedy
  uncountable_word = "ors"
  countable_word = "sponsor"

  ActiveSupport::Inflector.inflections.uncountable << uncountable_word

  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)

  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
test_underscore() click to toggle source
# File activesupport/test/inflector_test.rb, line 227
def test_underscore
  CamelToUnderscore.each do |camel, underscore|
    assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  end
  CamelToUnderscoreWithoutReverse.each do |camel, underscore|
    assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  end
end
test_underscore_acronym_sequence() click to toggle source
# File activesupport/test/inflector_test.rb, line 217
def test_underscore_acronym_sequence
  ActiveSupport::Inflector.inflections do |inflect|
    inflect.acronym("API")
    inflect.acronym("JSON")
    inflect.acronym("HTML")
  end

  assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI"))
end
test_underscore_as_reverse_of_dasherize() click to toggle source
# File activesupport/test/inflector_test.rb, line 389
def test_underscore_as_reverse_of_dasherize
  UnderscoresToDashes.each_key do |underscored|
    assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored)))
  end
end
test_underscore_to_lower_camel() click to toggle source
# File activesupport/test/inflector_test.rb, line 395
def test_underscore_to_lower_camel
  UnderscoreToLowerCamel.each do |underscored, lower_camel|
    assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false))
  end
end
test_underscore_with_slashes() click to toggle source
# File activesupport/test/inflector_test.rb, line 242
def test_underscore_with_slashes
  CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore|
    assert_equal(underscore, ActiveSupport::Inflector.underscore(camel))
  end
end