Class: Megatest::PatienceDiff::Differ

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Differ

Returns a new instance of Differ.



300
301
302
303
# File 'lib/megatest/patience_diff.rb', line 300

def initialize(color)
  @formatter = Formatter.new(self, color)
  @matcher = SequenceMatcher.new
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



298
299
300
# File 'lib/megatest/patience_diff.rb', line 298

def matcher
  @matcher
end

Instance Method Details

#diff_sequences(left, right) ⇒ Object

Generate a unified diff of the data specified. The left and right values should be strings, or any other indexable, sortable data. File names and timestamps do not affect the diff algorithm, but are used in the header text.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/megatest/patience_diff.rb', line 307

def diff_sequences(left, right)
  hunks = @matcher.grouped_opcodes(left, right)

  return nil if hunks.empty?

  lines = []
  first_hunk = true
  hunks.each do |opcodes|
    if first_hunk
      first_hunk = false
    else
      lines << @formatter.render_hunk_marker(opcodes)
    end
    lines << @formatter.render_hunk(left, right, opcodes)
  end
  lines.flatten!
  lines.compact!
  lines
end

#diff_text(left, right) ⇒ Object



327
328
329
330
331
332
333
334
335
# File 'lib/megatest/patience_diff.rb', line 327

def diff_text(left, right)
  left_lines = left.lines
  right_lines = right.lines

  left_lines[-1] += "\n" unless left_lines.empty? || left_lines.last.end_with?("\n")
  right_lines[-1] += "\n" unless right_lines.empty? || right_lines.last.end_with?("\n")

  diff_sequences(left_lines, right_lines)
end