Method: ApiMiniTester::TestStep#array_diff

Defined in:
lib/api_mini_tester/test_step.rb

#array_diff(a, b, path = nil, section = :body) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/api_mini_tester/test_step.rb', line 162

def array_diff(a, b, path = nil, section = :body)
  a.each do |a_item|
    if b.nil?
      add_result section, { result: false,
        name: "Response boby value: #{[path].join(".")}",
        desc: "Assert #{[path].join(".")} is empty" }
    elsif a_item.instance_of?(Hash)
      found = false
      b.each do |b_item|
        matching = true
        a_item.each_key do |k, v|
          matching = (b_item[k] == a_item[k]) if matching
        end
        found = true if matching
      end
      add_result section, { result: found,
                            name: "Response body value: #{[path].join(".")}",
                            desc: "Assert #{[path].join(".")} #{found ? 'contains' : 'does not contains'} #{a_item}" }
    elsif a_item.instance_of?(Array)
      # TODO: Add support for array of array it isn't so needed to compate so deep structures
    else
      add_result section, { result: b.include?(a_item),
                            name: "Response boby value: #{[path].join(".")}",
                            desc: "Assert #{[path].join(".")} #{b.include?(a_item) ? 'contains' : 'does not contains'} #{a_item}" }
    end
  end
end