Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / assert_select_test.rb
1 #--
2 # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
3 # Under MIT and/or CC By license.
4 #++
5
6 require 'abstract_unit'
7 require 'controller/fake_controllers'
8
9
10 unless defined?(ActionMailer)
11 begin
12 $:.unshift(File.dirname(__FILE__) + "/../../../actionmailer/lib")
13 require 'action_mailer'
14 rescue LoadError
15 require 'rubygems'
16 gem 'actionmailer'
17 end
18 end
19
20 ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
21
22 class AssertSelectTest < Test::Unit::TestCase
23 class AssertSelectController < ActionController::Base
24 def response_with=(content)
25 @content = content
26 end
27
28 def response_with(&block)
29 @update = block
30 end
31
32 def html()
33 render :text=>@content, :layout=>false, :content_type=>Mime::HTML
34 @content = nil
35 end
36
37 def rjs()
38 render :update do |page|
39 @update.call page
40 end
41 @update = nil
42 end
43
44 def xml()
45 render :text=>@content, :layout=>false, :content_type=>Mime::XML
46 @content = nil
47 end
48
49 def rescue_action(e)
50 raise e
51 end
52 end
53
54 class AssertSelectMailer < ActionMailer::Base
55 def test(html)
56 recipients "test <test@test.host>"
57 from "test@test.host"
58 subject "Test e-mail"
59 part :content_type=>"text/html", :body=>html
60 end
61 end
62
63 AssertionFailedError = Test::Unit::AssertionFailedError
64
65 def setup
66 @controller = AssertSelectController.new
67 @request = ActionController::TestRequest.new
68 @response = ActionController::TestResponse.new
69 ActionMailer::Base.delivery_method = :test
70 ActionMailer::Base.perform_deliveries = true
71 ActionMailer::Base.deliveries = []
72 end
73
74 def teardown
75 ActionMailer::Base.deliveries.clear
76 end
77
78 def assert_failure(message, &block)
79 e = assert_raises(AssertionFailedError, &block)
80 assert_match(message, e.message) if Regexp === message
81 assert_equal(message, e.message) if String === message
82 end
83
84 #
85 # Test assert select.
86 #
87
88 def test_assert_select
89 render_html %Q{<div id="1"></div><div id="2"></div>}
90 assert_select "div", 2
91 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
92 assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
93 end
94
95 def test_equality_true_false
96 render_html %Q{<div id="1"></div><div id="2"></div>}
97 assert_nothing_raised { assert_select "div" }
98 assert_raises(AssertionFailedError) { assert_select "p" }
99 assert_nothing_raised { assert_select "div", true }
100 assert_raises(AssertionFailedError) { assert_select "p", true }
101 assert_raises(AssertionFailedError) { assert_select "div", false }
102 assert_nothing_raised { assert_select "p", false }
103 end
104
105 def test_equality_string_and_regexp
106 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
107 assert_nothing_raised { assert_select "div", "foo" }
108 assert_raises(AssertionFailedError) { assert_select "div", "bar" }
109 assert_nothing_raised { assert_select "div", :text=>"foo" }
110 assert_raises(AssertionFailedError) { assert_select "div", :text=>"bar" }
111 assert_nothing_raised { assert_select "div", /(foo|bar)/ }
112 assert_raises(AssertionFailedError) { assert_select "div", /foobar/ }
113 assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
114 assert_raises(AssertionFailedError) { assert_select "div", :text=>/foobar/ }
115 assert_raises(AssertionFailedError) { assert_select "p", :text=>/foobar/ }
116 end
117
118 def test_equality_of_html
119 render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
120 text = "\"This is not a big problem,\" he said."
121 html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
122 assert_nothing_raised { assert_select "p", text }
123 assert_raises(AssertionFailedError) { assert_select "p", html }
124 assert_nothing_raised { assert_select "p", :html=>html }
125 assert_raises(AssertionFailedError) { assert_select "p", :html=>text }
126 # No stripping for pre.
127 render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
128 text = "\n\"This is not a big problem,\" he said.\n"
129 html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
130 assert_nothing_raised { assert_select "pre", text }
131 assert_raises(AssertionFailedError) { assert_select "pre", html }
132 assert_nothing_raised { assert_select "pre", :html=>html }
133 assert_raises(AssertionFailedError) { assert_select "pre", :html=>text }
134 end
135
136 def test_counts
137 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
138 assert_nothing_raised { assert_select "div", 2 }
139 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
140 assert_select "div", 3
141 end
142 assert_nothing_raised { assert_select "div", 1..2 }
143 assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
144 assert_select "div", 3..4
145 end
146 assert_nothing_raised { assert_select "div", :count=>2 }
147 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
148 assert_select "div", :count=>3
149 end
150 assert_nothing_raised { assert_select "div", :minimum=>1 }
151 assert_nothing_raised { assert_select "div", :minimum=>2 }
152 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
153 assert_select "div", :minimum=>3
154 end
155 assert_nothing_raised { assert_select "div", :maximum=>2 }
156 assert_nothing_raised { assert_select "div", :maximum=>3 }
157 assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
158 assert_select "div", :maximum=>1
159 end
160 assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
161 assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
162 assert_select "div", :minimum=>3, :maximum=>4
163 end
164 end
165
166 def test_substitution_values
167 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
168 assert_select "div#?", /\d+/ do |elements|
169 assert_equal 2, elements.size
170 end
171 assert_select "div" do
172 assert_select "div#?", /\d+/ do |elements|
173 assert_equal 2, elements.size
174 assert_select "#1"
175 assert_select "#2"
176 end
177 end
178 end
179
180 def test_nested_assert_select
181 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
182 assert_select "div" do |elements|
183 assert_equal 2, elements.size
184 assert_select elements[0], "#1"
185 assert_select elements[1], "#2"
186 end
187 assert_select "div" do
188 assert_select "div" do |elements|
189 assert_equal 2, elements.size
190 # Testing in a group is one thing
191 assert_select "#1,#2"
192 # Testing individually is another.
193 assert_select "#1"
194 assert_select "#2"
195 assert_select "#3", false
196 end
197 end
198
199 assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
200 assert_select "div" do
201 assert_select "#4"
202 end
203 end
204 end
205
206 def test_assert_select_text_match
207 render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
208 assert_select "div" do
209 assert_nothing_raised { assert_select "div", "foo" }
210 assert_nothing_raised { assert_select "div", "bar" }
211 assert_nothing_raised { assert_select "div", /\w*/ }
212 assert_nothing_raised { assert_select "div", /\w*/, :count=>2 }
213 assert_raises(AssertionFailedError) { assert_select "div", :text=>"foo", :count=>2 }
214 assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
215 assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
216 assert_nothing_raised { assert_select "div", :html=>/\w*/ }
217 assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
218 assert_raises(AssertionFailedError) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
219 end
220 end
221
222 # With single result.
223 def test_assert_select_from_rjs_with_single_result
224 render_rjs do |page|
225 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
226 end
227 assert_select "div" do |elements|
228 assert elements.size == 2
229 assert_select "#1"
230 assert_select "#2"
231 end
232 assert_select "div#?", /\d+/ do |elements|
233 assert_select "#1"
234 assert_select "#2"
235 end
236 end
237
238 # With multiple results.
239 def test_assert_select_from_rjs_with_multiple_results
240 render_rjs do |page|
241 page.replace_html "test", "<div id=\"1\">foo</div>"
242 page.replace_html "test2", "<div id=\"2\">foo</div>"
243 end
244 assert_select "div" do |elements|
245 assert elements.size == 2
246 assert_select "#1"
247 assert_select "#2"
248 end
249 end
250
251 #
252 # Test css_select.
253 #
254
255 def test_css_select
256 render_html %Q{<div id="1"></div><div id="2"></div>}
257 assert 2, css_select("div").size
258 assert 0, css_select("p").size
259 end
260
261 def test_nested_css_select
262 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
263 assert_select "div#?", /\d+/ do |elements|
264 assert_equal 1, css_select(elements[0], "div").size
265 assert_equal 1, css_select(elements[1], "div").size
266 end
267 assert_select "div" do
268 assert_equal 2, css_select("div").size
269 css_select("div").each do |element|
270 # Testing as a group is one thing
271 assert !css_select("#1,#2").empty?
272 # Testing individually is another
273 assert !css_select("#1").empty?
274 assert !css_select("#2").empty?
275 end
276 end
277 end
278
279 # With one result.
280 def test_css_select_from_rjs_with_single_result
281 render_rjs do |page|
282 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
283 end
284 assert_equal 2, css_select("div").size
285 assert_equal 1, css_select("#1").size
286 assert_equal 1, css_select("#2").size
287 end
288
289 # With multiple results.
290 def test_css_select_from_rjs_with_multiple_results
291 render_rjs do |page|
292 page.replace_html "test", "<div id=\"1\">foo</div>"
293 page.replace_html "test2", "<div id=\"2\">foo</div>"
294 end
295
296 assert_equal 2, css_select("div").size
297 assert_equal 1, css_select("#1").size
298 assert_equal 1, css_select("#2").size
299 end
300
301 #
302 # Test assert_select_rjs.
303 #
304
305 # Test that we can pick up all statements in the result.
306 def test_assert_select_rjs_picks_up_all_statements
307 render_rjs do |page|
308 page.replace "test", "<div id=\"1\">foo</div>"
309 page.replace_html "test2", "<div id=\"2\">foo</div>"
310 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
311 end
312
313 found = false
314 assert_select_rjs do
315 assert_select "#1"
316 assert_select "#2"
317 assert_select "#3"
318 found = true
319 end
320 assert found
321 end
322
323 # Test that we fail if there is nothing to pick.
324 def test_assert_select_rjs_fails_if_nothing_to_pick
325 render_rjs { }
326 assert_raises(AssertionFailedError) { assert_select_rjs }
327 end
328
329 def test_assert_select_rjs_with_unicode
330 # Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
331 render_rjs do |page|
332 page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
333 end
334 assert_select_rjs do
335 str = "#1"
336 assert_select str, :text => "\343\203\201\343\202\261\343\203\203\343\203\210"
337 assert_select str, "\343\203\201\343\202\261\343\203\203\343\203\210"
338 if str.respond_to?(:force_encoding)
339 str.force_encoding(Encoding::UTF_8)
340 assert_select str, /\343\203\201..\343\203\210/u
341 assert_raises(AssertionFailedError) { assert_select str, /\343\203\201.\343\203\210/u }
342 else
343 assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U')
344 assert_raises(AssertionFailedError) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
345 end
346 end
347 end
348
349 def test_assert_select_rjs_with_id
350 # Test that we can pick up all statements in the result.
351 render_rjs do |page|
352 page.replace "test1", "<div id=\"1\">foo</div>"
353 page.replace_html "test2", "<div id=\"2\">foo</div>"
354 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
355 end
356 assert_select_rjs "test1" do
357 assert_select "div", 1
358 assert_select "#1"
359 end
360 assert_select_rjs "test2" do
361 assert_select "div", 1
362 assert_select "#2"
363 end
364 assert_select_rjs "test3" do
365 assert_select "div", 1
366 assert_select "#3"
367 end
368 assert_raises(AssertionFailedError) { assert_select_rjs "test4" }
369 end
370
371 def test_assert_select_rjs_for_replace
372 render_rjs do |page|
373 page.replace "test1", "<div id=\"1\">foo</div>"
374 page.replace_html "test2", "<div id=\"2\">foo</div>"
375 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
376 end
377 # Replace.
378 assert_select_rjs :replace do
379 assert_select "div", 1
380 assert_select "#1"
381 end
382 assert_select_rjs :replace, "test1" do
383 assert_select "div", 1
384 assert_select "#1"
385 end
386 assert_raises(AssertionFailedError) { assert_select_rjs :replace, "test2" }
387 # Replace HTML.
388 assert_select_rjs :replace_html do
389 assert_select "div", 1
390 assert_select "#2"
391 end
392 assert_select_rjs :replace_html, "test2" do
393 assert_select "div", 1
394 assert_select "#2"
395 end
396 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
397 end
398
399 def test_assert_select_rjs_for_chained_replace
400 render_rjs do |page|
401 page['test1'].replace "<div id=\"1\">foo</div>"
402 page['test2'].replace_html "<div id=\"2\">foo</div>"
403 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
404 end
405 # Replace.
406 assert_select_rjs :chained_replace do
407 assert_select "div", 1
408 assert_select "#1"
409 end
410 assert_select_rjs :chained_replace, "test1" do
411 assert_select "div", 1
412 assert_select "#1"
413 end
414 assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" }
415 # Replace HTML.
416 assert_select_rjs :chained_replace_html do
417 assert_select "div", 1
418 assert_select "#2"
419 end
420 assert_select_rjs :chained_replace_html, "test2" do
421 assert_select "div", 1
422 assert_select "#2"
423 end
424 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
425 end
426
427 # Simple remove
428 def test_assert_select_rjs_for_remove
429 render_rjs do |page|
430 page.remove "test1"
431 end
432
433 assert_select_rjs :remove, "test1"
434 end
435
436 def test_assert_select_rjs_for_remove_offers_useful_error_when_assertion_fails
437 render_rjs do |page|
438 page.remove "test_with_typo"
439 end
440
441 assert_select_rjs :remove, "test1"
442
443 rescue Test::Unit::AssertionFailedError
444 assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message
445 end
446
447 def test_assert_select_rjs_for_remove_ignores_block
448 render_rjs do |page|
449 page.remove "test1"
450 end
451
452 assert_nothing_raised do
453 assert_select_rjs :remove, "test1" do
454 assert_select "p"
455 end
456 end
457 end
458
459 # Simple show
460 def test_assert_select_rjs_for_show
461 render_rjs do |page|
462 page.show "test1"
463 end
464
465 assert_select_rjs :show, "test1"
466 end
467
468 def test_assert_select_rjs_for_show_offers_useful_error_when_assertion_fails
469 render_rjs do |page|
470 page.show "test_with_typo"
471 end
472
473 assert_select_rjs :show, "test1"
474
475 rescue Test::Unit::AssertionFailedError
476 assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message
477 end
478
479 def test_assert_select_rjs_for_show_ignores_block
480 render_rjs do |page|
481 page.show "test1"
482 end
483
484 assert_nothing_raised do
485 assert_select_rjs :show, "test1" do
486 assert_select "p"
487 end
488 end
489 end
490
491 # Simple hide
492 def test_assert_select_rjs_for_hide
493 render_rjs do |page|
494 page.hide "test1"
495 end
496
497 assert_select_rjs :hide, "test1"
498 end
499
500 def test_assert_select_rjs_for_hide_offers_useful_error_when_assertion_fails
501 render_rjs do |page|
502 page.hide "test_with_typo"
503 end
504
505 assert_select_rjs :hide, "test1"
506
507 rescue Test::Unit::AssertionFailedError
508 assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message
509 end
510
511 def test_assert_select_rjs_for_hide_ignores_block
512 render_rjs do |page|
513 page.hide "test1"
514 end
515
516 assert_nothing_raised do
517 assert_select_rjs :hide, "test1" do
518 assert_select "p"
519 end
520 end
521 end
522
523 # Simple toggle
524 def test_assert_select_rjs_for_toggle
525 render_rjs do |page|
526 page.toggle "test1"
527 end
528
529 assert_select_rjs :toggle, "test1"
530 end
531
532 def test_assert_select_rjs_for_toggle_offers_useful_error_when_assertion_fails
533 render_rjs do |page|
534 page.toggle "test_with_typo"
535 end
536
537 assert_select_rjs :toggle, "test1"
538
539 rescue Test::Unit::AssertionFailedError
540 assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message
541 end
542
543 def test_assert_select_rjs_for_toggle_ignores_block
544 render_rjs do |page|
545 page.toggle "test1"
546 end
547
548 assert_nothing_raised do
549 assert_select_rjs :toggle, "test1" do
550 assert_select "p"
551 end
552 end
553 end
554
555 # Non-positioned insert.
556 def test_assert_select_rjs_for_nonpositioned_insert
557 render_rjs do |page|
558 page.replace "test1", "<div id=\"1\">foo</div>"
559 page.replace_html "test2", "<div id=\"2\">foo</div>"
560 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
561 end
562 assert_select_rjs :insert_html do
563 assert_select "div", 1
564 assert_select "#3"
565 end
566 assert_select_rjs :insert_html, "test3" do
567 assert_select "div", 1
568 assert_select "#3"
569 end
570 assert_raises(AssertionFailedError) { assert_select_rjs :insert_html, "test1" }
571 end
572
573 # Positioned insert.
574 def test_assert_select_rjs_for_positioned_insert
575 render_rjs do |page|
576 page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
577 page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
578 page.insert_html :before, "test3", "<div id=\"3\">foo</div>"
579 page.insert_html :after, "test4", "<div id=\"4\">foo</div>"
580 end
581 assert_select_rjs :insert, :top do
582 assert_select "div", 1
583 assert_select "#1"
584 end
585 assert_select_rjs :insert, :bottom do
586 assert_select "div", 1
587 assert_select "#2"
588 end
589 assert_select_rjs :insert, :before do
590 assert_select "div", 1
591 assert_select "#3"
592 end
593 assert_select_rjs :insert, :after do
594 assert_select "div", 1
595 assert_select "#4"
596 end
597 assert_select_rjs :insert_html do
598 assert_select "div", 4
599 end
600 end
601
602 def test_assert_select_rjs_raise_errors
603 assert_raises(ArgumentError) { assert_select_rjs(:destroy) }
604 assert_raises(ArgumentError) { assert_select_rjs(:insert, :left) }
605 end
606
607 # Simple selection from a single result.
608 def test_nested_assert_select_rjs_with_single_result
609 render_rjs do |page|
610 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
611 end
612
613 assert_select_rjs "test" do |elements|
614 assert_equal 2, elements.size
615 assert_select "#1"
616 assert_select "#2"
617 end
618 end
619
620 # Deal with two results.
621 def test_nested_assert_select_rjs_with_two_results
622 render_rjs do |page|
623 page.replace_html "test", "<div id=\"1\">foo</div>"
624 page.replace_html "test2", "<div id=\"2\">foo</div>"
625 end
626
627 assert_select_rjs "test" do |elements|
628 assert_equal 1, elements.size
629 assert_select "#1"
630 end
631
632 assert_select_rjs "test2" do |elements|
633 assert_equal 1, elements.size
634 assert_select "#2"
635 end
636 end
637
638 def test_feed_item_encoded
639 render_xml <<-EOF
640 <rss version="2.0">
641 <channel>
642 <item>
643 <description>
644 <![CDATA[
645 <p>Test 1</p>
646 ]]>
647 </description>
648 </item>
649 <item>
650 <description>
651 <![CDATA[
652 <p>Test 2</p>
653 ]]>
654 </description>
655 </item>
656 </channel>
657 </rss>
658 EOF
659 assert_select "channel item description" do
660 # Test element regardless of wrapper.
661 assert_select_encoded do
662 assert_select "p", :count=>2, :text=>/Test/
663 end
664 # Test through encoded wrapper.
665 assert_select_encoded do
666 assert_select "encoded p", :count=>2, :text=>/Test/
667 end
668 # Use :root instead (recommended)
669 assert_select_encoded do
670 assert_select ":root p", :count=>2, :text=>/Test/
671 end
672 # Test individually.
673 assert_select "description" do |elements|
674 assert_select_encoded elements[0] do
675 assert_select "p", "Test 1"
676 end
677 assert_select_encoded elements[1] do
678 assert_select "p", "Test 2"
679 end
680 end
681 end
682
683 # Test that we only un-encode element itself.
684 assert_select "channel item" do
685 assert_select_encoded do
686 assert_select "p", 0
687 end
688 end
689 end
690
691 #
692 # Test assert_select_email
693 #
694
695 def test_assert_select_email
696 assert_raises(AssertionFailedError) { assert_select_email {} }
697 AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
698 assert_select_email do
699 assert_select "div:root" do
700 assert_select "p:first-child", "foo"
701 assert_select "p:last-child", "bar"
702 end
703 end
704 end
705
706 protected
707 def render_html(html)
708 @controller.response_with = html
709 get :html
710 end
711
712 def render_rjs(&block)
713 @controller.response_with &block
714 get :rjs
715 end
716
717 def render_xml(xml)
718 @controller.response_with = xml
719 get :xml
720 end
721 end