Class: Merb::Test::Rspec::ViewMatchers::HaveXpath

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

Instance Method Summary collapse

Constructor Details

#initialize(expected, type) ⇒ HaveXpath

Returns a new instance of HaveXpath.



3
4
5
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 3

def initialize(expected, type)
  @expected, @type = expected, type
end

Instance Method Details

#failure_messageObject

Returns

String

The failure message.



41
42
43
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 41

def failure_message
  "expected following text to match xpath #{@expected}:\n#{@document}"
end

#matches?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 7

def matches?(stringlike)
  send("matches_#{@type}?", stringlike)
end

#matches_libxml?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 25

def matches_libxml?(stringlike)
  stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
  
  @document = case stringlike
  when LibXML::XML::Document, LibXML::XML::Node
    stringlike
  when StringIO
    LibXML::XML::HTMLParser.string(stringlike.string).parse
  else
    LibXML::XML::HTMLParser.string(stringlike).parse
  end
  !@document.find(@expected).empty?
end

#matches_rexml?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 11

def matches_rexml?(stringlike)
  stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
  
  @document = case stringlike
  when REXML::Document
    stringlike.root
  when REXML::Node
    stringlike
  when StringIO, String
    REXML::Document.new(stringlike).root
  end
  !REXML::XPath.match(@document, @expected).empty?
end

#negative_failure_messageObject

Returns

String

The failure message to be displayed in negative matches.



47
48
49
# File 'lib/merb-core/test/matchers/view_matchers.rb', line 47

def negative_failure_message
  "expected following text to not match xpath #{@expected}:\n#{@document}"
end