string_ext_test.rb 30.1 KB
Newer Older
1
require 'date'
2
require 'abstract_unit'
3
require 'inflector_test_cases'
4
require 'constantize_test_cases'
5

6
require 'active_support/inflector'
J
Jeremy Kemper 已提交
7
require 'active_support/core_ext/string'
8
require 'active_support/time'
X
Xavier Noria 已提交
9
require 'active_support/core_ext/string/strip'
10
require 'active_support/core_ext/string/output_safety'
11
require 'active_support/core_ext/string/indent'
12
require 'time_zone_test_helpers'
J
Jeremy Kemper 已提交
13

14
class StringInflectionsTest < ActiveSupport::TestCase
15
  include InflectorTestCases
16
  include ConstantizeTestCases
17
  include TimeZoneTestHelpers
18

X
Xavier Noria 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  def test_strip_heredoc_on_an_empty_string
    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
  end

  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

  def test_strip_heredoc_on_a_regular_indented_heredoc
    assert_equal "foo\n  bar\nbaz\n", <<-EOS.strip_heredoc
      foo
        bar
      baz
    EOS
  end

41 42 43 44 45 46 47 48 49
  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

50
  def test_pluralize
51
    SingularToPlural.each do |singular, plural|
52 53 54 55
      assert_equal(plural, singular.pluralize)
    end

    assert_equal("plurals", "plurals".pluralize)
56 57 58 59

    assert_equal("blargles", "blargle".pluralize(0))
    assert_equal("blargle", "blargle".pluralize(1))
    assert_equal("blargles", "blargle".pluralize(2))
60 61
  end

62 63 64 65 66
  test 'pluralize with count = 1 still returns new string' do
    name = "Kuldeep"
    assert_not_same name.pluralize(1), name
  end

67
  def test_singularize
68
    SingularToPlural.each do |singular, plural|
69 70 71 72
      assert_equal(singular, plural.singularize)
    end
  end

73
  def test_titleize
74
    MixtureToTitleCase.each do |before, titleized|
75 76 77 78
      assert_equal(titleized, before.titleize)
    end
  end

79
  def test_camelize
80
    CamelToUnderscore.each do |camel, underscore|
81 82 83 84
      assert_equal(camel, underscore.camelize)
    end
  end

85 86 87 88
  def test_camelize_lower
    assert_equal('capital', 'Capital'.camelize(:lower))
  end

A
Akira Matsuda 已提交
89 90 91 92 93 94
  def test_dasherize
    UnderscoresToDashes.each do |underscored, dasherized|
      assert_equal(dasherized, underscored.dasherize)
    end
  end

95
  def test_underscore
96
    CamelToUnderscore.each do |camel, underscore|
97 98
      assert_equal(underscore, camel.underscore)
    end
99

100 101 102 103
    assert_equal "html_tidy", "HTMLTidy".underscore
    assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
  end

104
  def test_underscore_to_lower_camel
105
    UnderscoreToLowerCamel.each do |underscored, lower_camel|
106 107 108 109
      assert_equal(lower_camel, underscored.camelize(:lower))
    end
  end

110
  def test_demodulize
111
    assert_equal "Account", "MyApplication::Billing::Account".demodulize
112 113
  end

114 115 116 117
  def test_deconstantize
    assert_equal "MyApplication::Billing", "MyApplication::Billing::Account".deconstantize
  end

118
  def test_foreign_key
119
    ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
120 121 122
      assert_equal(foreign_key, klass.foreign_key)
    end

123
    ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
124 125 126 127 128
      assert_equal(foreign_key, klass.foreign_key(false))
    end
  end

  def test_tableize
129
    ClassNameToTableName.each do |class_name, table_name|
130 131 132 133 134
      assert_equal(table_name, class_name.tableize)
    end
  end

  def test_classify
135
    ClassNameToTableName.each do |class_name, table_name|
136 137 138
      assert_equal(class_name, table_name.classify)
    end
  end
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  def test_string_parameterized_normal
    StringToParameterized.each do |normal, slugged|
      assert_equal(normal.parameterize, slugged)
    end
  end

  def test_string_parameterized_no_separator
    StringToParameterizeWithNoSeparator.each do |normal, slugged|
      assert_equal(normal.parameterize(''), slugged)
    end
  end

  def test_string_parameterized_underscore
    StringToParameterizeWithUnderscore.each do |normal, slugged|
      assert_equal(normal.parameterize('_'), slugged)
    end
  end

158
  def test_humanize
159
    UnderscoreToHuman.each do |underscore, human|
160 161 162 163
      assert_equal(human, underscore.humanize)
    end
  end

164 165 166 167 168 169
  def test_humanize_without_capitalize
    UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
      assert_equal(human, underscore.humanize(capitalize: false))
    end
  end

170 171 172 173
  def test_humanize_with_html_escape
    assert_equal 'Hello', ERB::Util.html_escape("hello").humanize
  end

J
Jeremy Kemper 已提交
174 175 176 177 178
  def test_ord
    assert_equal 97, 'a'.ord
    assert_equal 97, 'abc'.ord
  end

179
  def test_starts_ends_with_alias
180 181 182 183 184 185 186 187
    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')
188
  end
189

190
  def test_string_squish
191 192
    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}
193

194
    expected = "A string surrounded by various unicode spaces, " +
195
      "with tabs( ), newlines( ), unicode nextlines( ) and many spaces( )."
196 197

    # Make sure squish returns what we expect:
A
Akira Matsuda 已提交
198
    assert_equal expected, original.squish
199
    # But doesn't modify the original string:
A
Akira Matsuda 已提交
200
    assert_not_equal expected, original
201 202

    # Make sure squish! returns what we expect:
A
Akira Matsuda 已提交
203
    assert_equal expected, original.squish!
204
    # And changes the original string:
A
Akira Matsuda 已提交
205
    assert_equal expected, original
206
  end
207

208 209 210 211
  def test_string_inquiry
    assert "production".inquiry.production?
    assert !"production".inquiry.development?
  end
212

213 214 215 216 217
  def test_truncate
    assert_equal "Hello World!", "Hello World!".truncate(12)
    assert_equal "Hello Wor...", "Hello World!!".truncate(12)
  end

218
  def test_truncate_with_omission_and_separator
219 220 221 222 223 224
    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

225
  def test_truncate_with_omission_and_regexp_separator
226 227 228
    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/)
229 230 231 232 233 234 235
  end

  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

236
  def test_truncate_words_with_omission
237 238 239 240 241 242 243
    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>')
244
    assert_equal "Hello\n<br>Big...", "Hello\n<br>Big<br>Wide<br>World!".truncate_words(2, :separator => '<br>')
245 246
  end

247
  def test_truncate_words_with_separator_and_omission
248 249
    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>')
250 251
  end

252 253 254 255 256
  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.truncate_words(80), complex_string
    end
257 258
  rescue Timeout::Error
    assert false
259 260
  end

261
  def test_truncate_multibyte
262 263
    assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".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".force_encoding(Encoding::UTF_8).truncate(10)
264
  end
265

266 267 268
  def test_truncate_should_not_be_html_safe
    assert !"Hello World!".truncate(12).html_safe?
  end
269

270
  def test_remove
271 272 273 274 275 276
    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

277 278 279 280 281 282
  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

283 284 285 286 287 288
  def test_remove!
    original = "This is a very good day to die"
    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
289
  end
290

291
  def test_constantize
292
    run_constantize_tests_on(&:constantize)
293
  end
294

295
  def test_safe_constantize
296
    run_safe_constantize_tests_on(&:safe_constantize)
297
  end
298
end
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
class StringAccessTest < ActiveSupport::TestCase
  test "#at with Fixnum, returns a substring of one character at that position" do
    assert_equal "h", "hello".at(0)
  end

  test "#at with Range, returns a substring containing characters at offsets" do
    assert_equal "lo", "hello".at(-2..-1)
  end

  test "#at with Regex, returns the matching portion of the string" do
    assert_equal "lo", "hello".at(/lo/)
    assert_equal nil, "hello".at(/nonexisting/)
  end

  test "#from with positive Fixnum, 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
    assert_equal "lo", "hello".from(-2)
  end

  test "#to with positive Fixnum, 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
    assert_equal "hell", "hello".to(-2)
  end

  test "#from and #to can be combined" do
    assert_equal "hello", "hello".from(0).to(-1)
    assert_equal "ell", "hello".from(1).to(-2)
  end

  test "#first returns the first character" do
    assert_equal "h", "hello".first
    assert_equal 'x', 'x'.first
  end

Y
Yves Senn 已提交
340
  test "#first with Fixnum, returns a substring from the beginning to position" do
341 342 343 344 345 346
    assert_equal "he", "hello".first(2)
    assert_equal "", "hello".first(0)
    assert_equal "hello", "hello".first(10)
    assert_equal 'x', 'x'.first(4)
  end

347 348 349 350 351 352
  test "#first with Fixnum >= string length still returns a new string" do
    string = "hello"
    different_string = string.first(5)
    assert_not_same different_string, string
  end

353 354 355 356 357 358 359 360 361 362 363 364
  test "#last returns the last character" do
    assert_equal "o", "hello".last
    assert_equal 'x', 'x'.last
  end

  test "#last with Fixnum, 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)
  end

365 366 367 368 369 370
  test "#last with Fixnum >= string length still returns a new string" do
    string = "hello"
    different_string = string.last(5)
    assert_not_same different_string, string
  end

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  test "access returns a real string" do
    hash = {}
    hash["h"] = true
    hash["hello123".at(0)] = true
    assert_equal %w(h), hash.keys

    hash = {}
    hash["llo"] = true
    hash["hello".from(2)] = true
    assert_equal %w(llo), hash.keys

    hash = {}
    hash["hel"] = true
    hash["hello".to(2)] = true
    assert_equal %w(hel), hash.keys

    hash = {}
    hash["hello"] = true
    hash["123hello".last(5)] = true
    assert_equal %w(hello), hash.keys

    hash = {}
    hash["hello"] = true
    hash["hello123".first(5)] = true
    assert_equal %w(hello), hash.keys
  end
end

A
Andrew White 已提交
399
class StringConversionsTest < ActiveSupport::TestCase
400 401
  include TimeZoneTestHelpers

A
Andrew White 已提交
402
  def test_string_to_time
403
    with_env_tz "Europe/Moscow" do
404 405 406 407 408 409
      assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:utc)
      assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
      assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:utc)
      assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
      assert_equal Time.utc(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:utc)
      assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
410
      assert_equal Time.local(2011, 2, 27, 17, 50), "2011-02-27 13:50 -0100".to_time
411
      assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc)
412
      assert_equal Time.local(2005, 2, 27, 22, 50), "2005-02-27 14:50 -0500".to_time
413 414 415 416 417 418 419 420 421 422 423 424 425 426
      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)
    end
  end

  def test_partial_string_to_time
427
    with_env_tz "Europe/Moscow" do # use timezone which does not observe DST.
428 429 430
      now = Time.now
      assert_equal Time.local(now.year, now.month, now.day, 23, 50), "23:50".to_time
      assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "23:50".to_time(:utc)
431
      assert_equal Time.local(now.year, now.month, now.day, 17, 50), "13:50 -0100".to_time
432 433
      assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "22:50 -0100".to_time(:utc)
    end
A
Andrew White 已提交
434 435
  end

436 437
  def test_standard_time_string_to_time_when_current_time_is_standard_time
    with_env_tz "US/Eastern" do
438 439 440 441 442 443 444 445 446 447 448 449 450 451
      Time.stub(:now, Time.local(2012, 1, 1)) do
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
        assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
        assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
      end
452 453 454 455 456
    end
  end

  def test_standard_time_string_to_time_when_current_time_is_daylight_savings
    with_env_tz "US/Eastern" do
457 458 459 460 461 462 463 464 465 466 467 468 469 470
      Time.stub(:now, Time.local(2012, 7, 1)) do
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 -0800".to_time
        assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 -0800".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 -0500".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 -0500".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 5, 0), "2012-01-01 10:00 UTC".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0), "2012-01-01 10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 13, 0), "2012-01-01 10:00 PST".to_time
        assert_equal Time.utc(2012, 1, 1, 18, 0), "2012-01-01 10:00 PST".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "2012-01-01 10:00 EST".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "2012-01-01 10:00 EST".to_time(:utc)
      end
471 472 473 474 475
    end
  end

  def test_daylight_savings_string_to_time_when_current_time_is_standard_time
    with_env_tz "US/Eastern" do
476 477 478 479 480 481 482 483 484 485 486 487 488 489
      Time.stub(:now, Time.local(2012, 1, 1)) do
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
        assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
        assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
        assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
        assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
      end
490 491 492 493 494
    end
  end

  def test_daylight_savings_string_to_time_when_current_time_is_daylight_savings
    with_env_tz "US/Eastern" do
495 496 497 498 499 500 501 502 503 504 505 506 507 508
      Time.stub(:now, Time.local(2012, 7, 1)) do
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 -0700".to_time
        assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 -0700".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 -0400".to_time
        assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 -0400".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 6, 0), "2012-07-01 10:00 UTC".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "2012-07-01 10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 13, 0), "2012-07-01 10:00 PDT".to_time
        assert_equal Time.utc(2012, 7, 1, 17, 0), "2012-07-01 10:00 PDT".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 10, 0), "2012-07-01 10:00 EDT".to_time
        assert_equal Time.utc(2012, 7, 1, 14, 0), "2012-07-01 10:00 EDT".to_time(:utc)
      end
509 510 511 512 513
    end
  end

  def test_partial_string_to_time_when_current_time_is_standard_time
    with_env_tz "US/Eastern" do
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
      Time.stub(:now, Time.local(2012, 1, 1)) do
        assert_equal Time.local(2012, 1, 1, 10, 0), "10:00".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0),  "10:00".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 6, 0), "10:00 -0100".to_time
        assert_equal Time.utc(2012, 1, 1, 11, 0), "10:00 -0100".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 -0500".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 -0500".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 5, 0), "10:00 UTC".to_time
        assert_equal Time.utc(2012, 1, 1, 10, 0), "10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 13, 0), "10:00 PST".to_time
        assert_equal Time.utc(2012, 1, 1, 18, 0), "10:00 PST".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 12, 0), "10:00 PDT".to_time
        assert_equal Time.utc(2012, 1, 1, 17, 0), "10:00 PDT".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 10, 0), "10:00 EST".to_time
        assert_equal Time.utc(2012, 1, 1, 15, 0), "10:00 EST".to_time(:utc)
        assert_equal Time.local(2012, 1, 1, 9, 0), "10:00 EDT".to_time
        assert_equal Time.utc(2012, 1, 1, 14, 0), "10:00 EDT".to_time(:utc)
      end
532 533 534 535 536
    end
  end

  def test_partial_string_to_time_when_current_time_is_daylight_savings
    with_env_tz "US/Eastern" do
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
      Time.stub(:now, Time.local(2012, 7, 1)) do
        assert_equal Time.local(2012, 7, 1, 10, 0), "10:00".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 7, 0), "10:00 -0100".to_time
        assert_equal Time.utc(2012, 7, 1, 11, 0), "10:00 -0100".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 -0500".to_time
        assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 -0500".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 6, 0), "10:00 UTC".to_time
        assert_equal Time.utc(2012, 7, 1, 10, 0), "10:00 UTC".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 14, 0), "10:00 PST".to_time
        assert_equal Time.utc(2012, 7, 1, 18, 0), "10:00 PST".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 13, 0), "10:00 PDT".to_time
        assert_equal Time.utc(2012, 7, 1, 17, 0), "10:00 PDT".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 11, 0), "10:00 EST".to_time
        assert_equal Time.utc(2012, 7, 1, 15, 0), "10:00 EST".to_time(:utc)
        assert_equal Time.local(2012, 7, 1, 10, 0), "10:00 EDT".to_time
        assert_equal Time.utc(2012, 7, 1, 14, 0), "10:00 EDT".to_time(:utc)
      end
555 556 557
    end
  end

A
Andrew White 已提交
558 559 560 561 562 563 564 565
  def test_string_to_datetime
    assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
    assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
    assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
    assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime
    assert_nil "".to_datetime
  end

566 567 568 569 570 571
  def test_partial_string_to_datetime
    now = DateTime.now
    assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50), "23:50".to_datetime
    assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50, 0, "-04:00"), "23:50 -0400".to_datetime
  end

A
Andrew White 已提交
572 573 574
  def test_string_to_date
    assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
    assert_nil "".to_date
575
    assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date
A
Andrew White 已提交
576 577 578
  end
end

579
class StringBehaviourTest < ActiveSupport::TestCase
580 581 582
  def test_acts_like_string
    assert 'Bambi'.acts_like_string?
  end
583 584
end

585
class CoreExtStringMultibyteTest < ActiveSupport::TestCase
586 587 588 589
  UTF8_STRING = 'こにちわ'
  ASCII_STRING = 'ohayo'.encode('US-ASCII')
  EUC_JP_STRING = 'さよなら'.encode('EUC-JP')
  INVALID_UTF8_STRING = "\270\236\010\210\245"
590 591

  def test_core_ext_adds_mb_chars
592
    assert_respond_to UTF8_STRING, :mb_chars
593 594 595
  end

  def test_string_should_recognize_utf8_strings
596
    assert UTF8_STRING.is_utf8?
597
    assert ASCII_STRING.is_utf8?
598 599
    assert !EUC_JP_STRING.is_utf8?
    assert !INVALID_UTF8_STRING.is_utf8?
600 601
  end

602
  def test_mb_chars_returns_instance_of_proxy_class
603
    assert_kind_of ActiveSupport::Multibyte.proxy_class, UTF8_STRING.mb_chars
604
  end
605
end
606

607 608 609
class OutputSafetyTest < ActiveSupport::TestCase
  def setup
    @string = "hello"
610 611 612 613 614
    @object = Class.new(Object) do
      def to_s
        "other"
      end
    end.new
615 616 617 618 619 620 621
  end

  test "A string is unsafe by default" do
    assert !@string.html_safe?
  end

  test "A string can be marked safe" do
622 623
    string = @string.html_safe
    assert string.html_safe?
624 625 626
  end

  test "Marking a string safe returns the string" do
627
    assert_equal @string, @string.html_safe
628 629
  end

630 631 632
  test "A fixnum is safe by default" do
    assert 5.html_safe?
  end
633

634 635 636
  test "a float is safe by default" do
    assert 5.7.html_safe?
  end
637 638

  test "An object is unsafe by default" do
639 640
    assert !@object.html_safe?
  end
641

642 643 644
  test "Adding an object to a safe string returns a safe string" do
    string = @string.html_safe
    string << @object
645

646 647
    assert_equal "helloother", string
    assert string.html_safe?
648 649
  end

650
  test "Adding a safe string to another safe string returns a safe string" do
651 652 653
    @other_string = "other".html_safe
    string = @string.html_safe
    @combination = @other_string + string
654 655 656 657 658

    assert_equal "otherhello", @combination
    assert @combination.html_safe?
  end

659 660 661 662
  test "Adding an unsafe string to a safe string escapes it and returns a safe string" do
    @other_string = "other".html_safe
    @combination = @other_string + "<foo>"
    @other_combination = @string + "<foo>"
663

664 665
    assert_equal "other&lt;foo&gt;", @combination
    assert_equal "hello<foo>", @other_combination
666

667
    assert @combination.html_safe?
668 669 670
    assert !@other_combination.html_safe?
  end

671 672 673 674 675 676 677 678 679 680 681 682 683
  test "Prepending safe onto unsafe yields unsafe" do
    @string.prepend "other".html_safe
    assert !@string.html_safe?
    assert_equal @string, "otherhello"
  end

  test "Prepending unsafe onto safe yields escaped safe" do
    other = "other".html_safe
    other.prepend "<foo>"
    assert other.html_safe?
    assert_equal other, "&lt;foo&gt;other"
  end

684 685 686
  test "Concatting safe onto unsafe yields unsafe" do
    @other_string = "other"

687 688
    string = @string.html_safe
    @other_string.concat(string)
689 690 691
    assert !@other_string.html_safe?
  end

692 693 694 695 696
  test "Concatting unsafe onto safe yields escaped safe" do
    @other_string = "other".html_safe
    string = @other_string.concat("<foo>")
    assert_equal "other&lt;foo&gt;", string
    assert string.html_safe?
697 698 699
  end

  test "Concatting safe onto safe yields safe" do
700
    @other_string = "other".html_safe
701
    string = @string.html_safe
702

703
    @other_string.concat(string)
704 705 706 707 708
    assert @other_string.html_safe?
  end

  test "Concatting safe onto unsafe with << yields unsafe" do
    @other_string = "other"
709
    string = @string.html_safe
710

711
    @other_string << string
712 713 714
    assert !@other_string.html_safe?
  end

715 716 717 718 719
  test "Concatting unsafe onto safe with << yields escaped safe" do
    @other_string = "other".html_safe
    string = @other_string << "<foo>"
    assert_equal "other&lt;foo&gt;", string
    assert string.html_safe?
720 721 722
  end

  test "Concatting safe onto safe with << yields safe" do
723
    @other_string = "other".html_safe
724
    string = @string.html_safe
725

726
    @other_string << string
727 728
    assert @other_string.html_safe?
  end
729

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
  test "Concatting safe onto unsafe with % yields unsafe" do
    @other_string = "other%s"
    string = @string.html_safe

    @other_string = @other_string % string
    assert !@other_string.html_safe?
  end

  test "Concatting unsafe onto safe with % yields escaped safe" do
    @other_string = "other%s".html_safe
    string = @other_string % "<foo>"

    assert_equal "other&lt;foo&gt;", string
    assert string.html_safe?
  end

  test "Concatting safe onto safe with % yields safe" do
    @other_string = "other%s".html_safe
    string = @string.html_safe

    @other_string = @other_string % string
    assert @other_string.html_safe?
  end

754 755
  test "Concatting with % doesn't modify a string" do
    @other_string = ["<p>", "<b>", "<h1>"]
756
    _ = "%s %s %s".html_safe % @other_string
757 758 759 760

    assert_equal ["<p>", "<b>", "<h1>"], @other_string
  end

761
  test "Concatting a fixnum to safe always yields safe" do
762 763 764 765
    string = @string.html_safe
    string = string.concat(13)
    assert_equal "hello".concat(13), string
    assert string.html_safe?
766
  end
767 768

  test 'emits normal string yaml' do
769
    assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
770
  end
771

772 773 774 775 776
  test "call to_param returns a normal string" do
    string = @string.html_safe
    assert string.html_safe?
    assert !string.to_param.html_safe?
  end
777 778

  test "ERB::Util.html_escape should escape unsafe characters" do
779
    string = '<>&"\''
780
    expected = '&lt;&gt;&amp;&quot;&#39;'
781 782 783 784
    assert_equal expected, ERB::Util.html_escape(string)
  end

  test "ERB::Util.html_escape should correctly handle invalid UTF-8 strings" do
785 786
    string = "\251 <"
    expected = "© &lt;"
787 788 789 790 791 792 793
    assert_equal expected, ERB::Util.html_escape(string)
  end

  test "ERB::Util.html_escape should not escape safe strings" do
    string = "<b>hello</b>".html_safe
    assert_equal string, ERB::Util.html_escape(string)
  end
794 795 796 797 798 799 800 801

  test "ERB::Util.html_escape_once only escapes once" do
    string = '1 < 2 &amp; 3'
    escaped_string = "1 &lt; 2 &amp; 3"

    assert_equal escaped_string, ERB::Util.html_escape_once(string)
    assert_equal escaped_string, ERB::Util.html_escape_once(escaped_string)
  end
802 803 804 805 806 807

  test "ERB::Util.html_escape_once should correctly handle invalid UTF-8 strings" do
    string = "\251 <"
    expected = "© &lt;"
    assert_equal expected, ERB::Util.html_escape_once(string)
  end
808
end
809 810 811 812 813 814 815

class StringExcludeTest < ActiveSupport::TestCase
  test 'inverse of #include' do
    assert_equal false, 'foo'.exclude?('o')
    assert_equal true, 'foo'.exclude?('p')
  end
end
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

class StringIndentTest < ActiveSupport::TestCase
  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")
    end
  end

  test "by default, indents with spaces if the existing indentation uses them" do
    assert_equal "    foo\n      bar", "foo\n  bar".indent(4)
  end

  test "by default, indents with tabs if the existing indentation uses them" do
    assert_equal "\tfoo\n\t\t\bar", "foo\n\t\bar".indent(1)
  end

  test "by default, indents with spaces as a fallback if there is no indentation" do
    assert_equal "   foo\n   bar\n   baz", "foo\nbar\nbaz".indent(3)
  end

  # 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, '.')
....  def some_method(x, y)
....    some_code
....  end
EXPECTED
  def some_method(x, y)
    some_code
  end
ACTUAL

    assert_equal <<EXPECTED, <<ACTUAL.indent(2, '&nbsp;')
&nbsp;&nbsp;&nbsp;&nbsp;def some_method(x, y)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;some_code
&nbsp;&nbsp;&nbsp;&nbsp;end
EXPECTED
&nbsp;&nbsp;def some_method(x, y)
&nbsp;&nbsp;&nbsp;&nbsp;some_code
&nbsp;&nbsp;end
ACTUAL
  end

  test "does not indent blank lines by default" do
    assert_equal " foo\n\n bar", "foo\n\nbar".indent(1)
  end

  test 'indents blank lines if told so' do
    assert_equal " foo\n \n bar", "foo\n\nbar".indent(1, nil, true)
  end
end