Class: Matcher::Xml

Inherits:
Object
  • Object
show all
Defined in:
lib/matcher/xml.rb

Constant Summary collapse

NOT_FOUND =
"[Not found]"
EXISTENCE =
"[Existence]"
UNMATCHED =
"[Unmatched]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lhs, custom_matchers = {}) ⇒ Xml

Returns a new instance of Xml.



14
15
16
17
18
# File 'lib/matcher/xml.rb', line 14

def initialize(lhs, custom_matchers = {})
  @lhs = parse(lhs)
  @custom_matchers = custom_matchers
  @results = {}
end

Instance Attribute Details

#custom_matchersObject (readonly)

Returns the value of attribute custom_matchers.



12
13
14
# File 'lib/matcher/xml.rb', line 12

def custom_matchers
  @custom_matchers
end

#lhsObject (readonly)

Returns the value of attribute lhs.



12
13
14
# File 'lib/matcher/xml.rb', line 12

def lhs
  @lhs
end

#resultsObject (readonly)

Returns the value of attribute results.



12
13
14
# File 'lib/matcher/xml.rb', line 12

def results
  @results
end

#rhsObject (readonly)

Returns the value of attribute rhs.



12
13
14
# File 'lib/matcher/xml.rb', line 12

def rhs
  @rhs
end

Instance Method Details

#match(actual) ⇒ Object



24
25
26
27
28
# File 'lib/matcher/xml.rb', line 24

def match(actual)
  @results.clear
  @rhs = parse(actual)
  compare(@lhs, @rhs)
end

#match_on(path, &blk) ⇒ Object



20
21
22
# File 'lib/matcher/xml.rb', line 20

def match_on(path, &blk)
  @custom_matchers[path] = blk
end

#matchesObject



43
44
45
# File 'lib/matcher/xml.rb', line 43

def matches
  results_that_are(true)
end

#mismatchesObject



47
48
49
# File 'lib/matcher/xml.rb', line 47

def mismatches
  results_that_are(false)
end

#record(path, result, expected, actual) ⇒ Object



30
31
32
33
34
35
# File 'lib/matcher/xml.rb', line 30

def record(path, result, expected, actual)
  # support 0 as true (for regex matches)
  r = !result || result.nil? ? false : true
  was_custom_matched = @custom_matchers[path] ? true : false
  @results[path] = OpenStruct.new(:result => r, :expected => expected, :actual => actual, :was_custom_matched => was_custom_matched)
end

#result_for(path) ⇒ Object



37
38
39
40
41
# File 'lib/matcher/xml.rb', line 37

def result_for(path)
  return "matched" if matches[path]
  return "mismatched" if mismatches[path]
  "unmatched"
end