Added deprecation warning for invalid selectors and skipping assertions.

This commit is contained in:
Timm 2013-09-22 21:08:30 +02:00
parent 535a3b6af7
commit 9ef95a7019
2 changed files with 35 additions and 5 deletions

@ -1,3 +1,5 @@
require 'active_support/deprecation'
module ActionView
module Assertions
NO_STRIP = %w{pre script style textarea}
@ -27,6 +29,7 @@ module SelectorAssertions
# Returns an empty Nokogiri::XML::NodeSet if no match is found.
#
# The selector may be a CSS selector expression (String).
# css_select will return nil if called with an invalid css selector.
#
# # Selects all div tags
# divs = css_select("div")
@ -52,9 +55,11 @@ def css_select(*args)
root = args.size == 1 ? response_from_page : args.shift
selector = args.first
root.css(selector).tap do |matches|
if matches.empty? && root.matches?(selector)
return Nokogiri::XML::NodeSet.new(root.document, [root])
catch_invalid_selector do
root.css(selector).tap do |matches|
if matches.empty? && root.matches?(selector)
return Nokogiri::XML::NodeSet.new(root.document, [root])
end
end
end
end
@ -91,6 +96,7 @@ def css_select(*args)
# 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 will return nil if called with an invalid css selector.
#
# assert_select "div:match('id', ?)", /\d+/
#
@ -154,8 +160,12 @@ def assert_select(*args, &block)
selector = HTMLSelector.new(@selected, response_from_page, args)
matches = selector.select
assert_size_match!(matches.size, selector.equality_tests, selector.source, selector.message)
matches = nil
catch_invalid_selector do
matches = selector.select
assert_size_match!(matches.size, selector.equality_tests, selector.source, selector.message)
end
# Set @selected to allow nested assert_select.
# Can be nested several levels deep.
@ -278,6 +288,15 @@ def assert_select_email(&block)
end
protected
def catch_invalid_selector
begin
yield
rescue Nokogiri::CSS::SyntaxError => e
ActiveSupport::Deprecation.warn("You are using an invalid CSS selector and the assertion was not run. Please review it.\n#{e}")
end
end
# +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]

@ -266,6 +266,17 @@ def test_nested_css_select
end
end
# testing invalid selectors
def test_assert_select_with_invalid_selector
render_html '<a href="http://example.com">hello</a>'
assert_nil assert_select("[href=http://example.com]")
end
def test_css_select_with_invalid_selector
render_html '<a href="http://example.com">hello</a>'
assert_nil css_select("[href=http://example.com]")
end
def test_feed_item_encoded
render_xml <<-EOF
<rss version="2.0">