2 # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
3 # Under MIT and/or CC By license.
6 require "#{File.dirname(__FILE__)}/../abstract_unit"
7 require "#{File.dirname(__FILE__)}/fake_controllers"
10 unless defined?(ActionMailer)
12 $:.unshift(File.dirname(__FILE__) + "/../../../actionmailer/lib")
13 require 'action_mailer'
20 class AssertSelectTest < Test::Unit::TestCase
21 class AssertSelectController < ActionController::Base
22 def response_with=(content)
26 def response_with(&block)
31 render :text=>@content, :layout=>false, :content_type=>Mime::HTML
36 render :update do |page|
43 render :text=>@content, :layout=>false, :content_type=>Mime::XML
52 class AssertSelectMailer < ActionMailer::Base
54 recipients "test <test@test.host>"
57 part :content_type=>"text/html", :body=>html
61 AssertionFailedError = Test::Unit::AssertionFailedError
64 @controller = AssertSelectController.new
65 @request = ActionController::TestRequest.new
66 @response = ActionController::TestResponse.new
67 ActionMailer::Base.delivery_method = :test
68 ActionMailer::Base.perform_deliveries = true
69 ActionMailer::Base.deliveries = []
74 ActionMailer::Base.deliveries.clear
77 def assert_failure(message, &block)
78 e = assert_raises(AssertionFailedError, &block)
79 assert_match(message, e.message) if Regexp === message
80 assert_equal(message, e.message) if String === message
87 def test_assert_select
88 render_html %Q{<div id="1"></div><div id="2"></div>}
89 assert_select "div", 2
90 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
91 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 }
106 def test_equality_string_and_regexp
107 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
108 assert_nothing_raised { assert_select "div", "foo" }
109 assert_raises(AssertionFailedError) { assert_select "div", "bar" }
110 assert_nothing_raised { assert_select "div", :text=>"foo" }
111 assert_raises(AssertionFailedError) { assert_select "div", :text=>"bar" }
112 assert_nothing_raised { assert_select "div", /(foo|bar)/ }
113 assert_raises(AssertionFailedError) { assert_select "div", /foobar/ }
114 assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
115 assert_raises(AssertionFailedError) { assert_select "div", :text=>/foobar/ }
116 assert_raises(AssertionFailedError) { assert_select "p", :text=>/foobar/ }
120 def test_equality_of_html
121 render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
122 text = "\"This is not a big problem,\" he said."
123 html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
124 assert_nothing_raised { assert_select "p", text }
125 assert_raises(AssertionFailedError) { assert_select "p", html }
126 assert_nothing_raised { assert_select "p", :html=>html }
127 assert_raises(AssertionFailedError) { assert_select "p", :html=>text }
128 # No stripping for pre.
129 render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
130 text = "\n\"This is not a big problem,\" he said.\n"
131 html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
132 assert_nothing_raised { assert_select "pre", text }
133 assert_raises(AssertionFailedError) { assert_select "pre", html }
134 assert_nothing_raised { assert_select "pre", :html=>html }
135 assert_raises(AssertionFailedError) { assert_select "pre", :html=>text }
140 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
141 assert_nothing_raised { assert_select "div", 2 }
142 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
143 assert_select "div", 3
145 assert_nothing_raised { assert_select "div", 1..2 }
146 assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
147 assert_select "div", 3..4
149 assert_nothing_raised { assert_select "div", :count=>2 }
150 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
151 assert_select "div", :count=>3
153 assert_nothing_raised { assert_select "div", :minimum=>1 }
154 assert_nothing_raised { assert_select "div", :minimum=>2 }
155 assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
156 assert_select "div", :minimum=>3
158 assert_nothing_raised { assert_select "div", :maximum=>2 }
159 assert_nothing_raised { assert_select "div", :maximum=>3 }
160 assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
161 assert_select "div", :maximum=>1
163 assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
164 assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
165 assert_select "div", :minimum=>3, :maximum=>4
170 def test_substitution_values
171 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
172 assert_select "div#?", /\d+/ do |elements|
173 assert_equal 2, elements.size
175 assert_select "div" do
176 assert_select "div#?", /\d+/ do |elements|
177 assert_equal 2, elements.size
185 def test_nested_assert_select
186 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
187 assert_select "div" do |elements|
188 assert_equal 2, elements.size
189 assert_select elements[0], "#1"
190 assert_select elements[1], "#2"
192 assert_select "div" do
193 assert_select "div" do |elements|
194 assert_equal 2, elements.size
195 # Testing in a group is one thing
196 assert_select "#1,#2"
197 # Testing individually is another.
200 assert_select "#3", false
204 assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
205 assert_select "div" do
212 def test_assert_select_text_match
213 render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
214 assert_select "div" do
215 assert_nothing_raised { assert_select "div", "foo" }
216 assert_nothing_raised { assert_select "div", "bar" }
217 assert_nothing_raised { assert_select "div", /\w*/ }
218 assert_nothing_raised { assert_select "div", /\w*/, :count=>2 }
219 assert_raises(AssertionFailedError) { assert_select "div", :text=>"foo", :count=>2 }
220 assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
221 assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
222 assert_nothing_raised { assert_select "div", :html=>/\w*/ }
223 assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
224 assert_raises(AssertionFailedError) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
229 # With single result.
230 def test_assert_select_from_rjs_with_single_result
232 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
234 assert_select "div" do |elements|
235 assert elements.size == 2
239 assert_select "div#?", /\d+/ do |elements|
245 # With multiple results.
246 def test_assert_select_from_rjs_with_multiple_results
248 page.replace_html "test", "<div id=\"1\">foo</div>"
249 page.replace_html "test2", "<div id=\"2\">foo</div>"
251 assert_select "div" do |elements|
252 assert elements.size == 2
265 render_html %Q{<div id="1"></div><div id="2"></div>}
266 assert 2, css_select("div").size
267 assert 0, css_select("p").size
271 def test_nested_css_select
272 render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
273 assert_select "div#?", /\d+/ do |elements|
274 assert_equal 1, css_select(elements[0], "div").size
275 assert_equal 1, css_select(elements[1], "div").size
277 assert_select "div" do
278 assert_equal 2, css_select("div").size
279 css_select("div").each do |element|
280 # Testing as a group is one thing
281 assert !css_select("#1,#2").empty?
282 # Testing individually is another
283 assert !css_select("#1").empty?
284 assert !css_select("#2").empty?
291 def test_css_select_from_rjs_with_single_result
293 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
295 assert_equal 2, css_select("div").size
296 assert_equal 1, css_select("#1").size
297 assert_equal 1, css_select("#2").size
300 # With multiple results.
301 def test_css_select_from_rjs_with_multiple_results
303 page.replace_html "test", "<div id=\"1\">foo</div>"
304 page.replace_html "test2", "<div id=\"2\">foo</div>"
307 assert_equal 2, css_select("div").size
308 assert_equal 1, css_select("#1").size
309 assert_equal 1, css_select("#2").size
314 # Test assert_select_rjs.
318 # Test that we can pick up all statements in the result.
319 def test_assert_select_rjs_picks_up_all_statements
321 page.replace "test", "<div id=\"1\">foo</div>"
322 page.replace_html "test2", "<div id=\"2\">foo</div>"
323 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
336 # Test that we fail if there is nothing to pick.
337 def test_assert_select_rjs_fails_if_nothing_to_pick
339 assert_raises(AssertionFailedError) { assert_select_rjs }
342 def test_assert_select_rjs_with_unicode
343 # Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
345 page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
348 assert_select "#1", :text => "\343\203\201\343\202\261\343\203\203\343\203\210"
349 assert_select "#1", "\343\203\201\343\202\261\343\203\203\343\203\210"
350 assert_select "#1", Regexp.new("\343\203\201..\343\203\210",0,'U')
351 assert_raises(AssertionFailedError) { assert_select "#1", Regexp.new("\343\203\201.\343\203\210",0,'U') }
355 def test_assert_select_rjs_with_id
356 # Test that we can pick up all statements in the result.
358 page.replace "test1", "<div id=\"1\">foo</div>"
359 page.replace_html "test2", "<div id=\"2\">foo</div>"
360 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
362 assert_select_rjs "test1" do
363 assert_select "div", 1
366 assert_select_rjs "test2" do
367 assert_select "div", 1
370 assert_select_rjs "test3" do
371 assert_select "div", 1
374 assert_raises(AssertionFailedError) { assert_select_rjs "test4" }
378 def test_assert_select_rjs_for_replace
380 page.replace "test1", "<div id=\"1\">foo</div>"
381 page.replace_html "test2", "<div id=\"2\">foo</div>"
382 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
385 assert_select_rjs :replace do
386 assert_select "div", 1
389 assert_select_rjs :replace, "test1" do
390 assert_select "div", 1
393 assert_raises(AssertionFailedError) { assert_select_rjs :replace, "test2" }
395 assert_select_rjs :replace_html do
396 assert_select "div", 1
399 assert_select_rjs :replace_html, "test2" do
400 assert_select "div", 1
403 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
406 def test_assert_select_rjs_for_chained_replace
408 page['test1'].replace "<div id=\"1\">foo</div>"
409 page['test2'].replace_html "<div id=\"2\">foo</div>"
410 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
413 assert_select_rjs :chained_replace do
414 assert_select "div", 1
417 assert_select_rjs :chained_replace, "test1" do
418 assert_select "div", 1
421 assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" }
423 assert_select_rjs :chained_replace_html do
424 assert_select "div", 1
427 assert_select_rjs :chained_replace_html, "test2" do
428 assert_select "div", 1
431 assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
435 def test_assert_select_rjs_for_remove
440 assert_select_rjs :remove, "test1"
443 def test_assert_select_rjs_for_remove_ignores_block
448 assert_nothing_raised do
449 assert_select_rjs :remove, "test1" do
456 def test_assert_select_rjs_for_show
461 assert_select_rjs :show, "test1"
464 def test_assert_select_rjs_for_show_ignores_block
469 assert_nothing_raised do
470 assert_select_rjs :show, "test1" do
477 def test_assert_select_rjs_for_hide
482 assert_select_rjs :hide, "test1"
485 def test_assert_select_rjs_for_hide_ignores_block
490 assert_nothing_raised do
491 assert_select_rjs :hide, "test1" do
498 def test_assert_select_rjs_for_toggle
503 assert_select_rjs :toggle, "test1"
506 def test_assert_select_rjs_for_toggle_ignores_block
511 assert_nothing_raised do
512 assert_select_rjs :toggle, "test1" do
518 # Non-positioned insert.
519 def test_assert_select_rjs_for_nonpositioned_insert
521 page.replace "test1", "<div id=\"1\">foo</div>"
522 page.replace_html "test2", "<div id=\"2\">foo</div>"
523 page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
525 assert_select_rjs :insert_html do
526 assert_select "div", 1
529 assert_select_rjs :insert_html, "test3" do
530 assert_select "div", 1
533 assert_raises(AssertionFailedError) { assert_select_rjs :insert_html, "test1" }
537 def test_assert_select_rjs_for_positioned_insert
539 page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
540 page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
541 page.insert_html :before, "test3", "<div id=\"3\">foo</div>"
542 page.insert_html :after, "test4", "<div id=\"4\">foo</div>"
544 assert_select_rjs :insert, :top do
545 assert_select "div", 1
548 assert_select_rjs :insert, :bottom do
549 assert_select "div", 1
552 assert_select_rjs :insert, :before do
553 assert_select "div", 1
556 assert_select_rjs :insert, :after do
557 assert_select "div", 1
560 assert_select_rjs :insert_html do
561 assert_select "div", 4
565 # Simple selection from a single result.
566 def test_nested_assert_select_rjs_with_single_result
568 page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
571 assert_select_rjs "test" do |elements|
572 assert_equal 2, elements.size
578 # Deal with two results.
579 def test_nested_assert_select_rjs_with_two_results
581 page.replace_html "test", "<div id=\"1\">foo</div>"
582 page.replace_html "test2", "<div id=\"2\">foo</div>"
585 assert_select_rjs "test" do |elements|
586 assert_equal 1, elements.size
590 assert_select_rjs "test2" do |elements|
591 assert_equal 1, elements.size
597 def test_feed_item_encoded
618 assert_select "channel item description" do
619 # Test element regardless of wrapper.
620 assert_select_encoded do
621 assert_select "p", :count=>2, :text=>/Test/
623 # Test through encoded wrapper.
624 assert_select_encoded do
625 assert_select "encoded p", :count=>2, :text=>/Test/
627 # Use :root instead (recommended)
628 assert_select_encoded do
629 assert_select ":root p", :count=>2, :text=>/Test/
632 assert_select "description" do |elements|
633 assert_select_encoded elements[0] do
634 assert_select "p", "Test 1"
636 assert_select_encoded elements[1] do
637 assert_select "p", "Test 2"
642 # Test that we only un-encode element itself.
643 assert_select "channel item" do
644 assert_select_encoded do
652 # Test assert_select_email
655 def test_assert_select_email
656 assert_raises(AssertionFailedError) { assert_select_email {} }
657 AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
658 assert_select_email do
659 assert_select "div:root" do
660 assert_select "p:first-child", "foo"
661 assert_select "p:last-child", "bar"
668 def render_html(html)
669 @controller.response_with = html
673 def render_rjs(&block)
674 @controller.response_with &block
679 @controller.response_with = xml