Tue Aug 7 16:56:48 EST 2007 Matt Palmer * Add a clone of the assert_select tests, for better coverage. NOTE: This test suite DOES NOT WORK YET. diff -rN -u old-testsuite/test/abstract_unit.rb new-testsuite/test/abstract_unit.rb --- old-testsuite/test/abstract_unit.rb 1970-01-01 10:00:00.000000000 +1000 +++ new-testsuite/test/abstract_unit.rb 2008-10-20 23:31:06.295880671 +1100 @@ -0,0 +1,45 @@ +RAILS_ENV = 'test' + +require 'test/unit' +begin + require 'hpricot' +rescue LoadError => e + begin + require 'rubygems' + require_gem 'hpricot' + rescue LoadError => e + puts "It's hard to test a hpricot test_helper without hpricot." + puts "Please install hpricot ('gem install hpricot') and try again." + exit 0 + end +end + +begin + require 'action_controller' + require 'action_controller/test_process' +rescue LoadError => e + begin + require 'rubygems' + require 'action_controller' + require 'action_controller/test_process' + rescue LoadError => e + puts "Cannot find actionpack, which is needed to run the test suite." + puts "Please install it as a gem or somewhere in your load path, and try again." + exit 0 + end +end + +# Setup basic routes so our tests don't explode +ActionController::Routing::Routes.draw do |map| + map.connect ":controller/:action/:id" +end + +load File.dirname(__FILE__) + '/../init.rb' + +# Define this ourselves to avoid faffing around with including all of +# activesupport. +class Symbol + def to_proc + Proc.new { |*args| args.shift.__send__(self, *args) } + end +end diff -rN -u old-testsuite/test/assert_elements_test.rb new-testsuite/test/assert_elements_test.rb --- old-testsuite/test/assert_elements_test.rb 1970-01-01 10:00:00.000000000 +1000 +++ new-testsuite/test/assert_elements_test.rb 2008-10-20 23:31:06.295880671 +1100 @@ -0,0 +1,249 @@ +#-- +# Copyright (c) 2006 Assaf Arkin (http://labnotes.org) +# Under MIT and/or CC By license. +#++ + +require "#{File.dirname(__FILE__)}/abstract_unit" + +unless defined?(ActionMailer) + begin + $:.unshift(File.dirname(__FILE__) + "/../../../actionmailer/lib") + require 'action_mailer' + rescue LoadError + require 'rubygems' + gem 'actionmailer' + end +end + +class AssertElementsTest < Test::Unit::TestCase + class AssertElementsController < ActionController::Base + def response_with=(content) + @content = content + end + + def response_with(&block) + @update = block + end + + def html() + render :text=>@content, :layout=>false, :content_type=>Mime::HTML + @content = nil + end + + def rescue_action(e) + raise e + end + end + + class AssertElementsMailer < ActionMailer::Base + def test(html) + recipients "test " + from "test@test.host" + subject "Test e-mail" + part :content_type=>"text/html", :body=>html + end + end + + AssertionFailedError = Test::Unit::AssertionFailedError + + def setup + @controller = AssertElementsController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + ActionMailer::Base.delivery_method = :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + end + + + def teardown + ActionMailer::Base.deliveries.clear + end + + def assert_failure(message, &block) + e = assert_raises(AssertionFailedError, &block) + assert_match(message, e.message) if Regexp === message + assert_equal(message, e.message) if String === message + end + + # + # Test assert elements. + # + + def test_assert_elements + render_html %Q{
} + assert_elements "div", 2 + assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_elements "div", 3 } + assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_elements "p" } + end + + + def test_equality_true_false + render_html %Q{
} + assert_nothing_raised { assert_elements "div" } + assert_raises(AssertionFailedError) { assert_elements "p" } + assert_nothing_raised { assert_elements "div", true } + assert_raises(AssertionFailedError) { assert_elements "p", true } + assert_raises(AssertionFailedError) { assert_elements "div", false } + assert_nothing_raised { assert_elements "p", false } + end + + + def test_equality_string_and_regexp + render_html %Q{
foo
foo
} + assert_nothing_raised { assert_elements "div", "foo" } + assert_raises(AssertionFailedError) { assert_elements "div", "bar" } + assert_nothing_raised { assert_elements "div", :text=>"foo" } + assert_raises(AssertionFailedError) { assert_elements "div", :text=>"bar" } + assert_nothing_raised { assert_elements "div", /(foo|bar)/ } + assert_raises(AssertionFailedError) { assert_elements "div", /foobar/ } + assert_nothing_raised { assert_elements "div", :text=>/(foo|bar)/ } + assert_raises(AssertionFailedError) { assert_elements "div", :text=>/foobar/ } + assert_raises(AssertionFailedError) { assert_elements "p", :text=>/foobar/ } + end + + + def test_equality_of_html + render_html %Q{

\n"This is not a big problem," he said.\n

} + text = "\"This is not a big problem,\" he said." + html = "\"This is not a big problem,\" he said." + assert_nothing_raised { assert_elements "p", text } + assert_raises(AssertionFailedError) { assert_elements "p", html } + assert_nothing_raised { assert_elements "p", :html=>html } + assert_raises(AssertionFailedError) { assert_elements "p", :html=>text } + # No stripping for pre. + render_html %Q{
\n"This is not a big problem," he said.\n
} + text = "\n\"This is not a big problem,\" he said.\n" + html = "\n\"This is not a big problem,\" he said.\n" + assert_nothing_raised { assert_elements "pre", text } + assert_raises(AssertionFailedError) { assert_elements "pre", html } + assert_nothing_raised { assert_elements "pre", :html=>html } + assert_raises(AssertionFailedError) { assert_elements "pre", :html=>text } + end + + + def test_counts + render_html %Q{
foo
foo
} + assert_nothing_raised { assert_elements "div", 2 } + assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do + assert_elements "div", 3 + end + assert_nothing_raised { assert_elements "div", 1..2 } + assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do + assert_elements "div", 3..4 + end + assert_nothing_raised { assert_elements "div", :count=>2 } + assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do + assert_elements "div", :count=>3 + end + assert_nothing_raised { assert_elements "div", :minimum=>1 } + assert_nothing_raised { assert_elements "div", :minimum=>2 } + assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do + assert_elements "div", :minimum=>3 + end + assert_nothing_raised { assert_elements "div", :maximum=>2 } + assert_nothing_raised { assert_elements "div", :maximum=>3 } + assert_failure(/Expected at most 1 element matching \"div\", found 2/) do + assert_elements "div", :maximum=>1 + end + assert_nothing_raised { assert_elements "div", :minimum=>1, :maximum=>2 } + assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do + assert_elements "div", :minimum=>3, :maximum=>4 + end + end + + + def test_substitution_values + render_html %Q{
foo
foo
} + assert_elements "div#?", /\d+/ do |elements| + assert_equal 2, elements.size + end + assert_elements "div" do + assert_elements "div#?", /\d+/ do |elements| + assert_equal 2, elements.size + assert_elements "#1" + assert_elements "#2" + end + end + end + + + def test_nested_assert_elements + render_html %Q{
foo
foo
} + assert_elements "div" do |elements| + assert_equal 2, elements.size + assert_elements elements[0], "#1" + assert_elements elements[1], "#2" + end + assert_elements "div" do + assert_elements "div" do |elements| + assert_equal 2, elements.size + # Testing in a group is one thing + assert_elements "#1,#2" + # Testing individually is another. + assert_elements "#1" + assert_elements "#2" + assert_elements "#3", false + end + end + + assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do + assert_elements "div" do + assert_elements "#4" + end + end + end + + + def test_assert_elements_text_match + render_html %Q{
foo
bar
} + assert_elements "div" do + assert_nothing_raised { assert_elements "div", "foo" } + assert_nothing_raised { assert_elements "div", "bar" } + assert_nothing_raised { assert_elements "div", /\w*/ } + assert_nothing_raised { assert_elements "div", /\w*/, :count=>2 } + assert_raises(AssertionFailedError) { assert_elements "div", :text=>"foo", :count=>2 } + assert_nothing_raised { assert_elements "div", :html=>"bar" } + assert_nothing_raised { assert_elements "div", :html=>"bar" } + assert_nothing_raised { assert_elements "div", :html=>/\w*/ } + assert_nothing_raised { assert_elements "div", :html=>/\w*/, :count=>2 } + assert_raises(AssertionFailedError) { assert_elements "div", :html=>"foo", :count=>2 } + end + end + + # + # Test elements. + # + + + def test_elements + render_html %Q{
} + assert 2, elements("div").size + assert 0, elements("p").size + end + + + def test_nested_elements + render_html %Q{
foo
foo
} + assert_elements "div#?", /\d+/ do |elements| + assert_equal 1, elements(elements[0], "div").size + assert_equal 1, elements(elements[1], "div").size + end + assert_elements "div" do + assert_equal 2, elements("div").size + elements("div").each do |element| + # Testing as a group is one thing + assert !elements("#1,#2").empty? + # Testing individually is another + assert !elements("#1").empty? + assert !elements("#2").empty? + end + end + end + + protected + def render_html(html) + @controller.response_with = html + get :html + end +end diff -rN -u old-testsuite/test/hpricot_test_helper_test.rb new-testsuite/test/hpricot_test_helper_test.rb --- old-testsuite/test/hpricot_test_helper_test.rb 2008-10-20 23:31:06.287877297 +1100 +++ new-testsuite/test/hpricot_test_helper_test.rb 2008-10-20 23:31:06.291856425 +1100 @@ -1,28 +1,4 @@ -RAILS_ENV = 'test' - -require 'test/unit' -begin - require 'hpricot' -rescue LoadError => e - begin - require 'rubygems' - require_gem 'hpricot' - rescue LoadError => e - puts "It's hard to test a hpricot test_helper without hpricot." - puts "Please install hpricot ('gem install hpricot') and try again." - exit 0 - end -end - -load File.dirname(__FILE__) + '/../init.rb' - -# Define this ourselves to avoid faffing around with including all of -# activesupport. -class Symbol - def to_proc - Proc.new { |*args| args.shift.__send__(self, *args) } - end -end +require File.dirname(__FILE__) + '/abstract_unit' class HpricotTestHelperTest < Test::Unit::TestCase class AssertElementsController