class StringInflectionsTest

Public Instance Methods

test_camelize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 101
def test_camelize
  CamelToUnderscore.each do |camel, underscore|
    assert_equal(camel, underscore.camelize)
  end
end
test_camelize_invalid_option() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 111
def test_camelize_invalid_option
  e = assert_raise ArgumentError do
    "Capital".camelize(nil)
  end
  assert_equal("Invalid option, use either :upper or :lower.", e.message)
end
test_camelize_lower() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 107
def test_camelize_lower
  assert_equal("capital", "Capital".camelize(:lower))
end
test_classify() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 163
def test_classify
  ClassNameToTableName.each do |class_name, table_name|
    assert_equal(class_name, table_name.classify)
  end
end
test_constantize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 344
def test_constantize
  run_constantize_tests_on(&:constantize)
end
test_dasherize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 118
def test_dasherize
  UnderscoresToDashes.each do |underscored, dasherized|
    assert_equal(dasherized, underscored.dasherize)
  end
end
test_deconstantize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 143
def test_deconstantize
  assert_equal "MyApplication::Billing", "MyApplication::Billing::Account".deconstantize
end
test_demodulize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 139
def test_demodulize
  assert_equal "Account", "MyApplication::Billing::Account".demodulize
end
test_foreign_key() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 147
def test_foreign_key
  ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
    assert_equal(foreign_key, klass.foreign_key)
  end

  ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
    assert_equal(foreign_key, klass.foreign_key(false))
  end
end
test_humanize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 205
def test_humanize
  UnderscoreToHuman.each do |underscore, human|
    assert_equal(human, underscore.humanize)
  end
end
test_humanize_with_html_escape() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 223
def test_humanize_with_html_escape
  assert_equal "Hello", ERB::Util.html_escape("hello").humanize
end
test_humanize_with_keep_id_suffix() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 217
def test_humanize_with_keep_id_suffix
  UnderscoreToHumanWithKeepIdSuffix.each do |underscore, human|
    assert_equal(human, underscore.humanize(keep_id_suffix: true))
  end
end
test_humanize_without_capitalize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 211
def test_humanize_without_capitalize
  UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
    assert_equal(human, underscore.humanize(capitalize: false))
  end
end
test_ord() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 227
def test_ord
  assert_equal 97, "a".ord
  assert_equal 97, "abc".ord
end
test_pluralize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 54
def test_pluralize
  SingularToPlural.each do |singular, plural|
    assert_equal(plural, singular.pluralize)
  end

  assert_equal("plurals", "plurals".pluralize)

  assert_equal("blargles", "blargle".pluralize(0))
  assert_equal("blargle", "blargle".pluralize(1))
  assert_equal("blargles", "blargle".pluralize(2))
end
test_remove() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 323
def test_remove
  original = "This is a good day to die"
  assert_equal "This is a good day", original.remove(" to die")
  assert_equal "This is a good day", original.remove(" to ", /die/)
  assert_equal "This is a good day to die", original
end
test_remove!() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 336
def test_remove!
  original = "This is a very good day to die".dup
  assert_equal "This is a good day to die", original.remove!(" very")
  assert_equal "This is a good day to die", original
  assert_equal "This is a good day", original.remove!(" to ", /die/)
  assert_equal "This is a good day", original
end
test_remove_for_multiple_occurrences() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 330
def test_remove_for_multiple_occurrences
  original = "This is a good day to die to die"
  assert_equal "This is a good day", original.remove(" to die")
  assert_equal "This is a good day to die to die", original
end
test_safe_constantize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 348
def test_safe_constantize
  run_safe_constantize_tests_on(&:safe_constantize)
end
test_singularize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 71
def test_singularize
  SingularToPlural.each do |singular, plural|
    assert_equal(singular, plural.singularize)
  end
end
test_starts_ends_with_alias() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 232
def test_starts_ends_with_alias
  s = "hello"
  assert s.starts_with?("h")
  assert s.starts_with?("hel")
  assert !s.starts_with?("el")

  assert s.ends_with?("o")
  assert s.ends_with?("lo")
  assert !s.ends_with?("el")
end
test_string_inquiry() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 261
def test_string_inquiry
  assert "production".inquiry.production?
  assert !"production".inquiry.development?
end
test_string_parameterized_no_separator() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 181
def test_string_parameterized_no_separator
  StringToParameterizeWithNoSeparator.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize(separator: ""))
  end
end
test_string_parameterized_no_separator_preserve_case() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 187
def test_string_parameterized_no_separator_preserve_case
  StringToParameterizePreserveCaseWithNoSeparator.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize(separator: "", preserve_case: true))
  end
end
test_string_parameterized_normal() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 169
def test_string_parameterized_normal
  StringToParameterized.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize)
  end
end
test_string_parameterized_normal_preserve_case() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 175
def test_string_parameterized_normal_preserve_case
  StringToParameterizedPreserveCase.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize(preserve_case: true))
  end
end
test_string_parameterized_underscore() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 193
def test_string_parameterized_underscore
  StringToParameterizeWithUnderscore.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize(separator: "_"))
  end
end
test_string_parameterized_underscore_preserve_case() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 199
def test_string_parameterized_underscore_preserve_case
  StringToParameterizePreserceCaseWithUnderscore.each do |normal, slugged|
    assert_equal(slugged, normal.parameterize(separator: "_", preserve_case: true))
  end
end
test_string_squish() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 243
def test_string_squish
  original = %{\u205f\u3000 A string surrounded by various unicode spaces,
    with tabs(\t\t), newlines(\n\n), unicode nextlines(\u0085\u0085) and many spaces(  ). \u00a0\u2007}.dup

  expected = "A string surrounded by various unicode spaces, " \
    "with tabs( ), newlines( ), unicode nextlines( ) and many spaces( )."

  # Make sure squish returns what we expect:
  assert_equal expected, original.squish
  # But doesn't modify the original string:
  assert_not_equal expected, original

  # Make sure squish! returns what we expect:
  assert_equal expected, original.squish!
  # And changes the original string:
  assert_equal expected, original
end
test_strip_heredoc_on_a_heredoc_with_no_margin() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 32
def test_strip_heredoc_on_a_heredoc_with_no_margin
  assert_equal "foo\nbar", "foo\nbar".strip_heredoc
  assert_equal "foo\n  bar", "foo\n  bar".strip_heredoc
end
test_strip_heredoc_on_a_regular_indented_heredoc() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 37
  def test_strip_heredoc_on_a_regular_indented_heredoc
    assert_equal "foo\n  bar\nbaz\n", <<-EOS.strip_heredoc
      foo
        bar
      baz
    EOS
  end
test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 45
  def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines
    assert_equal "foo\n  bar\n\nbaz\n", <<-EOS.strip_heredoc
      foo
        bar

      baz
    EOS
  end
test_strip_heredoc_on_a_string_with_no_lines() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 27
def test_strip_heredoc_on_a_string_with_no_lines
  assert_equal "x", "x".strip_heredoc
  assert_equal "x", "    x".strip_heredoc
end
test_strip_heredoc_on_an_empty_string() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 23
def test_strip_heredoc_on_an_empty_string
  assert_equal "", "".strip_heredoc
end
test_tableize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 157
def test_tableize
  ClassNameToTableName.each do |class_name, table_name|
    assert_equal(table_name, class_name.tableize)
  end
end
test_titleize() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 77
def test_titleize
  MixtureToTitleCase.each do |before, titleized|
    assert_equal(titleized, before.titleize)
  end
end
test_titleize_with_keep_id_suffix() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 83
def test_titleize_with_keep_id_suffix
  MixtureToTitleCaseWithKeepIdSuffix.each do |before, titleized|
    assert_equal(titleized, before.titleize(keep_id_suffix: true))
  end
end
test_truncate() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 266
def test_truncate
  assert_equal "Hello World!", "Hello World!".truncate(12)
  assert_equal "Hello Wor...", "Hello World!!".truncate(12)
end
test_truncate_multibyte() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 314
def test_truncate_multibyte
  assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".dup.force_encoding(Encoding::UTF_8),
    "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".dup.force_encoding(Encoding::UTF_8).truncate(10)
end
test_truncate_should_not_be_html_safe() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 319
def test_truncate_should_not_be_html_safe
  assert !"Hello World!".truncate(12).html_safe?
end
test_truncate_with_omission_and_regexp_separator() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 278
def test_truncate_with_omission_and_regexp_separator
  assert_equal "Hello[...]", "Hello Big World!".truncate(13, omission: "[...]", separator: /\s/)
  assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, omission: "[...]", separator: /\s/)
  assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, omission: "[...]", separator: /\s/)
end
test_truncate_with_omission_and_separator() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 271
def test_truncate_with_omission_and_separator
  assert_equal "Hello[...]", "Hello World!".truncate(10, omission: "[...]")
  assert_equal "Hello[...]", "Hello Big World!".truncate(13, omission: "[...]", separator: " ")
  assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, omission: "[...]", separator: " ")
  assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, omission: "[...]", separator: " ")
end
test_truncate_words() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 284
def test_truncate_words
  assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3)
  assert_equal "Hello Big...", "Hello Big World!".truncate_words(2)
end
test_truncate_words_with_complex_string() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 305
def test_truncate_words_with_complex_string
  Timeout.timeout(10) do
    complex_string = "aa aa aaa aa aaa aaa aaa aa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaaa aaaaa aaaaa aaaaaa aa aa aa aaa aa  aaa aa aa aa aa a aaa aaa \n a aaa <<s"
    assert_equal complex_string, complex_string.truncate_words(80)
  end
rescue Timeout::Error
  assert false
end
test_truncate_words_with_omission() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 289
def test_truncate_words_with_omission
  assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3, omission: "[...]")
  assert_equal "Hello Big[...]", "Hello Big World!".truncate_words(2, omission: "[...]")
end
test_truncate_words_with_separator() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 294
def test_truncate_words_with_separator
  assert_equal "Hello<br>Big<br>World!...", "Hello<br>Big<br>World!<br>".truncate_words(3, separator: "<br>")
  assert_equal "Hello<br>Big<br>World!", "Hello<br>Big<br>World!".truncate_words(3, separator: "<br>")
  assert_equal "Hello\n<br>Big...", "Hello\n<br>Big<br>Wide<br>World!".truncate_words(2, separator: "<br>")
end
test_truncate_words_with_separator_and_omission() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 300
def test_truncate_words_with_separator_and_omission
  assert_equal "Hello<br>Big<br>World![...]", "Hello<br>Big<br>World!<br>".truncate_words(3, omission: "[...]", separator: "<br>")
  assert_equal "Hello<br>Big<br>World!", "Hello<br>Big<br>World!".truncate_words(3, omission: "[...]", separator: "<br>")
end
test_underscore() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 124
def test_underscore
  CamelToUnderscore.each do |camel, underscore|
    assert_equal(underscore, camel.underscore)
  end

  assert_equal "html_tidy", "HTMLTidy".underscore
  assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
end
test_underscore_to_lower_camel() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 133
def test_underscore_to_lower_camel
  UnderscoreToLowerCamel.each do |underscored, lower_camel|
    assert_equal(lower_camel, underscored.camelize(:lower))
  end
end
test_upcase_first() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 89
def test_upcase_first
  assert_equal "What a Lovely Day", "what a Lovely Day".upcase_first
end
test_upcase_first_with_empty_string() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 97
def test_upcase_first_with_empty_string
  assert_equal "", "".upcase_first
end
test_upcase_first_with_one_char() click to toggle source
# File activesupport/test/core_ext/string_ext_test.rb, line 93
def test_upcase_first_with_one_char
  assert_equal "W", "w".upcase_first
end