PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/string_ext_test.rb

https://github.com/thedarkone/rails
Ruby | 472 lines | 380 code | 87 blank | 5 comment | 4 complexity | 24865b791041570229bf59ebeeb54371 MD5 | raw file
  1. # encoding: utf-8
  2. require 'date'
  3. require 'abstract_unit'
  4. require 'inflector_test_cases'
  5. require 'active_support/inflector'
  6. require 'active_support/core_ext/string'
  7. require 'active_support/time'
  8. require 'active_support/core_ext/kernel/reporting'
  9. require 'active_support/core_ext/string/strip'
  10. class StringInflectionsTest < Test::Unit::TestCase
  11. include InflectorTestCases
  12. def test_strip_heredoc_on_an_empty_string
  13. assert_equal '', ''.strip_heredoc
  14. end
  15. def test_strip_heredoc_on_a_string_with_no_lines
  16. assert_equal 'x', 'x'.strip_heredoc
  17. assert_equal 'x', ' x'.strip_heredoc
  18. end
  19. def test_strip_heredoc_on_a_heredoc_with_no_margin
  20. assert_equal "foo\nbar", "foo\nbar".strip_heredoc
  21. assert_equal "foo\n bar", "foo\n bar".strip_heredoc
  22. end
  23. def test_strip_heredoc_on_a_regular_indented_heredoc
  24. assert_equal "foo\n bar\nbaz\n", <<-EOS.strip_heredoc
  25. foo
  26. bar
  27. baz
  28. EOS
  29. end
  30. def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines
  31. assert_equal "foo\n bar\n\nbaz\n", <<-EOS.strip_heredoc
  32. foo
  33. bar
  34. baz
  35. EOS
  36. end
  37. def test_pluralize
  38. SingularToPlural.each do |singular, plural|
  39. assert_equal(plural, singular.pluralize)
  40. end
  41. assert_equal("plurals", "plurals".pluralize)
  42. end
  43. def test_singularize
  44. SingularToPlural.each do |singular, plural|
  45. assert_equal(singular, plural.singularize)
  46. end
  47. end
  48. def test_titleize
  49. MixtureToTitleCase.each do |before, titleized|
  50. assert_equal(titleized, before.titleize)
  51. end
  52. end
  53. def test_camelize
  54. CamelToUnderscore.each do |camel, underscore|
  55. assert_equal(camel, underscore.camelize)
  56. end
  57. end
  58. def test_camelize_lower
  59. assert_equal('capital', 'Capital'.camelize(:lower))
  60. end
  61. def test_underscore
  62. CamelToUnderscore.each do |camel, underscore|
  63. assert_equal(underscore, camel.underscore)
  64. end
  65. assert_equal "html_tidy", "HTMLTidy".underscore
  66. assert_equal "html_tidy_generator", "HTMLTidyGenerator".underscore
  67. end
  68. def test_underscore_to_lower_camel
  69. UnderscoreToLowerCamel.each do |underscored, lower_camel|
  70. assert_equal(lower_camel, underscored.camelize(:lower))
  71. end
  72. end
  73. def test_demodulize
  74. assert_equal "Account", "MyApplication::Billing::Account".demodulize
  75. end
  76. def test_foreign_key
  77. ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
  78. assert_equal(foreign_key, klass.foreign_key)
  79. end
  80. ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
  81. assert_equal(foreign_key, klass.foreign_key(false))
  82. end
  83. end
  84. def test_tableize
  85. ClassNameToTableName.each do |class_name, table_name|
  86. assert_equal(table_name, class_name.tableize)
  87. end
  88. end
  89. def test_classify
  90. ClassNameToTableName.each do |class_name, table_name|
  91. assert_equal(class_name, table_name.classify)
  92. end
  93. end
  94. def test_string_parameterized_normal
  95. StringToParameterized.each do |normal, slugged|
  96. assert_equal(normal.parameterize, slugged)
  97. end
  98. end
  99. def test_string_parameterized_no_separator
  100. StringToParameterizeWithNoSeparator.each do |normal, slugged|
  101. assert_equal(normal.parameterize(''), slugged)
  102. end
  103. end
  104. def test_string_parameterized_underscore
  105. StringToParameterizeWithUnderscore.each do |normal, slugged|
  106. assert_equal(normal.parameterize('_'), slugged)
  107. end
  108. end
  109. def test_humanize
  110. UnderscoreToHuman.each do |underscore, human|
  111. assert_equal(human, underscore.humanize)
  112. end
  113. end
  114. def test_ord
  115. assert_equal 97, 'a'.ord
  116. assert_equal 97, 'abc'.ord
  117. end
  118. if RUBY_VERSION < '1.9'
  119. def test_getbyte
  120. assert_equal 97, 'a'.getbyte(0)
  121. assert_equal 99, 'abc'.getbyte(2)
  122. assert_nil 'abc'.getbyte(3)
  123. end
  124. end
  125. def test_string_to_time
  126. assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
  127. assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
  128. assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
  129. assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
  130. assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
  131. assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
  132. assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time
  133. assert_nil "".to_time
  134. end
  135. def test_string_to_datetime
  136. assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
  137. assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
  138. assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
  139. 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
  140. assert_nil "".to_datetime
  141. end
  142. def test_string_to_date
  143. assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
  144. assert_nil "".to_date
  145. end
  146. def test_access
  147. s = "hello"
  148. assert_equal "h", s.at(0)
  149. assert_equal "llo", s.from(2)
  150. assert_equal "hel", s.to(2)
  151. assert_equal "h", s.first
  152. assert_equal "he", s.first(2)
  153. assert_equal "", s.first(0)
  154. assert_equal "o", s.last
  155. assert_equal "llo", s.last(3)
  156. assert_equal "hello", s.last(10)
  157. assert_equal "", s.last(0)
  158. assert_equal 'x', 'x'.first
  159. assert_equal 'x', 'x'.first(4)
  160. assert_equal 'x', 'x'.last
  161. assert_equal 'x', 'x'.last(4)
  162. end
  163. def test_access_returns_a_real_string
  164. hash = {}
  165. hash["h"] = true
  166. hash["hello123".at(0)] = true
  167. assert_equal %w(h), hash.keys
  168. hash = {}
  169. hash["llo"] = true
  170. hash["hello".from(2)] = true
  171. assert_equal %w(llo), hash.keys
  172. hash = {}
  173. hash["hel"] = true
  174. hash["hello".to(2)] = true
  175. assert_equal %w(hel), hash.keys
  176. hash = {}
  177. hash["hello"] = true
  178. hash["123hello".last(5)] = true
  179. assert_equal %w(hello), hash.keys
  180. hash = {}
  181. hash["hello"] = true
  182. hash["hello123".first(5)] = true
  183. assert_equal %w(hello), hash.keys
  184. end
  185. def test_starts_ends_with_alias
  186. s = "hello"
  187. assert s.starts_with?('h')
  188. assert s.starts_with?('hel')
  189. assert !s.starts_with?('el')
  190. assert s.ends_with?('o')
  191. assert s.ends_with?('lo')
  192. assert !s.ends_with?('el')
  193. end
  194. def test_string_squish
  195. original = %{ A string with tabs(\t\t), newlines(\n\n), and
  196. many spaces( ). }
  197. expected = "A string with tabs( ), newlines( ), and many spaces( )."
  198. # Make sure squish returns what we expect:
  199. assert_equal original.squish, expected
  200. # But doesn't modify the original string:
  201. assert_not_equal original, expected
  202. # Make sure squish! returns what we expect:
  203. assert_equal original.squish!, expected
  204. # And changes the original string:
  205. assert_equal original, expected
  206. end
  207. def test_string_inquiry
  208. assert "production".inquiry.production?
  209. assert !"production".inquiry.development?
  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. class OutputSafetyTest < ActiveSupport::TestCase
  273. def setup
  274. @string = "hello"
  275. @object = Class.new(Object) do
  276. def to_s
  277. "other"
  278. end
  279. end.new
  280. end
  281. test "A string is unsafe by default" do
  282. assert !@string.html_safe?
  283. end
  284. test "A string can be marked safe" do
  285. string = @string.html_safe
  286. assert string.html_safe?
  287. end
  288. test "Marking a string safe returns the string" do
  289. assert_equal @string, @string.html_safe
  290. end
  291. test "A fixnum is safe by default" do
  292. assert 5.html_safe?
  293. end
  294. test "a float is safe by default" do
  295. assert 5.7.html_safe?
  296. end
  297. test "An object is unsafe by default" do
  298. assert !@object.html_safe?
  299. end
  300. test "Adding an object to a safe string returns a safe string" do
  301. string = @string.html_safe
  302. string << @object
  303. assert_equal "helloother", string
  304. assert string.html_safe?
  305. end
  306. test "Adding a safe string to another safe string returns a safe string" do
  307. @other_string = "other".html_safe
  308. string = @string.html_safe
  309. @combination = @other_string + string
  310. assert_equal "otherhello", @combination
  311. assert @combination.html_safe?
  312. end
  313. test "Adding an unsafe string to a safe string escapes it and returns a safe string" do
  314. @other_string = "other".html_safe
  315. @combination = @other_string + "<foo>"
  316. @other_combination = @string + "<foo>"
  317. assert_equal "other&lt;foo&gt;", @combination
  318. assert_equal "hello<foo>", @other_combination
  319. assert @combination.html_safe?
  320. assert !@other_combination.html_safe?
  321. end
  322. test "Concatting safe onto unsafe yields unsafe" do
  323. @other_string = "other"
  324. string = @string.html_safe
  325. @other_string.concat(string)
  326. assert !@other_string.html_safe?
  327. end
  328. test "Concatting unsafe onto safe yields escaped safe" do
  329. @other_string = "other".html_safe
  330. string = @other_string.concat("<foo>")
  331. assert_equal "other&lt;foo&gt;", string
  332. assert string.html_safe?
  333. end
  334. test "Concatting safe onto safe yields safe" do
  335. @other_string = "other".html_safe
  336. string = @string.html_safe
  337. @other_string.concat(string)
  338. assert @other_string.html_safe?
  339. end
  340. test "Concatting safe onto unsafe with << yields unsafe" do
  341. @other_string = "other"
  342. string = @string.html_safe
  343. @other_string << string
  344. assert !@other_string.html_safe?
  345. end
  346. test "Concatting unsafe onto safe with << yields escaped safe" do
  347. @other_string = "other".html_safe
  348. string = @other_string << "<foo>"
  349. assert_equal "other&lt;foo&gt;", string
  350. assert string.html_safe?
  351. end
  352. test "Concatting safe onto safe with << yields safe" do
  353. @other_string = "other".html_safe
  354. string = @string.html_safe
  355. @other_string << string
  356. assert @other_string.html_safe?
  357. end
  358. test "Concatting a fixnum to safe always yields safe" do
  359. string = @string.html_safe
  360. string = string.concat(13)
  361. assert_equal "hello".concat(13), string
  362. assert string.html_safe?
  363. end
  364. test 'emits normal string yaml' do
  365. assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
  366. end
  367. test 'knows whether it is encoding aware' do
  368. if RUBY_VERSION >= "1.9"
  369. assert 'ruby'.encoding_aware?
  370. else
  371. assert !'ruby'.encoding_aware?
  372. end
  373. end
  374. test "call to_param returns a normal string" do
  375. string = @string.html_safe
  376. assert string.html_safe?
  377. assert !string.to_param.html_safe?
  378. end
  379. end
  380. class StringExcludeTest < ActiveSupport::TestCase
  381. test 'inverse of #include' do
  382. assert_equal false, 'foo'.exclude?('o')
  383. assert_equal true, 'foo'.exclude?('p')
  384. end
  385. end