diff options
author | Timm <kaspth@gmail.com> | 2013-07-20 15:11:25 +0200 |
---|---|---|
committer | Timm <kaspth@gmail.com> | 2014-06-15 23:40:58 +0200 |
commit | ff0939a8f47bd64a762e4b5bf0122f7f0edb72e7 (patch) | |
tree | a9afccf7d20b2d8b6b15ebf6001a3237c724b95b /actionpack/lib/action_dispatch | |
parent | 63938f670bf74596e09ef6affbd850e4cff3f9e5 (diff) | |
download | rails-ff0939a8f47bd64a762e4b5bf0122f7f0edb72e7.tar.gz rails-ff0939a8f47bd64a762e4b5bf0122f7f0edb72e7.tar.bz2 rails-ff0939a8f47bd64a762e4b5bf0122f7f0edb72e7.zip |
Added filter_matches to reduce line count in assert_select.
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/selector.rb | 43 |
1 files changed, 19 insertions, 24 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb index 3c19b23fa6..11447b341d 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb @@ -161,7 +161,6 @@ module ActionDispatch @selected ||= nil filter = ArgumentFilter.new(@selected, response_from_page, args) - root = filter.root selector = filter.css_selector equals = filter.comparisons @@ -170,24 +169,10 @@ module ActionDispatch matches = root.css(selector) # If text/html, narrow down to those elements that match it. content_mismatch = nil - if match_with = equals[:text] - matches.delete_if do |match| - text = match.text - text.strip! unless NO_STRIP.include?(match.name) - text.sub!(/\A\n/, '') if match.name == "textarea" - content_matches?(match_with, text) do |error_message| - content_mismatch ||= error_message - end - end - elsif match_with = equals[:html] - matches.delete_if do |match| - html = match.to_s - html.strip! unless NO_STRIP.include?(match.name) - content_matches?(match_with, html) do |error_message| - content_mismatch ||= error_message - end - end + 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 ||= content_mismatch if matches.empty? @@ -322,13 +307,23 @@ module ActionDispatch end protected - def content_matches?(match_with, content) - if match_with.is_a?(Regexp) - content =~ match_with - else - content == match_with.to_s + def filter_matches(matches, options) + match_with = options[:text] || options[:html] + return unless match_with + + text_matches = options.has_key?(:text) + matches.delete_if do |match| + # Preserve html markup with to_s if not matching text elements + content = text_matches ? match.text : match.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 - yield sprintf("<%s> expected but was\n<%s>.", match_with, content) if block_given? end def response_from_page |