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.



219
220
221
# File 'lib/roadforest/test-support/matchers.rb', line 219

def initialize(expected)
  @expected = expected
end

Instance Method Details

#failure_message_for_shouldObject



244
245
246
247
248
249
# File 'lib/roadforest/test-support/matchers.rb', line 244

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)


239
240
241
242
# File 'lib/roadforest/test-support/matchers.rb', line 239

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

#missingObject



231
232
233
# File 'lib/roadforest/test-support/matchers.rb', line 231

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

#subtract(one, other) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/roadforest/test-support/matchers.rb', line 223

def subtract(one, other)
  one.find_all do |expected_stmt|
    not other.any? do |actual_stmt|
      actual_stmt.eql? expected_stmt
    end
  end
end

#surplusObject



235
236
237
# File 'lib/roadforest/test-support/matchers.rb', line 235

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