Class: Grntest::DiffReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/grntest/diff-reporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, actual) ⇒ DiffReporter

Returns a new instance of DiffReporter.



21
22
23
24
# File 'lib/grntest/diff-reporter.rb', line 21

def initialize(expected, actual)
  @expected = expected
  @actual = actual
end

Instance Method Details

#reportObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grntest/diff-reporter.rb', line 26

def report
  expected_lines = @expected.lines.collect(&:chomp)
  actual_lines = @actual.lines.collect(&:chomp)
  diffs = Diff::LCS.diff(expected_lines,
                         actual_lines)
  return if diffs.empty?

  report_expected_label("(expected)")
  report_actual_label("(actual)")

  context_n_lines = 3
  previous_hunk = nil
  file_length_diff = 0
  diffs.each do |diff|
    begin
      hunk = Diff::LCS::Hunk.new(expected_lines,
                                 actual_lines,
                                 diff,
                                 context_n_lines,
                                 file_length_diff)
      next if previous_hunk.nil?
      next if hunk.merge(previous_hunk)

      report_hunk(previous_hunk)
    ensure
      previous_hunk = hunk
    end
  end
  report_hunk(previous_hunk)
end