Class: ArrayWithKeyMessageGen

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

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ ArrayWithKeyMessageGen

Returns a new instance of ArrayWithKeyMessageGen.



5
6
7
8
# File 'lib/array_with_key_message_gen.rb', line 5

def initialize(matcher)
  @matcher = matcher
  @colorizer = EqJsonColorizer.new
end

Instance Method Details

#generateDifferentSizeArraysObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/array_with_key_message_gen.rb', line 26

def generateDifferentSizeArrays()
  objectsNotInExpected = getObjectsNotIn(@matcher.actual, @matcher.expected);
  objectsNotInActual = getObjectsNotIn(@matcher.expected, @matcher.actual);

  jsonErrorInfo = "Array size does not match. Expected #{@matcher.expected.length} actual #{@matcher.actual.length}\n"

  unless objectsNotInExpected.empty?
    jsonErrorInfo << "expected does not contain #{@matcher.key}s #{objectsNotInExpected}\n"
  end

  unless objectsNotInActual.empty?
    jsonErrorInfo << "actual does not contain #{@matcher.key}s #{objectsNotInActual}\n"
  end

  return jsonErrorInfo
end

#generateExpectedItemMissingKey(expectedItem) ⇒ Object



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

def generateExpectedItemMissingKey(expectedItem)
  return "Tester error expected item does not have key #{@matcher.key}.\n" +
      "Expected item: #{expectedItem.to_json}\n"
end

#generateExpectedNotInActual(expectedItem) ⇒ Object



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

def generateExpectedNotInActual(expectedItem)
  return "#{@matcher.key} #{expectedItem[@matcher.key]} not found in actual\n" +
      "Expected: #{expectedItem.to_json}\n"
end

#generateFailureMessage(expectedItem, eqFailureMessage) ⇒ Object



10
11
12
13
# File 'lib/array_with_key_message_gen.rb', line 10

def generateFailureMessage(expectedItem, eqFailureMessage)
  return "#{@matcher.key} #{expectedItem[@matcher.key]}\n" +
      eqFailureMessage
end

#getObjectsNotIn(array1, array2) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/array_with_key_message_gen.rb', line 43

def getObjectsNotIn(array1, array2)
  missing = []
  array1.each do |item1|
    item2 = array2.find {|item| item[@matcher.key] == item1[@matcher.key]}
    if item2.nil?
      missing.push(item1[@matcher.key])
    end
  end
  return missing
end