Class: Merb::Test::Rspec::ViewMatchers::HasTag
- Defined in:
- lib/merb-core/test/matchers/view_matchers.rb
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.
-
#inner_failure_message ⇒ Object
Returns String:: The failure message to be displayed in negative matches within the have_tag block.
-
#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).
124 125 126 127 128 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 124 def initialize(tag, attributes = {}, &blk) @tag, @attributes = tag, attributes @id, @class = @attributes.delete(:id), @attributes.delete(:class) @blk = blk end |
Instance Method Details
#attributes_for_error ⇒ Object
Returns
- String
-
Class for the error tag.
221 222 223 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 221 def attributes_for_error @attributes.map{|a,v| " #{a}=\"#{v}\""}.join end |
#class_for_error ⇒ Object
Returns
- String
-
Class for the error tag.
215 216 217 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 215 def class_for_error " class=\"#{@class}\"" unless @class.nil? end |
#class_selector ⇒ Object
Returns
- String
-
Class selector for use in element queries.
177 178 179 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 177 def class_selector ".#{@class}" if @class end |
#failure_message ⇒ Object
Returns
- String
-
The failure message.
183 184 185 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 183 def "expected following output to contain a #{tag_for_error} tag:\n#{@document}" end |
#id_for_error ⇒ Object
Returns
- String
-
ID for the error tag.
209 210 211 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 209 def id_for_error " id=\"#{@id}\"" unless @id.nil? end |
#id_selector ⇒ Object
Returns
- String
-
ID selector for use in element queries.
171 172 173 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 171 def id_selector "##{@id}" if @id end |
#inner_failure_message ⇒ Object
Returns
- String
-
The failure message to be displayed in negative matches within the have_tag block.
203 204 205 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 203 def "#{@inner_has_tag.tag_for_error} tag within a " unless @inner_has_tag.nil? 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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 136 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| @blk.call ele true end.empty? else !@document.search(selector).empty? end end |
#negative_failure_message ⇒ Object
Returns
- String
-
The failure message to be displayed in negative matches.
189 190 191 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 189 def "expected following output to omit a #{tag_for_error} tag:\n#{@document}" end |
#selector ⇒ Object
Returns
- String
-
The complete selector for element queries.
160 161 162 163 164 165 166 167 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 160 def selector @selector = "//#{@tag}#{id_selector}#{class_selector}" @selector << @attributes.map{|a, v| "[@#{a}=\"#{v}\"]"}.join @selector << @inner_has_tag.selector unless @inner_has_tag.nil? @selector end |
#tag_for_error ⇒ Object
Returns
- String
-
The tag used in failure messages.
195 196 197 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 195 def tag_for_error "#{}<#{@tag}#{id_for_error}#{class_for_error}#{attributes_for_error}>" 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).
230 231 232 |
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 230 def with_tag(name, attrs={}) @inner_has_tag = HasTag.new(name, attrs) end |