a370f1458fd5cde12b2fe5910b82629b36807af4
[feedcatcher.git] / vendor / rails / actionpack / test / template / text_helper_test.rb
1 require 'abstract_unit'
2 require 'testing_sandbox'
3
4 class TextHelperTest < ActionView::TestCase
5 tests ActionView::Helpers::TextHelper
6 include TestingSandbox
7
8 def setup
9 # This simulates the fact that instance variables are reset every time
10 # a view is rendered. The cycle helper depends on this behavior.
11 @_cycles = nil if (defined? @_cycles)
12 end
13
14 def test_concat
15 self.output_buffer = 'foo'
16 assert_equal 'foobar', concat('bar')
17 assert_equal 'foobar', output_buffer
18 end
19
20 def test_simple_format
21 assert_equal "<p></p>", simple_format(nil)
22
23 assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
24 assert_equal "<p>A paragraph</p>\n\n<p>and another one!</p>", simple_format("A paragraph\n\nand another one!")
25 assert_equal "<p>A paragraph\n<br /> With a newline</p>", simple_format("A paragraph\n With a newline")
26
27 text = "A\nB\nC\nD".freeze
28 assert_equal "<p>A\n<br />B\n<br />C\n<br />D</p>", simple_format(text)
29
30 text = "A\r\n \nB\n\n\r\n\t\nC\nD".freeze
31 assert_equal "<p>A\n<br /> \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text)
32
33 assert_equal %q(<p class="test">This is a classy test</p>), simple_format("This is a classy test", :class => 'test')
34 assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), simple_format("para 1\n\npara 2", :class => 'test')
35 end
36
37 def test_truncate
38 assert_equal "Hello World!", truncate("Hello World!", :length => 12)
39 assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12)
40 end
41
42 def test_truncate_should_use_default_length_of_30
43 str = "This is a string that will go longer then the default truncate length of 30"
44 assert_equal str[0...27] + "...", truncate(str)
45 end
46
47 def test_truncate_with_options_hash
48 assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]")
49 assert_equal "Hello W...", truncate("Hello World!", :length => 10)
50 assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10)
51 end
52
53 if RUBY_VERSION < '1.9.0'
54 def test_truncate_multibyte
55 with_kcode 'none' do
56 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", :length => 10)
57 end
58 with_kcode 'u' do
59 assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
60 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", :length => 10)
61 end
62 end
63 else
64 def test_truncate_multibyte
65 assert_equal "\354\225\210\353\205\225\355...",
66 truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10)
67
68 assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'),
69 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".force_encoding('UTF-8'), :length => 10)
70 end
71 end
72
73 def test_highlighter
74 assert_equal(
75 "This is a <strong class=\"highlight\">beautiful</strong> morning",
76 highlight("This is a beautiful morning", "beautiful")
77 )
78
79 assert_equal(
80 "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day",
81 highlight("This is a beautiful morning, but also a beautiful day", "beautiful")
82 )
83
84 assert_equal(
85 "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
86 highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>')
87 )
88
89 assert_equal(
90 "This text is not changed because we supplied an empty phrase",
91 highlight("This text is not changed because we supplied an empty phrase", nil)
92 )
93
94 assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
95 end
96
97 def test_highlight_with_regexp
98 assert_equal(
99 "This is a <strong class=\"highlight\">beautiful!</strong> morning",
100 highlight("This is a beautiful! morning", "beautiful!")
101 )
102
103 assert_equal(
104 "This is a <strong class=\"highlight\">beautiful! morning</strong>",
105 highlight("This is a beautiful! morning", "beautiful! morning")
106 )
107
108 assert_equal(
109 "This is a <strong class=\"highlight\">beautiful? morning</strong>",
110 highlight("This is a beautiful? morning", "beautiful? morning")
111 )
112 end
113
114 def test_highlight_with_multiple_phrases_in_one_pass
115 assert_equal %(<em>wow</em> <em>em</em>), highlight('wow em', %w(wow em), '<em>\1</em>')
116 end
117
118 def test_highlight_with_options_hash
119 assert_equal(
120 "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
121 highlight("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '<b>\1</b>')
122 )
123 end
124
125 def test_highlight_with_html
126 assert_equal(
127 "<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
128 highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful")
129 )
130 assert_equal(
131 "<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
132 highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful")
133 )
134 assert_equal(
135 "<p>This is a <em class=\"error\"><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> <span class=\"last\">day</span></p>",
136 highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful")
137 )
138 assert_equal(
139 "<p class=\"beautiful\">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
140 highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful")
141 )
142 assert_equal(
143 "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
144 highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful")
145 )
146 end
147
148 def test_excerpt
149 assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", 5))
150 assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
151 assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
152 assert_nil excerpt("This is a beautiful morning", "day")
153 end
154
155 def test_excerpt_in_borderline_cases
156 assert_equal("", excerpt("", "", 0))
157 assert_equal("a", excerpt("a", "a", 0))
158 assert_equal("...b...", excerpt("abc", "b", 0))
159 assert_equal("abc", excerpt("abc", "b", 1))
160 assert_equal("abc...", excerpt("abcd", "b", 1))
161 assert_equal("...abc", excerpt("zabc", "b", 1))
162 assert_equal("...abc...", excerpt("zabcd", "b", 1))
163 assert_equal("zabcd", excerpt("zabcd", "b", 2))
164
165 # excerpt strips the resulting string before ap-/prepending excerpt_string.
166 # whether this behavior is meaningful when excerpt_string is not to be
167 # appended is questionable.
168 assert_equal("zabcd", excerpt(" zabcd ", "b", 4))
169 assert_equal("...abc...", excerpt("z abc d", "b", 1))
170 end
171
172 def test_excerpt_with_regex
173 assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', 5))
174 assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', 5))
175 end
176
177 def test_excerpt_with_options_hash
178 assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5))
179 assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5))
180 assert_equal(
181 "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]",
182 excerpt("This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?", "very",
183 :omission => "[...]")
184 )
185 end
186
187 if RUBY_VERSION < '1.9'
188 def test_excerpt_with_utf8
189 with_kcode('u') do
190 assert_equal("...\357\254\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', 8))
191 end
192 with_kcode('none') do
193 assert_equal("...\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', 8))
194 end
195 end
196 else
197 def test_excerpt_with_utf8
198 assert_equal("...\357\254\203ciency could not be...".force_encoding('UTF-8'), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding('UTF-8'), 'could', 8))
199 assert_equal("...\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', 8))
200 end
201 end
202
203 def test_word_wrap
204 assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
205 end
206
207 def test_word_wrap_with_extra_newlines
208 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))
209 end
210
211 def test_word_wrap_with_options_hash
212 assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", :line_width => 15))
213 end
214
215 def test_pluralization
216 assert_equal("1 count", pluralize(1, "count"))
217 assert_equal("2 counts", pluralize(2, "count"))
218 assert_equal("1 count", pluralize('1', "count"))
219 assert_equal("2 counts", pluralize('2', "count"))
220 assert_equal("1,066 counts", pluralize('1,066', "count"))
221 assert_equal("1.25 counts", pluralize('1.25', "count"))
222 assert_equal("2 counters", pluralize(2, "count", "counters"))
223 assert_equal("0 counters", pluralize(nil, "count", "counters"))
224 assert_equal("2 people", pluralize(2, "person"))
225 assert_equal("10 buffaloes", pluralize(10, "buffalo"))
226 assert_equal("1 berry", pluralize(1, "berry"))
227 assert_equal("12 berries", pluralize(12, "berry"))
228 end
229
230 def test_auto_link_parsing
231 urls = %w(
232 http://www.rubyonrails.com
233 http://www.rubyonrails.com:80
234 http://www.rubyonrails.com/~minam
235 https://www.rubyonrails.com/~minam
236 http://www.rubyonrails.com/~minam/url%20with%20spaces
237 http://www.rubyonrails.com/foo.cgi?something=here
238 http://www.rubyonrails.com/foo.cgi?something=here&and=here
239 http://www.rubyonrails.com/contact;new
240 http://www.rubyonrails.com/contact;new%20with%20spaces
241 http://www.rubyonrails.com/contact;new?with=query&string=params
242 http://www.rubyonrails.com/~minam/contact;new?with=query&string=params
243 http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007
244 http://www.mail-archive.com/rails@lists.rubyonrails.org/
245 http://www.amazon.com/Testing-Equal-Sign-In-Path/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1198861734&sr=8-1
246 http://en.wikipedia.org/wiki/Texas_hold'em
247 https://www.google.com/doku.php?id=gps:resource:scs:start
248 http://connect.oraclecorp.com/search?search[q]=green+france&search[type]=Group
249 http://of.openfoundry.org/projects/492/download#4th.Release.3
250 http://maps.google.co.uk/maps?f=q&q=the+london+eye&ie=UTF8&ll=51.503373,-0.11939&spn=0.007052,0.012767&z=16&iwloc=A
251 )
252
253 urls.each do |url|
254 assert_equal generate_result(url), auto_link(url)
255 end
256 end
257
258 def generate_result(link_text, href = nil)
259 href ||= link_text
260 %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
261 end
262
263 def test_auto_linking
264 email_raw = 'david@loudthinking.com'
265 email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
266 link_raw = 'http://www.rubyonrails.com'
267 link_result = generate_result(link_raw)
268 link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
269
270 assert_equal '', auto_link(nil)
271 assert_equal '', auto_link('')
272 assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}")
273
274 assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses)
275 assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls)
276 assert_equal %(Go to #{link_raw}), auto_link("Go to #{link_raw}", :email_addresses)
277 assert_equal %(Go to #{link_result} and say hello to #{email_result}), auto_link("Go to #{link_raw} and say hello to #{email_raw}")
278 assert_equal %(<p>Link #{link_result}</p>), auto_link("<p>Link #{link_raw}</p>")
279 assert_equal %(<p>#{link_result} Link</p>), auto_link("<p>#{link_raw} Link</p>")
280 assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
281 assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.))
282 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>))
283
284 email2_raw = '+david@loudthinking.com'
285 email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>}
286 assert_equal email2_result, auto_link(email2_raw)
287
288 email3_raw = '+david@loudthinking.com'
289 email3_result = %{<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;+%64%61%76%69%64@%6c%6f%75%64%74%68%69%6e%6b%69%6e%67.%63%6f%6d">#{email3_raw}</a>}
290 assert_equal email3_result, auto_link(email3_raw, :all, :encode => :hex)
291 assert_equal email3_result, auto_link(email3_raw, :email_addresses, :encode => :hex)
292
293 link2_raw = 'www.rubyonrails.com'
294 link2_result = generate_result(link2_raw, "http://#{link2_raw}")
295 assert_equal %(Go to #{link2_result}), auto_link("Go to #{link2_raw}", :urls)
296 assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
297 assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
298 assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
299 assert_equal %(Go to #{link2_result}.), auto_link(%(Go to #{link2_raw}.))
300 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>))
301
302 link3_raw = 'http://manuals.ruby-on-rails.com/read/chapter.need_a-period/103#page281'
303 link3_result = generate_result(link3_raw)
304 assert_equal %(Go to #{link3_result}), auto_link("Go to #{link3_raw}", :urls)
305 assert_equal %(Go to #{link3_raw}), auto_link("Go to #{link3_raw}", :email_addresses)
306 assert_equal %(<p>Link #{link3_result}</p>), auto_link("<p>Link #{link3_raw}</p>")
307 assert_equal %(<p>#{link3_result} Link</p>), auto_link("<p>#{link3_raw} Link</p>")
308 assert_equal %(Go to #{link3_result}.), auto_link(%(Go to #{link3_raw}.))
309 assert_equal %(<p>Go to #{link3_result}. Seriously, #{link3_result}? I think I'll say hello to #{email_result}. Instead.</p>),
310 auto_link(%(<p>Go to #{link3_raw}. Seriously, #{link3_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
311
312 link4_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor123'
313 link4_result = generate_result(link4_raw)
314 assert_equal %(<p>Link #{link4_result}</p>), auto_link("<p>Link #{link4_raw}</p>")
315 assert_equal %(<p>#{link4_result} Link</p>), auto_link("<p>#{link4_raw} Link</p>")
316
317 link5_raw = 'http://foo.example.com:3000/controller/action'
318 link5_result = generate_result(link5_raw)
319 assert_equal %(<p>#{link5_result} Link</p>), auto_link("<p>#{link5_raw} Link</p>")
320
321 link6_raw = 'http://foo.example.com:3000/controller/action+pack'
322 link6_result = generate_result(link6_raw)
323 assert_equal %(<p>#{link6_result} Link</p>), auto_link("<p>#{link6_raw} Link</p>")
324
325 link7_raw = 'http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123'
326 link7_result = generate_result(link7_raw)
327 assert_equal %(<p>#{link7_result} Link</p>), auto_link("<p>#{link7_raw} Link</p>")
328
329 link8_raw = 'http://foo.example.com:3000/controller/action.html'
330 link8_result = generate_result(link8_raw)
331 assert_equal %(Go to #{link8_result}), auto_link("Go to #{link8_raw}", :urls)
332 assert_equal %(Go to #{link8_raw}), auto_link("Go to #{link8_raw}", :email_addresses)
333 assert_equal %(<p>Link #{link8_result}</p>), auto_link("<p>Link #{link8_raw}</p>")
334 assert_equal %(<p>#{link8_result} Link</p>), auto_link("<p>#{link8_raw} Link</p>")
335 assert_equal %(Go to #{link8_result}.), auto_link(%(Go to #{link8_raw}.))
336 assert_equal %(<p>Go to #{link8_result}. Seriously, #{link8_result}? I think I'll say hello to #{email_result}. Instead.</p>),
337 auto_link(%(<p>Go to #{link8_raw}. Seriously, #{link8_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
338
339 link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html'
340 link9_result = generate_result(link9_raw)
341 assert_equal %(Go to #{link9_result}), auto_link("Go to #{link9_raw}", :urls)
342 assert_equal %(Go to #{link9_raw}), auto_link("Go to #{link9_raw}", :email_addresses)
343 assert_equal %(<p>Link #{link9_result}</p>), auto_link("<p>Link #{link9_raw}</p>")
344 assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>")
345 assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.))
346 assert_equal %(<p>Go to #{link9_result}. Seriously, #{link9_result}? I think I'll say hello to #{email_result}. Instead.</p>),
347 auto_link(%(<p>Go to #{link9_raw}. Seriously, #{link9_raw}? I think I'll say hello to #{email_raw}. Instead.</p>))
348
349 link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/'
350 link10_result = generate_result(link10_raw)
351 assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
352 end
353
354 def test_auto_link_already_linked
355 linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com')
356 linked2 = generate_result('www.rubyonrails.com', 'http://www.rubyonrails.com')
357 assert_equal linked1, auto_link(linked1)
358 assert_equal linked2, auto_link(linked2)
359 end
360
361 def test_auto_link_with_brackets
362 link1_raw = 'http://en.wikipedia.org/wiki/Sprite_(computer_graphics)'
363 link1_result = generate_result(link1_raw)
364 assert_equal link1_result, auto_link(link1_raw)
365 assert_equal "(link: #{link1_result})", auto_link("(link: #{link1_raw})")
366
367 link2_raw = 'http://en.wikipedia.org/wiki/Sprite_[computer_graphics]'
368 link2_result = generate_result(link2_raw)
369 assert_equal link2_result, auto_link(link2_raw)
370 assert_equal "[link: #{link2_result}]", auto_link("[link: #{link2_raw}]")
371
372 link3_raw = 'http://en.wikipedia.org/wiki/Sprite_{computer_graphics}'
373 link3_result = generate_result(link3_raw)
374 assert_equal link3_result, auto_link(link3_raw)
375 assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}")
376 end
377
378 def test_auto_link_in_tags
379 link_raw = 'http://www.rubyonrails.org/images/rails.png'
380 link_result = %Q(<img src="#{link_raw}" />)
381 assert_equal link_result, auto_link(link_result)
382 end
383
384 def test_auto_link_at_eol
385 url1 = "http://api.rubyonrails.com/Foo.html"
386 url2 = "http://www.ruby-doc.org/core/Bar.html"
387
388 assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
389 end
390
391 def test_auto_link_with_block
392 url = "http://api.rubyonrails.com/Foo.html"
393 email = "fantabulous@shiznadel.ic"
394
395 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, :length => 10) }
396 end
397
398 def test_auto_link_with_options_hash
399 assert_dom_equal 'Welcome to my new blog at <a href="http://www.myblog.com/" class="menu" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a href="mailto:me@email.com" class="menu" target="_blank">me@email.com</a>.',
400 auto_link("Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com.",
401 :link => :all, :html => { :class => "menu", :target => "_blank" })
402 end
403
404 def test_cycle_class
405 value = Cycle.new("one", 2, "3")
406 assert_equal("one", value.to_s)
407 assert_equal("2", value.to_s)
408 assert_equal("3", value.to_s)
409 assert_equal("one", value.to_s)
410 value.reset
411 assert_equal("one", value.to_s)
412 assert_equal("2", value.to_s)
413 assert_equal("3", value.to_s)
414 end
415
416 def test_cycle_class_with_no_arguments
417 assert_raise(ArgumentError) { value = Cycle.new() }
418 end
419
420 def test_cycle
421 assert_equal("one", cycle("one", 2, "3"))
422 assert_equal("2", cycle("one", 2, "3"))
423 assert_equal("3", cycle("one", 2, "3"))
424 assert_equal("one", cycle("one", 2, "3"))
425 assert_equal("2", cycle("one", 2, "3"))
426 assert_equal("3", cycle("one", 2, "3"))
427 end
428
429 def test_cycle_with_no_arguments
430 assert_raise(ArgumentError) { value = cycle() }
431 end
432
433 def test_cycle_resets_with_new_values
434 assert_equal("even", cycle("even", "odd"))
435 assert_equal("odd", cycle("even", "odd"))
436 assert_equal("even", cycle("even", "odd"))
437 assert_equal("1", cycle(1, 2, 3))
438 assert_equal("2", cycle(1, 2, 3))
439 assert_equal("3", cycle(1, 2, 3))
440 assert_equal("1", cycle(1, 2, 3))
441 end
442
443 def test_named_cycles
444 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
445 assert_equal("red", cycle("red", "blue", :name => "colors"))
446 assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
447 assert_equal("blue", cycle("red", "blue", :name => "colors"))
448 assert_equal("3", cycle(1, 2, 3, :name => "numbers"))
449 assert_equal("red", cycle("red", "blue", :name => "colors"))
450 end
451
452 def test_current_cycle_with_default_name
453 cycle("even","odd")
454 assert_equal "even", current_cycle
455 cycle("even","odd")
456 assert_equal "odd", current_cycle
457 cycle("even","odd")
458 assert_equal "even", current_cycle
459 end
460
461 def test_current_cycle_with_named_cycles
462 cycle("red", "blue", :name => "colors")
463 assert_equal "red", current_cycle("colors")
464 cycle("red", "blue", :name => "colors")
465 assert_equal "blue", current_cycle("colors")
466 cycle("red", "blue", :name => "colors")
467 assert_equal "red", current_cycle("colors")
468 end
469
470 def test_current_cycle_safe_call
471 assert_nothing_raised { current_cycle }
472 assert_nothing_raised { current_cycle("colors") }
473 end
474
475 def test_current_cycle_with_more_than_two_names
476 cycle(1,2,3)
477 assert_equal "1", current_cycle
478 cycle(1,2,3)
479 assert_equal "2", current_cycle
480 cycle(1,2,3)
481 assert_equal "3", current_cycle
482 cycle(1,2,3)
483 assert_equal "1", current_cycle
484 end
485
486 def test_default_named_cycle
487 assert_equal("1", cycle(1, 2, 3))
488 assert_equal("2", cycle(1, 2, 3, :name => "default"))
489 assert_equal("3", cycle(1, 2, 3))
490 end
491
492 def test_reset_cycle
493 assert_equal("1", cycle(1, 2, 3))
494 assert_equal("2", cycle(1, 2, 3))
495 reset_cycle
496 assert_equal("1", cycle(1, 2, 3))
497 end
498
499 def test_reset_unknown_cycle
500 reset_cycle("colors")
501 end
502
503 def test_recet_named_cycle
504 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
505 assert_equal("red", cycle("red", "blue", :name => "colors"))
506 reset_cycle("numbers")
507 assert_equal("1", cycle(1, 2, 3, :name => "numbers"))
508 assert_equal("blue", cycle("red", "blue", :name => "colors"))
509 assert_equal("2", cycle(1, 2, 3, :name => "numbers"))
510 assert_equal("red", cycle("red", "blue", :name => "colors"))
511 end
512
513 def test_cycle_no_instance_variable_clashes
514 @cycles = %w{Specialized Fuji Giant}
515 assert_equal("red", cycle("red", "blue"))
516 assert_equal("blue", cycle("red", "blue"))
517 assert_equal("red", cycle("red", "blue"))
518 assert_equal(%w{Specialized Fuji Giant}, @cycles)
519 end
520 end