Class: Webrat::Matchers::HaveXpath
- Defined in:
- lib/webrat/core/matchers/have_xpath.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
- #add_attributes_conditions_to(query) ⇒ Object
- #add_content_condition_to(query) ⇒ Object
- #add_options_conditions_to(query) ⇒ Object
-
#failure_message ⇒ Object
Returns String:: The failure message.
-
#initialize(expected, options = {}, &block) ⇒ HaveXpath
constructor
A new instance of HaveXpath.
- #matches(stringlike) ⇒ Object
- #matches?(stringlike, &block) ⇒ Boolean
-
#negative_failure_message ⇒ Object
Returns String:: The failure message to be displayed in negative matches.
- #nokogiri_matches(stringlike) ⇒ Object
- #query ⇒ Object
- #rexml_matches(stringlike) ⇒ Object
Constructor Details
#initialize(expected, options = {}, &block) ⇒ HaveXpath
Returns a new instance of HaveXpath.
8 9 10 11 12 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 8 def initialize(expected, = {}, &block) @expected = expected @options = @block = block end |
Instance Method Details
#add_attributes_conditions_to(query) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 73 def add_attributes_conditions_to(query) attribute_conditions = [] @options.each do |key, value| next if [:content, :count].include?(key) attribute_conditions << "@#{key} = #{xpath_escape(value)}" end if attribute_conditions.any? query << "[#{attribute_conditions.join(' and ')}]" end end |
#add_content_condition_to(query) ⇒ Object
86 87 88 89 90 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 86 def add_content_condition_to(query) if @options[:content] query << "[contains(., #{xpath_escape(@options[:content])})]" end end |
#add_options_conditions_to(query) ⇒ Object
68 69 70 71 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 68 def (query) add_attributes_conditions_to(query) add_content_condition_to(query) end |
#failure_message ⇒ Object
Returns
- String
-
The failure message.
98 99 100 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 98 def "expected following text to match xpath #{@expected}:\n#{@document}" end |
#matches(stringlike) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 27 def matches(stringlike) if Webrat.configuration.parse_with_nokogiri? nokogiri_matches(stringlike) else rexml_matches(stringlike) end end |
#matches?(stringlike, &block) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 14 def matches?(stringlike, &block) @block ||= block matched = matches(stringlike) @block.call(matched) if @block if @options[:count] matched.size == @options[:count] else matched.any? end end |
#negative_failure_message ⇒ Object
Returns
- String
-
The failure message to be displayed in negative matches.
104 105 106 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 104 def "expected following text to not match xpath #{@expected}:\n#{@document}" end |
#nokogiri_matches(stringlike) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 55 def nokogiri_matches(stringlike) if Nokogiri::XML::NodeSet === stringlike @query = query.gsub(%r'//', './/') else @query = query end (@query) @document = Webrat::XML.document(stringlike) @document.xpath(*@query) end |
#query ⇒ Object
92 93 94 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 92 def query @expected end |
#rexml_matches(stringlike) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 35 def rexml_matches(stringlike) if REXML::Node === stringlike || Array === stringlike @query = query.map { |q| q.gsub(%r'//', './/') } else @query = query end (@query) @document = Webrat.rexml_document(stringlike) @query.map do |q| if @document.is_a?(Array) @document.map { |d| REXML::XPath.match(d, q) } else REXML::XPath.match(@document, q) end end.flatten.compact end |