PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activesupport/test/core_ext/string_ext_test.rb

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