diff options
Diffstat (limited to 'activesupport/test/core_ext/string_ext_test.rb')
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 185 |
1 files changed, 103 insertions, 82 deletions
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 2e69816364..4b40503d22 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -1,15 +1,16 @@ -require 'date' -require 'abstract_unit' -require 'inflector_test_cases' -require 'constantize_test_cases' - -require 'active_support/inflector' -require 'active_support/core_ext/string' -require 'active_support/time' -require 'active_support/core_ext/string/strip' -require 'active_support/core_ext/string/output_safety' -require 'active_support/core_ext/string/indent' -require 'time_zone_test_helpers' +require "date" +require "abstract_unit" +require "inflector_test_cases" +require "constantize_test_cases" + +require "active_support/inflector" +require "active_support/core_ext/string" +require "active_support/time" +require "active_support/core_ext/string/strip" +require "active_support/core_ext/string/output_safety" +require "active_support/core_ext/string/indent" +require "time_zone_test_helpers" +require "yaml" class StringInflectionsTest < ActiveSupport::TestCase include InflectorTestCases @@ -17,12 +18,12 @@ class StringInflectionsTest < ActiveSupport::TestCase include TimeZoneTestHelpers def test_strip_heredoc_on_an_empty_string - assert_equal '', ''.strip_heredoc + assert_equal "", "".strip_heredoc end def test_strip_heredoc_on_a_string_with_no_lines - assert_equal 'x', 'x'.strip_heredoc - assert_equal 'x', ' x'.strip_heredoc + assert_equal "x", "x".strip_heredoc + assert_equal "x", " x".strip_heredoc end def test_strip_heredoc_on_a_heredoc_with_no_margin @@ -59,7 +60,7 @@ class StringInflectionsTest < ActiveSupport::TestCase assert_equal("blargles", "blargle".pluralize(2)) end - test 'pluralize with count = 1 still returns new string' do + test "pluralize with count = 1 still returns new string" do name = "Kuldeep" assert_not_same name.pluralize(1), name end @@ -76,6 +77,18 @@ class StringInflectionsTest < ActiveSupport::TestCase end end + def test_upcase_first + assert_equal "What a Lovely Day", "what a Lovely Day".upcase_first + end + + def test_upcase_first_with_one_char + assert_equal "W", "w".upcase_first + end + + def test_upcase_first_with_empty_string + assert_equal "", "".upcase_first + end + def test_camelize CamelToUnderscore.each do |camel, underscore| assert_equal(camel, underscore.camelize) @@ -83,7 +96,7 @@ class StringInflectionsTest < ActiveSupport::TestCase end def test_camelize_lower - assert_equal('capital', 'Capital'.camelize(:lower)) + assert_equal("capital", "Capital".camelize(:lower)) end def test_dasherize @@ -151,41 +164,41 @@ class StringInflectionsTest < ActiveSupport::TestCase def test_string_parameterized_no_separator StringToParameterizeWithNoSeparator.each do |normal, slugged| - assert_equal(normal.parameterize(separator: ''), slugged) + assert_equal(normal.parameterize(separator: ""), slugged) end end def test_string_parameterized_no_separator_deprecated StringToParameterizeWithNoSeparator.each do |normal, slugged| assert_deprecated(/Passing the separator argument as a positional parameter is deprecated and will soon be removed. Use `separator: ''` instead./i) do - assert_equal(normal.parameterize(''), slugged) + assert_equal(normal.parameterize(""), slugged) end end end def test_string_parameterized_no_separator_preserve_case StringToParameterizePreserveCaseWithNoSeparator.each do |normal, slugged| - assert_equal(normal.parameterize(separator: '', preserve_case: true), slugged) + assert_equal(normal.parameterize(separator: "", preserve_case: true), slugged) end end def test_string_parameterized_underscore StringToParameterizeWithUnderscore.each do |normal, slugged| - assert_equal(normal.parameterize(separator: '_'), slugged) + assert_equal(normal.parameterize(separator: "_"), slugged) end end def test_string_parameterized_underscore_deprecated StringToParameterizeWithUnderscore.each do |normal, slugged| assert_deprecated(/Passing the separator argument as a positional parameter is deprecated and will soon be removed. Use `separator: '_'` instead./i) do - assert_equal(normal.parameterize('_'), slugged) + assert_equal(normal.parameterize("_"), slugged) end end end def test_string_parameterized_underscore_preserve_case StringToParameterizePreserceCaseWithUnderscore.each do |normal, slugged| - assert_equal(normal.parameterize(separator: '_', preserve_case: true), slugged) + assert_equal(normal.parameterize(separator: "_", preserve_case: true), slugged) end end @@ -202,23 +215,23 @@ class StringInflectionsTest < ActiveSupport::TestCase end def test_humanize_with_html_escape - assert_equal 'Hello', ERB::Util.html_escape("hello").humanize + assert_equal "Hello", ERB::Util.html_escape("hello").humanize end def test_ord - assert_equal 97, 'a'.ord - assert_equal 97, 'abc'.ord + assert_equal 97, "a".ord + assert_equal 97, "abc".ord end 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.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') + assert s.ends_with?("o") + assert s.ends_with?("lo") + assert !s.ends_with?("el") end def test_string_squish @@ -250,16 +263,16 @@ class StringInflectionsTest < ActiveSupport::TestCase end 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 => ' ') + 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 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/) + 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 def test_truncate_words @@ -268,19 +281,19 @@ class StringInflectionsTest < ActiveSupport::TestCase end 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 => "[...]") + assert_equal "Hello Big World!", "Hello Big World!".truncate_words(3, omission: "[...]") + assert_equal "Hello Big[...]", "Hello Big World!".truncate_words(2, omission: "[...]") end 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>') + 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 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>') + 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 def test_truncate_words_with_complex_string @@ -332,7 +345,7 @@ class StringInflectionsTest < ActiveSupport::TestCase end class StringAccessTest < ActiveSupport::TestCase - test "#at with Fixnum, returns a substring of one character at that position" do + test "#at with Integer, returns a substring of one character at that position" do assert_equal "h", "hello".at(0) end @@ -345,19 +358,19 @@ class StringAccessTest < ActiveSupport::TestCase assert_equal nil, "hello".at(/nonexisting/) end - test "#from with positive Fixnum, returns substring from the given position to the end" do + test "#from with positive Integer, returns substring from the given position to the end" do assert_equal "llo", "hello".from(2) end - test "#from with negative Fixnum, position is counted from the end" do + test "#from with negative Integer, position is counted from the end" do assert_equal "lo", "hello".from(-2) end - test "#to with positive Fixnum, substring from the beginning to the given position" do + test "#to with positive Integer, substring from the beginning to the given position" do assert_equal "hel", "hello".to(2) end - test "#to with negative Fixnum, position is counted from the end" do + test "#to with negative Integer, position is counted from the end" do assert_equal "hell", "hello".to(-2) end @@ -368,17 +381,17 @@ class StringAccessTest < ActiveSupport::TestCase test "#first returns the first character" do assert_equal "h", "hello".first - assert_equal 'x', 'x'.first + assert_equal "x", "x".first end - test "#first with Fixnum, returns a substring from the beginning to position" do + test "#first with Integer, returns a substring from the beginning to position" do assert_equal "he", "hello".first(2) assert_equal "", "hello".first(0) assert_equal "hello", "hello".first(10) - assert_equal 'x', 'x'.first(4) + assert_equal "x", "x".first(4) end - test "#first with Fixnum >= string length still returns a new string" do + test "#first with Integer >= string length still returns a new string" do string = "hello" different_string = string.first(5) assert_not_same different_string, string @@ -386,17 +399,17 @@ class StringAccessTest < ActiveSupport::TestCase test "#last returns the last character" do assert_equal "o", "hello".last - assert_equal 'x', 'x'.last + assert_equal "x", "x".last end - test "#last with Fixnum, returns a substring from the end to position" do + test "#last with Integer, returns a substring from the end to position" do assert_equal "llo", "hello".last(3) assert_equal "hello", "hello".last(10) assert_equal "", "hello".last(0) - assert_equal 'x', 'x'.last(4) + assert_equal "x", "x".last(4) end - test "#last with Fixnum >= string length still returns a new string" do + test "#last with Integer >= string length still returns a new string" do string = "hello" different_string = string.last(5) assert_not_same different_string, string @@ -444,16 +457,24 @@ class StringConversionsTest < ActiveSupport::TestCase assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc) assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time + assert_nil "010".to_time assert_nil "".to_time end end def test_string_to_time_utc_offset with_env_tz "US/Eastern" do - assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset - assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) - assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset - assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset) + if ActiveSupport.to_time_preserves_timezone + assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) + assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset + assert_equal(-3600, "2005-02-27 22:50 -0100".to_time.utc_offset) + else + assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) + assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset) + end end end @@ -612,14 +633,14 @@ end class StringBehaviourTest < ActiveSupport::TestCase def test_acts_like_string - assert 'Bambi'.acts_like_string? + assert "Bambi".acts_like_string? end end class CoreExtStringMultibyteTest < ActiveSupport::TestCase - UTF8_STRING = 'こにちわ' - ASCII_STRING = 'ohayo'.encode('US-ASCII') - EUC_JP_STRING = 'さよなら'.encode('EUC-JP') + UTF8_STRING = "こにちわ" + ASCII_STRING = "ohayo".encode("US-ASCII") + EUC_JP_STRING = "さよなら".encode("EUC-JP") INVALID_UTF8_STRING = "\270\236\010\210\245" def test_core_ext_adds_mb_chars @@ -661,7 +682,7 @@ class OutputSafetyTest < ActiveSupport::TestCase assert_equal @string, @string.html_safe end - test "A fixnum is safe by default" do + test "An integer is safe by default" do assert 5.html_safe? end @@ -792,15 +813,15 @@ class OutputSafetyTest < ActiveSupport::TestCase assert_equal ["<p>", "<b>", "<h1>"], @other_string end - test "Concatting a fixnum to safe always yields safe" do + test "Concatting an integer to safe always yields safe" do string = @string.html_safe string = string.concat(13) assert_equal "hello".concat(13), string assert string.html_safe? end - test 'emits normal string yaml' do - assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1) + test "emits normal string yaml" do + assert_equal "foo".to_yaml, "foo".html_safe.to_yaml(foo: 1) end test "call to_param returns a normal string" do @@ -811,7 +832,7 @@ class OutputSafetyTest < ActiveSupport::TestCase test "ERB::Util.html_escape should escape unsafe characters" do string = '<>&"\'' - expected = '<>&"'' + expected = "<>&"'" assert_equal expected, ERB::Util.html_escape(string) end @@ -827,7 +848,7 @@ class OutputSafetyTest < ActiveSupport::TestCase end test "ERB::Util.html_escape_once only escapes once" do - string = '1 < 2 & 3' + string = "1 < 2 & 3" escaped_string = "1 < 2 & 3" assert_equal escaped_string, ERB::Util.html_escape_once(string) @@ -842,15 +863,15 @@ class OutputSafetyTest < ActiveSupport::TestCase end class StringExcludeTest < ActiveSupport::TestCase - test 'inverse of #include' do - assert_equal false, 'foo'.exclude?('o') - assert_equal true, 'foo'.exclude?('p') + test "inverse of #include" do + assert_equal false, "foo".exclude?("o") + assert_equal true, "foo".exclude?("p") end end class StringIndentTest < ActiveSupport::TestCase - test 'does not indent strings that only contain newlines (edge cases)' do - ['', "\n", "\n" * 7].each do |str| + test "does not indent strings that only contain newlines (edge cases)" do + ["", "\n", "\n" * 7].each do |str| assert_nil str.indent!(8) assert_equal str, str.indent(8) assert_equal str, str.indent(1, "\t") @@ -872,8 +893,8 @@ class StringIndentTest < ActiveSupport::TestCase # Nothing is said about existing indentation that mixes spaces and tabs, so # there is nothing to test. - test 'uses the indent char if passed' do - assert_equal <<EXPECTED, <<ACTUAL.indent(4, '.') + test "uses the indent char if passed" do + assert_equal <<EXPECTED, <<ACTUAL.indent(4, ".") .... def some_method(x, y) .... some_code .... end @@ -883,7 +904,7 @@ EXPECTED end ACTUAL - assert_equal <<EXPECTED, <<ACTUAL.indent(2, ' ') + assert_equal <<EXPECTED, <<ACTUAL.indent(2, " ") def some_method(x, y) some_code end @@ -898,7 +919,7 @@ ACTUAL assert_equal " foo\n\n bar", "foo\n\nbar".indent(1) end - test 'indents blank lines if told so' do + test "indents blank lines if told so" do assert_equal " foo\n \n bar", "foo\n\nbar".indent(1, nil, true) end end |