Module: Dyph::Support::SanityCheck

Extended by:
SanityCheck
Included in:
SanityCheck
Defined in:
lib/dyph/support/sanity_check.rb

Instance Method Summary collapse

Instance Method Details

#ensure_no_lost_data(left, base, right, final_result) ⇒ Object

rubocop:disable Metrics/AbcSize



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

def ensure_no_lost_data(left, base, right, final_result)
  result_word_map = {}
  final_result.each do |result_block|
    blocks = case result_block
      when Outcome::Resolved then result_block.result
      when Outcome::Conflicted then [result_block.left, result_block.right].flatten
      else raise "Unknown block type, #{result_block[:type]}"
    end
    count_blocks(blocks, result_word_map)
  end

  left_word_map, base_word_map, right_word_map = [left, base, right].map { |str| count_blocks(str) }

  # new words are words that are in left or right, but not in base
  new_left_words = subtract_words(left_word_map, base_word_map)
  new_right_words = subtract_words(right_word_map, base_word_map)

  # now make sure all new words are somewhere in the result
  missing_new_left_words = subtract_words(new_left_words, result_word_map)
  missing_new_right_words = subtract_words(new_right_words, result_word_map)

  if missing_new_left_words.any? || missing_new_right_words.any?
    raise BadMergeException.new(final_result)
  end
end