Class: DiffMatcher::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/diff_matcher/difference.rb

Direct Known Subclasses

AllMatcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, opts = {}) ⇒ Matcher

Returns a new instance of Matcher.



15
16
17
18
# File 'lib/diff_matcher/difference.rb', line 15

def initialize(expected, opts={})
  @expecteds = [expected]
  @expected_opts = {expected => opts}
end

Instance Attribute Details

#expectedsObject (readonly)

Returns the value of attribute expecteds.



9
10
11
# File 'lib/diff_matcher/difference.rb', line 9

def expecteds
  @expecteds
end

Class Method Details

.[](*expecteds) ⇒ Object



11
12
13
# File 'lib/diff_matcher/difference.rb', line 11

def self.[](*expecteds)
  expecteds.inject(nil) { |obj, e| obj ? obj | new(e) : new(e) }
end

Instance Method Details

#diff(actual, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/diff_matcher/difference.rb', line 33

def diff(actual, opts={})
  difs = []
  matched = @expecteds.any? { |e|
    d = DiffMatcher::Difference.new(expected(e, actual), actual, opts.merge(expected_opts(e)))
    unless d.matching?
      difs << [ d.dif_count, d.dif ]
    end
    d.matching?
  }
  unless matched
    count, dif = difs.sort.last
    dif
  end
end

#expected(e, actual) ⇒ Object



25
26
27
# File 'lib/diff_matcher/difference.rb', line 25

def expected(e, actual)
  e
end

#expected_opts(e) ⇒ Object



29
30
31
# File 'lib/diff_matcher/difference.rb', line 29

def expected_opts(e)
  @expected_opts.fetch(e, {})
end

#|(other) ⇒ Object



20
21
22
23
# File 'lib/diff_matcher/difference.rb', line 20

def |(other)
  #"(#{expecteds.join(",")}|#{other.expecteds.join(",")})"
  tap { @expecteds += other.expecteds }
end