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.



17
18
19
20
21
# File 'lib/matcher/xml.rb', line 17

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.



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

def custom_matchers
  @custom_matchers
end

#lhsObject (readonly)

Returns the value of attribute lhs.



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

def lhs
  @lhs
end

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

#rhsObject (readonly)

Returns the value of attribute rhs.



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

def rhs
  @rhs
end

Instance Method Details

#evaluate(path, expected, actual) ⇒ Object



59
60
61
62
63
64
# File 'lib/matcher/xml.rb', line 59

def evaluate(path, expected, actual)
  custom_matcher = custom_matchers[path]
  match = custom_matcher ? evaluate_custom_matcher(custom_matcher, expected, actual) : expected == actual
  record(path, match, expected, actual)
  match
end

#match(actual) ⇒ Object



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

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

#match_on(path, options = {}, &blk) ⇒ Object Also known as: on

Raises:

  • (ArgumentError)


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

def match_on(path, options = {}, &blk)
  raise ArgumentError.new("Using block AND options is not supported for custom matching") if blk && !options.empty?
  excluding = options[:excluding]
  raise ArgumentError.new "'excluding' option must be a regular expression" if excluding && !excluding.kind_of?(Regexp)
  @custom_matchers[path] = blk || options
end

#matchesObject



51
52
53
# File 'lib/matcher/xml.rb', line 51

def matches
  results_that_are(true)
end

#mismatchesObject



55
56
57
# File 'lib/matcher/xml.rb', line 55

def mismatches
  results_that_are(false)
end

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



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

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



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

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