Class: Test::Diff::UnifiedDiffer

Inherits:
Differ
  • Object
show all
Defined in:
lib/test-unit-ext/diff.rb

Instance Method Summary collapse

Methods inherited from Differ

#initialize

Constructor Details

This class inherits a constructor from Test::Diff::Differ

Instance Method Details

#diff(options = {}) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/test-unit-ext/diff.rb', line 391

def diff(options={})
  groups = SequenceMatcher.new(@from, @to).grouped_operations
  return [] if groups.empty?
  return [] if same_content?(groups)

  show_context = options[:show_context]
  show_context = true if show_context.nil?
  result = ["--- #{options[:from_label]}".rstrip,
            "+++ #{options[:to_label]}".rstrip]
  groups.each do |operations|
    result << format_summary(operations, show_context)
    operations.each do |args|
      operation_tag, from_start, from_end, to_start, to_end = args
      case operation_tag
      when :replace
        result.concat(tag("-", @from[from_start...from_end]))
        result.concat(tag("+", @to[to_start...to_end]))
      when :delete
        result.concat(tag("-", @from[from_start...from_end]))
      when :insert
        result.concat(tag("+", @to[to_start...to_end]))
      when :equal
        result.concat(tag(" ", @from[from_start...from_end]))
      end
    end
  end
  result
end