class Mutest::Diff

Class to create diffs from source code

Constants

ADDITION
DELETION
NEWLINE

Public Class Methods

build(old, new) click to toggle source

Build new object from source strings

@param [String] old @param [String] new

@return [Diff]

# File lib/mutest/diff.rb, line 45
def self.build(old, new)
  new(lines(old), lines(new))
end

Private Class Methods

colorize_line(line) click to toggle source

Colorized a unified diff line

@param [String] line

@return [String]

# File lib/mutest/diff.rb, line 101
def self.colorize_line(line)
  case line[0]
  when ADDITION
    Color::GREEN
  when DELETION
    Color::RED
  else
    Color::NONE
  end.format(line)
end
lines(source) click to toggle source

Break up source into lines

@param [String] source

@return [Array<String>]

# File lib/mutest/diff.rb, line 54
def self.lines(source)
  source.lines.map(&:chomp)
end

Public Instance Methods

colorized_diff() click to toggle source

Colorized unified source diff between old and new

@return [String]

if there is a diff

@return [nil]

otherwise
# File lib/mutest/diff.rb, line 32
def colorized_diff
  return unless diff

  diff.lines.map(&self.class.method(:colorize_line)).join
end
diff() click to toggle source

Unified source diff between old and new

@return [String]

if there is exactly one diff

@return [nil]

otherwise
# File lib/mutest/diff.rb, line 18
def diff
  return if diffs.empty?

  minimized_hunk.diff(:unified) << NEWLINE
end

Private Instance Methods

diffs() click to toggle source

Diffs between old and new

@return [Array<Array>]

# File lib/mutest/diff.rb, line 64
def diffs
  ::Diff::LCS.diff(old, new)
end
hunks() click to toggle source

Raw diff-lcs hunks

@return [Array<Diff::LCS::Hunk>]

# File lib/mutest/diff.rb, line 71
def hunks
  diffs.map do |diff|
    ::Diff::LCS::Hunk.new(old, new, diff, max_length, 0)
  end
end
max_length() click to toggle source

Max length of source line in new and old

@return [Integer]

# File lib/mutest/diff.rb, line 92
def max_length
  [old, new].map(&:length).max
end
minimized_hunk() click to toggle source

Minimized hunk

@return Diff::LCS::Hunk

# File lib/mutest/diff.rb, line 80
def minimized_hunk
  head, *tail = hunks

  tail.reduce(head) do |left, right|
    right.merge(left)
    right
  end
end