Class: RoadForest::Testing::ListEquivalence

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/test-support/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ ListEquivalence

Returns a new instance of ListEquivalence.



31
32
33
# File 'lib/roadforest/test-support/matchers.rb', line 31

def initialize(expected)
  @expected = expected
end

Instance Method Details

#failure_message_for_shouldObject



57
58
59
60
61
62
# File 'lib/roadforest/test-support/matchers.rb', line 57

def failure_message_for_should
  "expected [\n  #{@actual.map(&:to_s).join("\n  ")}\n] " +
    "to have the same elements as [\n  #{@expected.map(&:to_s).join("\n  ")}\n]\n\n" +
    "missing: [\n  #{missing.map(&:to_s).join("\n  ")}\n]\n" +
    "surplus: [\n  #{surplus.map(&:to_s).join("\n  ")}]"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/roadforest/test-support/matchers.rb', line 52

def matches?(actual)
  @actual = actual
  missing.empty? and surplus.empty?
end

#missingObject



44
45
46
# File 'lib/roadforest/test-support/matchers.rb', line 44

def missing
  @missing ||= subtract(@expected, @actual)
end

#subtract(one, other) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/roadforest/test-support/matchers.rb', line 35

def subtract(one, other)
  sorted = one.sort_by{|stmt| stmt.to_a}
  one.find_all do |expected_stmt|
    not other.any? do |actual_stmt|
      actual_stmt.eql? expected_stmt
    end
  end
end

#surplusObject



48
49
50
# File 'lib/roadforest/test-support/matchers.rb', line 48

def surplus
  @surplus ||= subtract(@actual, @expected)
end