Class: Merb::Test::Rspec::ViewMatchers::HasTag

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-core/test/matchers/view_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag, attributes = {}, &blk) ⇒ HasTag

Parameters

tag<~to_s>

The tag to look for.

attributes<Hash>

Attributes for the tag (see below).



154
155
156
157
158
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 154

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_errorObject

Returns

String

Class for the error tag.



251
252
253
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 251

def attributes_for_error
  @attributes.map{|a,v| " #{a}=\"#{v}\""}.join
end

#class_for_errorObject

Returns

String

Class for the error tag.



245
246
247
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 245

def class_for_error
  " class=\"#{@class}\"" unless @class.nil?
end

#class_selectorObject

Returns

String

Class selector for use in element queries.



207
208
209
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 207

def class_selector
  ".#{@class}" if @class
end

#failure_messageObject

Returns

String

The failure message.



213
214
215
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 213

def failure_message
  "expected following output to contain a #{tag_for_error} tag:\n#{@document}"
end

#id_for_errorObject

Returns

String

ID for the error tag.



239
240
241
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 239

def id_for_error
  " id=\"#{@id}\"" unless @id.nil?
end

#id_selectorObject

Returns

String

ID selector for use in element queries.



201
202
203
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 201

def id_selector
  "##{@id}" if @id
end

#inner_failure_messageObject

Returns

String

The failure message to be displayed in negative matches within the have_tag block.



233
234
235
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 233

def inner_failure_message
  "#{@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.

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 166

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_messageObject

Returns

String

The failure message to be displayed in negative matches.



219
220
221
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 219

def negative_failure_message
  "expected following output to omit a #{tag_for_error} tag:\n#{@document}"
end

#selectorObject

Returns

String

The complete selector for element queries.



190
191
192
193
194
195
196
197
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 190

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_errorObject

Returns

String

The tag used in failure messages.



225
226
227
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 225

def tag_for_error
  "#{inner_failure_message}<#{@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).



260
261
262
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 260

def with_tag(name, attrs={})
  @inner_has_tag = HasTag.new(name, attrs)
end