Class: ArrayHelper

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

Class Method Summary collapse

Class Method Details

.compare(expected, actual) ⇒ Object

Compare two arrays.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/helpers/array_helper.rb', line 6

def self.compare(expected, actual)
  sdiff = Diff::LCS.sdiff(expected, actual)
  changes = {}
  action_words = {
      '!' => 'changed',
      '+' => 'unexpected',
      '-' => 'missing',
      '=' => 'unchanged'
  }
  sdiff.each_with_index do |change, i|
    action_word = action_words.fetch(change.action)
    key = "change_#{i}"
    attrs = %W/
        action=#{action_word}
        old_pos=#{change.old_position}
        old_ele=#{change.old_element}
        new_pos=#{change.old_position}
        new_ele=#{change.old_element}
    /
    value = attrs.join(' ')
    changes.store(key, value)
  end
  {:sdiff => changes}
end