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

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

Instance Attribute Summary collapse

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).



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_tagObject

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_tagObject

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_errorObject

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_errorObject

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_selectorObject

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_messageObject

Returns

String

The failure message.



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

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

#id_for_errorObject

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_selectorObject

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.

Returns:

  • (Boolean)


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" + $!.message
        false
      end
    end.empty?
  else
    !@document.search(selector).empty?
  end
end

#negative_failure_messageObject

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 negative_failure_message
  @error_message || "expected following output to omit a #{tag_for_error} tag:\n#{@document}"
end

#selectorObject

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_errorObject

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