aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorTimm <kaspth@gmail.com>2013-08-02 17:01:54 +0200
committerTimm <kaspth@gmail.com>2014-06-16 21:04:01 +0200
commit95c517b6d6c13bfff2a020b2a29ec5c9bacfebf3 (patch)
tree8e3622c9126cdb3e5688b4aec1d02a1290ab6b5b /actionpack
parent9f73f9f38cd572cdb7bee42b0ef250c8bbd91cc7 (diff)
downloadrails-95c517b6d6c13bfff2a020b2a29ec5c9bacfebf3.tar.gz
rails-95c517b6d6c13bfff2a020b2a29ec5c9bacfebf3.tar.bz2
rails-95c517b6d6c13bfff2a020b2a29ec5c9bacfebf3.zip
Moved Dom and Selector assertions from ActionDispatch to ActionView.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/test_case.rb1
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions.rb4
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/dom.rb75
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/selector.rb468
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb3
-rw-r--r--actionpack/test/controller/assert_select_test.rb350
6 files changed, 7 insertions, 894 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index e6695ffc90..89176736c8 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -673,6 +673,7 @@ module ActionController
included do
include ActionController::TemplateAssertions
include ActionDispatch::Assertions
+ include ActionView::Assertions
class_attribute :_controller_class
setup :setup_controller_request_and_response
end
diff --git a/actionpack/lib/action_dispatch/testing/assertions.rb b/actionpack/lib/action_dispatch/testing/assertions.rb
index 8e6b93ba67..5f923abbf9 100644
--- a/actionpack/lib/action_dispatch/testing/assertions.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions.rb
@@ -1,16 +1,12 @@
module ActionDispatch
module Assertions
- autoload :DomAssertions, 'action_dispatch/testing/assertions/dom'
autoload :ResponseAssertions, 'action_dispatch/testing/assertions/response'
autoload :RoutingAssertions, 'action_dispatch/testing/assertions/routing'
- autoload :SelectorAssertions, 'action_dispatch/testing/assertions/selector'
extend ActiveSupport::Concern
- include DomAssertions
include ResponseAssertions
include RoutingAssertions
- include SelectorAssertions
end
end
diff --git a/actionpack/lib/action_dispatch/testing/assertions/dom.rb b/actionpack/lib/action_dispatch/testing/assertions/dom.rb
index 4a05e4aee3..53d1b198e6 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/dom.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/dom.rb
@@ -1,74 +1,3 @@
-require 'loofah'
+require 'active_support/deprecation'
-module ActionDispatch
- module Assertions
- module DomAssertions
- # \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
- #
- # # assert that the referenced method generates the appropriate HTML string
- # assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
- def assert_dom_equal(expected, actual, message = nil)
- expected_dom, actual_dom = doms_from_strings(expected, actual)
- message ||= "Expected: #{expected_dom}\nActual: #{actual_dom}"
- assert compare_doms(expected_dom, actual_dom), message
- end
-
- # The negated form of +assert_dom_equal+.
- #
- # # assert that the referenced method does not generate the specified HTML string
- # assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
- def assert_dom_not_equal(expected, actual, message = nil)
- expected_dom, actual_dom = doms_from_strings(expected, actual)
- message ||= "Expected: #{expected_dom}\nActual: #{actual_dom}"
- assert_not compare_doms(expected_dom, actual_dom), message
- end
-
- protected
- # +doms_from_strings+ creates a Loofah::HTML::DocumentFragment for every string in strings
- def doms_from_strings(*strings)
- strings.map { |str| Loofah.fragment(str) }
- end
-
- # +compare_doms+ takes two doms loops over all their children and compares each child via +equal_children?+
- def compare_doms(expected, actual)
- expected.children.each_with_index do |child, i|
- return false unless equal_children?(child, actual.children[i])
- end
- true
- end
-
- # +equal_children?+ compares children according to their type
- # Determines further comparison via said type
- # i.e. element node children with equal names has their attributes compared using +attributes_are_equal?+
- def equal_children?(child, other_child)
- return false unless child.type == other_child.type
-
- case child.type
- when Nokogiri::XML::Node::ELEMENT_NODE
- child.name == other_child.name && attributes_are_equal?(child, other_child)
- else
- child.to_s == other_child.to_s
- end
- end
-
- # +attributes_are_equal?+ sorts elements attributes by name and compares
- # each attribute by calling +equal_attribute?+
- # If those are +true+ the attributes are considered equal
- def attributes_are_equal?(element, other_element)
- first_nodes = element.attribute_nodes.sort_by { |a| a.name }
- other_nodes = other_element.attribute_nodes.sort_by { |a| a.name }
-
- return false unless first_nodes.size == other_nodes.size
- first_nodes.each_with_index do |attr, i|
- return false unless equal_attribute?(attr, other_nodes[i])
- end
- true
- end
-
- # +equal_attribute?+ compares attributes by their name and value
- def equal_attribute?(attr, other_attr)
- attr.name == other_attr.name && attr.value == other_attr.value
- end
- end
- end
-end
+ActiveSupport::Deprecation.warn("ActionDispatch::Assertions::DomAssertions has been moved to ActionView. You can find it in action_view/testing/assertions/dom.rb.") \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
index 1fda4fb42a..588da094d5 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb
@@ -1,467 +1,3 @@
-require 'action_view/vendor/html-scanner'
-require 'active_support/core_ext/object/inclusion'
-require 'loofah'
+require 'active_support/deprecation'
-#--
-# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
-# Under MIT and/or CC By license.
-#++
-
-module ActionDispatch
- module Assertions
- NO_STRIP = %w{pre script style textarea}
-
- # Adds the +assert_select+ method for use in Rails functional
- # test cases, which can be used to make assertions on the response HTML of a controller
- # action. You can also call +assert_select+ within another +assert_select+ to
- # make assertions on elements selected by the enclosing assertion.
- #
- # Use +css_select+ to select elements without making an assertions, either
- # from the response HTML or elements selected by the enclosing assertion.
- #
- # In addition to HTML responses, you can make the following assertions:
- #
- # * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
- # * +assert_select_email+ - Assertions on the HTML body of an e-mail.
- module SelectorAssertions
- # Select and return all matching elements.
- #
- # If called with a single argument, uses that argument as a selector
- # to match all elements of the current page. Returns an empty
- # Nokogiri::XML::NodeSet if no match is found.
- #
- # If called with two arguments, uses the first argument as the root
- # element and the second argument as the selector. Attempts to match the
- # root element and any of its children.
- # Returns an empty Nokogiri::XML::NodeSet if no match is found.
- #
- # The selector may be a CSS selector expression (String).
- #
- # # Selects all div tags
- # divs = css_select("div")
- #
- # # Selects all paragraph tags and does something interesting
- # pars = css_select("p")
- # pars.each do |par|
- # # Do something fun with paragraphs here...
- # end
- #
- # # Selects all list items in unordered lists
- # items = css_select("ul>li")
- #
- # # Selects all form tags and then all inputs inside the form
- # forms = css_select("form")
- # forms.each do |form|
- # inputs = css_select(form, "input")
- # ...
- # end
- def css_select(*args)
- raise ArgumentError, "you at least need a selector" if args.empty?
-
- if args.first.is_a?(String)
- root, selector = response_from_page, args.first
- else
- root, selector = args.shift, args.first
- end
-
- root.css(selector).tap do |matches|
- if matches.empty? && root.matches?(selector)
- return Nokogiri::XML::NodeSet.new(root.document, [root])
- end
- end
- end
-
- # An assertion that selects elements and makes one or more equality tests.
- #
- # If the first argument is an element, selects all matching elements
- # starting from (and including) that element and all its children in
- # depth-first order.
- #
- # If no element is specified, calling +assert_select+ selects from the
- # response HTML unless +assert_select+ is called from within an +assert_select+ block.
- #
- # When called with a block +assert_select+ passes an array of selected elements
- # to the block. Calling +assert_select+ from the block, with no element specified,
- # runs the assertion on the complete set of elements selected by the enclosing assertion.
- # Alternatively the array may be iterated through so that +assert_select+ can be called
- # separately for each element.
- #
- #
- # ==== Example
- # If the response contains two ordered lists, each with four list elements then:
- # assert_select "ol" do |elements|
- # elements.each do |element|
- # assert_select element, "li", 4
- # end
- # end
- #
- # will pass, as will:
- # assert_select "ol" do
- # assert_select "li", 8
- # end
- #
- # The selector may be a CSS selector expression (String) or an expression
- # with substitution values (Array).
- # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
- #
- # assert_select "div:match('id', ?)", /\d+/
- #
- # === Equality Tests
- #
- # The equality test may be one of the following:
- # * <tt>true</tt> - Assertion is true if at least one element selected.
- # * <tt>false</tt> - Assertion is true if no element selected.
- # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
- # one element matches the string or regular expression.
- # * <tt>Integer</tt> - Assertion is true if exactly that number of
- # elements are selected.
- # * <tt>Range</tt> - Assertion is true if the number of selected
- # elements fit the range.
- # If no equality test specified, the assertion is true if at least one
- # element selected.
- #
- # To perform more than one equality tests, use a hash with the following keys:
- # * <tt>:text</tt> - Narrow the selection to elements that have this text
- # value (string or regexp).
- # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
- # content (string or regexp).
- # * <tt>:count</tt> - Assertion is true if the number of selected elements
- # is equal to this value.
- # * <tt>:minimum</tt> - Assertion is true if the number of selected
- # elements is at least this value.
- # * <tt>:maximum</tt> - Assertion is true if the number of selected
- # elements is at most this value.
- #
- # If the method is called with a block, once all equality tests are
- # evaluated the block is called with an array of all matched elements.
- #
- # # At least one form element
- # assert_select "form"
- #
- # # Form element includes four input fields
- # assert_select "form input", 4
- #
- # # Page title is "Welcome"
- # assert_select "title", "Welcome"
- #
- # # Page title is "Welcome" and there is only one title element
- # assert_select "title", {count: 1, text: "Welcome"},
- # "Wrong title or more than one title element"
- #
- # # Page contains no forms
- # assert_select "form", false, "This page must contain no forms"
- #
- # # Test the content and style
- # assert_select "body div.header ul.menu"
- #
- # # Use substitution values
- # assert_select "ol>li:match('id', ?)", /item-\d+/
- #
- # # All input fields in the form have a name
- # assert_select "form input" do
- # assert_select ":match('name', ?)", /.+/ # Not empty
- # end
- def assert_select(*args, &block)
- @selected ||= nil
-
- selector = HTMLSelector.new(@selected, response_from_page, args)
-
- matches = selector.select
- assert_size_match!(matches.size, selector.comparisons, selector.source, selector.message)
-
- # Set @selected to allow nested assert_select.
- # Can be nested several levels deep.
- if block_given? && !matches.empty?
- begin
- in_scope, @selected = @selected, matches
- yield matches
- ensure
- @selected = in_scope
- end
- end
-
- matches
- end
-
- def count_description(min, max, count) #:nodoc:
- pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')}
-
- if min && max && (max != min)
- "between #{min} and #{max} elements"
- elsif min && max && max == min && count
- "exactly #{count} #{pluralize['element', min]}"
- elsif min && !(min == 1 && max == 1)
- "at least #{min} #{pluralize['element', min]}"
- elsif max
- "at most #{max} #{pluralize['element', max]}"
- end
- end
-
- # Extracts the content of an element, treats it as encoded HTML and runs
- # nested assertion on it.
- #
- # You typically call this method within another assertion to operate on
- # all currently selected elements. You can also pass an element or array
- # of elements.
- #
- # The content of each element is un-encoded, and wrapped in the root
- # element +encoded+. It then calls the block with all un-encoded elements.
- #
- # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
- # assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
- # # Select each entry item and then the title item
- # assert_select "entry>title" do
- # # Run assertions on the encoded title elements
- # assert_select_encoded do
- # assert_select "b"
- # end
- # end
- # end
- #
- #
- # # Selects all paragraph tags from within the description of an RSS feed
- # assert_select "rss[version=2.0]" do
- # # Select description element of each feed item.
- # assert_select "channel>item>description" do
- # # Run assertions on the encoded elements.
- # assert_select_encoded do
- # assert_select "p"
- # end
- # end
- # end
- def assert_select_encoded(element = nil, &block)
- case element
- when Array
- elements = element
- when Nokogiri::XML::Node
- elements = [element]
- when nil
- unless elements = @selected
- raise ArgumentError, "First argument is optional, but must be called from a nested assert_select"
- end
- else
- raise ArgumentError, "Argument is optional, and may be node or array of nodes"
- end
-
- content = elements.map do |elem|
- elem.children.select(&:cdata?).map(&:content)
- end.join
- selected = Loofah.fragment(content)
-
- begin
- old_selected, @selected = @selected, selected
- if content.empty?
- yield selected
- else
- assert_select ":root", &block
- end
- ensure
- @selected = old_selected
- end
- end
-
- # Extracts the body of an email and runs nested assertions on it.
- #
- # You must enable deliveries for this assertion to work, use:
- # ActionMailer::Base.perform_deliveries = true
- #
- # assert_select_email do
- # assert_select "h1", "Email alert"
- # end
- #
- # assert_select_email do
- # items = assert_select "ol>li"
- # items.each do
- # # Work with items here...
- # end
- # end
- def assert_select_email(&block)
- deliveries = ActionMailer::Base.deliveries
- assert !deliveries.empty?, "No e-mail in delivery list"
-
- deliveries.each do |delivery|
- (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
- if part["Content-Type"].to_s =~ /^text\/html\W/
- root = Loofah.fragment(part.body.to_s)
- assert_select root, ":root", &block
- end
- end
- end
- end
-
- protected
- # +equals+ must contain :minimum, :maximum and :count keys
- def assert_size_match!(size, equals, css_selector, message = nil)
- min, max, count = equals[:minimum], equals[:maximum], equals[:count]
-
- message ||= %(Expected #{count_description(min, max, count)} matching "#{css_selector}", found #{size}.)
- if count
- assert_equal size, count, message
- else
- assert_operator size, :>=, min, message if min
- assert_operator size, :<=, max, message if max
- end
- end
-
- # +html_document+ is used in testing/integration.rb
- def html_document
- @html_document ||= if @response.content_type =~ /xml$/
- Loofah.xml_document(@response.body)
- else
- Loofah.document(@response.body)
- end
- end
-
- def response_from_page
- html_document.root
- end
-
- class HTMLSelector #:nodoc:
- attr_accessor :root, :css_selector, :comparisons, :message
-
- alias :source :css_selector
-
- def initialize(selected, page, args)
- @selected, @page = selected, page
-
- # Start with optional element followed by mandatory selector.
- @root = determine_root_from(args.first)
-
- # First or second argument is the selector
- selector = @css_selector_is_second_argument ? args.shift(2).last : args.shift
- @css_selector = selector_from(selector, args)
-
- # Next argument is used for equality tests.
- @comparisons = comparisons_from(args.shift)
-
- # Last argument is the message we use if the assertion fails.
- @message = args.shift
-
- if args.shift
- raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type"
- end
- end
-
- def select
- filter root.css(css_selector, context)
- end
-
- def filter(matches)
- match_with = comparisons[:text] || comparisons[:html]
- return matches if matches.empty? || !match_with
-
- content_mismatch = nil
- text_matches = comparisons.has_key?(:text)
-
- remaining = matches.reject do |match|
- # Preserve markup with to_s for html elements
- content = text_matches ? match.text : match.children.to_s
-
- content.strip! unless NO_STRIP.include?(match.name)
- content.sub!(/\A\n/, '') if text_matches && match.name == "textarea"
-
- unless match_with.is_a?(Regexp) ? (content =~ match_with) : (content == match_with.to_s)
- content_mismatch ||= sprintf("<%s> expected but was\n<%s>.", match_with, content)
- true
- end
- end
-
- # Expecting foo found bar element only if found zero, not if
- # found one but expecting two.
- self.message ||= content_mismatch if remaining.empty?
-
- Nokogiri::XML::NodeSet.new(matches.document, remaining)
- end
-
- def determine_root_from(root_or_selector)
- @css_selector_is_second_argument = false
- if root_or_selector == nil
- raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
- elsif root_or_selector.is_a?(Nokogiri::XML::Node) || root_or_selector.is_a?(Nokogiri::XML::NodeSet)
- # First argument is a node (tag or text, but also HTML root),
- # so we know what we're selecting from.
- @css_selector_is_second_argument = true
-
- root_or_selector
- elsif @selected
- if @selected.is_a?(Array)
- doc = @selected.empty? ? @page.document : @selected[0].document
- @selected = Nokogiri::XML::NodeSet.new(doc, @selected)
- end
- @selected
- else
- @page
- end
- end
-
- def selector_from(selector, substitution_values)
- unless selector.is_a? String
- raise ArgumentError, "Expecting a selector as the first argument"
- end
- context.substitute!(selector, substitution_values)
- end
-
- def comparisons_from(comparator)
- comparisons = {}
- case comparator
- when Hash
- comparisons = comparator
- when String, Regexp
- comparisons[:text] = comparator
- when Integer
- comparisons[:count] = comparator
- when Range
- comparisons[:minimum] = comparator.begin
- comparisons[:maximum] = comparator.end
- when FalseClass
- comparisons[:count] = 0
- when NilClass, TrueClass
- comparisons[:minimum] = 1
- else raise ArgumentError, "I don't understand what you're trying to match"
- end
-
- # By default we're looking for at least one match.
- if comparisons[:count]
- comparisons[:minimum] = comparisons[:maximum] = comparisons[:count]
- else
- comparisons[:minimum] ||= 1
- end
- comparisons
- end
-
- def context
- @context ||= SubstitutionContext.new
- end
-
- class SubstitutionContext
- def initialize(substitute = '?')
- @substitute = substitute
- @regexes = []
- end
-
- def add_regex(regex)
- # Nokogiri doesn't like arbitrary values without quotes, hence inspect.
- return regex.inspect unless regex.is_a?(Regexp)
- @regexes.push(regex)
- last_id.to_s # avoid implicit conversions of Fixnum to String
- end
-
- def last_id
- @regexes.count - 1
- end
-
- def match(matches, attribute, id)
- matches.find_all { |node| node[attribute] =~ @regexes[id] }
- end
-
- def substitute!(selector, values)
- while !values.empty? && selector.index(@substitute)
- selector.sub!(@substitute, add_regex(values.shift))
- end
- selector
- end
- end
- end
- end
- end
-end
+ActiveSupport::Deprecation.warn("ActionDispatch::Assertions::SelectorAssertions has been moved to ActionView. You can find it in action_view/testing/assertions/selector.rb.") \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index af3bc26691..96f12b95a9 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -2,6 +2,7 @@ require 'stringio'
require 'uri'
require 'active_support/core_ext/kernel/singleton_class'
require 'active_support/core_ext/object/try'
+require 'action_view/testing/assertions'
require 'rack/test'
module ActionDispatch
@@ -313,7 +314,7 @@ module ActionDispatch
end
module Runner
- include ActionDispatch::Assertions
+ include ActionDispatch::Assertions, ActionView::Assertions
def app
@app ||= nil
diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb
deleted file mode 100644
index 42d6bb4432..0000000000
--- a/actionpack/test/controller/assert_select_test.rb
+++ /dev/null
@@ -1,350 +0,0 @@
-# encoding: utf-8
-#--
-# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
-# Under MIT and/or CC By license.
-#++
-
-require 'abstract_unit'
-require 'controller/fake_controllers'
-
-require 'action_mailer'
-require 'action_view'
-
-ActionMailer::Base.send(:include, ActionView::Layouts)
-ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
-
-class AssertSelectTest < ActionController::TestCase
- Assertion = ActiveSupport::TestCase::Assertion
-
- class AssertSelectMailer < ActionMailer::Base
- def test(html)
- mail :body => html, :content_type => "text/html",
- :subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>"
- end
- end
-
- class AssertMultipartSelectMailer < ActionMailer::Base
- def test(options)
- mail :subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>" do |format|
- format.text { render :text => options[:text] }
- format.html { render :text => options[:html] }
- end
- end
- end
-
- class AssertSelectController < 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 xml()
- render :text=>@content, :layout=>false, :content_type=>Mime::XML
- @content = nil
- end
- end
-
- tests AssertSelectController
-
- def setup
- super
- @old_delivery_method = ActionMailer::Base.delivery_method
- @old_perform_deliveries = ActionMailer::Base.perform_deliveries
- ActionMailer::Base.delivery_method = :test
- ActionMailer::Base.perform_deliveries = true
- end
-
- def teardown
- super
- ActionMailer::Base.delivery_method = @old_delivery_method
- ActionMailer::Base.perform_deliveries = @old_perform_deliveries
- ActionMailer::Base.deliveries.clear
- end
-
- def assert_failure(message, &block)
- e = assert_raise(Assertion, &block)
- assert_match(message, e.message) if Regexp === message
- assert_equal(message, e.message) if String === message
- end
-
- #
- # Test assert select.
- #
-
- def test_assert_select
- render_html %Q{<div id="1"></div><div id="2"></div>}
- assert_select "div", 2
- assert_failure(/\AExpected at least 1 element matching \"p\", found 0\.$/) { assert_select "p" }
- end
-
- def test_equality_integer
- render_html %Q{<div id="1"></div><div id="2"></div>}
- assert_failure(/\AExpected exactly 3 elements matching \"div\", found 2\.$/) { assert_select "div", 3 }
- assert_failure(/\AExpected exactly 0 elements matching \"div\", found 2\.$/) { assert_select "div", 0 }
- end
-
- def test_equality_true_false
- render_html %Q{<div id="1"></div><div id="2"></div>}
- assert_nothing_raised { assert_select "div" }
- assert_raise(Assertion) { assert_select "p" }
- assert_nothing_raised { assert_select "div", true }
- assert_raise(Assertion) { assert_select "p", true }
- assert_raise(Assertion) { assert_select "div", false }
- assert_nothing_raised { assert_select "p", false }
- end
-
- def test_equality_false_message
- render_html %Q{<div id="1"></div><div id="2"></div>}
- assert_failure(/\AExpected exactly 0 elements matching \"div\", found 2\.$/) { assert_select "div", false }
- end
-
- def test_equality_string_and_regexp
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
- assert_nothing_raised { assert_select "div", "foo" }
- assert_raise(Assertion) { assert_select "div", "bar" }
- assert_failure(/\A<bar> expected but was\n<foo>\.$/) { assert_select "div", "bar" }
- assert_nothing_raised { assert_select "div", :text=>"foo" }
- assert_raise(Assertion) { assert_select "div", :text=>"bar" }
- assert_nothing_raised { assert_select "div", /(foo|bar)/ }
- assert_raise(Assertion) { assert_select "div", /foobar/ }
- assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
- assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
- assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
- end
-
- def test_equality_of_html
- render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
- text = "\"This is not a big problem,\" he said."
- html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
- assert_nothing_raised { assert_select "p", text }
- assert_raise(Assertion) { assert_select "p", html }
- assert_nothing_raised { assert_select "p", :html=>html }
- assert_raise(Assertion) { assert_select "p", :html=>text }
- assert_failure(/\A<#{text}> expected but was\n<#{html}>\.$/) { assert_select "p", :html=>text }
- # No stripping for pre.
- render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
- text = "\n\"This is not a big problem,\" he said.\n"
- html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
- assert_nothing_raised { assert_select "pre", text }
- assert_raise(Assertion) { assert_select "pre", html }
- assert_nothing_raised { assert_select "pre", :html=>html }
- assert_raise(Assertion) { assert_select "pre", :html=>text }
- end
-
- def test_strip_textarea
- render_html %Q{<textarea>\n\nfoo\n</textarea>}
- assert_select "textarea", "\nfoo\n"
- render_html %Q{<textarea>\nfoo</textarea>}
- assert_select "textarea", "foo"
- end
-
- def test_counts
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
- assert_nothing_raised { assert_select "div", 2 }
- assert_failure(/\AExpected exactly 3 elements matching \"div\", found 2\.$/) do
- assert_select "div", 3
- end
- assert_nothing_raised { assert_select "div", 1..2 }
- assert_failure(/\AExpected between 3 and 4 elements matching \"div\", found 2\.$/) do
- assert_select "div", 3..4
- end
- assert_nothing_raised { assert_select "div", :count=>2 }
- assert_failure(/\AExpected exactly 3 elements matching \"div\", found 2\.$/) do
- assert_select "div", :count=>3
- end
- assert_nothing_raised { assert_select "div", :minimum=>1 }
- assert_nothing_raised { assert_select "div", :minimum=>2 }
- assert_failure(/\AExpected at least 3 elements matching \"div\", found 2\.$/) do
- assert_select "div", :minimum=>3
- end
- assert_nothing_raised { assert_select "div", :maximum=>2 }
- assert_nothing_raised { assert_select "div", :maximum=>3 }
- assert_failure(/\AExpected at most 1 element matching \"div\", found 2\.$/) do
- assert_select "div", :maximum=>1
- end
- assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
- assert_failure(/\AExpected between 3 and 4 elements matching \"div\", found 2\.$/) do
- assert_select "div", :minimum=>3, :maximum=>4
- end
- end
-
- def test_substitution_values
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
- assert_select "div:match('id', ?)", /\d+/ do |elements|
- assert_equal 2, elements.size
- end
- assert_select "div" do
- assert_select ":match('id', ?)", /\d+/ do |elements|
- assert_equal 2, elements.size
- assert_select "#1"
- assert_select "#2"
- end
- end
- end
-
- def test_nested_assert_select
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
- assert_select "div" do |elements|
- assert_equal 2, elements.size
- assert_select elements, "#1"
- assert_select elements, "#2"
- end
- assert_select "div" do
- assert_select "div" do |elements|
- assert_equal 2, elements.size
- # Testing in a group is one thing
- assert_select "#1,#2"
- # Testing individually is another.
- assert_select "#1"
- assert_select "#2"
- assert_select "#3", false
- end
- end
-
- assert_failure(/\AExpected at least 1 element matching \"#4\", found 0\.$/) do
- assert_select "div" do
- assert_select "#4"
- end
- end
- end
-
- def test_assert_select_text_match
- render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
- assert_select "div" do
- assert_nothing_raised { assert_select "div", "foo" }
- assert_nothing_raised { assert_select "div", "bar" }
- assert_nothing_raised { assert_select "div", /\w*/ }
- assert_nothing_raised { assert_select "div", :text => /\w*/, :count=>2 }
- assert_raise(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
- assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
- assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
- assert_nothing_raised { assert_select "div", :html=>/\w*/ }
- assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
- assert_raise(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
- end
- end
-
- def test_select_with_xml_namespace_attributes
- skip "Nokogiri doesn't recognize this the xmlns:special as a namespace. Perhaps it's because it isn't on the root node?"
- render_html %Q{<link xmlns:special="http://nowhere.com"></link>}
- assert_nothing_raised { assert_select %(special|link) }
- end
-
- #
- # Test css_select.
- #
-
- def test_css_select
- render_html %Q{<div id="1"></div><div id="2"></div>}
- assert_equal 2, css_select("div").size
- assert_equal 0, css_select("p").size
- end
-
- def test_nested_css_select
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
- assert_select "div:match('id', ?)", /\d+/ do |elements|
- assert_equal 1, css_select(elements[0], "div").size
- assert_equal 1, css_select(elements[1], "div").size
- end
- assert_select "div" do
- assert_equal 2, css_select("div").size
- css_select("div").each do |element|
- # Testing as a group is one thing
- assert !css_select("#1,#2").empty?
- # Testing individually is another
- assert !css_select("#1").empty?
- assert !css_select("#2").empty?
- end
- end
- end
-
- def test_feed_item_encoded
- render_xml <<-EOF
-<rss version="2.0">
- <channel>
- <item>
- <description>
- <![CDATA[
- <p>Test 1</p>
- ]]>
- </description>
- </item>
- <item>
- <description>
- <![CDATA[
- <p>Test 2</p>
- ]]>
- </description>
- </item>
- </channel>
-</rss>
-EOF
- assert_select "channel item description" do
-
- assert_select_encoded do
- assert_select "p", :count=>2, :text=>/Test/
- end
-
- # Test individually.
- assert_select "description" do |elements|
- assert_select_encoded elements[0] do
- assert_select "p", "Test 1"
- end
- assert_select_encoded elements[1] do
- assert_select "p", "Test 2"
- end
- end
- end
-
- # Test that we only un-encode element itself.
- assert_select "channel item" do
- assert_select_encoded do
- assert_select "p", 0
- end
- end
- end
-
- #
- # Test assert_select_email
- #
-
- def test_assert_select_email
- assert_raise(Assertion) { assert_select_email {} }
- AssertSelectMailer.test("<div><p>foo</p><p>bar</p></div>").deliver
- assert_select_email do
- assert_select "div:root" do
- assert_select "p:first-child", "foo"
- assert_select "p:last-child", "bar"
- end
- end
- end
-
- def test_assert_select_email_multipart
- AssertMultipartSelectMailer.test(:html => "<div><p>foo</p><p>bar</p></div>", :text => 'foo bar').deliver
- assert_select_email do
- assert_select "div:root" do
- assert_select "p:first-child", "foo"
- assert_select "p:last-child", "bar"
- end
- end
- end
-
- protected
- def render_html(html)
- @controller.response_with = html
- get :html
- end
-
- def render_xml(xml)
- @controller.response_with = xml
- get :xml
- end
-end