class Test::Unit::Diff::UnifiedDiffer

Public Instance Methods

diff(options={}) click to toggle source
# File test-unit-3.6.2/lib/test/unit/diff.rb, line 627
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

Private Instance Methods

define_line?(line) click to toggle source
# File test-unit-3.6.2/lib/test/unit/diff.rb, line 696
def define_line?(line)
  /\A(?:[_a-zA-Z$]|\s*(?:class|module|def)\b)/ =~ line
end
find_interesting_line(from_start, to_start, predicate) click to toggle source
# File test-unit-3.6.2/lib/test/unit/diff.rb, line 682
def find_interesting_line(from_start, to_start, predicate)
  from_index = from_start
  to_index = to_start
  while from_index >= 0 or to_index >= 0
    [@from[from_index], @to[to_index]].each do |line|
      return line if line and __send__(predicate, line)
    end

    from_index -= 1
    to_index -= 1
  end
  nil
end
format_summary(operations, show_context) click to toggle source
# File test-unit-3.6.2/lib/test/unit/diff.rb, line 666
def format_summary(operations, show_context)
  _, first_from_start, _, first_to_start, _ = operations[0]
  _, _, last_from_end, _, last_to_end = operations[-1]
  summary = "@@ -%d,%d +%d,%d @@" % [first_from_start + 1,
                                     last_from_end - first_from_start,
                                     first_to_start + 1,
                                     last_to_end - first_to_start,]
  if show_context
    interesting_line = find_interesting_line(first_from_start,
                                             first_to_start,
                                             :define_line?)
    summary << " #{interesting_line}" if interesting_line
  end
  summary
end
same_content?(groups) click to toggle source
# File test-unit-3.6.2/lib/test/unit/diff.rb, line 657
def same_content?(groups)
  return false if groups.size != 1
  group = groups[0]
  return false if group.size != 1
  tag, from_start, from_end, to_start, to_end = group[0]

  tag == :equal and [from_start, from_end] == [to_start, to_end]
end