PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/string_ext_test.rb

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