diff-lcs 1.2.2 → 1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +0 -1
  3. data/Code-of-Conduct.md +74 -0
  4. data/Contributing.md +84 -0
  5. data/History.md +247 -0
  6. data/{License.rdoc → License.md} +0 -0
  7. data/Manifest.txt +15 -9
  8. data/README.rdoc +21 -18
  9. data/Rakefile +35 -23
  10. data/autotest/discover.rb +3 -1
  11. data/bin/htmldiff +7 -4
  12. data/bin/ldiff +4 -1
  13. data/lib/diff-lcs.rb +1 -1
  14. data/lib/diff/lcs.rb +181 -254
  15. data/lib/diff/lcs/array.rb +1 -1
  16. data/lib/diff/lcs/backports.rb +9 -0
  17. data/lib/diff/lcs/block.rb +1 -1
  18. data/lib/diff/lcs/callbacks.rb +15 -12
  19. data/lib/diff/lcs/change.rb +34 -37
  20. data/lib/diff/lcs/htmldiff.rb +17 -16
  21. data/lib/diff/lcs/hunk.rb +59 -47
  22. data/lib/diff/lcs/internals.rb +44 -40
  23. data/lib/diff/lcs/ldiff.rb +45 -65
  24. data/lib/diff/lcs/string.rb +1 -1
  25. data/spec/change_spec.rb +31 -7
  26. data/spec/diff_spec.rb +28 -18
  27. data/spec/fixtures/aX +1 -0
  28. data/spec/fixtures/bXaX +1 -0
  29. data/spec/fixtures/ds1.csv +50 -0
  30. data/spec/fixtures/ds2.csv +51 -0
  31. data/spec/fixtures/ldiff/output.diff +4 -0
  32. data/spec/fixtures/ldiff/output.diff-c +7 -0
  33. data/spec/fixtures/ldiff/output.diff-e +3 -0
  34. data/spec/fixtures/ldiff/output.diff-f +3 -0
  35. data/spec/fixtures/ldiff/output.diff-u +5 -0
  36. data/spec/hunk_spec.rb +54 -45
  37. data/spec/issues_spec.rb +50 -17
  38. data/spec/lcs_spec.rb +24 -22
  39. data/spec/ldiff_spec.rb +72 -0
  40. data/spec/patch_spec.rb +182 -180
  41. data/spec/sdiff_spec.rb +99 -87
  42. data/spec/spec_helper.rb +141 -58
  43. data/spec/traverse_balanced_spec.rb +177 -177
  44. data/spec/traverse_sequences_spec.rb +63 -63
  45. metadata +100 -169
  46. data.tar.gz.sig +0 -0
  47. data/.autotest +0 -3
  48. data/.gemtest +0 -0
  49. data/.hoerc +0 -2
  50. data/.travis.yml +0 -22
  51. data/Contributing.rdoc +0 -64
  52. data/Gemfile +0 -19
  53. data/History.rdoc +0 -117
  54. data/diff-lcs.gemspec +0 -63
  55. metadata.gz.sig +0 -4
@@ -1,41 +1,51 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe "Diff::LCS.diff" do
5
+ describe Diff::LCS, '.diff' do
6
6
  include Diff::LCS::SpecHelper::Matchers
7
7
 
8
- it "should correctly diff seq1 to seq2" do
8
+ it 'correctly diffs seq1 to seq2' do
9
9
  diff_s1_s2 = Diff::LCS.diff(seq1, seq2)
10
- change_diff(correct_forward_diff).should == diff_s1_s2
10
+ expect(change_diff(correct_forward_diff)).to eq(diff_s1_s2)
11
11
  end
12
12
 
13
- it "should correctly diff seq2 to seq1" do
13
+ it 'correctly diffs seq2 to seq1' do
14
14
  diff_s2_s1 = Diff::LCS.diff(seq2, seq1)
15
- change_diff(correct_backward_diff).should == diff_s2_s1
15
+ expect(change_diff(correct_backward_diff)).to eq(diff_s2_s1)
16
16
  end
17
17
 
18
- it "should correctly diff against an empty sequence" do
18
+ it 'correctly diffs against an empty sequence' do
19
19
  diff = Diff::LCS.diff(word_sequence, [])
20
20
  correct_diff = [
21
- [ [ '-', 0, 'abcd' ],
22
- [ '-', 1, 'efgh' ],
23
- [ '-', 2, 'ijkl' ],
24
- [ '-', 3, 'mnopqrstuvwxyz' ] ]
21
+ [
22
+ ['-', 0, 'abcd'],
23
+ ['-', 1, 'efgh'],
24
+ ['-', 2, 'ijkl'],
25
+ ['-', 3, 'mnopqrstuvwxyz']
26
+ ]
25
27
  ]
26
28
 
27
- change_diff(correct_diff).should == diff
29
+ expect(change_diff(correct_diff)).to eq(diff)
28
30
 
29
31
  diff = Diff::LCS.diff([], word_sequence)
30
- correct_diff.each { |hunk| hunk.each { |change| change[0] = '+' } }
31
- change_diff(correct_diff).should == diff
32
+ correct_diff.each do |hunk|
33
+ hunk.each do |change| change[0] = '+' end
34
+ end
35
+ expect(change_diff(correct_diff)).to eq(diff)
32
36
  end
33
37
 
34
- it "should return an empty diff with (hello, hello)" do
35
- Diff::LCS.diff(hello, hello).should == []
38
+ it "correctly diffs 'xx' and 'xaxb'" do
39
+ left = 'xx'
40
+ right = 'xaxb'
41
+ expect(Diff::LCS.patch(left, Diff::LCS.diff(left, right))).to eq(right)
36
42
  end
37
43
 
38
- it "should return an empty diff with (hello_ary, hello_ary)" do
39
- Diff::LCS.diff(hello_ary, hello_ary).should == []
44
+ it 'returns an empty diff with (hello, hello)' do
45
+ expect(Diff::LCS.diff(hello, hello)).to be_empty
46
+ end
47
+
48
+ it 'returns an empty diff with (hello_ary, hello_ary)' do
49
+ expect(Diff::LCS.diff(hello_ary, hello_ary)).to be_empty
40
50
  end
41
51
  end
@@ -0,0 +1 @@
1
+ aX
@@ -0,0 +1 @@
1
+ bXaX
@@ -0,0 +1,50 @@
1
+ 1,3
2
+ 2,7
3
+ 3,13
4
+ 4,21
5
+ 5,31
6
+ 6,43
7
+ 7,57
8
+ 8,73
9
+ 9,91
10
+ 10,111
11
+ 11,133
12
+ 12,157
13
+ 13,183
14
+ 14,211
15
+ 15,241
16
+ 16,273
17
+ 17,307
18
+ 18,343
19
+ 19,381
20
+ 20,421
21
+ 21,463
22
+ 22,507
23
+ 23,553
24
+ 24,601
25
+ 25,651
26
+ 26,703
27
+ 27,757
28
+ 28,813
29
+ 29,871
30
+ 30,931
31
+ 31,993
32
+ 32,1057
33
+ 33,1123
34
+ 34,1191
35
+ 35,1261
36
+ 36,1333
37
+ 37,1407
38
+ 38,1483
39
+ 39,1561
40
+ 40,1641
41
+ 41,1723
42
+ 42,1807
43
+ 43,1893
44
+ 44,1981
45
+ 45,2071
46
+ 46,2163
47
+ 47,2257
48
+ 48,2353
49
+ 49,2451
50
+ 50,2500
@@ -0,0 +1,51 @@
1
+ 1,3
2
+ 2,7
3
+ 3,13
4
+ 4,21
5
+ 5,31
6
+ 6,42
7
+ 7,57
8
+ 8,73
9
+ 9,91
10
+ 10,111
11
+ 11,133
12
+ 12,157
13
+ 13,183
14
+ 14,211
15
+ 15,241
16
+ 16,273
17
+ 17,307
18
+ 18,343
19
+ 19,200
20
+ 20,421
21
+ 21,463
22
+ 22,507
23
+ 23,553
24
+ 24,601
25
+ 25,651
26
+ 26,703
27
+ 27,757
28
+ 28,813
29
+ 29,871
30
+ 30,931
31
+ 31,123
32
+ 32,1057
33
+ 33,1123
34
+ 34,1000
35
+ 35,1261
36
+ 36,1333
37
+ 37,1407
38
+ 38,1483
39
+ 39,1561
40
+ 40,1641
41
+ 41,1723
42
+ 42,1807
43
+ 43,1893
44
+ 44,1981
45
+ 45,2071
46
+ 46,2163
47
+ 47,1524
48
+ 48,2353
49
+ 49,2451
50
+ 50,2500
51
+ 51,2520
@@ -0,0 +1,4 @@
1
+ 1c1
2
+ < aX
3
+ ---
4
+ > bXaX
@@ -0,0 +1,7 @@
1
+ *** spec/fixtures/aX 2019-02-01 22:29:34.000000000 -0500
2
+ --- spec/fixtures/bXaX 2019-02-01 22:29:43.000000000 -0500
3
+ ***************
4
+ *** 1 ****
5
+ ! aX
6
+ --- 1 ----
7
+ ! bXaX
@@ -0,0 +1,3 @@
1
+ 1c
2
+ bXaX
3
+ .
@@ -0,0 +1,3 @@
1
+ c1
2
+ bXaX
3
+ .
@@ -0,0 +1,5 @@
1
+ --- spec/fixtures/aX 2019-02-01 22:29:34.000000000 -0500
2
+ +++ spec/fixtures/bXaX 2019-02-01 22:29:43.000000000 -0500
3
+ @@ -1 +1 @@
4
+ -aX
5
+ +bXaX
@@ -1,63 +1,72 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- def h(v)
6
- v.to_s.bytes.to_a.map { |e| "%02x" % e }.join
7
- end
8
-
9
- describe "Diff::LCS::Hunk" do
10
- if String.method_defined?(:encoding)
5
+ if String.method_defined?(:encoding)
6
+ require 'diff/lcs/hunk'
11
7
 
12
- let(:old_data) { ["Tu avec carté {count} itém has".encode('UTF-16LE')] }
13
- let(:new_data) { ["Tu avec carte {count} item has".encode('UTF-16LE')] }
8
+ describe Diff::LCS::Hunk do
9
+ let(:old_data) { ['Tu avec carté {count} itém has'.encode('UTF-16LE')] }
10
+ let(:new_data) { ['Tu avec carte {count} item has'.encode('UTF-16LE')] }
14
11
  let(:pieces) { Diff::LCS.diff old_data, new_data }
15
12
  let(:hunk) { Diff::LCS::Hunk.new(old_data, new_data, pieces[0], 3, 0) }
16
13
 
17
- it 'should be able to produce a unified diff from the two pieces' do
18
- expected =
19
- (<<-EOD.encode('UTF-16LE').chomp)
20
- @@ -1,2 +1,2 @@
21
- -Tu avec carté {count} itém has
22
- +Tu avec carte {count} item has
23
- EOD
24
- expect(hunk.diff(:unified).to_s == expected).to eql true
14
+ it 'produces a unified diff from the two pieces' do
15
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
16
+ @@ -1 +1 @@
17
+ -Tu avec carté {count} itém has
18
+ +Tu avec carte {count} item has
19
+ EXPECTED
20
+
21
+ expect(hunk.diff(:unified)).to eq(expected)
22
+ end
23
+
24
+ it 'produces a context diff from the two pieces' do
25
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
26
+ ***************
27
+ *** 1 ****
28
+ ! Tu avec carté {count} itém has
29
+ --- 1 ----
30
+ ! Tu avec carte {count} item has
31
+ EXPECTED
32
+
33
+ expect(hunk.diff(:context)).to eq(expected)
25
34
  end
26
35
 
27
- it 'should be able to produce a context diff from the two pieces' do
28
- expected =
29
- (<<-EOD.encode('UTF-16LE').chomp)
30
- ***************
31
- *** 1,2 ****
32
- !Tu avec carté {count} itém has
33
- --- 1,2 ----
34
- !Tu avec carte {count} item has
35
- EOD
36
-
37
- expect(hunk.diff(:context).to_s == expected).to eql true
36
+ it 'produces an old diff from the two pieces' do
37
+ expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp
38
+ 1c1
39
+ < Tu avec carté {count} itém has
40
+ ---
41
+ > Tu avec carte {count} item has
42
+
43
+ EXPECTED
44
+
45
+ expect(hunk.diff(:old)).to eq(expected)
38
46
  end
39
47
 
40
- it 'should be able to produce an old diff from the two pieces' do
41
- expected =
42
- (<<-EOD.encode('UTF-16LE').chomp)
43
- 1,2c1,2
44
- < Tu avec carté {count} itém has
45
- ---
46
- > Tu avec carte {count} item has
48
+ it 'produces a reverse ed diff from the two pieces' do
49
+ expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp
50
+ c1
51
+ Tu avec carte {count} item has
52
+ .
47
53
 
48
- EOD
49
- expect(hunk.diff(:old).to_s == expected).to eql true
54
+ EXPECTED
55
+
56
+ expect(hunk.diff(:reverse_ed)).to eq(expected)
50
57
  end
51
58
 
52
- it 'should be able to produce a reverse ed diff from the two pieces' do
53
- expected =
54
- (<<-EOD.encode('UTF-16LE').chomp)
55
- c1,2
56
- Tu avec carte {count} item has
57
- .
59
+ context 'with empty first data set' do
60
+ let(:old_data) { [] }
61
+
62
+ it 'produces a unified diff' do
63
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
64
+ @@ -1 +1,2 @@
65
+ +Tu avec carte {count} item has
66
+ EXPECTED
58
67
 
59
- EOD
60
- expect(hunk.diff(:reverse_ed).to_s == expected).to eql true
68
+ expect(hunk.diff(:unified)).to eq(expected)
69
+ end
61
70
  end
62
71
  end
63
72
  end
@@ -1,24 +1,57 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe "Diff::LCS Issues" do
5
+ describe 'Diff::LCS Issues' do
6
6
  include Diff::LCS::SpecHelper::Matchers
7
7
 
8
- it "should not fail to provide a simple patchset (issue 1)" do
9
- s1, s2 = *%W(aX bXaX)
10
- correct_forward_diff = [
11
- [ [ '+', 0, 'b' ],
12
- [ '+', 1, 'X' ] ],
13
- ]
14
-
15
- diff_s1_s2 = Diff::LCS.diff(s1, s2)
16
- change_diff(correct_forward_diff).should == diff_s1_s2
17
- expect do
18
- Diff::LCS.patch(s1, diff_s1_s2).should == s2
19
- end.to_not raise_error(RuntimeError, /provided patchset/)
20
- expect do
21
- Diff::LCS.patch(s2, diff_s1_s2).should == s1
22
- end.to_not raise_error(RuntimeError, /provided patchset/)
8
+ describe 'issue #1' do
9
+ shared_examples 'handles simple diffs' do |s1, s2, forward_diff|
10
+ before do
11
+ @diff_s1_s2 = Diff::LCS.diff(s1, s2)
12
+ end
13
+
14
+ it 'creates the correct diff' do
15
+ expect(change_diff(forward_diff)).to eq(@diff_s1_s2)
16
+ end
17
+
18
+ it 'creates the correct patch s1->s2' do
19
+ expect(Diff::LCS.patch(s1, @diff_s1_s2)).to eq(s2)
20
+ end
21
+
22
+ it 'creates the correct patch s2->s1' do
23
+ expect(Diff::LCS.patch(s2, @diff_s1_s2)).to eq(s1)
24
+ end
25
+ end
26
+
27
+ describe 'string' do
28
+ it_has_behavior 'handles simple diffs', 'aX', 'bXaX', [
29
+ [
30
+ ['+', 0, 'b'],
31
+ ['+', 1, 'X']
32
+ ]
33
+ ]
34
+ it_has_behavior 'handles simple diffs', 'bXaX', 'aX', [
35
+ [
36
+ ['-', 0, 'b'],
37
+ ['-', 1, 'X']
38
+ ]
39
+ ]
40
+ end
41
+
42
+ describe 'array' do
43
+ it_has_behavior 'handles simple diffs', %w(a X), %w(b X a X), [
44
+ [
45
+ ['+', 0, 'b'],
46
+ ['+', 1, 'X']
47
+ ]
48
+ ]
49
+ it_has_behavior 'handles simple diffs', %w(b X a X), %w(a X), [
50
+ [
51
+ ['-', 0, 'b'],
52
+ ['-', 1, 'X']
53
+ ]
54
+ ]
55
+ end
23
56
  end
24
57
  end
@@ -1,54 +1,56 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe "Diff::LCS::Internals.lcs" do
5
+ describe Diff::LCS::Internals, '.lcs' do
6
6
  include Diff::LCS::SpecHelper::Matchers
7
7
 
8
- it "should return a meaningful LCS array with (seq1, seq2)" do
8
+ it 'returns a meaningful LCS array with (seq1, seq2)' do
9
9
  res = Diff::LCS::Internals.lcs(seq1, seq2)
10
10
  # The result of the LCS (less the +nil+ values) must be as long as the
11
11
  # correct result.
12
- res.compact.size.should == correct_lcs.size
13
- res.should correctly_map_sequence(seq1).to_other_sequence(seq2)
12
+ expect(res.compact.size).to eq(correct_lcs.size)
13
+ expect(res).to correctly_map_sequence(seq1).to_other_sequence(seq2)
14
14
 
15
15
  # Compact these transformations and they should be the correct LCS.
16
16
  x_seq1 = (0...res.size).map { |ix| res[ix] ? seq1[ix] : nil }.compact
17
17
  x_seq2 = (0...res.size).map { |ix| res[ix] ? seq2[res[ix]] : nil }.compact
18
18
 
19
- x_seq1.should == correct_lcs
20
- x_seq2.should == correct_lcs
19
+ expect(x_seq1).to eq(correct_lcs)
20
+ expect(x_seq2).to eq(correct_lcs)
21
21
  end
22
22
 
23
- it "should return all indexes with (hello, hello)" do
24
- Diff::LCS::Internals.lcs(hello, hello).should == (0...hello.size).to_a
23
+ it 'returns all indexes with (hello, hello)' do
24
+ expect(Diff::LCS::Internals.lcs(hello, hello)).to \
25
+ eq((0...hello.size).to_a)
25
26
  end
26
27
 
27
- it "should return all indexes with (hello_ary, hello_ary)" do
28
- Diff::LCS::Internals.lcs(hello_ary, hello_ary).should == (0...hello_ary.size).to_a
28
+ it 'returns all indexes with (hello_ary, hello_ary)' do
29
+ expect(Diff::LCS::Internals.lcs(hello_ary, hello_ary)).to \
30
+ eq((0...hello_ary.size).to_a)
29
31
  end
30
32
  end
31
33
 
32
- describe "Diff::LCS.LCS" do
34
+ describe Diff::LCS, '.LCS' do
33
35
  include Diff::LCS::SpecHelper::Matchers
34
36
 
35
- it "should return the correct compacted values from Diff::LCS.LCS" do
37
+ it 'returns the correct compacted values from Diff::LCS.LCS' do
36
38
  res = Diff::LCS.LCS(seq1, seq2)
37
- res.should == correct_lcs
38
- res.compact.should == res
39
+ expect(res).to eq(correct_lcs)
40
+ expect(res.compact).to eq(res)
39
41
  end
40
42
 
41
- it "should be transitive" do
43
+ it 'is transitive' do
42
44
  res = Diff::LCS.LCS(seq2, seq1)
43
- res.should == correct_lcs
44
- res.compact.should == res
45
+ expect(res).to eq(correct_lcs)
46
+ expect(res.compact).to eq(res)
45
47
  end
46
48
 
47
- it "should return %W(h e l l o) with (hello, hello)" do
48
- Diff::LCS.LCS(hello, hello).should == hello.split(//)
49
+ it 'returns %W(h e l l o) with (hello, hello)' do
50
+ expect(Diff::LCS.LCS(hello, hello)).to eq(hello.split(//))
49
51
  end
50
52
 
51
- it "should return hello_ary with (hello_ary, hello_ary)" do
52
- Diff::LCS.LCS(hello_ary, hello_ary).should == hello_ary
53
+ it 'returns hello_ary with (hello_ary, hello_ary)' do
54
+ expect(Diff::LCS.LCS(hello_ary, hello_ary)).to eq(hello_ary)
53
55
  end
54
56
  end