diff-lcs 1.2.5 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +6 -14
  2. data/.rspec +0 -1
  3. data/Code-of-Conduct.md +74 -0
  4. data/Contributing.md +118 -0
  5. data/History.md +319 -0
  6. data/{License.rdoc → License.md} +0 -0
  7. data/Manifest.txt +15 -8
  8. data/README.rdoc +18 -19
  9. data/Rakefile +57 -24
  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 +188 -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 +33 -36
  20. data/lib/diff/lcs/htmldiff.rb +17 -16
  21. data/lib/diff/lcs/hunk.rb +156 -74
  22. data/lib/diff/lcs/internals.rb +43 -40
  23. data/lib/diff/lcs/ldiff.rb +51 -75
  24. data/lib/diff/lcs/string.rb +1 -1
  25. data/spec/change_spec.rb +31 -7
  26. data/spec/diff_spec.rb +24 -20
  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 -43
  37. data/spec/issues_spec.rb +147 -17
  38. data/spec/lcs_spec.rb +24 -22
  39. data/spec/ldiff_spec.rb +87 -0
  40. data/spec/patch_spec.rb +182 -180
  41. data/spec/sdiff_spec.rb +91 -91
  42. data/spec/spec_helper.rb +143 -58
  43. data/spec/traverse_balanced_spec.rb +177 -177
  44. data/spec/traverse_sequences_spec.rb +63 -63
  45. metadata +87 -143
  46. checksums.yaml.gz.sig +0 -0
  47. data.tar.gz.sig +0 -3
  48. data/.autotest +0 -3
  49. data/.gemtest +0 -0
  50. data/.hoerc +0 -2
  51. data/.travis.yml +0 -22
  52. data/Contributing.rdoc +0 -64
  53. data/Gemfile +0 -20
  54. data/History.rdoc +0 -152
  55. metadata.gz.sig +0 -2
@@ -1,47 +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 correctly diff 'xx' and 'xaxb'" do
38
+ it "correctly diffs 'xx' and 'xaxb'" do
35
39
  left = 'xx'
36
40
  right = 'xaxb'
37
- Diff::LCS.patch(left, Diff::LCS.diff(left, right)).should == right
41
+ expect(Diff::LCS.patch(left, Diff::LCS.diff(left, right))).to eq(right)
38
42
  end
39
43
 
40
- it "should return an empty diff with (hello, hello)" do
41
- Diff::LCS.diff(hello, hello).should == []
44
+ it 'returns an empty diff with (hello, hello)' do
45
+ expect(Diff::LCS.diff(hello, hello)).to be_empty
42
46
  end
43
47
 
44
- it "should return an empty diff with (hello_ary, hello_ary)" do
45
- Diff::LCS.diff(hello_ary, hello_ary).should == []
48
+ it 'returns an empty diff with (hello_ary, hello_ary)' do
49
+ expect(Diff::LCS.diff(hello_ary, hello_ary)).to be_empty
46
50
  end
47
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 2020-06-23 11:15:32.000000000 -0400
2
+ --- spec/fixtures/bXaX 2020-06-23 11:15:32.000000000 -0400
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 2020-06-23 11:15:32.000000000 -0400
2
+ +++ spec/fixtures/bXaX 2020-06-23 11:15:32.000000000 -0400
3
+ @@ -1 +1 @@
4
+ -aX
5
+ +bXaX
@@ -1,72 +1,83 @@
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 a un carté avec {count} itéms'.encode('UTF-16LE')] }
10
+ let(:new_data) { ['Tu a un carte avec {count} items'.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 = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
19
- @@ -1,2 +1,2 @@
20
- -Tu avec carté {count} itém has
21
- +Tu avec carte {count} item has
22
- EOD
23
- 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 a un carté avec {count} itéms
18
+ +Tu a un carte avec {count} items
19
+ EXPECTED
20
+
21
+ expect(hunk.diff(:unified)).to eq(expected)
22
+ end
23
+
24
+ it 'produces a unified diff from the two pieces (last entry)' do
25
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
26
+ @@ -1 +1 @@
27
+ -Tu a un carté avec {count} itéms
28
+ +Tu a un carte avec {count} items
29
+ \
30
+ EXPECTED
31
+
32
+ expect(hunk.diff(:unified, true)).to eq(expected)
24
33
  end
25
34
 
26
- it 'should be able to produce a context diff from the two pieces' do
27
- expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
35
+ it 'produces a context diff from the two pieces' do
36
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
28
37
  ***************
29
- *** 1,2 ****
30
- !Tu avec carté {count} itém has
31
- --- 1,2 ----
32
- !Tu avec carte {count} item has
33
- EOD
38
+ *** 1 ****
39
+ ! Tu a un carté avec {count} itéms
40
+ --- 1 ----
41
+ ! Tu a un carte avec {count} items
42
+ EXPECTED
34
43
 
35
- expect(hunk.diff(:context).to_s == expected).to eql true
44
+ expect(hunk.diff(:context)).to eq(expected)
36
45
  end
37
46
 
38
- it 'should be able to produce an old diff from the two pieces' do
39
- expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
40
- 1,2c1,2
41
- < Tu avec carté {count} itém has
47
+ it 'produces an old diff from the two pieces' do
48
+ expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp
49
+ 1c1
50
+ < Tu a un carté avec {count} itéms
42
51
  ---
43
- > Tu avec carte {count} item has
52
+ > Tu a un carte avec {count} items
53
+
54
+ EXPECTED
44
55
 
45
- EOD
46
- expect(hunk.diff(:old).to_s == expected).to eql true
56
+ expect(hunk.diff(:old)).to eq(expected)
47
57
  end
48
58
 
49
- it 'should be able to produce a reverse ed diff from the two pieces' do
50
- expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
51
- c1,2
52
- Tu avec carte {count} item has
59
+ it 'produces a reverse ed diff from the two pieces' do
60
+ expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp
61
+ c1
62
+ Tu a un carte avec {count} items
53
63
  .
54
64
 
55
- EOD
56
- expect(hunk.diff(:reverse_ed).to_s == expected).to eql true
65
+ EXPECTED
66
+
67
+ expect(hunk.diff(:reverse_ed)).to eq(expected)
57
68
  end
58
69
 
59
70
  context 'with empty first data set' do
60
71
  let(:old_data) { [] }
61
72
 
62
- it 'should be able to produce a unified diff' do
63
- expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
73
+ it 'produces a unified diff' do
74
+ expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp
64
75
  @@ -1 +1,2 @@
65
- +Tu avec carte {count} item has
66
- EOD
67
- expect(hunk.diff(:unified).to_s == expected).to eql true
76
+ +Tu a un carte avec {count} items
77
+ EXPECTED
78
+
79
+ expect(hunk.diff(:unified)).to eq(expected)
68
80
  end
69
81
  end
70
-
71
82
  end
72
83
  end
@@ -1,24 +1,154 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
+ require 'diff/lcs/hunk'
4
5
 
5
- describe "Diff::LCS Issues" do
6
+ describe 'Diff::LCS Issues' do
6
7
  include Diff::LCS::SpecHelper::Matchers
7
8
 
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/)
9
+ describe 'issue #1' do
10
+ shared_examples 'handles simple diffs' do |s1, s2, forward_diff|
11
+ before do
12
+ @diff_s1_s2 = Diff::LCS.diff(s1, s2)
13
+ end
14
+
15
+ it 'creates the correct diff' do
16
+ expect(change_diff(forward_diff)).to eq(@diff_s1_s2)
17
+ end
18
+
19
+ it 'creates the correct patch s1->s2' do
20
+ expect(Diff::LCS.patch(s1, @diff_s1_s2)).to eq(s2)
21
+ end
22
+
23
+ it 'creates the correct patch s2->s1' do
24
+ expect(Diff::LCS.patch(s2, @diff_s1_s2)).to eq(s1)
25
+ end
26
+ end
27
+
28
+ describe 'string' do
29
+ it_has_behavior 'handles simple diffs', 'aX', 'bXaX', [
30
+ [
31
+ ['+', 0, 'b'],
32
+ ['+', 1, 'X']
33
+ ]
34
+ ]
35
+ it_has_behavior 'handles simple diffs', 'bXaX', 'aX', [
36
+ [
37
+ ['-', 0, 'b'],
38
+ ['-', 1, 'X']
39
+ ]
40
+ ]
41
+ end
42
+
43
+ describe 'array' do
44
+ it_has_behavior 'handles simple diffs', %w(a X), %w(b X a X), [
45
+ [
46
+ ['+', 0, 'b'],
47
+ ['+', 1, 'X']
48
+ ]
49
+ ]
50
+ it_has_behavior 'handles simple diffs', %w(b X a X), %w(a X), [
51
+ [
52
+ ['-', 0, 'b'],
53
+ ['-', 1, 'X']
54
+ ]
55
+ ]
56
+ end
57
+ end
58
+
59
+ describe 'issue #57' do
60
+ it 'should fail with a correct error' do
61
+ expect {
62
+ actual = { :category => 'app.rack.request' }
63
+ expected = { :category => 'rack.middleware', :title => 'Anonymous Middleware' }
64
+ expect(actual).to eq(expected)
65
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
66
+ end
67
+ end
68
+
69
+ describe 'issue #60' do
70
+ it 'should produce unified output with correct context' do
71
+ old_data = <<-DATA_OLD.strip.split("\n").map(&:chomp)
72
+ {
73
+ "name": "x",
74
+ "description": "hi"
75
+ }
76
+ DATA_OLD
77
+
78
+ new_data = <<-DATA_NEW.strip.split("\n").map(&:chomp)
79
+ {
80
+ "name": "x",
81
+ "description": "lo"
82
+ }
83
+ DATA_NEW
84
+
85
+ diff = ::Diff::LCS.diff(old_data, new_data)
86
+ hunk = ::Diff::LCS::Hunk.new(old_data, new_data, diff.first, 3, 0)
87
+
88
+ expect(hunk.diff(:unified)).to eq(<<-EXPECTED.chomp)
89
+ @@ -1,5 +1,5 @@
90
+ {
91
+ "name": "x",
92
+ - "description": "hi"
93
+ + "description": "lo"
94
+ }
95
+ EXPECTED
96
+ end
97
+ end
98
+
99
+ describe 'issue #65' do
100
+ def diff_lines(old_lines, new_lines)
101
+ file_length_difference = 0
102
+ previous_hunk = nil
103
+ output = []
104
+
105
+ Diff::LCS.diff(old_lines, new_lines).each do |piece|
106
+ hunk = Diff::LCS::Hunk.new(old_lines, new_lines, piece, 3, file_length_difference)
107
+ file_length_difference = hunk.file_length_difference
108
+ maybe_contiguous_hunks = (previous_hunk.nil? || hunk.merge(previous_hunk))
109
+
110
+ output << "#{previous_hunk.diff(:unified)}\n" unless maybe_contiguous_hunks
111
+
112
+ previous_hunk = hunk
113
+ end
114
+ output << "#{previous_hunk.diff(:unified, true)}\n" unless previous_hunk.nil?
115
+ output.join
116
+ end
117
+
118
+ it 'should not misplace the new chunk' do
119
+ old_data = [
120
+ 'recipe[a::default]', 'recipe[b::default]', 'recipe[c::default]',
121
+ 'recipe[d::default]', 'recipe[e::default]', 'recipe[f::default]',
122
+ 'recipe[g::default]', 'recipe[h::default]', 'recipe[i::default]',
123
+ 'recipe[j::default]', 'recipe[k::default]', 'recipe[l::default]',
124
+ 'recipe[m::default]', 'recipe[n::default]'
125
+ ]
126
+
127
+ new_data = [
128
+ 'recipe[a::default]', 'recipe[c::default]', 'recipe[d::default]',
129
+ 'recipe[e::default]', 'recipe[f::default]', 'recipe[g::default]',
130
+ 'recipe[h::default]', 'recipe[i::default]', 'recipe[j::default]',
131
+ 'recipe[k::default]', 'recipe[l::default]', 'recipe[m::default]',
132
+ 'recipe[n::default]', 'recipe[o::new]', 'recipe[p::new]',
133
+ 'recipe[q::new]', 'recipe[r::new]'
134
+ ]
135
+
136
+ expect(diff_lines(old_data, new_data)).to eq(<<-EODIFF)
137
+ @@ -1,5 +1,4 @@
138
+ recipe[a::default]
139
+ -recipe[b::default]
140
+ recipe[c::default]
141
+ recipe[d::default]
142
+ recipe[e::default]
143
+ @@ -12,3 +11,7 @@
144
+ recipe[l::default]
145
+ recipe[m::default]
146
+ recipe[n::default]
147
+ +recipe[o::new]
148
+ +recipe[p::new]
149
+ +recipe[q::new]
150
+ +recipe[r::new]
151
+ EODIFF
152
+ end
23
153
  end
24
154
  end