PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/template/text_helper_test.rb

http://monkeycharger.googlecode.com/
Ruby | 374 lines | 321 code | 51 blank | 2 comment | 1 complexity | 38a8462c1853ec862b3ecb0819f12c3e MD5 | raw file
  1. require "#{File.dirname(__FILE__)}/../abstract_unit"
  2. require "#{File.dirname(__FILE__)}/../testing_sandbox"
  3. class TextHelperTest < Test::Unit::TestCase
  4. include ActionView::Helpers::TextHelper
  5. include ActionView::Helpers::TagHelper
  6. include TestingSandbox
  7. def setup
  8. # This simulates the fact that instance variables are reset every time
  9. # a view is rendered. The cycle helper depends on this behavior.
  10. @_cycles = nil if (defined? @_cycles)
  11. end
  12. def test_simple_format
  13. assert_equal "<p></p>", simple_format(nil)
  14. assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
  15. assert_equal "<p>A paragraph</p>\n\n<p>and another one!</p>", simple_format("A paragraph\n\nand another one!")
  16. assert_equal "<p>A paragraph\n<br /> With a newline</p>", simple_format("A paragraph\n With a newline")
  17. text = "A\nB\nC\nD".freeze
  18. assert_equal "<p>A\n<br />B\n<br />C\n<br />D</p>", simple_format(text)
  19. text = "A\r\n \nB\n\n\r\n\t\nC\nD".freeze
  20. assert_equal "<p>A\n<br /> \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text)
  21. end
  22. def test_truncate
  23. assert_equal "Hello World!", truncate("Hello World!", 12)
  24. assert_equal "Hello Wor...", truncate("Hello World!!", 12)
  25. end
  26. def test_truncate_should_use_default_length_of_30
  27. str = "This is a string that will go longer then the default truncate length of 30"
  28. assert_equal str[0...27] + "...", truncate(str)
  29. end
  30. def test_truncate_multibyte
  31. with_kcode 'none' do
  32. assert_equal "\354\225\210\353\205\225\355...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10)
  33. end
  34. with_kcode 'u' do
  35. assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
  36. truncate("\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", 10)
  37. end
  38. end
  39. def test_strip_links
  40. assert_equal "on my mind\nall day long", strip_links("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
  41. end
  42. def test_highlighter
  43. assert_equal(
  44. "This is a <strong class=\"highlight\">beautiful</strong> morning",
  45. highlight("This is a beautiful morning", "beautiful")
  46. )
  47. assert_equal(
  48. "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day",
  49. highlight("This is a beautiful morning, but also a beautiful day", "beautiful")
  50. )
  51. assert_equal(
  52. "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
  53. highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>')
  54. )
  55. assert_equal(
  56. "This text is not changed because we supplied an empty phrase",
  57. highlight("This text is not changed because we supplied an empty phrase", nil)
  58. )
  59. assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
  60. end
  61. def test_highlighter_with_regexp
  62. assert_equal(
  63. "This is a <strong class=\"highlight\">beautiful!</strong> morning",
  64. highlight("This is a beautiful! morning", "beautiful!")
  65. )
  66. assert_equal(
  67. "This is a <strong class=\"highlight\">beautiful! morning</strong>",
  68. highlight("This is a beautiful! morning", "beautiful! morning")
  69. )
  70. assert_equal(
  71. "This is a <strong class=\"highlight\">beautiful? morning</strong>",
  72. highlight("This is a beautiful? morning", "beautiful? morning")
  73. )
  74. end
  75. def test_highlighting_multiple_phrases_in_one_pass
  76. assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
  77. end
  78. def test_excerpt
  79. assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
  80. assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
  81. assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
  82. assert_nil excerpt("This is a beautiful morning", "day")
  83. end
  84. def test_excerpt_with_regex
  85. assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5))
  86. assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5))
  87. end
  88. def test_excerpt_with_utf8
  89. with_kcode('u') do
  90. assert_equal("...?ciency could not be h...", excerpt("That's why e?ciency could not be helped", 'could', 8))
  91. end
  92. with_kcode('none') do
  93. assert_equal("...\203ciency could not be h...", excerpt("That's why e?ciency could not be helped", 'could', 8))
  94. end
  95. end
  96. def test_word_wrap
  97. assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
  98. end
  99. def test_pluralization
  100. assert_equal("1 count", pluralize(1, "count"))
  101. assert_equal("2 counts", pluralize(2, "count"))
  102. assert_equal("1 count", pluralize('1', "count"))
  103. assert_equal("2 counts", pluralize('2', "count"))
  104. assert_equal("1,066 counts", pluralize('1,066', "count"))
  105. assert_equal("1.25 counts", pluralize('1.25', "count"))
  106. assert_equal("2 counters", pluralize(2, "count", "counters"))
  107. assert_equal("0 counters", pluralize(nil, "count", "counters"))
  108. assert_equal("2 people", pluralize(2, "person"))
  109. assert_equal("10 buffaloes", pluralize(10, "buffalo"))
  110. end
  111. uses_mocha("should_just_add_s_for_pluralize_without_inflector_loaded") do
  112. def test_should_just_add_s_for_pluralize_without_inflector_loaded
  113. Object.expects(:const_defined?).with("Inflector").times(4).returns(false)
  114. assert_equal("1 count", pluralize(1, "count"))
  115. assert_equal("2 persons", pluralize(2, "person"))
  116. assert_equal("2 personss", pluralize("2", "persons"))
  117. assert_equal("2 counts", pluralize(2, "count"))
  118. assert_equal("10 buffalos", pluralize(10, "buffalo"))
  119. end
  120. end
  121. def test_auto_link_parsing
  122. urls = %w(http://www.rubyonrails.com
  123. http://www.rubyonrails.com:80
  124. http://www.rubyonrails.com/~minam
  125. https://www.rubyonrails.com/~minam
  126. http://www.rubyonrails.com/~minam/url%20with%20spaces
  127. http://www.rubyonrails.com/foo.cgi?something=here
  128. http://www.rubyonrails.com/foo.cgi?something=here&and=here
  129. http://www.rubyonrails.com/contact;new
  130. http://www.rubyonrails.com/contact;new%20with%20spaces
  131. http://www.rubyonrails.com/contact;new?with=query&string=params
  132. http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
  133. http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
  134. )
  135. urls.each do |url|
  136. assert_equal %(<a href="#{url}">#{url}</a>), auto_link(url)
  137. end
  138. end
  139. def test_auto_linking
  140. email_raw = 'david@loudthinking.com'
  141. email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
  142. link_raw = 'http://www.rubyonrails.com'
  143. link_result = %{<a href="#{link_raw}">#{link_raw}</a>}
  144. link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
  145. link2_raw = 'www.rubyonrails.com'
  146. link2_result = %{<a href="http://#{link2_raw}">#{link2_raw}</a>}
  147. link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
  148. link3_result = %{<a href="#{link3_raw}">#{link3_raw}</a>}
  149. link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
  150. link4_result = %{<a href="#{link4_raw}">#{link4_raw}</a>}
  151. link5_raw = 'http://foo.example.com:3000/controller/action'
  152. link5_result = %{<a href="#{link5_raw}">#{link5_raw}</a>}
  153. link6_raw = 'http://foo.example.com:3000/controller/action+pack'
  154. link6_result = %{<a href="#{link6_raw}">#{link6_raw}</a>}
  155. link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
  156. link7_result = %{<a href="#{link7_raw}">#{link7_raw}</a>}
  157. link8_raw = 'http://foo.example.com:3000/controller/action.html'
  158. link8_result = %{<a href="#{link8_raw}">#{link8_raw}</a>}
  159. link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
  160. link9_result = %{<a href="#{link9_raw}">#{link9_raw}</a>}
  161. assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
  162. assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
  163. assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
  164. assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
  165. assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
  166. assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
  167. assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
  168. assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
  169. assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>))
  170. assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
  171. assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
  172. assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
  173. assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
  174. assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
  175. assert_equal %(<p>Say hello to #{email_result}, then go to #{link2_result}.</p>), auto_link(%(<p>Say hello to #{email_raw}, then go to #{link2_raw}.</p>))
  176. assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
  177. assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
  178. assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
  179. assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
  180. assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
  181. assert_equal %(<p>Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
  182. assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
  183. assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
  184. assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
  185. assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
  186. assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
  187. assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
  188. assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
  189. assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
  190. assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
  191. assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
  192. assert_equal %(<p>Go to #{link8_result}. seriously, #{link8_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link8_raw}. seriously, #{link8_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
  193. assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
  194. assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
  195. assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
  196. assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
  197. assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
  198. assert_equal %(<p>Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
  199. assert_equal '', auto_link(nil)
  200. assert_equal '', auto_link('')
  201. end
  202. def test_auto_link_at_eol
  203. url1 = "http://api.rubyonrails.com/Foo.html"
  204. url2 = "http://www.ruby-doc.org/core/Bar.html"
  205. assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
  206. end
  207. def test_auto_link_with_block
  208. url = "http://api.rubyonrails.com/Foo.html"
  209. email = "fantabulous@shiznadel.ic"
  210. assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, 10) }
  211. end
  212. def test_sanitize_form
  213. raw = "<form action=\"/foo/bar\" method=\"post\"><input></form>"
  214. result = sanitize(raw)
  215. assert_equal %(&lt;form action="/foo/bar" method="post"><input>&lt;/form>), result
  216. end
  217. def test_sanitize_plaintext
  218. raw = "<plaintext><span>foo</span></plaintext>"
  219. result = sanitize(raw)
  220. assert_equal "&lt;plaintext><span>foo</span>&lt;/plaintext>", result
  221. end
  222. def test_sanitize_script
  223. raw = "<script language=\"Javascript\">blah blah blah</script>"
  224. result = sanitize(raw)
  225. assert_equal %{&lt;script language="Javascript">blah blah blah&lt;/script>}, result
  226. end
  227. def test_sanitize_js_handlers
  228. raw = %{onthis="do that" <a href="#" onclick="hello" name="foo" onbogus="remove me">hello</a>}
  229. result = sanitize(raw)
  230. assert_equal %{onthis="do that" <a name="foo" href="#">hello</a>}, result
  231. end
  232. def test_sanitize_javascript_href
  233. raw = %{href="javascript:bang" <a href="javascript:bang" name="hello">foo</a>, <span href="javascript:bang">bar</span>}
  234. result = sanitize(raw)
  235. assert_equal %{href="javascript:bang" <a name="hello">foo</a>, <span>bar</span>}, result
  236. end
  237. def test_sanitize_image_src
  238. raw = %{src="javascript:bang" <img src="javascript:bang" width="5">foo</img>, <span src="javascript:bang">bar</span>}
  239. result = sanitize(raw)
  240. assert_equal %{src="javascript:bang" <img width="5">foo</img>, <span>bar</span>}, result
  241. end
  242. def test_cycle_class
  243. value = Cycle.new("one", 2, "3")
  244. assert_equal("one", value.to_s)
  245. assert_equal("2", value.to_s)
  246. assert_equal("3", value.to_s)
  247. assert_equal("one", value.to_s)
  248. value.reset
  249. assert_equal("one", value.to_s)
  250. assert_equal("2", value.to_s)
  251. assert_equal("3", value.to_s)
  252. end
  253. def test_cycle_class_with_no_arguments
  254. assert_raise(ArgumentError) { value = Cycle.new() }
  255. end
  256. def test_cycle
  257. assert_equal("one", cycle("one", 2, "3"))
  258. assert_equal("2", cycle("one", 2, "3"))
  259. assert_equal("3", cycle("one", 2, "3"))
  260. assert_equal("one", cycle("one", 2, "3"))
  261. assert_equal("2", cycle("one", 2, "3"))
  262. assert_equal("3", cycle("one", 2, "3"))
  263. end
  264. def test_cycle_with_no_arguments
  265. assert_raise(ArgumentError) { value = cycle() }
  266. end
  267. def test_cycle_resets_with_new_values
  268. assert_equal("even", cycle("even", "odd"))
  269. assert_equal("odd", cycle("even", "odd"))
  270. assert_equal("even", cycle("even", "odd"))
  271. assert_equal("1", cycle(1, 2, 3))
  272. assert_equal("2", cycle(1, 2, 3))
  273. assert_equal("3", cycle(1, 2, 3))
  274. assert_equal("1", cycle(1, 2, 3))
  275. end
  276. def test_named_cycles
  277. assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
  278. assert_equal("red", cycle("red", "blue", :name => "colors"))
  279. assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
  280. assert_equal("blue", cycle("red", "blue", :name => "colors"))
  281. assert_equal("3", cycle(1, 2, 3, :name => "numbers"))
  282. assert_equal("red", cycle("red", "blue", :name => "colors"))
  283. end
  284. def test_default_named_cycle
  285. assert_equal("1", cycle(1, 2, 3))
  286. assert_equal("2", cycle(1, 2, 3, :name => "default"))
  287. assert_equal("3", cycle(1, 2, 3))
  288. end
  289. def test_reset_cycle
  290. assert_equal("1", cycle(1, 2, 3))
  291. assert_equal("2", cycle(1, 2, 3))
  292. reset_cycle
  293. assert_equal("1", cycle(1, 2, 3))
  294. end
  295. def test_reset_unknown_cycle
  296. reset_cycle("colors")
  297. end
  298. def test_recet_named_cycle
  299. assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
  300. assert_equal("red", cycle("red", "blue", :name => "colors"))
  301. reset_cycle("numbers")
  302. assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
  303. assert_equal("blue", cycle("red", "blue", :name => "colors"))
  304. assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
  305. assert_equal("red", cycle("red", "blue", :name => "colors"))
  306. end
  307. def test_cycle_no_instance_variable_clashes
  308. @cycles = %w{Specialized Fuji Giant}
  309. assert_equal("red", cycle("red", "blue"))
  310. assert_equal("blue", cycle("red", "blue"))
  311. assert_equal("red", cycle("red", "blue"))
  312. assert_equal(%w{Specialized Fuji Giant}, @cycles)
  313. end
  314. def test_strip_tags
  315. assert_equal("This is a test.", strip_tags("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
  316. assert_equal("This is a test.", strip_tags("This is a test."))
  317. assert_equal(
  318. %{This is a test.\n\n\nIt no longer contains any HTML.\n}, strip_tags(
  319. %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n}))
  320. assert_equal "This has a here.", strip_tags("This has a <!-- comment --> here.")
  321. [nil, '', ' '].each { |blank| assert_equal blank, strip_tags(blank) }
  322. end
  323. end