Class: EqualJsonArrayWithKey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, key) ⇒ EqualJsonArrayWithKey

Returns a new instance of EqualJsonArrayWithKey.



11
12
13
14
15
16
17
18
# File 'lib/eq_json_array_with_key.rb', line 11

def initialize(expected, key)
  unless key.is_a? Symbol
    raise "Key should be a symbol"
  end
  @expected = expected
  @key = key
  @messageGenerator = ArrayWithKeyMessageGen.new(self)
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual.



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

def actual
  @actual
end

#expectedObject

Returns the value of attribute expected.



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

def expected
  @expected
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#failure_messageObject



49
50
51
# File 'lib/eq_json_array_with_key.rb', line 49

def failure_message
  return @failureMessage
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/eq_json_array_with_key.rb', line 20

def matches?(actual)
  @actual = actual

  unless actual.length == expected.length
    @failureMessage = @messageGenerator.generateDifferentSizeArrays()
    return false
  end

  @expected.each() do |expectedItem|
    if expectedItem[@key].nil?
      @failureMessage = @messageGenerator.generateExpectedItemMissingKey(expectedItem)
      return false
    end

    actualItem = actual.find {|item| item[@key] == expectedItem[@key]}
    if actualItem.nil?
      @failureMessage = @messageGenerator.generateExpectedNotInActual(expectedItem)
      return false;
    end
    @eqJsonMatcher=EqualJson.new(expectedItem)
    if !@eqJsonMatcher.matches?(actualItem)
      @failureMessage = @messageGenerator.generateFailureMessage(expectedItem, @eqJsonMatcher.failure_message)
      return false;
    end
  end

  return true;
end