PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/activesupport/test/core_ext/string_ext_test.rb

https://github.com/applicake/instantcompanyhandbook
Ruby | 554 lines | 441 code | 98 blank | 15 comment | 4 complexity | da038e3203b95dfadac8840deffa0058 MD5 | raw file
Possible License(s): ISC
  1. # encoding: utf-8
  2. require 'date'
  3. require 'abstract_unit'
  4. require 'inflector_test_cases'
  5. require 'active_support/core_ext/string'
  6. require 'active_support/time'
  7. require 'active_support/core_ext/kernel/reporting'
  8. require 'active_support/core_ext/string/strip'
  9. require 'active_support/core_ext/string/output_safety'
  10. class StringInflectionsTest < Test::Unit::TestCase
  11. include InflectorTestCases
  12. def test_erb_escape
  13. string = [192, 60].pack('CC')
  14. expected = 192.chr + "&lt;"
  15. assert_equal expected, ERB::Util.html_escape(string)
  16. end
  17. def test_strip_heredoc_on_an_empty_string
  18. assert_equal '', ''.strip_heredoc
  19. end
  20. def test_strip_heredoc_on_a_string_with_no_lines
  21. assert_equal 'x', 'x'.strip_heredoc
  22. assert_equal 'x', ' x'.strip_heredoc
  23. end
  24. def test_strip_heredoc_on_a_heredoc_with_no_margin
  25. assert_equal "foo\nbar", "foo\nbar".strip_heredoc
  26. assert_equal "foo\n bar", "foo\n bar".strip_heredoc
  27. end
  28. def test_strip_heredoc_on_a_regular_indented_heredoc
  29. assert_equal "foo\n bar\nbaz\n", <<-EOS.strip_heredoc
  30. foo
  31. bar
  32. baz
  33. EOS
  34. end
  35. def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines
  36. assert_equal "foo\n bar\n\nbaz\n", <<-EOS.strip_heredoc
  37. foo
  38. bar
  39. baz
  40. EOS
  41. end
  42. def test_pluralize
  43. SingularToPlural.each do |singular, plural|
  44. assert_equal(plural, singular.pluralize)
  45. end
  46. assert_equal("plurals", "plurals".pluralize)
  47. end
  48. def test_singularize
  49. SingularToPlural.each do |singular, plural|
  50. assert_equal(singular, plural.singularize)
  51. end
  52. end
  53. def test_titleize
  54. MixtureToTitleCase.each do |before, titleized|
  55. assert_equal(titleized, before.titleize)
  56. end
  57. end
  58. def test_camelize
  59. CamelToUnderscore.each do |camel, underscore|
  60. assert_equal(camel, underscore.camelize)
  61. end
  62. end
  63. def test_camelize_lower
  64. assert_equal('capital', 'Capital'.camelize(:lower))
  65. end
  66. def test_underscore
  67. CamelToUnderscore.each do |camel, underscore|
  68. assert_equal(underscore, camel.underscore)
  69. end
  70. assert_equal "html_tidy", "HTMLTidy".underscore
  71. assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
  72. end
  73. def test_underscore_to_lower_camel
  74. UnderscoreToLowerCamel.each do |underscored, lower_camel|
  75. assert_equal(lower_camel, underscored.camelize(:lower))
  76. end
  77. end
  78. def test_demodulize
  79. assert_equal "Account", "MyApplication::Billing::Account".demodulize
  80. end
  81. def test_foreign_key
  82. ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
  83. assert_equal(foreign_key, klass.foreign_key)
  84. end
  85. ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
  86. assert_equal(foreign_key, klass.foreign_key(false))
  87. end
  88. end
  89. def test_tableize
  90. ClassNameToTableName.each do |class_name, table_name|
  91. assert_equal(table_name, class_name.tableize)
  92. end
  93. end
  94. def test_classify
  95. ClassNameToTableName.each do |class_name, table_name|
  96. assert_equal(class_name, table_name.classify)
  97. end
  98. end
  99. def test_string_parameterized_normal
  100. StringToParameterized.each do |normal, slugged|
  101. assert_equal(normal.parameterize, slugged)
  102. end
  103. end
  104. def test_string_parameterized_no_separator
  105. StringToParameterizeWithNoSeparator.each do |normal, slugged|
  106. assert_equal(normal.parameterize(''), slugged)
  107. end
  108. end
  109. def test_string_parameterized_underscore
  110. StringToParameterizeWithUnderscore.each do |normal, slugged|
  111. assert_equal(normal.parameterize('_'), slugged)
  112. end
  113. end
  114. def test_humanize
  115. UnderscoreToHuman.each do |underscore, human|
  116. assert_equal(human, underscore.humanize)
  117. end
  118. end
  119. def test_ord
  120. assert_equal 97, 'a'.ord
  121. assert_equal 97, 'abc'.ord
  122. end
  123. if RUBY_VERSION < '1.9'
  124. def test_getbyte
  125. assert_equal 97, 'a'.getbyte(0)
  126. assert_equal 99, 'abc'.getbyte(2)
  127. assert_nil 'abc'.getbyte(3)
  128. end
  129. end
  130. def test_string_to_time
  131. assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
  132. assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
  133. assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
  134. assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
  135. assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
  136. assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
  137. assert_nil "".to_time
  138. end
  139. def test_string_to_datetime
  140. assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
  141. assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
  142. assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
  143. 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
  144. assert_nil "".to_datetime
  145. end
  146. def test_string_to_date
  147. assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
  148. assert_nil "".to_date
  149. end
  150. def test_access
  151. s = "hello"
  152. assert_equal "h", s.at(0)
  153. assert_equal "llo", s.from(2)
  154. assert_equal "hel", s.to(2)
  155. assert_equal "h", s.first
  156. assert_equal "he", s.first(2)
  157. assert_equal "", s.first(0)
  158. assert_equal "o", s.last
  159. assert_equal "llo", s.last(3)
  160. assert_equal "hello", s.last(10)
  161. assert_equal "", s.last(0)
  162. assert_equal 'x', 'x'.first
  163. assert_equal 'x', 'x'.first(4)
  164. assert_equal 'x', 'x'.last
  165. assert_equal 'x', 'x'.last(4)
  166. end
  167. def test_access_returns_a_real_string
  168. hash = {}
  169. hash["h"] = true
  170. hash["hello123".at(0)] = true
  171. assert_equal %w(h), hash.keys
  172. hash = {}
  173. hash["llo"] = true
  174. hash["hello".from(2)] = true
  175. assert_equal %w(llo), hash.keys
  176. hash = {}
  177. hash["hel"] = true
  178. hash["hello".to(2)] = true
  179. assert_equal %w(hel), hash.keys
  180. hash = {}
  181. hash["hello"] = true
  182. hash["123hello".last(5)] = true
  183. assert_equal %w(hello), hash.keys
  184. hash = {}
  185. hash["hello"] = true
  186. hash["hello123".first(5)] = true
  187. assert_equal %w(hello), hash.keys
  188. end
  189. def test_starts_ends_with_alias
  190. s = "hello"
  191. assert s.starts_with?('h')
  192. assert s.starts_with?('hel')
  193. assert !s.starts_with?('el')
  194. assert s.ends_with?('o')
  195. assert s.ends_with?('lo')
  196. assert !s.ends_with?('el')
  197. end
  198. def test_string_squish
  199. original = %{ A string with tabs(\t\t), newlines(\n\n), and
  200. many spaces( ). }
  201. expected = "A string with tabs( ), newlines( ), and many spaces( )."
  202. # Make sure squish returns what we expect:
  203. assert_equal original.squish, expected
  204. # But doesn't modify the original string:
  205. assert_not_equal original, expected
  206. # Make sure squish! returns what we expect:
  207. assert_equal original.squish!, expected
  208. # And changes the original string:
  209. assert_equal original, expected
  210. end
  211. def test_truncate
  212. assert_equal "Hello World!", "Hello World!".truncate(12)
  213. assert_equal "Hello Wor...", "Hello World!!".truncate(12)
  214. end
  215. def test_truncate_with_omission_and_seperator
  216. assert_equal "Hello[...]", "Hello World!".truncate(10, :omission => "[...]")
  217. assert_equal "Hello[...]", "Hello Big World!".truncate(13, :omission => "[...]", :separator => ' ')
  218. assert_equal "Hello Big[...]", "Hello Big World!".truncate(14, :omission => "[...]", :separator => ' ')
  219. assert_equal "Hello Big[...]", "Hello Big World!".truncate(15, :omission => "[...]", :separator => ' ')
  220. end
  221. if RUBY_VERSION < '1.9.0'
  222. def test_truncate_multibyte
  223. with_kcode 'none' do
  224. assert_equal "\354\225\210\353\205\225\355...", "\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224".truncate(10)
  225. end
  226. with_kcode 'u' do
  227. assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
  228. "\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".truncate(10)
  229. end
  230. end
  231. else
  232. def test_truncate_multibyte
  233. assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'),
  234. "\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('UTF-8').truncate(10)
  235. end
  236. end
  237. end
  238. class StringBehaviourTest < Test::Unit::TestCase
  239. def test_acts_like_string
  240. assert 'Bambi'.acts_like_string?
  241. end
  242. end
  243. class CoreExtStringMultibyteTest < ActiveSupport::TestCase
  244. UNICODE_STRING = 'こにちわ'
  245. ASCII_STRING = 'ohayo'
  246. BYTE_STRING = "\270\236\010\210\245"
  247. def test_core_ext_adds_mb_chars
  248. assert_respond_to UNICODE_STRING, :mb_chars
  249. end
  250. def test_string_should_recognize_utf8_strings
  251. assert UNICODE_STRING.is_utf8?
  252. assert ASCII_STRING.is_utf8?
  253. assert !BYTE_STRING.is_utf8?
  254. end
  255. if RUBY_VERSION < '1.9'
  256. def test_mb_chars_returns_self_when_kcode_not_set
  257. with_kcode('none') do
  258. assert_kind_of String, UNICODE_STRING.mb_chars
  259. end
  260. end
  261. def test_mb_chars_returns_an_instance_of_the_chars_proxy_when_kcode_utf8
  262. with_kcode('UTF8') do
  263. assert_kind_of ActiveSupport::Multibyte.proxy_class, UNICODE_STRING.mb_chars
  264. end
  265. end
  266. else
  267. def test_mb_chars_returns_instance_of_proxy_class
  268. assert_kind_of ActiveSupport::Multibyte.proxy_class, UNICODE_STRING.mb_chars
  269. end
  270. end
  271. end
  272. =begin
  273. string.rb - Interpolation for String.
  274. Copyright (C) 2005-2009 Masao Mutoh
  275. You may redistribute it and/or modify it under the same
  276. license terms as Ruby.
  277. =end
  278. class TestGetTextString < Test::Unit::TestCase
  279. def test_sprintf
  280. assert_equal("foo is a number", "%{msg} is a number" % {:msg => "foo"})
  281. assert_equal("bar is a number", "%s is a number" % ["bar"])
  282. assert_equal("bar is a number", "%s is a number" % "bar")
  283. assert_equal("1, test", "%{num}, %{record}" % {:num => 1, :record => "test"})
  284. assert_equal("test, 1", "%{record}, %{num}" % {:num => 1, :record => "test"})
  285. assert_equal("1, test", "%d, %s" % [1, "test"])
  286. assert_equal("test, 1", "%2$s, %1$d" % [1, "test"])
  287. assert_raise(ArgumentError) { "%-%" % [1] }
  288. end
  289. def test_percent
  290. assert_equal("% 1", "%% %<num>d" % {:num => 1.0})
  291. assert_equal("%{num} %<num>d 1", "%%{num} %%<num>d %<num>d" % {:num => 1})
  292. end
  293. def test_sprintf_percent_in_replacement
  294. assert_equal("%<not_translated>s", "%{msg}" % { :msg => '%<not_translated>s', :not_translated => 'should not happen' })
  295. end
  296. def test_sprintf_lack_argument
  297. assert_raises(KeyError) { "%{num}, %{record}" % {:record => "test"} }
  298. assert_raises(KeyError) { "%{record}" % {:num => 1} }
  299. end
  300. def test_no_placeholder
  301. # Causes a "too many arguments for format string" warning
  302. # on 1.8.7 and 1.9 but we still want to make sure the behavior works
  303. silence_warnings do
  304. assert_equal("aaa", "aaa" % {:num => 1})
  305. assert_equal("bbb", "bbb" % [1])
  306. end
  307. end
  308. def test_sprintf_ruby19_style
  309. assert_equal("1", "%<num>d" % {:num => 1})
  310. assert_equal("0b1", "%<num>#b" % {:num => 1})
  311. assert_equal("foo", "%<msg>s" % {:msg => "foo"})
  312. assert_equal("1.000000", "%<num>f" % {:num => 1.0})
  313. assert_equal(" 1", "%<num>3.0f" % {:num => 1.0})
  314. assert_equal("100.00", "%<num>2.2f" % {:num => 100.0})
  315. assert_equal("0x64", "%<num>#x" % {:num => 100.0})
  316. assert_raise(ArgumentError) { "%<num>,d" % {:num => 100} }
  317. assert_raise(ArgumentError) { "%<num>/d" % {:num => 100} }
  318. end
  319. def test_sprintf_old_style
  320. assert_equal("foo 1.000000", "%s %f" % ["foo", 1.0])
  321. end
  322. def test_sprintf_mix_unformatted_and_formatted_named_placeholders
  323. assert_equal("foo 1.000000", "%{name} %<num>f" % {:name => "foo", :num => 1.0})
  324. end
  325. def test_string_interpolation_raises_an_argument_error_when_mixing_named_and_unnamed_placeholders
  326. assert_raises(ArgumentError) { "%{name} %f" % [1.0] }
  327. assert_raises(ArgumentError) { "%{name} %f" % [1.0, 2.0] }
  328. end
  329. end
  330. class OutputSafetyTest < ActiveSupport::TestCase
  331. def setup
  332. @string = "hello"
  333. @object = Class.new(Object) do
  334. def to_s
  335. "other"
  336. end
  337. end.new
  338. end
  339. test "A string is unsafe by default" do
  340. assert !@string.html_safe?
  341. end
  342. test "A string can be marked safe" do
  343. string = @string.html_safe
  344. assert string.html_safe?
  345. end
  346. test "Marking a string safe returns the string" do
  347. assert_equal @string, @string.html_safe
  348. end
  349. test "A fixnum is safe by default" do
  350. assert 5.html_safe?
  351. end
  352. test "An object is unsafe by default" do
  353. assert !@object.html_safe?
  354. end
  355. test "Adding an object to a safe string returns a safe string" do
  356. string = @string.html_safe
  357. string << @object
  358. assert_equal "helloother", string
  359. assert string.html_safe?
  360. end
  361. test "Adding a safe string to another safe string returns a safe string" do
  362. @other_string = "other".html_safe
  363. string = @string.html_safe
  364. @combination = @other_string + string
  365. assert_equal "otherhello", @combination
  366. assert @combination.html_safe?
  367. end
  368. test "Adding an unsafe string to a safe string escapes it and returns a safe string" do
  369. @other_string = "other".html_safe
  370. @combination = @other_string + "<foo>"
  371. @other_combination = @string + "<foo>"
  372. assert_equal "other&lt;foo&gt;", @combination
  373. assert_equal "hello<foo>", @other_combination
  374. assert @combination.html_safe?
  375. assert !@other_combination.html_safe?
  376. end
  377. test "Concatting safe onto unsafe yields unsafe" do
  378. @other_string = "other"
  379. string = @string.html_safe
  380. @other_string.concat(string)
  381. assert !@other_string.html_safe?
  382. end
  383. test "Concatting unsafe onto safe yields escaped safe" do
  384. @other_string = "other".html_safe
  385. string = @other_string.concat("<foo>")
  386. assert_equal "other&lt;foo&gt;", string
  387. assert string.html_safe?
  388. end
  389. test "Concatting safe onto safe yields safe" do
  390. @other_string = "other".html_safe
  391. string = @string.html_safe
  392. @other_string.concat(string)
  393. assert @other_string.html_safe?
  394. end
  395. test "Concatting safe onto unsafe with << yields unsafe" do
  396. @other_string = "other"
  397. string = @string.html_safe
  398. @other_string << string
  399. assert !@other_string.html_safe?
  400. end
  401. test "Concatting unsafe onto safe with << yields escaped safe" do
  402. @other_string = "other".html_safe
  403. string = @other_string << "<foo>"
  404. assert_equal "other&lt;foo&gt;", string
  405. assert string.html_safe?
  406. end
  407. test "Concatting safe onto safe with << yields safe" do
  408. @other_string = "other".html_safe
  409. string = @string.html_safe
  410. @other_string << string
  411. assert @other_string.html_safe?
  412. end
  413. test "Concatting a fixnum to safe always yields safe" do
  414. string = @string.html_safe
  415. string = string.concat(13)
  416. assert_equal "hello".concat(13), string
  417. assert string.html_safe?
  418. end
  419. test 'emits normal string yaml' do
  420. assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
  421. end
  422. test 'knows whether it is encoding aware' do
  423. if RUBY_VERSION >= "1.9"
  424. assert 'ruby'.encoding_aware?
  425. else
  426. assert !'ruby'.encoding_aware?
  427. end
  428. end
  429. test "call to_param returns a normal string" do
  430. string = @string.html_safe
  431. assert string.html_safe?
  432. assert !string.to_param.html_safe?
  433. end
  434. test "ERB::Util.html_escape should escape unsafe characters" do
  435. string = '<>&"\''
  436. expected = '&lt;&gt;&amp;&quot;&#x27;'
  437. assert_equal expected, ERB::Util.html_escape(string)
  438. end
  439. test "ERB::Util.html_escape should correctly handle invalid UTF-8 strings" do
  440. string = [192, 60].pack('CC')
  441. expected = 192.chr + "&lt;"
  442. assert_equal expected, ERB::Util.html_escape(string)
  443. end
  444. test "ERB::Util.html_escape should not escape safe strings" do
  445. string = "<b>hello</b>".html_safe
  446. assert_equal string, ERB::Util.html_escape(string)
  447. end
  448. end
  449. class StringExcludeTest < ActiveSupport::TestCase
  450. test 'inverse of #include' do
  451. assert_equal false, 'foo'.exclude?('o')
  452. assert_equal true, 'foo'.exclude?('p')
  453. end
  454. end