summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAustin Ziegler <austin@halostatue.ca>2011-07-31 03:39:42 -0400
committerAustin Ziegler <austin@halostatue.ca>2011-07-31 03:39:42 -0400
commit2276593be732e5c91cac144d1fad0310e072613b (patch)
treeecb9e96942c3cf0990b8ff288c0af2f0b02e1653
parent743cff65ed6826c48b967c3064783809da7bc1b7 (diff)
downloaddiff-lcs-2276593be732e5c91cac144d1fad0310e072613b.tar.gz
Convert to RSpec
-rw-r--r--License.rdoc6
-rw-r--r--README.rdoc2
-rw-r--r--lib/diff-lcs.rb5
-rw-r--r--lib/diff/lcs.rb942
-rw-r--r--spec/diff_lcs_balanced_spec.rb169
-rw-r--r--spec/diff_lcs_diff_spec.rb33
-rw-r--r--spec/diff_lcs_lcs_spec.rb36
-rw-r--r--spec/diff_lcs_patch_spec.rb428
-rw-r--r--spec/diff_lcs_sdiff_spec.rb180
-rw-r--r--spec/diff_lcs_sequences_spec.rb83
-rw-r--r--spec/spec_helper.rb270
-rw-r--r--tests/00test.rb626
12 files changed, 1679 insertions, 1101 deletions
diff --git a/License.rdoc b/License.rdoc
index bf3f43f..4237146 100644
--- a/License.rdoc
+++ b/License.rdoc
@@ -3,9 +3,9 @@
This software is available under three licenses: the GNU GPL version 2 (or at
your option, a later version), the Perl Artistic license, or the MIT license.
Note that my preference for licensing is the MIT license, but Algorithm::Diff
-was dually originally licensed with the Perl Artistic and the GNU GPL and that
-the Ruby implementation hews pretty closely to the Perl version, so I must
-maintain the additional licensing terms.
+was dually originally licensed with the Perl Artistic and the GNU GPL ("the
+same terms as Perl itself") and that the Ruby implementation hews pretty
+closely to the Perl version, so I must maintain the additional licensing terms.
* Copyright 2004–2011 Austin Ziegler.
* Adapted from Algorithm::Diff (Perl) by Ned Konz and a Smalltalk versionby
diff --git a/README.rdoc b/README.rdoc
index 889142b..a887b16 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -6,7 +6,7 @@ longest common subsequence (LCS) algorithm to compute intelligent differences
between two sequenced enumerable containers. The implementation is based on
Mario I. Wolczko's {Smalltalk version 1.2}[ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st]
(1993) and Ned Konz's Perl version
-{Algorithm::Diff}[http://search.cpan.org/~nedkonz/Algorithm-Diff-1.15/].
+{Algorithm::Diff 1.15}[http://search.cpan.org/~nedkonz/Algorithm-Diff-1.15/].
This is release 1.1.3, fixing several small bugs found over the years. Version
1.1.0 added new features, including the ability to #patch and #unpatch changes
diff --git a/lib/diff-lcs.rb b/lib/diff-lcs.rb
new file mode 100644
index 0000000..f1609a7
--- /dev/null
+++ b/lib/diff-lcs.rb
@@ -0,0 +1,5 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'diff/lcs'
+
+# vim: ft=ruby
diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb
index b74b2d3..1013121 100644
--- a/lib/diff/lcs.rb
+++ b/lib/diff/lcs.rb
@@ -1,211 +1,196 @@
-#! /usr/env/bin ruby
-#--
-# Copyright 2004 Austin Ziegler <diff-lcs@halostatue.ca>
-# adapted from:
-# Algorithm::Diff (Perl) by Ned Konz <perl@bike-nomad.com>
-# Smalltalk by Mario I. Wolczko <mario@wolczko.com>
-# implements McIlroy-Hunt diff algorithm
-#
-# This program is free software. It may be redistributed and/or modified
-# under the terms of the GPL version 2 (or later), the Perl Artistic
-# licence, or the Ruby licence.
-#
-# $Id$
-#++
+# -*- ruby encoding: utf-8 -*-
module Diff
- # = Diff::LCS 1.1.2
- # Computes "intelligent" differences between two sequenced Enumerables.
- # This is an implementation of the McIlroy-Hunt "diff" algorithm for
- # Enumerable objects that include Diffable.
- #
- # Based on Mario I. Wolczko's <mario@wolczko.com> Smalltalk version
- # (1.2, 1993) and Ned Konz's <perl@bike-nomad.com> Perl version
- # (Algorithm::Diff).
- #
- # == Synopsis
- # require 'diff/lcs'
- #
- # seq1 = %w(a b c e h j l m n p)
- # seq2 = %w(b c d e f j k l m r s t)
- #
- # lcs = Diff::LCS.LCS(seq1, seq2)
- # diffs = Diff::LCS.diff(seq1, seq2)
- # sdiff = Diff::LCS.sdiff(seq1, seq2)
- # seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj)
- # bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj)
- # seq2 == Diff::LCS.patch(seq1, diffs)
- # seq2 == Diff::LCS.patch!(seq1, diffs)
- # seq1 == Diff::LCS.unpatch(seq2, diffs)
- # seq1 == Diff::LCS.unpatch!(seq2, diffs)
- # seq2 == Diff::LCS.patch(seq1, sdiff)
- # seq2 == Diff::LCS.patch!(seq1, sdiff)
- # seq1 == Diff::LCS.unpatch(seq2, sdiff)
- # seq1 == Diff::LCS.unpatch!(seq2, sdiff)
- #
- # Alternatively, objects can be extended with Diff::LCS:
- #
- # seq1.extend(Diff::LCS)
- # lcs = seq1.lcs(seq2)
- # diffs = seq1.diff(seq2)
- # sdiff = seq1.sdiff(seq2)
- # seq = seq1.traverse_sequences(seq2, callback_obj)
- # bal = seq1.traverse_balanced(seq2, callback_obj)
- # seq2 == seq1.patch(diffs)
- # seq2 == seq1.patch!(diffs)
- # seq1 == seq2.unpatch(diffs)
- # seq1 == seq2.unpatch!(diffs)
- # seq2 == seq1.patch(sdiff)
- # seq2 == seq1.patch!(sdiff)
- # seq1 == seq2.unpatch(sdiff)
- # seq1 == seq2.unpatch!(sdiff)
- #
- # Default extensions are provided for Array and String objects through
- # the use of 'diff/lcs/array' and 'diff/lcs/string'.
- #
- # == Introduction (by Mark-Jason Dominus)
- #
- # <em>The following text is from the Perl documentation. The only
- # changes have been to make the text appear better in Rdoc</em>.
- #
- # I once read an article written by the authors of +diff+; they said
- # that they hard worked very hard on the algorithm until they found the
- # right one.
- #
- # I think what they ended up using (and I hope someone will correct me,
- # because I am not very confident about this) was the `longest common
- # subsequence' method. In the LCS problem, you have two sequences of
- # items:
- #
- # a b c d f g h j q z
- # a b c d e f g i j k r x y z
- #
- # and you want to find the longest sequence of items that is present in
- # both original sequences in the same order. That is, you want to find a
- # new sequence *S* which can be obtained from the first sequence by
- # deleting some items, and from the second sequence by deleting other
- # items. You also want *S* to be as long as possible. In this case *S*
- # is:
- #
- # a b c d f g j z
- #
- # From there it's only a small step to get diff-like output:
- #
- # e h i k q r x y
- # + - + + - + + +
- #
- # This module solves the LCS problem. It also includes a canned function
- # to generate +diff+-like output.
- #
- # It might seem from the example above that the LCS of two sequences is
- # always pretty obvious, but that's not always the case, especially when
- # the two sequences have many repeated elements. For example, consider
- #
- # a x b y c z p d q
- # a b c a x b y c z
- #
- # A naive approach might start by matching up the +a+ and +b+ that
- # appear at the beginning of each sequence, like this:
- #
- # a x b y c z p d q
- # a b c a b y c z
- #
- # This finds the common subsequence +a b c z+. But actually, the LCS is
- # +a x b y c z+:
- #
- # a x b y c z p d q
- # a b c a x b y c z
- #
- # == Author
- # This version is by Austin Ziegler <diff-lcs@halostatue.ca>.
- #
- # It is based on the Perl Algorithm::Diff by Ned Konz
- # <perl@bike-nomad.com>, copyright &copy; 2000 - 2002 and the Smalltalk
- # diff version by Mario I. Wolczko <mario@wolczko.com>, copyright &copy;
- # 1993. Documentation includes work by Mark-Jason Dominus.
- #
- # == Licence
- # Copyright &copy; 2004 Austin Ziegler
- # This program is free software; you can redistribute it and/or modify it
- # under the same terms as Ruby, or alternatively under the Perl Artistic
- # licence.
- #
- # == Credits
- # Much of the documentation is taken directly from the Perl
- # Algorithm::Diff implementation and was written originally by Mark-Jason
- # Dominus <mjd-perl-diff@plover.com> and later by Ned Konz. The basic Ruby
- # implementation was re-ported from the Smalltalk implementation, available
- # at ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
- #
- # #sdiff and #traverse_balanced were written for the Perl version by Mike
- # Schilli <m@perlmeister.com>.
- #
- # "The algorithm is described in <em>A Fast Algorithm for Computing Longest
- # Common Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May 1977, with
- # a few minor improvements to improve the speed."
+ # = Diff::LCS 1.1.3
+ # Computes "intelligent" differences between two sequenced Enumerables.
+ # This is an implementation of the McIlroy-Hunt "diff" algorithm for
+ # Enumerable objects that include Diffable.
+ #
+ # Based on Mario I. Wolczko's Smalltalk version (1.2, 1993) and Ned Konz's
+ # Perl version (Algorithm::Diff 1.15).
+ #
+ # == Synopsis
+ # require 'diff/lcs'
+ #
+ # seq1 = %w(a b c e h j l m n p)
+ # seq2 = %w(b c d e f j k l m r s t)
+ #
+ # lcs = Diff::LCS.LCS(seq1, seq2)
+ # diffs = Diff::LCS.diff(seq1, seq2)
+ # sdiff = Diff::LCS.sdiff(seq1, seq2)
+ # seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj)
+ # bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj)
+ # seq2 == Diff::LCS.patch(seq1, diffs)
+ # seq2 == Diff::LCS.patch!(seq1, diffs)
+ # seq1 == Diff::LCS.unpatch(seq2, diffs)
+ # seq1 == Diff::LCS.unpatch!(seq2, diffs)
+ # seq2 == Diff::LCS.patch(seq1, sdiff)
+ # seq2 == Diff::LCS.patch!(seq1, sdiff)
+ # seq1 == Diff::LCS.unpatch(seq2, sdiff)
+ # seq1 == Diff::LCS.unpatch!(seq2, sdiff)
+ #
+ # Alternatively, objects can be extended with Diff::LCS:
+ #
+ # seq1.extend(Diff::LCS)
+ # lcs = seq1.lcs(seq2)
+ # diffs = seq1.diff(seq2)
+ # sdiff = seq1.sdiff(seq2)
+ # seq = seq1.traverse_sequences(seq2, callback_obj)
+ # bal = seq1.traverse_balanced(seq2, callback_obj)
+ # seq2 == seq1.patch(diffs)
+ # seq2 == seq1.patch!(diffs)
+ # seq1 == seq2.unpatch(diffs)
+ # seq1 == seq2.unpatch!(diffs)
+ # seq2 == seq1.patch(sdiff)
+ # seq2 == seq1.patch!(sdiff)
+ # seq1 == seq2.unpatch(sdiff)
+ # seq1 == seq2.unpatch!(sdiff)
+ #
+ # Default extensions are provided for Array and String objects through the
+ # use of 'diff/lcs/array' and 'diff/lcs/string'.
+ #
+ # == Introduction (by Mark-Jason Dominus)
+ #
+ # <em>The following text is from the Perl documentation. The only changes
+ # have been to make the text appear better in Rdoc</em>.
+ #
+ # I once read an article written by the authors of +diff+; they said that
+ # they hard worked very hard on the algorithm until they found the right
+ # one.
+ #
+ # I think what they ended up using (and I hope someone will correct me,
+ # because I am not very confident about this) was the `longest common
+ # subsequence' method. In the LCS problem, you have two sequences of
+ # items:
+ #
+ # a b c d f g h j q z
+ # a b c d e f g i j k r x y z
+ #
+ # and you want to find the longest sequence of items that is present in
+ # both original sequences in the same order. That is, you want to find a
+ # new sequence *S* which can be obtained from the first sequence by
+ # deleting some items, and from the second sequence by deleting other
+ # items. You also want *S* to be as long as possible. In this case *S* is:
+ #
+ # a b c d f g j z
+ #
+ # From there it's only a small step to get diff-like output:
+ #
+ # e h i k q r x y
+ # + - + + - + + +
+ #
+ # This module solves the LCS problem. It also includes a canned function
+ # to generate +diff+-like output.
+ #
+ # It might seem from the example above that the LCS of two sequences is
+ # always pretty obvious, but that's not always the case, especially when
+ # the two sequences have many repeated elements. For example, consider
+ #
+ # a x b y c z p d q
+ # a b c a x b y c z
+ #
+ # A naive approach might start by matching up the +a+ and +b+ that appear
+ # at the beginning of each sequence, like this:
+ #
+ # a x b y c z p d q
+ # a b c a b y c z
+ #
+ # This finds the common subsequence +a b c z+. But actually, the LCS is
+ # +a x b y c z+:
+ #
+ # a x b y c z p d q
+ # a b c a x b y c z
+ #
+ # == Author
+ # This version is by Austin Ziegler <austin@rubyforge.org>.
+ #
+ # It is based on the Perl Algorithm::Diff (1.15) by Ned Konz , copyright
+ # &copy; 2000&ndash;2002 and the Smalltalk diff version by Mario I.
+ # Wolczko, copyright &copy; 1993. Documentation includes work by
+ # Mark-Jason Dominus.
+ #
+ # == Licence
+ # Copyright &copy; 2004 Austin Ziegler
+ # This program is free software; you can redistribute it and/or modify it
+ # under the same terms as Ruby, or alternatively under the Perl Artistic
+ # licence.
+ #
+ # == Credits
+ # Much of the documentation is taken directly from the Perl
+ # Algorithm::Diff implementation and was written originally by Mark-Jason
+ # Dominus and later by Ned Konz. The basic Ruby implementation was
+ # re-ported from the Smalltalk implementation, available at
+ # ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
+ #
+ # #sdiff and #traverse_balanced were written for the Perl version by Mike
+ # Schilli <m@perlmeister.com>.
+ #
+ # "The algorithm is described in <em>A Fast Algorithm for Computing
+ # Longest Common Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May
+ # 1977, with a few minor improvements to improve the speed."
module LCS
- VERSION = '1.1.2'
+ VERSION = '1.1.3'
end
end
require 'diff/lcs/callbacks'
module Diff::LCS
- # Returns an Array containing the longest common subsequence(s) between
- # +self+ and +other+. See Diff::LCS#LCS.
- #
- # lcs = seq1.lcs(seq2)
+ # Returns an Array containing the longest common subsequence(s) between
+ # +self+ and +other+. See Diff::LCS#LCS.
+ #
+ # lcs = seq1.lcs(seq2)
def lcs(other, &block) #:yields self[ii] if there are matched subsequences:
Diff::LCS.LCS(self, other, &block)
end
- # Returns the difference set between +self+ and +other+. See
- # Diff::LCS#diff.
+ # Returns the difference set between +self+ and +other+. See
+ # Diff::LCS#diff.
def diff(other, callbacks = nil, &block)
Diff::LCS::diff(self, other, callbacks, &block)
end
- # Returns the balanced ("side-by-side") difference set between +self+ and
- # +other+. See Diff::LCS#sdiff.
+ # Returns the balanced ("side-by-side") difference set between +self+ and
+ # +other+. See Diff::LCS#sdiff.
def sdiff(other, callbacks = nil, &block)
Diff::LCS::sdiff(self, other, callbacks, &block)
end
- # Traverses the discovered longest common subsequences between +self+ and
- # +other+. See Diff::LCS#traverse_sequences.
+ # Traverses the discovered longest common subsequences between +self+ and
+ # +other+. See Diff::LCS#traverse_sequences.
def traverse_sequences(other, callbacks = nil, &block)
- traverse_sequences(self, other, callbacks || Diff::LCS::YieldingCallbacks,
- &block)
+ traverse_sequences(self, other, callbacks ||
+ Diff::LCS::YieldingCallbacks, &block)
end
- # Traverses the discovered longest common subsequences between +self+ and
- # +other+ using the alternate, balanced algorithm. See
- # Diff::LCS#traverse_balanced.
+ # Traverses the discovered longest common subsequences between +self+ and
+ # +other+ using the alternate, balanced algorithm. See
+ # Diff::LCS#traverse_balanced.
def traverse_balanced(other, callbacks = nil, &block)
- traverse_balanced(self, other, callbacks || Diff::LCS::YieldingCallbacks,
- &block)
+ traverse_balanced(self, other, callbacks ||
+ Diff::LCS::YieldingCallbacks, &block)
end
- # Attempts to patch a copy of +self+ with the provided +patchset+. See
- # Diff::LCS#patch.
+ # Attempts to patch a copy of +self+ with the provided +patchset+. See
+ # Diff::LCS#patch.
def patch(patchset)
Diff::LCS::patch(self.dup, patchset)
end
- # Attempts to unpatch a copy of +self+ with the provided +patchset+.
- # See Diff::LCS#patch.
+ # Attempts to unpatch a copy of +self+ with the provided +patchset+. See
+ # Diff::LCS#patch.
def unpatch(patchset)
Diff::LCS::unpatch(self.dup, patchset)
end
- # Attempts to patch +self+ with the provided +patchset+. See
- # Diff::LCS#patch!. Does no autodiscovery.
+ # Attempts to patch +self+ with the provided +patchset+. See
+ # Diff::LCS#patch!. Does no autodiscovery.
def patch!(patchset)
Diff::LCS::patch!(self, patchset)
end
- # Attempts to unpatch +self+ with the provided +patchset+. See
- # Diff::LCS#unpatch. Does no autodiscovery.
+ # Attempts to unpatch +self+ with the provided +patchset+. See
+ # Diff::LCS#unpatch. Does no autodiscovery.
def unpatch!(patchset)
Diff::LCS::unpatch!(self, patchset)
end
@@ -213,20 +198,20 @@ end
module Diff::LCS
class << self
- # Given two sequenced Enumerables, LCS returns an Array containing their
- # longest common subsequences.
- #
- # lcs = Diff::LCS.LCS(seq1, seq2)
- #
- # This array whose contents is such that:
- #
- # lcs.each_with_index do |ee, ii|
- # assert(ee.nil? || (seq1[ii] == seq2[ee]))
- # end
- #
- # If a block is provided, the matching subsequences will be yielded from
- # +seq1+ in turn and may be modified before they are placed into the
- # returned Array of subsequences.
+ # Given two sequenced Enumerables, LCS returns an Array containing their
+ # longest common subsequences.
+ #
+ # lcs = Diff::LCS.LCS(seq1, seq2)
+ #
+ # This array whose contents is such that:
+ #
+ # lcs.each_with_index do |ee, ii|
+ # assert(ee.nil? || (seq1[ii] == seq2[ee]))
+ # end
+ #
+ # If a block is provided, the matching subsequences will be yielded from
+ # +seq1+ in turn and may be modified before they are placed into the
+ # returned Array of subsequences.
def LCS(seq1, seq2, &block) #:yields seq1[ii] for each matched:
matches = Diff::LCS.__lcs(seq1, seq2)
ret = []
@@ -242,15 +227,15 @@ module Diff::LCS
ret
end
- # Diff::LCS.diff computes the smallest set of additions and deletions
- # necessary to turn the first sequence into the second, and returns a
- # description of these changes.
- #
- # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks.
- # If a Class argument is provided for +callbacks+, #diff will attempt
- # to initialise it. If the +callbacks+ object (possibly initialised)
- # responds to #finish, it will be called.
+ # Diff::LCS.diff computes the smallest set of additions and deletions
+ # necessary to turn the first sequence into the second, and returns a
+ # description of these changes.
+ #
+ # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
+ # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
+ # a Class argument is provided for +callbacks+, #diff will attempt to
+ # initialise it. If the +callbacks+ object (possibly initialised)
+ # responds to #finish, it will be called.
def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
callbacks ||= Diff::LCS::DiffCallbacks
if callbacks.kind_of?(Class)
@@ -263,7 +248,7 @@ module Diff::LCS
if block_given?
res = callbacks.diffs.map do |hunk|
if hunk.kind_of?(Array)
- hunk = hunk.map { |block| yield block }
+ hunk = hunk.map { |hunk_block| yield hunk_block }
else
yield hunk
end
@@ -274,20 +259,20 @@ module Diff::LCS
end
end
- # Diff::LCS.sdiff computes all necessary components to show two sequences
- # and their minimized differences side by side, just like the Unix
- # utility <em>sdiff</em> does:
- #
- # old < -
- # same same
- # before | after
- # - > new
- #
- # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
- # a Class argument is provided for +callbacks+, #diff will attempt to
- # initialise it. If the +callbacks+ object (possibly initialised)
- # responds to #finish, it will be called.
+ # Diff::LCS.sdiff computes all necessary components to show two sequences
+ # and their minimized differences side by side, just like the Unix
+ # utility <em>sdiff</em> does:
+ #
+ # old < -
+ # same same
+ # before | after
+ # - > new
+ #
+ # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
+ # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
+ # a Class argument is provided for +callbacks+, #diff will attempt to
+ # initialise it. If the +callbacks+ object (possibly initialised)
+ # responds to #finish, it will be called.
def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
callbacks ||= Diff::LCS::SDiffCallbacks
if callbacks.kind_of?(Class)
@@ -300,7 +285,7 @@ module Diff::LCS
if block_given?
res = callbacks.diffs.map do |hunk|
if hunk.kind_of?(Array)
- hunk = hunk.map { |block| yield block }
+ hunk = hunk.map { |hunk_block| yield hunk_block }
else
yield hunk
end
@@ -311,87 +296,88 @@ module Diff::LCS
end
end
- # Diff::LCS.traverse_sequences is the most general facility provided by this
- # module; +diff+ and +LCS+ are implemented as calls to it.
- #
- # The arguments to #traverse_sequences are the two sequences to
- # traverse, and a callback object, like this:
- #
- # traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
- #
- # #diff is implemented with #traverse_sequences.
- #
- # == Callback Methods
- # Optional callback methods are <em>emphasized</em>.
- #
- # callbacks#match:: Called when +a+ and +b+ are pointing
- # to common elements in +A+ and +B+.
- # callbacks#discard_a:: Called when +a+ is pointing to an
- # element not in +B+.
- # callbacks#discard_b:: Called when +b+ is pointing to an
- # element not in +A+.
- # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
- # sequence +A+.
- # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
- # sequence +B+.
- #
- # == Algorithm
- # a---+
- # v
- # A = a b c e h j l m n p
- # B = b c d e f j k l m r s t
- # ^
- # b---+
- #
- # If there are two arrows (+a+ and +b+) pointing to elements of
- # sequences +A+ and +B+, the arrows will initially point to the first
- # elements of their respective sequences. #traverse_sequences will
- # advance the arrows through the sequences one element at a time,
- # calling a method on the user-specified callback object before each
- # advance. It will advance the arrows in such a way that if there are
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
- # part of the longest common subsequence, there will be some moment
- # during the execution of #traverse_sequences when arrow +a+ is pointing
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
- # and then it will advance both arrows.
- #
- # Otherwise, one of the arrows is pointing to an element of its sequence
- # that is not part of the longest common subsequence.
- # #traverse_sequences will advance that arrow and will call
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>, depending
- # on which arrow it advanced. If both arrows point to elements that are
- # not part of the longest common subsequence, then #traverse_sequences
- # will advance one of them and call the appropriate callback, but it is
- # not specified which it will call.
- #
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
- # and <tt>callbacks#discard_b</tt> are invoked with an event comprising
- # the action ("=", "+", or "-", respectively), the indicies +ii+ and
- # +jj+, and the elements <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return
- # values are discarded by #traverse_sequences.
- #
- # === End of Sequences
- # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
- # #traverse_sequence try to call <tt>callbacks#finished_a</tt> with the
- # last index and element of +A+ (<tt>A[-1]</tt>) and the current index
- # and element of +B+ (<tt>B[jj]</tt>). If <tt>callbacks#finished_a</tt>
- # does not exist, then <tt>callbacks#discard_b</tt> will be called on
- # each element of +B+ until the end of the sequence is reached (the call
- # will be done with <tt>A[-1]</tt> and <tt>B[jj]</tt> for each element).
- #
- # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
- # <tt>callbacks#finished_b</tt> will be called with the current index
- # and element of +A+ (<tt>A[ii]</tt>) and the last index and element of
- # +B+ (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not
- # exist on the callback object, then <tt>callbacks#discard_a</tt> will
- # be called on each element of +A+ until the end of the sequence is
- # reached (<tt>A[ii]</tt> and <tt>B[-1]</tt>).
- #
- # There is a chance that one additional <tt>callbacks#discard_a</tt> or
- # <tt>callbacks#discard_b</tt> will be called after the end of the
- # sequence is reached, if +a+ has not yet reached the end of +A+ or +b+
- # has not yet reached the end of +B+.
+ # Diff::LCS.traverse_sequences is the most general facility provided by this
+ # module; +diff+ and +LCS+ are implemented as calls to it.
+ #
+ # The arguments to #traverse_sequences are the two sequences to
+ # traverse, and a callback object, like this:
+ #
+ # traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
+ #
+ # #diff is implemented with #traverse_sequences.
+ #
+ # == Callback Methods
+ # Optional callback methods are <em>emphasized</em>.
+ #
+ # callbacks#match:: Called when +a+ and +b+ are pointing
+ # to common elements in +A+ and +B+.
+ # callbacks#discard_a:: Called when +a+ is pointing to an
+ # element not in +B+.
+ # callbacks#discard_b:: Called when +b+ is pointing to an
+ # element not in +A+.
+ # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
+ # sequence +A+.
+ # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
+ # sequence +B+.
+ #
+ # == Algorithm
+ # a---+
+ # v
+ # A = a b c e h j l m n p
+ # B = b c d e f j k l m r s t
+ # ^
+ # b---+
+ #
+ # If there are two arrows (+a+ and +b+) pointing to elements of
+ # sequences +A+ and +B+, the arrows will initially point to the first
+ # elements of their respective sequences. #traverse_sequences will
+ # advance the arrows through the sequences one element at a time,
+ # calling a method on the user-specified callback object before each
+ # advance. It will advance the arrows in such a way that if there are
+ # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
+ # part of the longest common subsequence, there will be some moment
+ # during the execution of #traverse_sequences when arrow +a+ is pointing
+ # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
+ # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
+ # and then it will advance both arrows.
+ #
+ # Otherwise, one of the arrows is pointing to an element of its sequence
+ # that is not part of the longest common subsequence.
+ # #traverse_sequences will advance that arrow and will call
+ # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>, depending
+ # on which arrow it advanced. If both arrows point to elements that are
+ # not part of the longest common subsequence, then #traverse_sequences
+ # will advance one of them and call the appropriate callback, but it is
+ # not specified which it will call.
+ #
+ # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
+ # and <tt>callbacks#discard_b</tt> are invoked with an event comprising
+ # the action ("=", "+", or "-", respectively), the indicies +ii+ and
+ # +jj+, and the elements <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return
+ # values are discarded by #traverse_sequences.
+ #
+ # === End of Sequences
+ # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
+ # #traverse_sequence will try to call <tt>callbacks#finished_a</tt> with
+ # the last index and element of +A+ (<tt>A[-1]</tt>) and the current
+ # index and element of +B+ (<tt>B[jj]</tt>). If
+ # <tt>callbacks#finished_a</tt> does not exist, then
+ # <tt>callbacks#discard_b</tt> will be called on each element of +B+
+ # until the end of the sequence is reached (the call
+ # will be done with <tt>A[-1]</tt> and <tt>B[jj]</tt> for each element).
+ #
+ # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
+ # <tt>callbacks#finished_b</tt> will be called with the current index
+ # and element of +A+ (<tt>A[ii]</tt>) and the last index and element of
+ # +B+ (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not
+ # exist on the callback object, then <tt>callbacks#discard_a</tt> will
+ # be called on each element of +A+ until the end of the sequence is
+ # reached (<tt>A[ii]</tt> and <tt>B[-1]</tt>).
+ #
+ # There is a chance that one additional <tt>callbacks#discard_a</tt> or
+ # <tt>callbacks#discard_b</tt> will be called after the end of the
+ # sequence is reached, if +a+ has not yet reached the end of +A+ or +b+
+ # has not yet reached the end of +B+.
def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks, &block) #:yields change events:
matches = Diff::LCS.__lcs(seq1, seq2)
@@ -433,10 +419,10 @@ module Diff::LCS
end
ai += 1
- # The last entry (if any) processed was a match. +ai+ and +bj+ point
- # just past the last matching lines in their sequences.
+ # The last entry (if any) processed was a match. +ai+ and +bj+ point
+ # just past the last matching lines in their sequences.
while (ai < a_size) or (bj < b_size)
- # last A?
+ # last A?
if ai == a_size and bj < b_size
if callbacks.respond_to?(:finished_a) and not run_finished_a
ax = string ? seq1[-1, 1] : seq1[-1]
@@ -458,7 +444,7 @@ module Diff::LCS
end
end
- # last B?
+ # last B?
if bj == b_size and ai < a_size
if callbacks.respond_to?(:finished_b) and not run_finished_b
ax = string ? seq1[ai, 1] : seq1[ai]
@@ -500,88 +486,88 @@ module Diff::LCS
end
end
- # #traverse_balanced is an alternative to #traverse_sequences. It
- # uses a different algorithm to iterate through the entries in the
- # computed longest common subsequence. Instead of viewing the changes as
- # insertions or deletions from one of the sequences, #traverse_balanced
- # will report <em>changes</em> between the sequences. To represent a
- #
- # The arguments to #traverse_balanced are the two sequences to traverse
- # and a callback object, like this:
- #
- # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
- #
- # #sdiff is implemented with #traverse_balanced.
- #
- # == Callback Methods
- # Optional callback methods are <em>emphasized</em>.
- #
- # callbacks#match:: Called when +a+ and +b+ are pointing
- # to common elements in +A+ and +B+.
- # callbacks#discard_a:: Called when +a+ is pointing to an
- # element not in +B+.
- # callbacks#discard_b:: Called when +b+ is pointing to an
- # element not in +A+.
- # <em>callbacks#change</em>:: Called when +a+ and +b+ are pointing
- # to the same relative position, but
- # <tt>A[a]</tt> and <tt>B[b]</tt> are
- # not the same; a <em>change</em> has
- # occurred.
- #
- # #traverse_balanced might be a bit slower than #traverse_sequences,
- # noticable only while processing huge amounts of data.
- #
- # The +sdiff+ function of this module is implemented as call to
- # #traverse_balanced.
- #
- # == Algorithm
- # a---+
- # v
- # A = a b c e h j l m n p
- # B = b c d e f j k l m r s t
- # ^
- # b---+
- #
- # === Matches
- # If there are two arrows (+a+ and +b+) pointing to elements of
- # sequences +A+ and +B+, the arrows will initially point to the first
- # elements of their respective sequences. #traverse_sequences will
- # advance the arrows through the sequences one element at a time,
- # calling a method on the user-specified callback object before each
- # advance. It will advance the arrows in such a way that if there are
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
- # part of the longest common subsequence, there will be some moment
- # during the execution of #traverse_sequences when arrow +a+ is pointing
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
- # and then it will advance both arrows.
- #
- # === Discards
- # Otherwise, one of the arrows is pointing to an element of its sequence
- # that is not part of the longest common subsequence.
- # #traverse_sequences will advance that arrow and will call
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>,
- # depending on which arrow it advanced.
- #
- # === Changes
- # If both +a+ and +b+ point to elements that are not part of the longest
- # common subsequence, then #traverse_sequences will try to call
- # <tt>callbacks#change</tt> and advance both arrows. If
- # <tt>callbacks#change</tt> is not implemented, then
- # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
- # called in turn.
- #
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
- # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are
- # invoked with an event comprising the action ("=", "+", "-", or "!",
- # respectively), the indicies +ii+ and +jj+, and the elements
- # <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return values are discarded by
- # #traverse_balanced.
- #
- # === Context
- # Note that +ii+ and +jj+ may not be the same index position, even if
- # +a+ and +b+ are considered to be pointing to matching or changed
- # elements.
+ # #traverse_balanced is an alternative to #traverse_sequences. It
+ # uses a different algorithm to iterate through the entries in the
+ # computed longest common subsequence. Instead of viewing the changes as
+ # insertions or deletions from one of the sequences, #traverse_balanced
+ # will report <em>changes</em> between the sequences. To represent a
+ #
+ # The arguments to #traverse_balanced are the two sequences to traverse
+ # and a callback object, like this:
+ #
+ # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
+ #
+ # #sdiff is implemented with #traverse_balanced.
+ #
+ # == Callback Methods
+ # Optional callback methods are <em>emphasized</em>.
+ #
+ # callbacks#match:: Called when +a+ and +b+ are pointing
+ # to common elements in +A+ and +B+.
+ # callbacks#discard_a:: Called when +a+ is pointing to an
+ # element not in +B+.
+ # callbacks#discard_b:: Called when +b+ is pointing to an
+ # element not in +A+.
+ # <em>callbacks#change</em>:: Called when +a+ and +b+ are pointing
+ # to the same relative position, but
+ # <tt>A[a]</tt> and <tt>B[b]</tt> are
+ # not the same; a <em>change</em> has
+ # occurred.
+ #
+ # #traverse_balanced might be a bit slower than #traverse_sequences,
+ # noticable only while processing huge amounts of data.
+ #
+ # The +sdiff+ function of this module is implemented as call to
+ # #traverse_balanced.
+ #
+ # == Algorithm
+ # a---+
+ # v
+ # A = a b c e h j l m n p
+ # B = b c d e f j k l m r s t
+ # ^
+ # b---+
+ #
+ # === Matches
+ # If there are two arrows (+a+ and +b+) pointing to elements of
+ # sequences +A+ and +B+, the arrows will initially point to the first
+ # elements of their respective sequences. #traverse_sequences will
+ # advance the arrows through the sequences one element at a time,
+ # calling a method on the user-specified callback object before each
+ # advance. It will advance the arrows in such a way that if there are
+ # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
+ # part of the longest common subsequence, there will be some moment
+ # during the execution of #traverse_sequences when arrow +a+ is pointing
+ # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
+ # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
+ # and then it will advance both arrows.
+ #
+ # === Discards
+ # Otherwise, one of the arrows is pointing to an element of its sequence
+ # that is not part of the longest common subsequence.
+ # #traverse_sequences will advance that arrow and will call
+ # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>,
+ # depending on which arrow it advanced.
+ #
+ # === Changes
+ # If both +a+ and +b+ point to elements that are not part of the longest
+ # common subsequence, then #traverse_sequences will try to call
+ # <tt>callbacks#change</tt> and advance both arrows. If
+ # <tt>callbacks#change</tt> is not implemented, then
+ # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
+ # called in turn.
+ #
+ # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
+ # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are
+ # invoked with an event comprising the action ("=", "+", "-", or "!",
+ # respectively), the indicies +ii+ and +jj+, and the elements
+ # <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return values are discarded by
+ # #traverse_balanced.
+ #
+ # === Context
+ # Note that +ii+ and +jj+ may not be the same index position, even if
+ # +a+ and +b+ are considered to be pointing to matching or changed
+ # elements.
def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
matches = Diff::LCS.__lcs(seq1, seq2)
a_size = seq1.size
@@ -690,10 +676,10 @@ module Diff::LCS
:unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }
}
- # Given a patchset, convert the current version to the new
- # version. If +direction+ is not specified (must be
- # <tt>:patch</tt> or <tt>:unpatch</tt>), then discovery of the
- # direction of the patch will be attempted.
+ # Given a patchset, convert the current version to the new
+ # version. If +direction+ is not specified (must be
+ # <tt>:patch</tt> or <tt>:unpatch</tt>), then discovery of the
+ # direction of the patch will be attempted.
def patch(src, patchset, direction = nil)
string = src.kind_of?(String)
# Start with a new empty type of the source's class
@@ -793,51 +779,54 @@ module Diff::LCS
res
end
- # Given a set of patchset, convert the current version to the prior
- # version. Does no auto-discovery.
+ # Given a set of patchset, convert the current version to the prior
+ # version. Does no auto-discovery.
def unpatch!(src, patchset)
Diff::LCS.patch(src, patchset, :unpatch)
end
- # Given a set of patchset, convert the current version to the next
- # version. Does no auto-discovery.
+ # Given a set of patchset, convert the current version to the next
+ # version. Does no auto-discovery.
def patch!(src, patchset)
Diff::LCS.patch(src, patchset, :patch)
end
# private
- # Compute the longest common subsequence between the sequenced Enumerables
- # +a+ and +b+. The result is an array whose contents is such that
- #
- # result = Diff::LCS.__lcs(a, b)
- # result.each_with_index do |e, ii|
- # assert_equal(a[ii], b[e]) unless e.nil?
- # end
+ # Compute the longest common subsequence between the sequenced
+ # Enumerables +a+ and +b+. The result is an array whose contents is such
+ # that
+ #
+ # result = Diff::LCS.__lcs(a, b)
+ # result.each_with_index do |e, ii|
+ # assert_equal(a[ii], b[e]) unless e.nil?
+ # end
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __lcs(a, b)
a_start = b_start = 0
a_finish = a.size - 1
b_finish = b.size - 1
vector = []
- # Prune off any common elements at the beginning...
+ # Prune off any common elements at the beginning...
while (a_start <= a_finish) and
- (b_start <= b_finish) and
- (a[a_start] == b[b_start])
+ (b_start <= b_finish) and
+ (a[a_start] == b[b_start])
vector[a_start] = b_start
a_start += 1
b_start += 1
end
- # Now the end...
+ # Now the end...
while (a_start <= a_finish) and
- (b_start <= b_finish) and
- (a[a_finish] == b[b_finish])
+ (b_start <= b_finish) and
+ (a[a_finish] == b[b_finish])
vector[a_finish] = b_finish
a_finish -= 1
b_finish -= 1
end
- # Now, compute the equivalence classes of positions of elements.
+ # Now, compute the equivalence classes of positions of elements.
b_matches = Diff::LCS.__position_hash(b, b_start .. b_finish)
thresh = []
@@ -868,14 +857,16 @@ module Diff::LCS
vector
end
- # Find the place at which +value+ would normally be inserted into the
- # Enumerable. If that place is already occupied by +value+, do nothing
- # and return +nil+. If the place does not exist (i.e., it is off the end
- # of the Enumerable), add it to the end. Otherwise, replace the element
- # at that point with +value+. It is assumed that the Enumerable's values
- # are numeric.
- #
- # This operation preserves the sort order.
+ # Find the place at which +value+ would normally be inserted into the
+ # Enumerable. If that place is already occupied by +value+, do nothing
+ # and return +nil+. If the place does not exist (i.e., it is off the end
+ # of the Enumerable), add it to the end. Otherwise, replace the element
+ # at that point with +value+. It is assumed that the Enumerable's values
+ # are numeric.
+ #
+ # This operation preserves the sort order.
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __replace_next_larger(enum, value, last_index = nil)
# Off the end?
if enum.empty? or (value > enum[-1])
@@ -906,9 +897,11 @@ module Diff::LCS
return first_index
end
- # If +vector+ maps the matching elements of another collection onto this
- # Enumerable, compute the inverse +vector+ that maps this Enumerable
- # onto the collection. (Currently unused.)
+ # If +vector+ maps the matching elements of another collection onto this
+ # Enumerable, compute the inverse +vector+ that maps this Enumerable
+ # onto the collection. (Currently unused.)
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __inverse_vector(a, vector)
inverse = a.dup
(0 ... vector.size).each do |ii|
@@ -917,9 +910,11 @@ module Diff::LCS
inverse
end
- # Returns a hash mapping each element of an Enumerable to the set of
- # positions it occupies in the Enumerable, optionally restricted to the
- # elements specified in the range of indexes specified by +interval+.
+ # Returns a hash mapping each element of an Enumerable to the set of
+ # positions it occupies in the Enumerable, optionally restricted to the
+ # elements specified in the range of indexes specified by +interval+.
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __position_hash(enum, interval = 0 .. -1)
hash = Hash.new { |hh, kk| hh[kk] = [] }
interval.each do |ii|
@@ -929,13 +924,15 @@ module Diff::LCS
hash
end
- # Examine the patchset and the source to see in which direction the
- # patch should be applied.
- #
- # WARNING: By default, this examines the whole patch, so this could take
- # some time. This also works better with Diff::LCS::ContextChange or
- # Diff::LCS::Change as its source, as an array will cause the creation
- # of one of the above.
+ # Examine the patchset and the source to see in which direction the
+ # patch should be applied.
+ #
+ # WARNING: By default, this examines the whole patch, so this could take
+ # some time. This also works better with Diff::LCS::ContextChange or
+ # Diff::LCS::Change as its source, as an array will cause the creation
+ # of one of the above.
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __diff_direction(src, patchset, limit = nil)
count = left = left_miss = right = right_miss = 0
string = src.kind_of?(String)
@@ -945,9 +942,9 @@ module Diff::LCS
case change
when Diff::LCS::Change
- # With a simplistic change, we can't tell the difference between
- # the left and right on '!' actions, so we ignore those. On '='
- # actions, if there's a miss, we miss both left and right.
+ # With a simplistic change, we can't tell the difference between
+ # the left and right on '!' actions, so we ignore those. On '='
+ # actions, if there's a miss, we miss both left and right.
element = string ? src[change.position, 1] : src[change.position]
case change.action
@@ -1007,7 +1004,7 @@ module Diff::LCS
end
end
- break if not limit.nil? and count > limit
+ break if (not limit.nil?) && (count > limit)
end
no_left = (left == 0) and (left_miss >= 0)
@@ -1023,55 +1020,56 @@ module Diff::LCS
end
end
- # Normalize the patchset. A patchset is always a sequence of changes, but
- # how those changes are represented may vary, depending on how they were
- # generated. In all cases we support, we also support the array
- # representation of the changes. The formats are:
- #
- # [ # patchset <- Diff::LCS.diff(a, b)
- # [ # one or more hunks
- # Diff::LCS::Change # one or more changes
- # ] ]
- #
- # [ # patchset, equivalent to the above
- # [ # one or more hunks
- # [ action, line, value ] # one or more changes
- # ] ]
- #
- # [ # patchset <- Diff::LCS.diff(a, b, Diff::LCS::ContextDiffCallbacks)
- # # OR <- Diff::LCS.sdiff(a, b, Diff::LCS::ContextDiffCallbacks)
- # [ # one or more hunks
- # Diff::LCS::ContextChange # one or more changes
- # ] ]
- #
- # [ # patchset, equivalent to the above
- # [ # one or more hunks
- # [ action, [ old line, old value ], [ new line, new value ] ]
- # # one or more changes
- # ] ]
- #
- # [ # patchset <- Diff::LCS.sdiff(a, b)
- # # OR <- Diff::LCS.diff(a, b, Diff::LCS::SDiffCallbacks)
- # Diff::LCS::ContextChange # one or more changes
- # ]
- #
- # [ # patchset, equivalent to the above
- # [ action, [ old line, old value ], [ new line, new value ] ]
- # # one or more changes
- # ]
- #
- # The result of this will be either of the following.
- #
- # [ # patchset
- # Diff::LCS::ContextChange # one or more changes
- # ]
- #
- # [ # patchset
- # Diff::LCS::Change # one or more changes
- # ]
- #
- # If either of the above is provided, it will be returned as such.
- #
+ # Normalize the patchset. A patchset is always a sequence of changes, but
+ # how those changes are represented may vary, depending on how they were
+ # generated. In all cases we support, we also support the array
+ # representation of the changes. The formats are:
+ #
+ # [ # patchset <- Diff::LCS.diff(a, b)
+ # [ # one or more hunks
+ # Diff::LCS::Change # one or more changes
+ # ] ]
+ #
+ # [ # patchset, equivalent to the above
+ # [ # one or more hunks
+ # [ action, line, value ] # one or more changes
+ # ] ]
+ #
+ # [ # patchset <- Diff::LCS.diff(a, b, Diff::LCS::ContextDiffCallbacks)
+ # # OR <- Diff::LCS.sdiff(a, b, Diff::LCS::ContextDiffCallbacks)
+ # [ # one or more hunks
+ # Diff::LCS::ContextChange # one or more changes
+ # ] ]
+ #
+ # [ # patchset, equivalent to the above
+ # [ # one or more hunks
+ # [ action, [ old line, old value ], [ new line, new value ] ]
+ # # one or more changes
+ # ] ]
+ #
+ # [ # patchset <- Diff::LCS.sdiff(a, b)
+ # # OR <- Diff::LCS.diff(a, b, Diff::LCS::SDiffCallbacks)
+ # Diff::LCS::ContextChange # one or more changes
+ # ]
+ #
+ # [ # patchset, equivalent to the above
+ # [ action, [ old line, old value ], [ new line, new value ] ]
+ # # one or more changes
+ # ]
+ #
+ # The result of this will be either of the following.
+ #
+ # [ # patchset
+ # Diff::LCS::ContextChange # one or more changes
+ # ]
+ #
+ # [ # patchset
+ # Diff::LCS::Change # one or more changes
+ # ]
+ #
+ # If either of the above is provided, it will be returned as such.
+ #
+ # Note: This will be deprecated as a public function in a future release.
def __normalize_patchset(patchset)
patchset.map do |hunk|
case hunk
@@ -1103,3 +1101,5 @@ module Diff::LCS
end
end
end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_balanced_spec.rb b/spec/diff_lcs_balanced_spec.rb
new file mode 100644
index 0000000..36e11c9
--- /dev/null
+++ b/spec/diff_lcs_balanced_spec.rb
@@ -0,0 +1,169 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.traverse_balanced should traverse sequences correctly" do
+ include Diff::LCS::SpecHelper::Matchers
+
+ def reverse(change_result)
+ new_result = []
+ change_result.each { |line|
+ line = [ line[0], line[2], line[1] ]
+ case line[0]
+ when '<'
+ line[0] = '>'
+ when '>'
+ line[0] = '<'
+ end
+ new_result << line
+ }
+ new_result.sort_by { |line| line[1] }
+ end
+
+ def no_change(change_result)
+ new_result = []
+ change_result.each { |line|
+ case line[0]
+ when '!'
+ new_result << [ '<', line[1], line[2] ]
+ new_result << [ '>', line[1] + 1, line[2] ]
+ else
+ new_result << line
+ end
+ }
+ new_result
+ end
+
+ def traverse(s1, s2, callback_type)
+ callback = __send__(callback_type)
+ Diff::LCS.traverse_balanced(s1, s2, callback)
+ callback
+ end
+
+ def do_balanced_traversal(s1, s2, result)
+ balanced_s1_s2 = traverse(s1, s2, :balanced_callback)
+ balanced_s2_s1 = traverse(s2, s1, :balanced_callback)
+ balanced_s1_s2_no_change = traverse(s1, s2, :balanced_callback_no_change)
+ balanced_s2_s1_no_change = traverse(s2, s1, :balanced_callback_no_change)
+
+ balanced_s1_s2.result.should == result
+ balanced_s2_s1.result.should == reverse(result)
+
+ balanced_s1_s2_no_change.result.should == no_change(result)
+ balanced_s2_s1_no_change.result.should == no_change(reverse(result))
+ end
+
+ it "sequence-a" do
+ s1 = %w(a b c)
+ s2 = %w(a x c)
+
+ result = [
+ [ '=', 0, 0 ],
+ [ '!', 1, 1 ],
+ [ '=', 2, 2 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-b" do
+ s1 = %w(a x y c)
+ s2 = %w(a v w c)
+
+ result = [
+ [ '=', 0, 0 ],
+ [ '!', 1, 1 ],
+ [ '!', 2, 2 ],
+ [ '=', 3, 3 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-c" do
+ s1 = %w(x y c)
+ s2 = %w(v w c)
+ result = [
+ [ '!', 0, 0 ],
+ [ '!', 1, 1 ],
+ [ '=', 2, 2 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-d" do
+ s1 = %w(a x y z)
+ s2 = %w(b v w)
+ result = [
+ [ '!', 0, 0 ],
+ [ '!', 1, 1 ],
+ [ '!', 2, 2 ],
+ [ '<', 3, 3 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-e" do
+ s1 = %w(a z)
+ s2 = %w(a)
+ result = [
+ [ '=', 0, 0 ],
+ [ '<', 1, 1 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-f" do
+ s1 = %w(z a)
+ s2 = %w(a)
+ result = [
+ [ '<', 0, 0 ],
+ [ '=', 1, 0 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-g" do
+ s1 = %w(a b c)
+ s2 = %w(x y z)
+ result = [
+ [ '!', 0, 0 ],
+ [ '!', 1, 1 ],
+ [ '!', 2, 2 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-h" do
+ s1 = %w(abcd efgh ijkl mnopqrstuvwxyz)
+ s2 = []
+ result = [
+ [ '<', 0, 0 ],
+ [ '<', 1, 0 ],
+ [ '<', 2, 0 ],
+ [ '<', 3, 0 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+
+ it "sequence-i" do
+ s1 = []
+ s2 = %w(abcd efgh ijkl mnopqrstuvwxyz)
+ result = [
+ [ '>', 0, 0 ],
+ [ '>', 0, 1 ],
+ [ '>', 0, 2 ],
+ [ '>', 0, 3 ]
+ ]
+
+ do_balanced_traversal(s1, s2, result)
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_diff_spec.rb b/spec/diff_lcs_diff_spec.rb
new file mode 100644
index 0000000..43ea5c7
--- /dev/null
+++ b/spec/diff_lcs_diff_spec.rb
@@ -0,0 +1,33 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.diff" do
+ include Diff::LCS::SpecHelper::Matchers
+
+ it "should correctly diff the sequences" do
+ diff_s1_s2 = Diff::LCS.diff(seq1, seq2)
+ diff_s2_s1 = Diff::LCS.diff(seq2, seq1)
+
+ change_diff(correct_forward_diff).should == diff_s1_s2
+ change_diff(correct_backward_diff).should == diff_s2_s1
+ end
+
+ it "should correctly diff against an empty sequence" do
+ diff = Diff::LCS.diff(word_sequence, [])
+ correct_diff = [
+ [ [ '-', 0, 'abcd' ],
+ [ '-', 1, 'efgh' ],
+ [ '-', 2, 'ijkl' ],
+ [ '-', 3, 'mnopqrstuvwxyz' ] ]
+ ]
+
+ change_diff(correct_diff).should == diff
+
+ diff = Diff::LCS.diff([], word_sequence)
+ correct_diff.each { |hunk| hunk.each { |change| change[0] = '+' } }
+ change_diff(correct_diff).should == diff
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_lcs_spec.rb b/spec/diff_lcs_lcs_spec.rb
new file mode 100644
index 0000000..c95ba61
--- /dev/null
+++ b/spec/diff_lcs_lcs_spec.rb
@@ -0,0 +1,36 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.LCS and Diff::LCS.__lcs" do
+ include Diff::LCS::SpecHelper::Matchers
+
+ it "should return the correct raw values from Diff::LCS.__lcs" do
+ res = Diff::LCS.__lcs(seq1, seq2)
+ # The result of the LCS (less the +nil+ values) must be as long as the
+ # correct result.
+ res.compact.size.should == correct_lcs.size
+ res.should correctly_map_sequence(seq1).to_other_sequence(seq2)
+
+ # Compact these transformations and they should be the correct LCS.
+ x_seq1 = (0...res.size).map { |ix| res[ix] ? seq1[ix] : nil }.compact
+ x_seq2 = (0...res.size).map { |ix| res[ix] ? seq2[res[ix]] : nil }.compact
+
+ x_seq1.should == correct_lcs
+ x_seq2.should == correct_lcs
+ end
+
+ it "should return the correct compacted values from Diff::LCS.LCS" do
+ res = Diff::LCS.LCS(seq1, seq2)
+ res.should == correct_lcs
+ res.compact.should == res
+ end
+
+ it "should be transitive" do
+ res = Diff::LCS.LCS(seq2, seq1)
+ res.should == correct_lcs
+ res.compact.should == res
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_patch_spec.rb b/spec/diff_lcs_patch_spec.rb
new file mode 100644
index 0000000..7e480e0
--- /dev/null
+++ b/spec/diff_lcs_patch_spec.rb
@@ -0,0 +1,428 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.patch" do
+ include Diff::LCS::SpecHelper::Matchers
+
+ describe "using a Diff::LCS.diff patchset" do
+ describe "with default diff callbacks (DiffCallbacks)" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(seq1, seq2)
+ @patch_set_s2_s1 = Diff::LCS.diff(seq2, seq1)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+
+ describe "with context diff callbacks (ContextDiffCallbacks)" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(seq1, seq2, Diff::LCS::ContextDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.diff(seq2, seq1, Diff::LCS::ContextDiffCallbacks)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+
+ describe "with sdiff callbacks (SDiffCallbacks)" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(seq1, seq2, Diff::LCS::SDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.diff(seq2, seq1, Diff::LCS::SDiffCallbacks)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+ end
+
+ describe "using a Diff::LCS.sdiff patchset" do
+ describe "with default diff callbacks (DiffCallbacks)" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::DiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(seq2, seq1, Diff::LCS::DiffCallbacks)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+
+ describe "with context diff callbacks (ContextDiffCallbacks)" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(seq2, seq1, Diff::LCS::ContextDiffCallbacks)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+
+ describe "with sdiff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(seq1, seq2)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(seq2, seq1)
+ end
+
+ it "should correctly patch left-to-right (patch autodiscovery)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(seq1, @patch_set_s1_s2, :patch).should == seq2
+ Diff::LCS.patch(seq2, @patch_set_s2_s1, :patch).should == seq1
+ Diff::LCS.patch!(seq1, @patch_set_s1_s2).should == seq2
+ Diff::LCS.patch!(seq2, @patch_set_s2_s1).should == seq1
+ end
+
+ it "should correctly patch right-to-left (unpatch autodiscovery)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1).should == seq2
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(seq2, @patch_set_s1_s2, :unpatch).should == seq1
+ Diff::LCS.patch(seq1, @patch_set_s2_s1, :unpatch).should == seq2
+ Diff::LCS.unpatch!(seq2, @patch_set_s1_s2).should == seq1
+ Diff::LCS.unpatch!(seq1, @patch_set_s2_s1).should == seq2
+ end
+ end
+ end
+
+ describe "fix bug 891: patchsets do not contain the last equal part" do
+ before(:each) do
+ @s1 = %w(a b c d e f g h i j k)
+ @s2 = %w(a b c d D e f g h i j k)
+ end
+
+ describe "using Diff::LCS.diff with default diff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2)
+ @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+
+ describe "using Diff::LCS.diff with context diff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2, Diff::LCS::ContextDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1, Diff::LCS::ContextDiffCallbacks)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+
+ describe "using Diff::LCS.diff with sdiff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2, Diff::LCS::SDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1, Diff::LCS::SDiffCallbacks)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+
+ describe "using Diff::LCS.sdiff with default sdiff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(@s1, @s2)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(@s2, @s1)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+
+ describe "using Diff::LCS.sdiff with context diff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(@s1, @s2, Diff::LCS::ContextDiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(@s2, @s1, Diff::LCS::ContextDiffCallbacks)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+
+ describe "using Diff::LCS.xdiff with default diff callbacks" do
+ before(:each) do
+ @patch_set_s1_s2 = Diff::LCS.sdiff(@s1, @s2, Diff::LCS::DiffCallbacks)
+ @patch_set_s2_s1 = Diff::LCS.sdiff(@s2, @s1, Diff::LCS::DiffCallbacks)
+ end
+
+ it "does not autodiscover s1 to s2 patches" do
+ # It should, but it doesn't.
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+
+ expect do
+ Diff::LCS.patch(@s1, @patch_set_s2_s1).should == @s2
+ end.to raise_error(RuntimeError, /provided patchset/)
+ end
+
+ it "should autodiscover s2 to s1 the left-to-right patches" do
+ Diff::LCS.patch(@s2, @patch_set_s2_s1).should == @s1
+ Diff::LCS.patch(@s2, @patch_set_s1_s2).should == @s1
+ end
+
+ it "should correctly patch left-to-right (explicit patch)" do
+ Diff::LCS.patch(@s1, @patch_set_s1_s2, :patch).should == @s2
+ Diff::LCS.patch(@s2, @patch_set_s2_s1, :patch).should == @s1
+ Diff::LCS.patch!(@s1, @patch_set_s1_s2).should == @s2
+ Diff::LCS.patch!(@s2, @patch_set_s2_s1).should == @s1
+ end
+
+ it "should correctly patch right-to-left (explicit unpatch)" do
+ Diff::LCS.patch(@s2, @patch_set_s1_s2, :unpatch).should == @s1
+ Diff::LCS.patch(@s1, @patch_set_s2_s1, :unpatch).should == @s2
+ Diff::LCS.unpatch!(@s2, @patch_set_s1_s2).should == @s1
+ Diff::LCS.unpatch!(@s1, @patch_set_s2_s1).should == @s2
+ end
+ end
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_sdiff_spec.rb b/spec/diff_lcs_sdiff_spec.rb
new file mode 100644
index 0000000..b6a3f10
--- /dev/null
+++ b/spec/diff_lcs_sdiff_spec.rb
@@ -0,0 +1,180 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.sdiff should compare sequences correctly" do
+ include Diff::LCS::SpecHelper::Matchers
+
+ def do_sdiff_comparison(s1, s2, forward_sdiff)
+ sdiff_s1_s2 = Diff::LCS.sdiff(s1, s2)
+ sdiff_s2_s1 = Diff::LCS.sdiff(s2, s1)
+
+ sdiff_s1_s2.should == context_diff(forward_sdiff)
+ sdiff_s2_s1.should == context_diff(reverse_sdiff(forward_sdiff))
+ end
+
+ it "sequence-a" do
+ do_sdiff_comparison(seq1, seq2, correct_forward_sdiff)
+ end
+
+ it "sequence-b" do
+ s1 = %w(abc def yyy xxx ghi jkl)
+ s2 = %w(abc dxf xxx ghi jkl)
+ forward_sdiff = [
+ [ '=', [ 0, 'abc' ], [ 0, 'abc' ] ],
+ [ '!', [ 1, 'def' ], [ 1, 'dxf' ] ],
+ [ '-', [ 2, 'yyy' ], [ 2, nil ] ],
+ [ '=', [ 3, 'xxx' ], [ 2, 'xxx' ] ],
+ [ '=', [ 4, 'ghi' ], [ 3, 'ghi' ] ],
+ [ '=', [ 5, 'jkl' ], [ 4, 'jkl' ] ]
+ ]
+
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-c" do
+ s1 = %w(a b c d e)
+ s2 = %w(a e)
+ forward_sdiff = [
+ [ '=', [ 0, 'a' ], [ 0, 'a' ] ],
+ [ '-', [ 1, 'b' ], [ 1, nil ] ],
+ [ '-', [ 2, 'c' ], [ 1, nil ] ],
+ [ '-', [ 3, 'd' ], [ 1, nil ] ],
+ [ '=', [ 4, 'e' ], [ 1, 'e' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-d" do
+ s1 = %w(a e)
+ s2 = %w(a b c d e)
+ forward_sdiff = [
+ [ '=', [ 0, 'a' ], [ 0, 'a' ] ],
+ [ '+', [ 1, nil ], [ 1, 'b' ] ],
+ [ '+', [ 1, nil ], [ 2, 'c' ] ],
+ [ '+', [ 1, nil ], [ 3, 'd' ] ],
+ [ '=', [ 1, 'e' ], [ 4, 'e' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-e" do
+ s1 = %w(v x a e)
+ s2 = %w(w y a b c d e)
+ forward_sdiff = [
+ [ '!', [ 0, 'v' ], [ 0, 'w' ] ],
+ [ '!', [ 1, 'x' ], [ 1, 'y' ] ],
+ [ '=', [ 2, 'a' ], [ 2, 'a' ] ],
+ [ '+', [ 3, nil ], [ 3, 'b' ] ],
+ [ '+', [ 3, nil ], [ 4, 'c' ] ],
+ [ '+', [ 3, nil ], [ 5, 'd' ] ],
+ [ '=', [ 3, 'e' ], [ 6, 'e' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-f" do
+ s1 = %w(x a e)
+ s2 = %w(a b c d e)
+ forward_sdiff = [
+ [ '-', [ 0, 'x' ], [ 0, nil ] ],
+ [ '=', [ 1, 'a' ], [ 0, 'a' ] ],
+ [ '+', [ 2, nil ], [ 1, 'b' ] ],
+ [ '+', [ 2, nil ], [ 2, 'c' ] ],
+ [ '+', [ 2, nil ], [ 3, 'd' ] ],
+ [ '=', [ 2, 'e' ], [ 4, 'e' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-g" do
+ s1 = %w(a e)
+ s2 = %w(x a b c d e)
+ forward_sdiff = [
+ [ '+', [ 0, nil ], [ 0, 'x' ] ],
+ [ '=', [ 0, 'a' ], [ 1, 'a' ] ],
+ [ '+', [ 1, nil ], [ 2, 'b' ] ],
+ [ '+', [ 1, nil ], [ 3, 'c' ] ],
+ [ '+', [ 1, nil ], [ 4, 'd' ] ],
+ [ '=', [ 1, 'e' ], [ 5, 'e' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-h" do
+ s1 = %w(a e v)
+ s2 = %w(x a b c d e w x)
+ forward_sdiff = [
+ [ '+', [ 0, nil ], [ 0, 'x' ] ],
+ [ '=', [ 0, 'a' ], [ 1, 'a' ] ],
+ [ '+', [ 1, nil ], [ 2, 'b' ] ],
+ [ '+', [ 1, nil ], [ 3, 'c' ] ],
+ [ '+', [ 1, nil ], [ 4, 'd' ] ],
+ [ '=', [ 1, 'e' ], [ 5, 'e' ] ],
+ [ '!', [ 2, 'v' ], [ 6, 'w' ] ],
+ [ '+', [ 3, nil ], [ 7, 'x' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-i" do
+ s1 = %w()
+ s2 = %w(a b c)
+ forward_sdiff = [
+ [ '+', [ 0, nil ], [ 0, 'a' ] ],
+ [ '+', [ 0, nil ], [ 1, 'b' ] ],
+ [ '+', [ 0, nil ], [ 2, 'c' ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-j" do
+ s1 = %w(a b c)
+ s2 = %w()
+ forward_sdiff = [
+ [ '-', [ 0, 'a' ], [ 0, nil ] ],
+ [ '-', [ 1, 'b' ], [ 0, nil ] ],
+ [ '-', [ 2, 'c' ], [ 0, nil ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-k" do
+ s1 = %w(a b c)
+ s2 = %w(1)
+ forward_sdiff = [
+ [ '!', [ 0, 'a' ], [ 0, '1' ] ],
+ [ '-', [ 1, 'b' ], [ 1, nil ] ],
+ [ '-', [ 2, 'c' ], [ 1, nil ] ] ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-l" do
+ s1 = %w(a b c)
+ s2 = %w(c)
+ forward_sdiff = [
+ [ '-', [ 0, 'a' ], [ 0, nil ] ],
+ [ '-', [ 1, 'b' ], [ 0, nil ] ],
+ [ '=', [ 2, 'c' ], [ 0, 'c' ] ]
+ ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-m" do
+ s1 = %w(abcd efgh ijkl mnop)
+ s2 = []
+ forward_sdiff = [
+ [ '-', [ 0, 'abcd' ], [ 0, nil ] ],
+ [ '-', [ 1, 'efgh' ], [ 0, nil ] ],
+ [ '-', [ 2, 'ijkl' ], [ 0, nil ] ],
+ [ '-', [ 3, 'mnop' ], [ 0, nil ] ]
+ ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+
+ it "sequence-n" do
+ s1 = []
+ s2 = %w(abcd efgh ijkl mnop)
+ forward_sdiff = [
+ [ '+', [ 0, nil ], [ 0, 'abcd' ] ],
+ [ '+', [ 0, nil ], [ 1, 'efgh' ] ],
+ [ '+', [ 0, nil ], [ 2, 'ijkl' ] ],
+ [ '+', [ 0, nil ], [ 3, 'mnop' ] ]
+ ]
+ do_sdiff_comparison(s1, s2, forward_sdiff)
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/diff_lcs_sequences_spec.rb b/spec/diff_lcs_sequences_spec.rb
new file mode 100644
index 0000000..c7a5558
--- /dev/null
+++ b/spec/diff_lcs_sequences_spec.rb
@@ -0,0 +1,83 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'spec_helper'
+
+describe "Diff::LCS.traverse_sequences" do
+ describe "callback with no finishers" do
+ before(:each) do
+ @callback_s1_s2 = simple_callback_no_finishers
+ Diff::LCS.traverse_sequences(seq1, seq2, @callback_s1_s2)
+
+ @callback_s2_s1 = simple_callback_no_finishers
+ Diff::LCS.traverse_sequences(seq2, seq1, @callback_s2_s1)
+ end
+
+ it "should have the correct LCS result on left-matches" do
+ @callback_s1_s2.matched_a.should == correct_lcs
+ @callback_s2_s1.matched_a.should == correct_lcs
+ end
+
+ it "should have the correct LCS result on right-matches" do
+ @callback_s1_s2.matched_b.should == correct_lcs
+ @callback_s2_s1.matched_b.should == correct_lcs
+ end
+
+ it "should have the correct skipped sequences for the left sequence" do
+ @callback_s1_s2.discards_a.should == skipped_seq1
+ @callback_s2_s1.discards_a.should == skipped_seq2
+ end
+
+ it "should have the correct skipped sequences for the right sequence" do
+ @callback_s1_s2.discards_b.should == skipped_seq2
+ @callback_s2_s1.discards_b.should == skipped_seq1
+ end
+
+ it "should not have anything done markers from the left or right sequences" do
+ @callback_s1_s2.done_a.should be_empty
+ @callback_s1_s2.done_b.should be_empty
+ @callback_s2_s1.done_a.should be_empty
+ @callback_s2_s1.done_b.should be_empty
+ end
+ end
+
+ describe "callback with finisher" do
+ before(:each) do
+ @callback_s1_s2 = simple_callback
+ Diff::LCS.traverse_sequences(seq1, seq2, @callback_s1_s2)
+ @callback_s2_s1 = simple_callback
+ Diff::LCS.traverse_sequences(seq2, seq1, @callback_s2_s1)
+ end
+
+ it "should have the correct LCS result on left-matches" do
+ @callback_s1_s2.matched_a.should == correct_lcs
+ @callback_s2_s1.matched_a.should == correct_lcs
+ end
+
+ it "should have the correct LCS result on right-matches" do
+ @callback_s1_s2.matched_b.should == correct_lcs
+ @callback_s2_s1.matched_b.should == correct_lcs
+ end
+
+ it "should have the correct skipped sequences for the left sequence" do
+ @callback_s1_s2.discards_a.should == skipped_seq1
+ @callback_s2_s1.discards_a.should == skipped_seq2
+ end
+
+ it "should have the correct skipped sequences for the right sequence" do
+ @callback_s1_s2.discards_b.should == skipped_seq2
+ @callback_s2_s1.discards_b.should == skipped_seq1
+ end
+
+ it "should have done markers differently-sized sequences" do
+ @callback_s1_s2.done_a.should == [[ "p", 9, "s", 10 ]]
+ @callback_s1_s2.done_b.should be_empty
+
+ # 20110731 I don't yet understand why this particular behaviour
+ # isn't transitive.
+ @callback_s2_s1.done_a.should be_empty
+ @callback_s2_s1.done_b.should be_empty
+ end
+ end
+end
+
+# vim: ft=ruby
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..1c934e7
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,270 @@
+# -*- ruby encoding: utf-8 -*-
+
+require 'rubygems'
+require 'bundler'
+
+require 'pathname'
+
+file = Pathname.new(__FILE__).expand_path
+path = file.parent
+parent = path.parent
+
+$:.unshift parent.join('lib')
+
+require 'diff-lcs'
+
+module Diff::LCS::SpecHelper
+ def seq1
+ %w(a b c e h j l m n p)
+ end
+
+ def skipped_seq1
+ %w(a h n p)
+ end
+
+ def seq2
+ %w(b c d e f j k l m r s t)
+ end
+
+ def skipped_seq2
+ %w(d f k r s t)
+ end
+
+ def word_sequence
+ %w(abcd efgh ijkl mnopqrstuvwxyz)
+ end
+
+ def correct_lcs
+ %w(b c e j l m)
+ end
+
+ def correct_forward_diff
+ [
+ [ [ '-', 0, 'a' ] ],
+ [ [ '+', 2, 'd' ] ],
+ [ [ '-', 4, 'h' ],
+ [ '+', 4, 'f' ] ],
+ [ [ '+', 6, 'k' ] ],
+ [ [ '-', 8, 'n' ],
+ [ '-', 9, 'p' ],
+ [ '+', 9, 'r' ],
+ [ '+', 10, 's' ],
+ [ '+', 11, 't' ] ]
+ ]
+ end
+
+ def correct_backward_diff
+ [
+ [ [ '+', 0, 'a' ] ],
+ [ [ '-', 2, 'd' ] ],
+ [ [ '-', 4, 'f' ],
+ [ '+', 4, 'h' ] ],
+ [ [ '-', 6, 'k' ] ],
+ [
+ [ '-', 9, 'r' ],
+ [ '-', 10, 's' ],
+ [ '+', 8, 'n' ],
+ [ '-', 11, 't' ],
+ [ '+', 9, 'p' ] ]
+ ]
+ end
+
+ def correct_forward_sdiff
+ [
+ [ '-', [ 0, 'a' ], [ 0, nil ] ],
+ [ '=', [ 1, 'b' ], [ 0, 'b' ] ],
+ [ '=', [ 2, 'c' ], [ 1, 'c' ] ],
+ [ '+', [ 3, nil ], [ 2, 'd' ] ],
+ [ '=', [ 3, 'e' ], [ 3, 'e' ] ],
+ [ '!', [ 4, 'h' ], [ 4, 'f' ] ],
+ [ '=', [ 5, 'j' ], [ 5, 'j' ] ],
+ [ '+', [ 6, nil ], [ 6, 'k' ] ],
+ [ '=', [ 6, 'l' ], [ 7, 'l' ] ],
+ [ '=', [ 7, 'm' ], [ 8, 'm' ] ],
+ [ '!', [ 8, 'n' ], [ 9, 'r' ] ],
+ [ '!', [ 9, 'p' ], [ 10, 's' ] ],
+ [ '+', [ 10, nil ], [ 11, 't' ] ]
+ ]
+ end
+
+ def reverse_sdiff(forward_sdiff)
+ forward_sdiff.map { |line|
+ line[1], line[2] = line[2], line[1]
+ case line[0]
+ when '-' then line[0] = '+'
+ when '+' then line[0] = '-'
+ end
+ line
+ }
+ end
+
+ def change_diff(diff)
+ map_diffs(diff, Diff::LCS::Change)
+ end
+
+ def context_diff(diff)
+ map_diffs(diff, Diff::LCS::ContextChange)
+ end
+
+ def format_diffs(diffs)
+ diffs.map do |e|
+ if e.kind_of?(Array)
+ e.map { |f| f.to_a.join }.join(", ")
+ else
+ e.to_a.join
+ end
+ end.join("\n")
+ end
+
+ def map_diffs(diffs, klass = Diff::LCS::ContextChange)
+ diffs.map do |chunks|
+ if klass == Diff::LCS::ContextChange
+ klass.from_a(chunks)
+ else
+ chunks.map { |changes| klass.from_a(changes) }
+ end
+ end
+ end
+
+ def simple_callback
+ callbacks = Object.new
+ class << callbacks
+ attr_reader :matched_a
+ attr_reader :matched_b
+ attr_reader :discards_a
+ attr_reader :discards_b
+ attr_reader :done_a
+ attr_reader :done_b
+
+ def reset
+ @matched_a = []
+ @matched_b = []
+ @discards_a = []
+ @discards_b = []
+ @done_a = []
+ @done_b = []
+ end
+
+ def match(event)
+ @matched_a << event.old_element
+ @matched_b << event.new_element
+ end
+
+ def discard_b(event)
+ @discards_b << event.new_element
+ end
+
+ def discard_a(event)
+ @discards_a << event.old_element
+ end
+
+ def finished_a(event)
+ @done_a << [event.old_element, event.old_position,
+ event.new_element, event.new_position]
+ end
+
+ def finished_b(event)
+ p "called #finished_b"
+ @done_b << [event.old_element, event.old_position,
+ event.new_element, event.new_position]
+ end
+ end
+ callbacks.reset
+ callbacks
+ end
+
+ def simple_callback_no_finishers
+ simple = simple_callback
+ class << simple
+ undef :finished_a
+ undef :finished_b
+ end
+ simple
+ end
+
+ def balanced_callback
+ cb = Object.new
+ class << cb
+ attr_reader :result
+
+ def reset
+ @result = []
+ end
+
+ def match(event)
+ @result << [ "=", event.old_position, event.new_position ]
+ end
+
+ def discard_a(event)
+ @result << [ "<", event.old_position, event.new_position ]
+ end
+
+ def discard_b(event)
+ @result << [ ">", event.old_position, event.new_position ]
+ end
+
+ def change(event)
+ @result << [ "!", event.old_position, event.new_position ]
+ end
+ end
+ cb.reset
+ cb
+ end
+
+ def balanced_callback_no_change
+ balanced = balanced_callback
+ class << balanced
+ undef :change
+ end
+ balanced
+ end
+
+ module Matchers
+ extend RSpec::Matchers::DSL
+
+ matcher :be_nil_or_match_values do |ii, s1, s2|
+ match do |ee|
+ ee.should satisfy { |vee| vee.nil? || s1[ii] == s2[ee] }
+ end
+ end
+
+ matcher :correctly_map_sequence do |s1|
+ match do |actual|
+ actual.each_with_index { |ee, ii|
+ ee.should be_nil_or_match_values(ii, s1, @s2)
+ }
+ end
+
+ chain :to_other_sequence do |s2|
+ @s2 = s2
+ end
+ end
+ end
+end
+
+RSpec.configure do |conf|
+ conf.include Diff::LCS::SpecHelper
+end
+
+
+=begin
+RSpec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+
+ failure_message_for_should do |actual|
+ "expected that #{actual} would be a multiple of #{expected}"
+ end
+
+ failure_message_for_should_not do |actual|
+ "expected that #{actual} would not be a multiple of #{expected}"
+ end
+
+ description do
+ "be multiple of #{expected}"
+ end
+end
+=end
+
+# vim: ft=ruby
diff --git a/tests/00test.rb b/tests/00test.rb
deleted file mode 100644
index 8872dfd..0000000
--- a/tests/00test.rb
+++ /dev/null
@@ -1,626 +0,0 @@
-#! /usr/bin/env ruby
-#
-$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
-
-require 'diff/lcs'
-require 'test/unit'
-require 'pp'
-require 'diff/lcs/array'
-
-module Diff::LCS::Tests
- def __format_diffs(diffs)
- diffs.map do |e|
- if e.kind_of?(Array)
- e.map { |f| f.to_a.join }.join(", ")
- else
- e.to_a.join
- end
- end.join("; ")
- end
-
- def __map_diffs(diffs, klass = Diff::LCS::ContextChange)
- diffs.map do |chunks|
- if klass == Diff::LCS::ContextChange
- klass.from_a(chunks)
- else
- chunks.map { |changes| klass.from_a(changes) }
- end
- end
- end
-
- def __simple_callbacks
- callbacks = Object.new
- class << callbacks
- attr_reader :matched_a
- attr_reader :matched_b
- attr_reader :discards_a
- attr_reader :discards_b
- attr_reader :done_a
- attr_reader :done_b
-
- def reset
- @matched_a = []
- @matched_b = []
- @discards_a = []
- @discards_b = []
- @done_a = []
- @done_b = []
- end
-
- def match(event)
- @matched_a << event.old_element
- @matched_b << event.new_element
- end
-
- def discard_b(event)
- @discards_b << event.new_element
- end
-
- def discard_a(event)
- @discards_a << event.old_element
- end
-
- def finished_a(event)
- @done_a << [event.old_element, event.old_position]
- end
-
- def finished_b(event)
- @done_b << [event.new_element, event.new_position]
- end
- end
- callbacks.reset
- callbacks
- end
-
- def __balanced_callback
- cb = Object.new
- class << cb
- attr_reader :result
-
- def reset
- @result = ""
- end
-
- def match(event)
- @result << "M#{event.old_position}#{event.new_position} "
- end
-
- def discard_a(event)
- @result << "DA#{event.old_position}#{event.new_position} "
- end
-
- def discard_b(event)
- @result << "DB#{event.old_position}#{event.new_position} "
- end
-
- def change(event)
- @result << "C#{event.old_position}#{event.new_position} "
- end
- end
- cb.reset
- cb
- end
-
- def setup
- @seq1 = %w(a b c e h j l m n p)
- @seq2 = %w(b c d e f j k l m r s t)
-
- @correct_lcs = %w(b c e j l m)
-
- @skipped_seq1 = 'a h n p'
- @skipped_seq2 = 'd f k r s t'
-
- correct_diff = [
- [ [ '-', 0, 'a' ] ],
- [ [ '+', 2, 'd' ] ],
- [ [ '-', 4, 'h' ],
- [ '+', 4, 'f' ] ],
- [ [ '+', 6, 'k' ] ],
- [ [ '-', 8, 'n' ],
- [ '-', 9, 'p' ],
- [ '+', 9, 'r' ],
- [ '+', 10, 's' ],
- [ '+', 11, 't' ] ] ]
- @correct_diff = __map_diffs(correct_diff, Diff::LCS::Change)
- end
-end
-
-class TestLCS < Test::Unit::TestCase
- include Diff::LCS::Tests
-
- def test_lcs
- res = ares = bres = nil
- assert_nothing_raised { res = Diff::LCS.__lcs(@seq1, @seq2) }
- # The result of the LCS (less the +nil+ values) must be as long as the
- # correct result.
- assert_equal(res.compact.size, @correct_lcs.size)
- res.each_with_index { |ee, ii| assert(ee.nil? || (@seq1[ii] == @seq2[ee])) }
- assert_nothing_raised { ares = (0...res.size).map { |ii| res[ii] ? @seq1[ii] : nil } }
- assert_nothing_raised { bres = (0...res.size).map { |ii| res[ii] ? @seq2[res[ii]] : nil } }
- assert_equal(@correct_lcs, ares.compact)
- assert_equal(@correct_lcs, bres.compact)
- assert_nothing_raised { res = Diff::LCS.LCS(@seq1, @seq2) }
- assert_equal(res.compact, @correct_lcs)
- end
-end
-
-class TestSequences < Test::Unit::TestCase
- include Diff::LCS::Tests
-
- def test_sequences
- callbacks = nil
- assert_nothing_raised do
- callbacks = __simple_callbacks
- class << callbacks
- undef :finished_a
- undef :finished_b
- end
- Diff::LCS.traverse_sequences(@seq1, @seq2, callbacks)
- end
- assert_equal(@correct_lcs.size, callbacks.matched_a.size)
- assert_equal(@correct_lcs.size, callbacks.matched_b.size)
- assert_equal(@skipped_seq1, callbacks.discards_a.join(" "))
- assert_equal(@skipped_seq2, callbacks.discards_b.join(" "))
- assert_nothing_raised do
- callbacks = __simple_callbacks
- Diff::LCS.traverse_sequences(@seq1, @seq2, callbacks)
- end
- assert_equal(@correct_lcs.size, callbacks.matched_a.size)
- assert_equal(@correct_lcs.size, callbacks.matched_b.size)
- assert_equal(@skipped_seq1, callbacks.discards_a.join(" "))
- assert_equal(@skipped_seq2, callbacks.discards_b.join(" "))
- assert_equal(9, callbacks.done_a[0][1])
- assert_nil(callbacks.done_b[0])
-
-# seqw = %w(abcd efgh ijkl mnopqrstuvwxyz)
-# assert_nothing_raised do
-# callbacks = __simple_callbacks
-# class << callbacks
-# undef :finished_a
-# undef :finished_b
-# end
-# Diff::LCS.traverse_sequences(seqw, [], callbacks)
-# end
- end
-
- def test_diff
- diff = nil
- assert_nothing_raised { diff = Diff::LCS.diff(@seq1, @seq2) }
- assert_equal(__format_diffs(@correct_diff), __format_diffs(diff))
- assert_equal(@correct_diff, diff)
- end
-
- def test_diff_empty
- seqw = %w(abcd efgh ijkl mnopqrstuvwxyz)
- correct_diff = [
- [ [ '-', 0, 'abcd' ],
- [ '-', 1, 'efgh' ],
- [ '-', 2, 'ijkl' ],
- [ '-', 3, 'mnopqrstuvwxyz' ] ] ]
- diff = nil
-
- assert_nothing_raised { diff = Diff::LCS.diff(seqw, []) }
- assert_equal(__format_diffs(correct_diff), __format_diffs(diff))
-
- correct_diff = [
- [ [ '+', 0, 'abcd' ],
- [ '+', 1, 'efgh' ],
- [ '+', 2, 'ijkl' ],
- [ '+', 3, 'mnopqrstuvwxyz' ] ] ]
- assert_nothing_raised { diff = Diff::LCS.diff([], seqw) }
- assert_equal(__format_diffs(correct_diff), __format_diffs(diff))
- end
-end
-
-class TestBalanced < Test::Unit::TestCase
- include Diff::LCS::Tests
-
- def test_sdiff_a
- sdiff = nil
- seq1 = %w(abc def yyy xxx ghi jkl)
- seq2 = %w(abc dxf xxx ghi jkl)
- correct_sdiff = [
- [ '=', [ 0, 'abc' ], [ 0, 'abc' ] ],
- [ '!', [ 1, 'def' ], [ 1, 'dxf' ] ],
- [ '-', [ 2, 'yyy' ], [ 2, nil ] ],
- [ '=', [ 3, 'xxx' ], [ 2, 'xxx' ] ],
- [ '=', [ 4, 'ghi' ], [ 3, 'ghi' ] ],
- [ '=', [ 5, 'jkl' ], [ 4, 'jkl' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_b
- sdiff = nil
- correct_sdiff = [
- [ '-', [ 0, 'a' ], [ 0, nil ] ],
- [ '=', [ 1, 'b' ], [ 0, 'b' ] ],
- [ '=', [ 2, 'c' ], [ 1, 'c' ] ],
- [ '+', [ 3, nil ], [ 2, 'd' ] ],
- [ '=', [ 3, 'e' ], [ 3, 'e' ] ],
- [ '!', [ 4, 'h' ], [ 4, 'f' ] ],
- [ '=', [ 5, 'j' ], [ 5, 'j' ] ],
- [ '+', [ 6, nil ], [ 6, 'k' ] ],
- [ '=', [ 6, 'l' ], [ 7, 'l' ] ],
- [ '=', [ 7, 'm' ], [ 8, 'm' ] ],
- [ '!', [ 8, 'n' ], [ 9, 'r' ] ],
- [ '!', [ 9, 'p' ], [ 10, 's' ] ],
- [ '+', [ 10, nil ], [ 11, 't' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(@seq1, @seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_c
- sdiff = nil
- seq1 = %w(a b c d e)
- seq2 = %w(a e)
- correct_sdiff = [
- [ '=', [ 0, 'a' ], [ 0, 'a' ] ],
- [ '-', [ 1, 'b' ], [ 1, nil ] ],
- [ '-', [ 2, 'c' ], [ 1, nil ] ],
- [ '-', [ 3, 'd' ], [ 1, nil ] ],
- [ '=', [ 4, 'e' ], [ 1, 'e' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_d
- sdiff = nil
- seq1 = %w(a e)
- seq2 = %w(a b c d e)
- correct_sdiff = [
- [ '=', [ 0, 'a' ], [ 0, 'a' ] ],
- [ '+', [ 1, nil ], [ 1, 'b' ] ],
- [ '+', [ 1, nil ], [ 2, 'c' ] ],
- [ '+', [ 1, nil ], [ 3, 'd' ] ],
- [ '=', [ 1, 'e' ], [ 4, 'e' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_e
- sdiff = nil
- seq1 = %w(v x a e)
- seq2 = %w(w y a b c d e)
- correct_sdiff = [
- [ '!', [ 0, 'v' ], [ 0, 'w' ] ],
- [ '!', [ 1, 'x' ], [ 1, 'y' ] ],
- [ '=', [ 2, 'a' ], [ 2, 'a' ] ],
- [ '+', [ 3, nil ], [ 3, 'b' ] ],
- [ '+', [ 3, nil ], [ 4, 'c' ] ],
- [ '+', [ 3, nil ], [ 5, 'd' ] ],
- [ '=', [ 3, 'e' ], [ 6, 'e' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_f
- sdiff = nil
- seq1 = %w(x a e)
- seq2 = %w(a b c d e)
- correct_sdiff = [
- [ '-', [ 0, 'x' ], [ 0, nil ] ],
- [ '=', [ 1, 'a' ], [ 0, 'a' ] ],
- [ '+', [ 2, nil ], [ 1, 'b' ] ],
- [ '+', [ 2, nil ], [ 2, 'c' ] ],
- [ '+', [ 2, nil ], [ 3, 'd' ] ],
- [ '=', [ 2, 'e' ], [ 4, 'e' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_g
- sdiff = nil
- seq1 = %w(a e)
- seq2 = %w(x a b c d e)
- correct_sdiff = [
- [ '+', [ 0, nil ], [ 0, 'x' ] ],
- [ '=', [ 0, 'a' ], [ 1, 'a' ] ],
- [ '+', [ 1, nil ], [ 2, 'b' ] ],
- [ '+', [ 1, nil ], [ 3, 'c' ] ],
- [ '+', [ 1, nil ], [ 4, 'd' ] ],
- [ '=', [ 1, 'e' ], [ 5, 'e' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_h
- sdiff = nil
- seq1 = %w(a e v)
- seq2 = %w(x a b c d e w x)
- correct_sdiff = [
- [ '+', [ 0, nil ], [ 0, 'x' ] ],
- [ '=', [ 0, 'a' ], [ 1, 'a' ] ],
- [ '+', [ 1, nil ], [ 2, 'b' ] ],
- [ '+', [ 1, nil ], [ 3, 'c' ] ],
- [ '+', [ 1, nil ], [ 4, 'd' ] ],
- [ '=', [ 1, 'e' ], [ 5, 'e' ] ],
- [ '!', [ 2, 'v' ], [ 6, 'w' ] ],
- [ '+', [ 3, nil ], [ 7, 'x' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_i
- sdiff = nil
- seq1 = %w()
- seq2 = %w(a b c)
- correct_sdiff = [
- [ '+', [ 0, nil ], [ 0, 'a' ] ],
- [ '+', [ 0, nil ], [ 1, 'b' ] ],
- [ '+', [ 0, nil ], [ 2, 'c' ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_j
- sdiff = nil
- seq1 = %w(a b c)
- seq2 = %w()
- correct_sdiff = [
- [ '-', [ 0, 'a' ], [ 0, nil ] ],
- [ '-', [ 1, 'b' ], [ 0, nil ] ],
- [ '-', [ 2, 'c' ], [ 0, nil ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_k
- sdiff = nil
- seq1 = %w(a b c)
- seq2 = %w(1)
- correct_sdiff = [
- [ '!', [ 0, 'a' ], [ 0, '1' ] ],
- [ '-', [ 1, 'b' ], [ 1, nil ] ],
- [ '-', [ 2, 'c' ], [ 1, nil ] ] ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_l
- sdiff = nil
- seq1 = %w(a b c)
- seq2 = %w(c)
- correct_sdiff = [
- [ '-', [ 0, 'a' ], [ 0, nil ] ],
- [ '-', [ 1, 'b' ], [ 0, nil ] ],
- [ '=', [ 2, 'c' ], [ 0, 'c' ] ]
- ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_m
- sdiff = nil
- seq1 = %w(abcd efgh ijkl mnop)
- seq2 = []
- correct_sdiff = [
- [ '-', [ 0, 'abcd' ], [ 0, nil ] ],
- [ '-', [ 1, 'efgh' ], [ 0, nil ] ],
- [ '-', [ 2, 'ijkl' ], [ 0, nil ] ],
- [ '-', [ 3, 'mnop' ], [ 0, nil ] ]
- ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_sdiff_n
- sdiff = nil
- seq1 = []
- seq2 = %w(abcd efgh ijkl mnop)
- correct_sdiff = [
- [ '+', [ 0, nil ], [ 0, 'abcd' ] ],
- [ '+', [ 0, nil ], [ 1, 'efgh' ] ],
- [ '+', [ 0, nil ], [ 2, 'ijkl' ] ],
- [ '+', [ 0, nil ], [ 3, 'mnop' ] ]
- ]
- correct_sdiff = __map_diffs(correct_sdiff)
- assert_nothing_raised { sdiff = Diff::LCS.sdiff(seq1, seq2) }
- assert_equal(correct_sdiff, sdiff)
- end
-
- def test_balanced_a
- seq1 = %w(a b c)
- seq2 = %w(a x c)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("M00 C11 M22 ", callback.result)
- end
-
- def test_balanced_b
- seq1 = %w(a b c)
- seq2 = %w(a x c)
- callback = nil
- assert_nothing_raised do
- callback = __balanced_callback
- class << callback
- undef change
- end
- end
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("M00 DA11 DB21 M22 ", callback.result)
- end
-
- def test_balanced_c
- seq1 = %w(a x y c)
- seq2 = %w(a v w c)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("M00 C11 C22 M33 ", callback.result)
- end
-
- def test_balanced_d
- seq1 = %w(x y c)
- seq2 = %w(v w c)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("C00 C11 M22 ", callback.result)
- end
-
- def test_balanced_e
- seq1 = %w(a x y z)
- seq2 = %w(b v w)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("C00 C11 C22 DA33 ", callback.result)
- end
-
- def test_balanced_f
- seq1 = %w(a z)
- seq2 = %w(a)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("M00 DA11 ", callback.result)
- end
-
- def test_balanced_g
- seq1 = %w(z a)
- seq2 = %w(a)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("DA00 M10 ", callback.result)
- end
-
- def test_balanced_h
- seq1 = %w(a b c)
- seq2 = %w(x y z)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("C00 C11 C22 ", callback.result)
- end
-
- def test_balanced_i
- seq1 = %w(abcd efgh ijkl mnopqrstuvwxyz)
- seq2 = []
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("DA00 DA10 DA20 DA30 ", callback.result)
- end
-
- def test_balanced_j
- seq1 = []
- seq2 = %w(abcd efgh ijkl mnopqrstuvwxyz)
- callback = nil
- assert_nothing_raised { callback = __balanced_callback }
- assert_nothing_raised { Diff::LCS.traverse_balanced(seq1, seq2, callback) }
- assert_equal("DB00 DB01 DB02 DB03 ", callback.result)
- end
-end
-
-class TestPatching < Test::Unit::TestCase
- include Diff::LCS::Tests
-
- def test_patch_diff
- ps = ms1 = ms2 = ms3 = nil
- assert_nothing_raised do
- ps = Diff::LCS.diff(@seq1, @seq2)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms3 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- assert_nothing_raised do
- ps = Diff::LCS.diff(@seq1, @seq2, Diff::LCS::ContextDiffCallbacks)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms2 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- assert_nothing_raised do
- ps = Diff::LCS.diff(@seq1, @seq2, Diff::LCS::SDiffCallbacks)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms3 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- end
-
- # Tests patch bug #891:
- # http://rubyforge.org/tracker/?func=detail&atid=407&aid=891&group_id=84
- def test_patch_bug891
- s1 = s2 = s3 = s4 = s5 = ps = nil
- assert_nothing_raised do
- s1 = %w{a b c d e f g h i j k }
- s2 = %w{a b c d D e f g h i j k }
- ps = Diff::LCS::diff(s1, s2)
- s3 = Diff::LCS.patch(s1, ps, :patch)
- ps = Diff::LCS::diff(s1, s2, Diff::LCS::ContextDiffCallbacks)
- s4 = Diff::LCS.patch(s1, ps, :patch)
- ps = Diff::LCS::diff(s1, s2, Diff::LCS::SDiffCallbacks)
- s5 = Diff::LCS.patch(s1, ps, :patch)
- end
- assert_equal(s2, s3)
- assert_equal(s2, s4)
- assert_equal(s2, s5)
-
- assert_nothing_raised do
- ps = Diff::LCS::sdiff(s1, s2)
- s3 = Diff::LCS.patch(s1, ps, :patch)
- ps = Diff::LCS::diff(s1, s2, Diff::LCS::ContextDiffCallbacks)
- s4 = Diff::LCS.patch(s1, ps, :patch)
- ps = Diff::LCS::diff(s1, s2, Diff::LCS::DiffCallbacks)
- s5 = Diff::LCS.patch(s1, ps, :patch)
- end
- assert_equal(s2, s3)
- assert_equal(s2, s4)
- assert_equal(s2, s5)
- end
-
- def test_patch_sdiff
- ps = ms1 = ms2 = ms3 = nil
- assert_nothing_raised do
- ps = Diff::LCS.sdiff(@seq1, @seq2)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms3 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- assert_nothing_raised do
- ps = Diff::LCS.sdiff(@seq1, @seq2, Diff::LCS::ContextDiffCallbacks)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms3 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- assert_nothing_raised do
- ps = Diff::LCS.sdiff(@seq1, @seq2, Diff::LCS::DiffCallbacks)
- ms1 = Diff::LCS.patch(@seq1, ps)
- ms2 = Diff::LCS.patch(@seq2, ps, :unpatch)
- ms3 = Diff::LCS.patch(@seq2, ps)
- end
- assert_equal(@seq2, ms1)
- assert_equal(@seq1, ms2)
- assert_equal(@seq1, ms3)
- end
-end