2 # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
3 # Under MIT and/or CC By license.
6 require 'abstract_unit'
7 require 'controller/fake_controllers'
10 unless defined?(ActionMailer
)
12 $
:.unshift(File
.dirname(__FILE__
) + "/../../../actionmailer/lib")
13 require 'action_mailer'
20 ActionMailer
::Base.template_root
= FIXTURE_LOAD_PATH
22 class AssertSelectTest
< Test
::Unit::TestCase
23 class AssertSelectController
< ActionController
::Base
24 def response_with
=(content
)
28 def response_with(&block
)
33 render
:text=>@content, :layout=>false, :content_type=>Mime
::HTML
38 render
:update do |page
|
45 render
:text=>@content, :layout=>false, :content_type=>Mime
::XML
54 class AssertSelectMailer
< ActionMailer
::Base
56 recipients
"test <test@test.host>"
59 part
:content_type=>"text/html", :body=>html
63 AssertionFailedError
= Test
::Unit::AssertionFailedError
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
= []
75 ActionMailer
::Base.deliveries
.clear
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
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" }
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 }
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/ }
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 }
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
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
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
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
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
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
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
171 assert_select "div" do
172 assert_select "div#?", /\d+/ do |elements|
173 assert_equal 2, elements.size
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"
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.
195 assert_select "#3", false
199 assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
200 assert_select "div" do
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 }
222 # With single result.
223 def test_assert_select_from_rjs_with_single_result
225 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
227 assert_select "div" do |elements|
228 assert elements.size == 2
232 assert_select "div#?", /\d+/ do |elements|
238 # With multiple results.
239 def test_assert_select_from_rjs_with_multiple_results
241 page.replace_html "test", "<div id=\"1\">foo</div>"
242 page.replace_html "test2", "<div id=\"2\">foo</div>"
244 assert_select "div" do |elements|
245 assert elements.size == 2
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
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
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?
280 def test_css_select_from_rjs_with_single_result
282 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
284 assert_equal 2, css_select("div").size
285 assert_equal 1, css_select("#1").size
286 assert_equal 1, css_select("#2").size
289 # With multiple results.
290 def test_css_select_from_rjs_with_multiple_results
292 page.replace_html "test", "<div id=\"1\">foo</div>"
293 page.replace_html "test2", "<div id=\"2\">foo</div>"
296 assert_equal 2, css_select("div").size
297 assert_equal 1, css_select("#1").size
298 assert_equal 1, css_select("#2").size
302 # Test assert_select_rjs.
305 # Test that we can pick up all statements in the result.
306 def test_assert_select_rjs_picks_up_all_statements
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>"
323 # Test that we fail if there is nothing to pick.
324 def test_assert_select_rjs_fails_if_nothing_to_pick
326 assert_raises(AssertionFailedError) { assert_select_rjs }
329 def test_assert_select_rjs_with_unicode
330 # Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
332 page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
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 }
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') }
349 def test_assert_select_rjs_with_id
350 # Test that we can pick up all statements in the result.
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>"
356 assert_select_rjs "test1" do
357 assert_select "div", 1
360 assert_select_rjs "test2" do
361 assert_select "div", 1
364 assert_select_rjs "test3" do
365 assert_select "div", 1
368 assert_raises(AssertionFailedError) { assert_select_rjs "test4" }
371 def test_assert_select_rjs_for_replace
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>"
378 assert_select_rjs :replace do
379 assert_select "div", 1
382 assert_select_rjs :replace, "test1" do
383 assert_select "div", 1
386 assert_raises(AssertionFailedError) { assert_select_rjs :replace, "test2" }
388 assert_select_rjs :replace_html do
389 assert_select "div", 1
392 assert_select_rjs :replace_html, "test2" do
393 assert_select "div", 1
396 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
399 def test_assert_select_rjs_for_chained_replace
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>"
406 assert_select_rjs :chained_replace do
407 assert_select "div", 1
410 assert_select_rjs :chained_replace, "test1" do
411 assert_select "div", 1
414 assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" }
416 assert_select_rjs :chained_replace_html do
417 assert_select "div", 1
420 assert_select_rjs :chained_replace_html, "test2" do
421 assert_select "div", 1
424 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
428 def test_assert_select_rjs_for_remove
433 assert_select_rjs :remove, "test1"
436 def test_assert_select_rjs_for_remove_offers_useful_error_when_assertion_fails
438 page.remove "test_with_typo"
441 assert_select_rjs :remove, "test1"
443 rescue Test::Unit::AssertionFailedError
444 assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message
447 def test_assert_select_rjs_for_remove_ignores_block
452 assert_nothing_raised do
453 assert_select_rjs :remove, "test1" do
460 def test_assert_select_rjs_for_show
465 assert_select_rjs :show, "test1"
468 def test_assert_select_rjs_for_show_offers_useful_error_when_assertion_fails
470 page.show "test_with_typo"
473 assert_select_rjs :show, "test1"
475 rescue Test::Unit::AssertionFailedError
476 assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message
479 def test_assert_select_rjs_for_show_ignores_block
484 assert_nothing_raised do
485 assert_select_rjs :show, "test1" do
492 def test_assert_select_rjs_for_hide
497 assert_select_rjs :hide, "test1"
500 def test_assert_select_rjs_for_hide_offers_useful_error_when_assertion_fails
502 page.hide "test_with_typo"
505 assert_select_rjs :hide, "test1"
507 rescue Test::Unit::AssertionFailedError
508 assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message
511 def test_assert_select_rjs_for_hide_ignores_block
516 assert_nothing_raised do
517 assert_select_rjs :hide, "test1" do
524 def test_assert_select_rjs_for_toggle
529 assert_select_rjs :toggle, "test1"
532 def test_assert_select_rjs_for_toggle_offers_useful_error_when_assertion_fails
534 page.toggle "test_with_typo"
537 assert_select_rjs :toggle, "test1"
539 rescue Test::Unit::AssertionFailedError
540 assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message
543 def test_assert_select_rjs_for_toggle_ignores_block
548 assert_nothing_raised do
549 assert_select_rjs :toggle, "test1" do
555 # Non-positioned insert.
556 def test_assert_select_rjs_for_nonpositioned_insert
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>"
562 assert_select_rjs :insert_html do
563 assert_select "div", 1
566 assert_select_rjs :insert_html, "test3" do
567 assert_select "div", 1
570 assert_raises(AssertionFailedError) { assert_select_rjs :insert_html, "test1" }
574 def test_assert_select_rjs_for_positioned_insert
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>"
581 assert_select_rjs :insert, :top do
582 assert_select "div", 1
585 assert_select_rjs :insert, :bottom do
586 assert_select "div", 1
589 assert_select_rjs :insert, :before do
590 assert_select "div", 1
593 assert_select_rjs :insert, :after do
594 assert_select "div", 1
597 assert_select_rjs :insert_html do
598 assert_select "div", 4
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) }
607 # Simple selection from a single result.
608 def test_nested_assert_select_rjs_with_single_result
610 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
613 assert_select_rjs "test" do |elements|
614 assert_equal 2, elements.size
620 # Deal with two results.
621 def test_nested_assert_select_rjs_with_two_results
623 page.replace_html "test", "<div id=\"1\">foo</div>"
624 page.replace_html "test2", "<div id=\"2\">foo</div>"
627 assert_select_rjs "test" do |elements|
628 assert_equal 1, elements.size
632 assert_select_rjs "test2" do |elements|
633 assert_equal 1, elements.size
638 def test_feed_item_encoded
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/
664 # Test through encoded wrapper.
665 assert_select_encoded
do
666 assert_select
"encoded p", :count=>2, :text=>/Test/
668 # Use :root instead (recommended)
669 assert_select_encoded
do
670 assert_select
":root p", :count=>2, :text=>/Test/
673 assert_select
"description" do |elements
|
674 assert_select_encoded elements
[0] do
675 assert_select
"p", "Test 1"
677 assert_select_encoded elements
[1] do
678 assert_select
"p", "Test 2"
683 # Test that we only un-encode element itself.
684 assert_select
"channel item" do
685 assert_select_encoded
do
692 # Test assert_select_email
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"
707 def render_html(html
)
708 @controller.response_with
= html
712 def render_rjs(&block
)
713 @controller.response_with
&block
718 @controller.response_with
= xml