Class: Merb::Test::Rspec::ViewMatchers::HasTag
- Defined in:
- lib/merb-core/test/matchers/view_matchers.rb
Instance Attribute Summary collapse
-
#inner_has_tag ⇒ Object
Returns the value of attribute inner_has_tag.
-
#outer_has_tag ⇒ Object
Returns the value of attribute outer_has_tag.
Instance Method Summary collapse
-
#attributes_for_error ⇒ Object
Returns String:: Class for the error tag.
-
#class_for_error ⇒ Object
Returns String:: Class for the error tag.
-
#class_selector ⇒ Object
Returns String:: Class selector for use in element queries.
-
#failure_message ⇒ Object
Returns String:: The failure message.
-
#id_for_error ⇒ Object
Returns String:: ID for the error tag.
-
#id_selector ⇒ Object
Returns String:: ID selector for use in element queries.
-
#initialize(tag, attributes = {}, &blk) ⇒ HasTag
constructor
Parameters tag<~to_s>:: The tag to look for.
-
#matches?(stringlike, &blk) ⇒ Boolean
Parameters stringlike<Hpricot::Elem, StringIO, String>:: The thing to search in.
-
#negative_failure_message ⇒ Object
Returns String:: The failure message to be displayed in negative matches.
-
#selector ⇒ Object
Returns String:: The complete selector for element queries.
-
#tag_for_error ⇒ Object
Returns String:: The tag used in failure messages.
-
#with_tag(name, attrs = {}) ⇒ Object
Search for a child tag within a have_tag block.
Constructor Details
#initialize(tag, attributes = {}, &blk) ⇒ HasTag
Parameters
- tag<~to_s>
-
The tag to look for.
- attributes<Hash>
-
Attributes for the tag (see below).
176 177 178 179 180 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 176 def initialize(tag, attributes = {}, &blk) @tag, @attributes = tag, attributes @id, @class = @attributes.delete(:id), @attributes.delete(:class) @blk = blk end |
Instance Attribute Details
#inner_has_tag ⇒ Object
Returns the value of attribute inner_has_tag.
171 172 173 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 171 def inner_has_tag @inner_has_tag end |
#outer_has_tag ⇒ Object
Returns the value of attribute outer_has_tag.
171 172 173 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 171 def outer_has_tag @outer_has_tag end |
Instance Method Details
#attributes_for_error ⇒ Object
Returns
- String
-
Class for the error tag.
269 270 271 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 269 def attributes_for_error @attributes.map{|a,v| "[#{a}=\"#{v}\"]"}.join end |
#class_for_error ⇒ Object
Returns
- String
-
Class for the error tag.
263 264 265 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 263 def class_for_error ".#{@class}" unless @class.nil? end |
#class_selector ⇒ Object
Returns
- String
-
Class selector for use in element queries.
232 233 234 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 232 def class_selector ".#{@class}" if @class end |
#failure_message ⇒ Object
Returns
- String
-
The failure message.
238 239 240 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 238 def @error_message || "expected following output to contain a #{tag_for_error} tag:\n#{@document}" end |
#id_for_error ⇒ Object
Returns
- String
-
ID for the error tag.
257 258 259 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 257 def id_for_error "##{@id}" unless @id.nil? end |
#id_selector ⇒ Object
Returns
- String
-
ID selector for use in element queries.
226 227 228 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 226 def id_selector "##{@id}" if @id end |
#matches?(stringlike, &blk) ⇒ Boolean
Parameters
- stringlike<Hpricot::Elem, StringIO, String>
-
The thing to search in.
- &blk
-
An optional block for searching in child elements using with_tag.
Returns
- Boolean
-
True if there was at least one match.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 188 def matches?(stringlike, &blk) @document = case stringlike when Hpricot::Elem stringlike when StringIO Hpricot.parse(stringlike.string) else Hpricot.parse(stringlike) end @blk = blk unless blk.nil? unless @blk.nil? !@document.search(selector).select do |ele| begin @blk.call ele true rescue Spec::Expectations::ExpectationNotMetError @error_message = "#{tag_for_error}:\n" + $!. false end end.empty? else !@document.search(selector).empty? end end |
#negative_failure_message ⇒ Object
Returns
- String
-
The failure message to be displayed in negative matches.
244 245 246 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 244 def @error_message || "expected following output to omit a #{tag_for_error} tag:\n#{@document}" end |
#selector ⇒ Object
Returns
- String
-
The complete selector for element queries.
217 218 219 220 221 222 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 217 def selector @selector = @outer_has_tag ? @outer_has_tag.selector : '' @selector << "//#{@tag}#{id_selector}#{class_selector}" @selector << @attributes.map{|a, v| "[@#{a}=\"#{v}\"]"}.join end |
#tag_for_error ⇒ Object
Returns
- String
-
The tag used in failure messages.
250 251 252 253 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 250 def tag_for_error result = "#{@tag}#{id_for_error}#{class_for_error}#{attributes_for_error}" inner_has_tag ? result << " > #{inner_has_tag.tag_for_error}" : result end |
#with_tag(name, attrs = {}) ⇒ Object
Search for a child tag within a have_tag block.
Parameters
- tag<~to_s>
-
The tag to look for.
- attributes<Hash>
-
Attributes for the tag (see below).
278 279 280 281 282 283 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 278 def with_tag(name, attrs={}) @inner_has_tag = HasTag.new(name, attrs) @inner_has_tag.outer_has_tag = self @inner_has_tag end |