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



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_errorObject

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_errorObject

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_selectorObject

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_messageObject

Returns

String

The failure message.



183
184
185
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 183

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.



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_selectorObject

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_messageObject

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


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_messageObject

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



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_errorObject

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
  "#{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).



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