Module: Test::Unit::Diff

Defined in:
lib/test/unit/diff.rb

Defined Under Namespace

Classes: Differ, ReadableDiffer, SequenceMatcher, UTF8Line, UnifiedDiffer

Class Method Summary collapse

Class Method Details

.diff(differ_class, from, to, options = {}) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/test/unit/diff.rb', line 724

def diff(differ_class, from, to, options={})
  if from.respond_to?(:valid_encoding?) and not from.valid_encoding?
    from = from.dup.force_encoding("ASCII-8BIT")
  end
  if to.respond_to?(:valid_encoding?) and not to.valid_encoding?
    to = to.dup.force_encoding("ASCII-8BIT")
  end
  differ = differ_class.new(from.split(/\r?\n/), to.split(/\r?\n/))
  lines = differ.diff(options)
  if Object.const_defined?(:EncodingError)
    begin
      lines.join("\n")
    rescue EncodingError
      lines.collect {|line| line.force_encoding("ASCII-8BIT")}.join("\n")
    end
  else
    lines.join("\n")
  end
end

.fold(string) ⇒ Object



706
707
708
709
710
# File 'lib/test/unit/diff.rb', line 706

def fold(string)
  string.split(/\r?\n/).collect do |line|
    line.gsub(/(.{78})/, "\\1\n")
  end.join("\n")
end

.folded_readable(from, to, options = {}) ⇒ Object



712
713
714
# File 'lib/test/unit/diff.rb', line 712

def folded_readable(from, to, options={})
  readable(fold(from), fold(to), options)
end

.need_fold?(diff) ⇒ Boolean

Returns:

  • (Boolean)


702
703
704
# File 'lib/test/unit/diff.rb', line 702

def need_fold?(diff)
  /^[-+].{79}/ =~ diff
end

.readable(from, to, options = {}) ⇒ Object



716
717
718
# File 'lib/test/unit/diff.rb', line 716

def readable(from, to, options={})
  diff(ReadableDiffer, from, to, options)
end

.unified(from, to, options = {}) ⇒ Object



720
721
722
# File 'lib/test/unit/diff.rb', line 720

def unified(from, to, options={})
  diff(UnifiedDiffer, from, to, options)
end