Class: Test::Diff::Differ

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

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Differ

Returns a new instance of Differ.



128
129
130
131
# File 'lib/test-unit-ext/diff.rb', line 128

def initialize(from, to)
  @from = from
  @to = to
end

Instance Method Details

#compareObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/test-unit-ext/diff.rb', line 133

def compare
  result = []
  matcher = SequenceMatcher.new(@from, @to)
  matcher.operations.each do |args|
    tag, from_start, from_end, to_start, to_end = args
    case tag
    when :replace
      result.concat(fancy_replace(from_start, from_end, to_start, to_end))
    when :delete
      result.concat(tagging('-', @from[from_start..from_end]))
    when :insert
      result.concat(tagging('+', @to[to_start..to_end]))
    when :equal
      result.concat(tagging(' ', @from[from_start..from_end]))
    else
      raise "unknown tag: #{tag}"
    end
  end
  result
end