1 require "#{File.dirname(__FILE__)}/../abstract_unit"
2 require "#{File.dirname(__FILE__)}/../testing_sandbox"
4 class TextHelperTest < Test::Unit::TestCase
5 include ActionView::Helpers::TextHelper
6 include ActionView::Helpers::TagHelper
10 # This simulates the fact that instance variables are reset every time
11 # a view is rendered. The cycle helper depends on this behavior.
12 @_cycles = nil if (defined? @_cycles)
15 def test_simple_format
16 assert_equal "<p></p>", simple_format(nil)
18 assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
19 assert_equal "<p>A paragraph</p>\n\n<p>and another one!</p>", simple_format("A paragraph\n\nand another one!")
20 assert_equal "<p>A paragraph\n<br /> With a newline</p>", simple_format("A paragraph\n With a newline")
22 text = "A\nB\nC\nD".freeze
23 assert_equal "<p>A\n<br />B\n<br />C\n<br />D</p>", simple_format(text)
25 text = "A\r\n \nB\n\n\r\n\t\nC\nD".freeze
26 assert_equal "<p>A\n<br /> \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text)
30 assert_equal "Hello World!", truncate("Hello World!", 12)
31 assert_equal "Hello Wor...", truncate("Hello World!!", 12)
34 def test_truncate_should_use_default_length_of_30
35 str = "This is a string that will go longer then the default truncate length of 30"
36 assert_equal str[0...27] + "...", truncate(str)
39 def test_truncate_multibyte
41 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)
44 assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
45 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)
51 "This is a <strong class=\"highlight\">beautiful</strong> morning",
52 highlight("This is a beautiful morning", "beautiful")
56 "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day",
57 highlight("This is a beautiful morning, but also a beautiful day", "beautiful")
61 "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
62 highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>')
66 "This text is not changed because we supplied an empty phrase",
67 highlight("This text is not changed because we supplied an empty phrase", nil)
70 assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
73 def test_highlighter_with_regexp
75 "This is a <strong class=\"highlight\">beautiful!</strong> morning",
76 highlight("This is a beautiful! morning", "beautiful!")
80 "This is a <strong class=\"highlight\">beautiful! morning</strong>",
81 highlight("This is a beautiful! morning", "beautiful! morning")
85 "This is a <strong class=\"highlight\">beautiful? morning</strong>",
86 highlight("This is a beautiful? morning", "beautiful? morning")
90 def test_highlighting_multiple_phrases_in_one_pass
91 assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
95 assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
96 assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
97 assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
98 assert_nil excerpt("This is a beautiful morning", "day")
101 def test_excerpt_with_regex
102 assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5))
103 assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5))
106 def test_excerpt_with_utf8
108 assert_equal("...fficiency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
110 with_kcode('none') do
111 assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
116 assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
119 def test_word_wrap_with_extra_newlines
120 assert_equal("my very very\nvery long\nstring\n\nwith another\nline", word_wrap("my very very very long string\n\nwith another line", 15))
123 def test_pluralization
124 assert_equal("1 count", pluralize(1, "count"))
125 assert_equal("2 counts", pluralize(2, "count"))
126 assert_equal("1 count", pluralize('1', "count"))
127 assert_equal("2 counts", pluralize('2', "count"))
128 assert_equal("1,066 counts", pluralize('1,066', "count"))
129 assert_equal("1.25 counts", pluralize('1.25', "count"))
130 assert_equal("2 counters", pluralize(2, "count", "counters"))
131 assert_equal("0 counters", pluralize(nil, "count", "counters"))
132 assert_equal("2 people", pluralize(2, "person"))
133 assert_equal("10 buffaloes", pluralize(10, "buffalo"))
136 uses_mocha("should_just_add_s_for_pluralize_without_inflector_loaded") do
137 def test_should_just_add_s_for_pluralize_without_inflector_loaded
138 Object.expects(:const_defined?).with("Inflector").times(4).returns(false)
139 assert_equal("1 count", pluralize(1, "count"))
140 assert_equal("2 persons", pluralize(2, "person"))
141 assert_equal("2 personss", pluralize("2", "persons"))
142 assert_equal("2 counts", pluralize(2, "count"))
143 assert_equal("10 buffalos", pluralize(10, "buffalo"))
147 def test_auto_link_parsing
148 urls = %w(http://www.rubyonrails.com
149 http://www.rubyonrails.com:80
150 http://www.rubyonrails.com/~minam
151 https://www.rubyonrails.com/~minam
152 http://www.rubyonrails.com/~minam/url%20with%20spaces
153 http://www.rubyonrails.com/foo.cgi?something=here
154 http://www.rubyonrails.com/foo.cgi?something=here&and=here
155 http://www.rubyonrails.com/contact;new
156 http://www.rubyonrails.com/contact;new%20with%20spaces
157 http://www.rubyonrails.com/contact;new?with=query&string=params
158 http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
159 http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
160 http://www.mail-archive.com/rails@lists.rubyonrails.org/
164 assert_equal %(<a href="#{url}">#{url}</a>), auto_link(url)
168 def test_auto_linking
169 email_raw = 'david@loudthinking.com'
170 email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
171 email2_raw = '+david@loudthinking.com'
172 email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
173 link_raw = 'http://www.rubyonrails.com'
174 link_result = %{<a href="#{link_raw}">#{link_raw}</a>}
175 link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
176 link2_raw = 'www.rubyonrails.com'
177 link2_result = %{<a href="http://#{link2_raw}">#{link2_raw}</a>}
178 link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
179 link3_result = %{<a href="#{link3_raw}">#{link3_raw}</a>}
180 link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
181 link4_result = %{<a href="#{link4_raw}">#{link4_raw}</a>}
182 link5_raw = 'http://foo.example.com:3000/controller/action'
183 link5_result = %{<a href="#{link5_raw}">#{link5_raw}</a>}
184 link6_raw = 'http://foo.example.com:3000/controller/action+pack'
185 link6_result = %{<a href="#{link6_raw}">#{link6_raw}</a>}
186 link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
187 link7_result = %{<a href="#{link7_raw}">#{link7_raw}</a>}
188 link8_raw = 'http://foo.example.com:3000/controller/action.html'
189 link8_result = %{<a href="#{link8_raw}">#{link8_raw}</a>}
190 link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
191 link9_result = %{<a href="#{link9_raw}">#{link9_raw}</a>}
192 link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
193 link10_result = %{<a href="#{link10_raw}">#{link10_raw}</a>}
195 assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
196 assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
197 assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
198 assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
199 assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
200 assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
201 assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
202 assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
203 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>))
204 assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
205 assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
206 assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
207 assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
208 assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
209 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>))
210 assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
211 assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
212 assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
213 assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
214 assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
215 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>))
216 assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
217 assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
218 assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
219 assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
220 assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
221 assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
222 assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
223 assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
224 assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
225 assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
226 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>))
227 assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
228 assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
229 assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
230 assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
231 assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
232 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>))
233 assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
234 assert_equal email2_result, auto_link(email2_raw)
235 assert_equal '', auto_link(nil)
236 assert_equal '', auto_link('')
239 def test_auto_link_at_eol
240 url1 = "http://api.rubyonrails.com/Foo.html"
241 url2 = "http://www.ruby-doc.org/core/Bar.html"
243 assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
246 def test_auto_link_with_block
247 url = "http://api.rubyonrails.com/Foo.html"
248 email = "fantabulous@shiznadel.ic"
250 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) }
254 value = Cycle.new("one", 2, "3")
255 assert_equal("one", value.to_s)
256 assert_equal("2", value.to_s)
257 assert_equal("3", value.to_s)
258 assert_equal("one", value.to_s)
260 assert_equal("one", value.to_s)
261 assert_equal("2", value.to_s)
262 assert_equal("3", value.to_s)
265 def test_cycle_class_with_no_arguments
266 assert_raise(ArgumentError) { value = Cycle.new() }
270 assert_equal("one", cycle("one", 2, "3"))
271 assert_equal("2", cycle("one", 2, "3"))
272 assert_equal("3", cycle("one", 2, "3"))
273 assert_equal("one", cycle("one", 2, "3"))
274 assert_equal("2", cycle("one", 2, "3"))
275 assert_equal("3", cycle("one", 2, "3"))
278 def test_cycle_with_no_arguments
279 assert_raise(ArgumentError) { value = cycle() }
282 def test_cycle_resets_with_new_values
283 assert_equal("even", cycle("even", "odd"))
284 assert_equal("odd", cycle("even", "odd"))
285 assert_equal("even", cycle("even", "odd"))
286 assert_equal("1", cycle(1, 2, 3))
287 assert_equal("2", cycle(1, 2, 3))
288 assert_equal("3", cycle(1, 2, 3))
289 assert_equal("1", cycle(1, 2, 3))
292 def test_named_cycles
293 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
294 assert_equal("red", cycle("red", "blue", :name => "colors"))
295 assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
296 assert_equal("blue", cycle("red", "blue", :name => "colors"))
297 assert_equal("3", cycle(1, 2, 3, :name => "numbers"))
298 assert_equal("red", cycle("red", "blue", :name => "colors"))
301 def test_default_named_cycle
302 assert_equal("1", cycle(1, 2, 3))
303 assert_equal("2", cycle(1, 2, 3, :name => "default"))
304 assert_equal("3", cycle(1, 2, 3))
308 assert_equal("1", cycle(1, 2, 3))
309 assert_equal("2", cycle(1, 2, 3))
311 assert_equal("1", cycle(1, 2, 3))
314 def test_reset_unknown_cycle
315 reset_cycle("colors")
318 def test_recet_named_cycle
319 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
320 assert_equal("red", cycle("red", "blue", :name => "colors"))
321 reset_cycle("numbers")
322 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
323 assert_equal("blue", cycle("red", "blue", :name => "colors"))
324 assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
325 assert_equal("red", cycle("red", "blue", :name => "colors"))
328 def test_cycle_no_instance_variable_clashes
329 @cycles = %w{Specialized Fuji Giant}
330 assert_equal("red", cycle("red", "blue"))
331 assert_equal("blue", cycle("red", "blue"))
332 assert_equal("red", cycle("red", "blue"))
333 assert_equal(%w{Specialized Fuji Giant}, @cycles)