diff options
author | Timm <kaspth@gmail.com> | 2013-07-30 12:31:58 +0200 |
---|---|---|
committer | Timm <kaspth@gmail.com> | 2014-06-16 21:03:57 +0200 |
commit | 99ac0cdcad5584fb1347866532749355dc597429 (patch) | |
tree | 6e09dbd84dc7b6fc8472d355df899b89c8fc6888 /actionpack/lib/action_dispatch | |
parent | 4ed784188d329315c79916242a5961c3c128b06c (diff) | |
download | rails-99ac0cdcad5584fb1347866532749355dc597429.tar.gz rails-99ac0cdcad5584fb1347866532749355dc597429.tar.bz2 rails-99ac0cdcad5584fb1347866532749355dc597429.zip |
Simplified assert_select further by moving match filtering into HTMLSelector select.
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/selector.rb | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb index dc18136be2..59bd6dc3b4 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb @@ -161,17 +161,7 @@ module ActionDispatch selector = HTMLSelector.new(@selected, response_from_page, args) matches = selector.select - equals = selector.comparisons - content_mismatch = nil - matches = filter_matches(matches, equals) do |mismatch| - content_mismatch ||= mismatch - end - - # Expecting foo found bar element only if found zero, not if - # found one but expecting two. - message = selector.message - message ||= content_mismatch if matches.empty? - assert_size_match!(matches.size, equals, selector.source, message) + assert_size_match!(matches.size, selector.comparisons, selector.source, message) # Set @selected to allow nested assert_select. # Can be nested several levels deep. @@ -290,26 +280,6 @@ module ActionDispatch end protected - def filter_matches(matches, options) - match_with = options[:text] || options[:html] - return matches unless match_with - - text_matches = options.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) - yield sprintf("<%s> expected but was\n<%s>.", match_with, content) - true - end - end - Nokogiri::XML::NodeSet.new(matches.document, remaining) - 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] @@ -360,7 +330,34 @@ module ActionDispatch alias :source :css_selector def select - root.css(css_selector, context) + filter root.css(css_selector, context) + end + + def filter(matches) + match_with = comparisons[:text] || comparisons[:html] + return matches unless 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) |