From 0f4ec0bdb6b909aa8b487d7e65be8833987f03bd Mon Sep 17 00:00:00 2001 From: Timm Date: Sun, 23 Mar 2014 13:47:33 +0100 Subject: [PATCH] Introduce nodeset method. --- .../testing/assertions/selector_assertions.rb | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/lib/rails/dom/testing/assertions/selector_assertions.rb b/lib/rails/dom/testing/assertions/selector_assertions.rb index a0e69da..2e34cdc 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions.rb @@ -62,11 +62,7 @@ def css_select(*args) selector = args.first 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 + nodeset(root).css(selector) end end @@ -229,20 +225,11 @@ def count_description(min, max, count) #:nodoc: # 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" + if !element && !@selected + raise ArgumentError, "Element is required when called from a nonnested assert_select" end - content = elements.map do |elem| + content = nodeset(element || @selected).map do |elem| elem.children.select(&:cdata?).map(&:content) end.join @@ -321,11 +308,7 @@ def determine_root_from(args, previous_selection = nil) args.shift # remove the root, so selector is the first argument possible_root elsif previous_selection - if previous_selection.is_a?(Array) - Nokogiri::XML::NodeSet.new(previous_selection[0].document, previous_selection) - else - previous_selection - end + previous_selection else document_root_element end @@ -341,8 +324,16 @@ def nest_selection(selection) @selected = old_selected end end + + def nodeset(node) + if node.is_a?(Nokogiri::XML::NodeSet) + node + else + Nokogiri::XML::NodeSet.new(node.document, [node]) + end + end end end end end -end \ No newline at end of file +end