diff-lcs 2.0.0 → 2.1.0

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +170 -85
  3. data/CODE_OF_CONDUCT.md +49 -48
  4. data/CONTRIBUTING.md +29 -28
  5. data/CONTRIBUTORS.md +15 -11
  6. data/LICENCE.md +19 -38
  7. data/Manifest.txt +16 -3
  8. data/README.md +24 -22
  9. data/Rakefile +3 -0
  10. data/SECURITY.md +5 -5
  11. data/lib/diff/lcs/array.rb +3 -1
  12. data/lib/diff/lcs/callbacks.rb +1 -1
  13. data/lib/diff/lcs/change.rb +74 -32
  14. data/lib/diff/lcs/frozen_value.rb +74 -0
  15. data/lib/diff/lcs/hunk.rb +146 -58
  16. data/lib/diff/lcs/internals.rb +183 -72
  17. data/lib/diff/lcs/ldiff.rb +23 -45
  18. data/lib/diff/lcs/string.rb +3 -1
  19. data/lib/diff/lcs/version.rb +1 -1
  20. data/lib/diff/lcs.rb +29 -26
  21. data/licences/MIT.txt +19 -0
  22. data/spec/ldiff_spec.rb +9 -7
  23. data/test/fixtures/ldiff/output.diff.bin1-q +0 -0
  24. data/test/fixtures/ldiff/output.diff.bin2-q +1 -0
  25. data/test/fixtures/ldiff/output.diff.hunks +9 -0
  26. data/test/fixtures/ldiff/output.diff.hunks-c +35 -0
  27. data/test/fixtures/ldiff/output.diff.hunks-u +20 -0
  28. data/test/fixtures/ldiff/output.diff.same +0 -0
  29. data/test/fixtures/ldiff/output.diff.same-c +0 -0
  30. data/test/fixtures/ldiff/output.diff.same-u +0 -0
  31. data/test/fixtures/new-hunks +31 -0
  32. data/test/fixtures/old-hunks +30 -0
  33. data/test/test_change.rb +23 -19
  34. data/test/test_frozen_value.rb +155 -0
  35. data/test/test_helper.rb +5 -0
  36. data/test/test_hunk.rb +238 -0
  37. data/test/test_lcs.rb +147 -0
  38. data/test/test_ldiff.rb +64 -7
  39. metadata +24 -11
  40. /data/{licenses → licences}/COPYING.txt +0 -0
  41. /data/{licenses → licences}/artistic.txt +0 -0
  42. /data/{licenses → licences}/dco.txt +0 -0
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "yaml"
5
+
6
+ # Diff::LCS::Change and ContextChange keep the API they had as Data classes, checked
7
+ # against Data classes with the same members.
8
+ class TestFrozenValue < Minitest::Test
9
+ CHANGE = Diff::LCS::Change
10
+ CONTEXT_CHANGE = Diff::LCS::ContextChange
11
+ DATA_BY_CLASS = {
12
+ CHANGE => Data.define(*CHANGE.members),
13
+ CONTEXT_CHANGE => Data.define(*CONTEXT_CHANGE.members)
14
+ }.freeze
15
+
16
+ def context_change = CONTEXT_CHANGE.new("!", 1, "old", 2, "new")
17
+
18
+ # The members (`to_h`) of the value built by `operation` on `klass`, or the message of
19
+ # the ArgumentError it raised.
20
+ def outcome(klass, &operation)
21
+ operation.call(klass).to_h
22
+ rescue ArgumentError => exception
23
+ exception.message
24
+ end
25
+
26
+ def assert_outcome_matches_data(klass, &operation)
27
+ assert_equal outcome(DATA_BY_CLASS.fetch(klass), &operation), outcome(klass, &operation)
28
+ end
29
+
30
+ def test_construction_matches_data
31
+ assert_outcome_matches_data(CHANGE) { _1.new("+", 0, "a") }
32
+ assert_outcome_matches_data(CHANGE) { _1["+", 0, "a"] }
33
+ assert_outcome_matches_data(CHANGE) { _1.new(action: "+", position: 0, element: "a") }
34
+ assert_outcome_matches_data(CHANGE) { _1[element: "a", position: 0, action: "+"] }
35
+ assert_outcome_matches_data(CHANGE) { _1.new("+", 0) }
36
+ assert_outcome_matches_data(CHANGE) { _1.new }
37
+ assert_outcome_matches_data(CHANGE) { _1.new(action: "+", position: 0, element: "a", extra: 1) }
38
+ assert_outcome_matches_data(CONTEXT_CHANGE) { _1.new("!", 1, "old", 2, "new") }
39
+ assert_outcome_matches_data(CONTEXT_CHANGE) { _1.new(action: "+", old_position: 0, new_element: "a") }
40
+ assert_raises(ArgumentError) { CHANGE.new("+", 0, "a", "b") }
41
+ end
42
+
43
+ def test_with_matches_data
44
+ [
45
+ [CHANGE, ["+", 0, "a"], {}],
46
+ [CHANGE, ["+", 0, "a"], {position: 1}],
47
+ [CHANGE, ["+", 0, "a"], {element: nil, action: "-"}],
48
+ [CHANGE, ["+", 0, "a"], {extra: 1}],
49
+ [CONTEXT_CHANGE, ["<", 1, "old", 2, "new"], {action: "-", new_element: nil}],
50
+ [CONTEXT_CHANGE, ["<", 1, "old", 2, "new"], {position: 3}]
51
+ ].each do |klass, values, updates|
52
+ assert_outcome_matches_data(klass) { _1.new(*values).with(**updates) }
53
+ end
54
+
55
+ assert_outcome_matches_data(CHANGE) { _1.new("+", 0, "a").with(3) }
56
+ end
57
+
58
+ def test_with_validates_and_returns_self_without_changes
59
+ change = context_change
60
+
61
+ assert_same change, change.with
62
+ assert_raises_with_message RuntimeError, "Invalid Change Action '~'" do
63
+ change.with(action: "~")
64
+ end
65
+ end
66
+
67
+ def test_keyword_construction_validates
68
+ assert_raises_with_message RuntimeError, "Invalid Position Type" do
69
+ CHANGE.new(action: "+", position: "0", element: "a")
70
+ end
71
+ end
72
+
73
+ def test_members_and_to_h
74
+ assert_equal %i[action position element], CHANGE.members
75
+ assert_equal %i[action old_position old_element new_position new_element], context_change.members
76
+ CHANGE.members << :extra
77
+ context_change.members << :extra
78
+ assert_equal %i[action position element], CHANGE.members
79
+ refute_respond_to context_change, :marshal_dump
80
+ assert_equal({"action" => "+", "position" => 0, "element" => "a"},
81
+ CHANGE.new("+", 0, "a").to_h { |member, value| [member.to_s, value] })
82
+ end
83
+
84
+ def test_to_h_returns_a_hash_of_its_own
85
+ change = CHANGE.new("+", 0, "a")
86
+ change.to_h[:position] = 1
87
+
88
+ assert_equal({action: "+", position: 0, element: "a"}, change.to_h)
89
+ end
90
+
91
+ # Telling these apart from a Hash given as the last member would take a keyword
92
+ # parameter, which allocates on every positional call.
93
+ def test_a_hash_as_only_or_last_argument_is_not_rejected_like_data
94
+ assert_equal CHANGE.new("+", 0, "a"), CHANGE.new({action: "+", position: 0, element: "a"})
95
+ assert_equal({element: "a"}, CHANGE.new("+", 0, element: "a").element)
96
+ end
97
+
98
+ def test_value_equality_as_hash_keys
99
+ counts = Hash.new(0)
100
+ 2.times { counts[CONTEXT_CHANGE.new("!", 1, "old", 2, "new")] += 1 }
101
+ counts[CONTEXT_CHANGE.new("!", 1, "old", 2, "other")] += 1
102
+
103
+ assert_equal [2, 1], counts.values
104
+ refute CHANGE.new("+", 1, 1).eql?(CHANGE.new("+", 1, 1.0))
105
+ refute CHANGE.new("+", 1, 1).eql?(CONTEXT_CHANGE.new("+", 1, 1, 1, 1))
106
+ end
107
+
108
+ def test_instances_and_their_copies_are_frozen
109
+ change = CHANGE.new("+", 0, "a")
110
+
111
+ assert_predicate change, :frozen?
112
+ assert_predicate change.dup, :frozen?
113
+ assert_predicate change.clone(freeze: false), :frozen?
114
+ assert_raises(FrozenError) { change.instance_variable_set(:@position, 1) }
115
+ assert_raises(FrozenError) { change.send(:marshal_load, ["-", 1, "b"]) }
116
+ end
117
+
118
+ def test_marshal_round_trip
119
+ copy = Marshal.load(Marshal.dump(context_change))
120
+
121
+ assert_equal context_change, copy
122
+ assert_predicate copy, :frozen?
123
+ end
124
+
125
+ def test_yaml_round_trip
126
+ copy = YAML.unsafe_load(YAML.dump(context_change))
127
+
128
+ assert_equal context_change, copy
129
+ assert_predicate copy, :frozen?
130
+ assert_raises_with_message RuntimeError, "Invalid Change Action '~'" do
131
+ YAML.unsafe_load(YAML.dump(CHANGE.new("+", 0, "a")).sub('"+"', '"~"'))
132
+ end
133
+ end
134
+
135
+ def test_yaml_load_requires_exactly_the_members
136
+ dump = YAML.dump(CHANGE.new("+", 0, "a"))
137
+
138
+ assert_raises_with_message ArgumentError, "missing keyword: :element" do
139
+ YAML.unsafe_load(dump.sub(/^element: .*\n/, ""))
140
+ end
141
+ assert_raises_with_message ArgumentError, "unknown keyword: :extra" do
142
+ YAML.unsafe_load("#{dump}extra: 1\n")
143
+ end
144
+ assert_raises_with_message ArgumentError, "unknown keyword: 1" do
145
+ YAML.unsafe_load("#{dump}1: 2\n")
146
+ end
147
+ end
148
+
149
+ def test_inspect_and_to_s
150
+ assert_equal '#<Diff::LCS::ContextChange action="!", old_position=1, old_element="old", ' \
151
+ 'new_position=2, new_element="new">', context_change.to_s
152
+ assert_equal '#<Diff::LCS::Change: ["+", 0, "a"]>', CHANGE.new("+", 0, "a").inspect
153
+ assert_equal '#<Diff::LCS::Change action="+", position=0, element="a">', CHANGE.new("+", 0, "a").to_s
154
+ end
155
+ end
data/test/test_helper.rb CHANGED
@@ -129,6 +129,11 @@ module Diff::LCS::TestHelper
129
129
  new_result
130
130
  end
131
131
 
132
+ def assert_raises_with_message(klass, message, &block)
133
+ exception = assert_raises klass, &block
134
+ assert_match message, exception.message
135
+ end
136
+
132
137
  def assert_nil_or_match_values(ee, ii, s1, s2)
133
138
  assert(ee.nil? || s1[ii] == s2[ee])
134
139
  end
data/test/test_hunk.rb CHANGED
@@ -69,4 +69,242 @@ class TestHunk < Minitest::Test
69
69
 
70
70
  assert_equal expected, hunk.diff(:unified)
71
71
  end
72
+
73
+ def test_from_diff_without_differences
74
+ assert_empty Diff::LCS::Hunk.from_diff(numbered_lines, numbered_lines, 3)
75
+ end
76
+
77
+ def test_from_diff_without_context_has_one_hunk_per_difference
78
+ hunks = Diff::LCS::Hunk.from_diff(numbered_lines, changed_numbered_lines, 0)
79
+
80
+ assert_equal 3, hunks.size
81
+ assert_equal [1, 1, 1], hunks.map { _1.blocks.size }
82
+ end
83
+
84
+ def test_from_diff_defaults_to_three_lines_of_context
85
+ hunks = Diff::LCS::Hunk.from_diff(numbered_lines, changed_numbered_lines)
86
+
87
+ assert_equal [[0, 9], [16, 19]], hunks.map { [_1.start_old, _1.end_old] }
88
+ end
89
+
90
+ def test_from_diff_rejects_invalid_context
91
+ [nil, "3", -1, 1.5].each do |context|
92
+ assert_raises_with_message(ArgumentError, "context must be a non-negative Integer") do
93
+ Diff::LCS::Hunk.from_diff(numbered_lines, numbered_lines, context)
94
+ end
95
+ end
96
+ end
97
+
98
+ def test_old_diff_of_a_hunk_with_context_has_no_context
99
+ old_data = (1..10).map { "#{_1}\n" }
100
+ new_data = old_data.dup.tap { _1[4] = "x\n" }
101
+ hunks = Diff::LCS::Hunk.from_diff(old_data, new_data, 2)
102
+
103
+ assert_equal ["5c5\n< 5\n---\n> x\n"], hunks.map { _1.diff(:old, true) }
104
+ end
105
+
106
+ def test_old_diff_of_merged_hunks_writes_each_change
107
+ hunks = Diff::LCS::Hunk.from_diff(numbered_lines, changed_numbered_lines, 3)
108
+
109
+ assert_silent do
110
+ assert_equal "3c3\n< 3\n---\n> three\n7c7\n< 7\n---\n> seven\n", hunks[0].diff(:old)
111
+ end
112
+ end
113
+
114
+ def test_from_diff_merges_hunks_that_overlap_because_of_context
115
+ hunks = Diff::LCS::Hunk.from_diff(numbered_lines, changed_numbered_lines, 3)
116
+
117
+ assert_equal 2, hunks.size
118
+ assert_equal [2, 1], hunks.map { _1.blocks.size }
119
+ assert_equal [[0, 9], [16, 19]], hunks.map { [_1.start_old, _1.end_old] }
120
+ assert_equal [[0, 9], [16, 20]], hunks.map { [_1.start_new, _1.end_new] }
121
+ end
122
+
123
+ def test_diff_without_differences
124
+ %i[old unified context].each do |format|
125
+ assert_equal "", Diff::LCS::Hunk.diff(numbered_lines, numbered_lines, format, 3)
126
+ end
127
+ end
128
+
129
+ def test_diff_old
130
+ expected = <<~EXPECTED
131
+ 3c3
132
+ < 3
133
+ ---
134
+ > three
135
+ 7c7
136
+ < 7
137
+ ---
138
+ > seven
139
+ 19a20
140
+ > nineteen and a half
141
+ EXPECTED
142
+
143
+ assert_equal expected, Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines)
144
+ end
145
+
146
+ def test_diff_old_ignores_context
147
+ assert_equal Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, :old, 0),
148
+ Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, :old, 3)
149
+ end
150
+
151
+ def test_diff_defaults_to_three_lines_of_context
152
+ %i[unified context].each do |format|
153
+ assert_equal Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, format, 3),
154
+ Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, format)
155
+ end
156
+ end
157
+
158
+ def test_diff_rejects_invalid_context_without_differences
159
+ assert_raises(ArgumentError) do
160
+ Diff::LCS::Hunk.diff(numbered_lines, numbered_lines, :unified, nil)
161
+ end
162
+ end
163
+
164
+ def test_diff_with_one_empty_string_is_an_empty_file
165
+ assert_equal "@@ -0,0 +1 @@\n+a\n", Diff::LCS::Hunk.diff([""], ["a\n"], :unified)
166
+
167
+ %i[old unified context].each do |format|
168
+ assert_equal Diff::LCS::Hunk.diff([], ["a\n"], format), Diff::LCS::Hunk.diff([""], ["a\n"], format)
169
+ assert_equal Diff::LCS::Hunk.diff(["a\n"], [], format), Diff::LCS::Hunk.diff(["a\n"], [""], format)
170
+ end
171
+ end
172
+
173
+ def test_diff_old_marks_missing_newline_after_the_last_change_of_merged_hunks
174
+ expected = <<~EXPECTED
175
+ 2c2
176
+ < b
177
+ ---
178
+ > x
179
+ 4c4
180
+ < d
181
+ ---
182
+ > y
183
+ \
184
+ EXPECTED
185
+
186
+ assert_equal expected, Diff::LCS::Hunk.diff(%W[a\n b\n c\n d\n], %W[a\n x\n c\n y], :old, 3)
187
+ end
188
+
189
+ def test_diff_unified
190
+ expected = <<~EXPECTED
191
+ @@ -1,10 +1,10 @@
192
+ 1
193
+ 2
194
+ -3
195
+ +three
196
+ 4
197
+ 5
198
+ 6
199
+ -7
200
+ +seven
201
+ 8
202
+ 9
203
+ 10
204
+ @@ -17,4 +17,5 @@
205
+ 17
206
+ 18
207
+ 19
208
+ +nineteen and a half
209
+ 20
210
+ EXPECTED
211
+
212
+ assert_equal expected, Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, :unified, 3)
213
+ end
214
+
215
+ def test_diff_context
216
+ expected = <<~EXPECTED
217
+ ***************
218
+ *** 1,10 ****
219
+ 1
220
+ 2
221
+ ! 3
222
+ 4
223
+ 5
224
+ 6
225
+ ! 7
226
+ 8
227
+ 9
228
+ 10
229
+ --- 1,10 ----
230
+ 1
231
+ 2
232
+ ! three
233
+ 4
234
+ 5
235
+ 6
236
+ ! seven
237
+ 8
238
+ 9
239
+ 10
240
+ ***************
241
+ *** 17,20 ****
242
+ --- 17,21 ----
243
+ 17
244
+ 18
245
+ 19
246
+ + nineteen and a half
247
+ 20
248
+ EXPECTED
249
+
250
+ assert_equal expected, Diff::LCS::Hunk.diff(numbered_lines, changed_numbered_lines, :context, 3)
251
+ end
252
+
253
+ def test_diff_missing_newline_at_end_of_file
254
+ expected = <<~EXPECTED
255
+ @@ -1,2 +1,2 @@
256
+ a
257
+ -b
258
+ +c
259
+ \
260
+ EXPECTED
261
+
262
+ assert_equal expected, Diff::LCS::Hunk.diff(["a\n", "b\n"], ["a\n", "c"], :unified, 3)
263
+ end
264
+
265
+ def test_diff_preserves_data_encoding
266
+ newline = "\n".encode("UTF-16LE")
267
+ expected = <<~EXPECTED.encode("UTF-16LE")
268
+ @@ -1 +1 @@
269
+ -Tu a un carté avec {count} itéms
270
+ +Tu a un carte avec {count} items
271
+ EXPECTED
272
+
273
+ assert_equal expected,
274
+ Diff::LCS::Hunk.diff(@old_data.map { _1 + newline }, @new_data.map { _1 + newline }, :unified, 3)
275
+ end
276
+
277
+ def test_diff_of_an_empty_string_and_lines_in_another_encoding
278
+ new_data = ["é\n".encode("UTF-16LE")]
279
+
280
+ assert_equal "@@ -0,0 +1 @@\n+é\n".encode("UTF-16LE"), Diff::LCS::Hunk.diff([""], new_data, :unified)
281
+ end
282
+
283
+ def test_diff_has_the_encoding_of_ascii_only_or_equal_data
284
+ old_data = ["a\n", "b\n"].map { _1.encode("ISO-8859-1") }
285
+ new_data = ["a\n", "c\n"].map { _1.encode("ISO-8859-1") }
286
+
287
+ assert_equal Encoding::ISO_8859_1, Diff::LCS::Hunk.diff(old_data, new_data, :unified).encoding
288
+ assert_equal Encoding::ISO_8859_1, Diff::LCS::Hunk.diff(old_data, old_data, :unified).encoding
289
+ end
290
+
291
+ def test_diff_unknown_format
292
+ [changed_numbered_lines, numbered_lines].each do |new_data|
293
+ assert_raises_with_message(RuntimeError, "Unknown diff format html.") do
294
+ Diff::LCS::Hunk.diff(numbered_lines, new_data, :html)
295
+ end
296
+ end
297
+ end
298
+
299
+ private
300
+
301
+ def numbered_lines = (1..20).map { "#{_1}\n" }
302
+
303
+ def changed_numbered_lines
304
+ numbered_lines.tap do
305
+ _1[2] = "three\n"
306
+ _1[6] = "seven\n"
307
+ _1.insert(19, "nineteen and a half\n")
308
+ end
309
+ end
72
310
  end
data/test/test_lcs.rb CHANGED
@@ -22,6 +22,153 @@ class TestLCSInternals < Minitest::Test
22
22
  def test_returns_all_indexes_with_hello_ary_hello_ary
23
23
  assert_equal (0...hello_ary.size).to_a, internal_lcs(hello_ary, hello_ary)
24
24
  end
25
+
26
+ def test_maps_strings_like_their_characters
27
+ assert_equal internal_lcs("abcabba", "cbabac"), internal_lcs("abcabba".chars, "cbabac".chars)
28
+ end
29
+
30
+ def test_finds_a_longest_common_subsequence_among_repeated_elements
31
+ random = Random.new(20261103)
32
+
33
+ 500.times do
34
+ alphabet_size = [1, 2, 3, 5].sample(random:)
35
+ s1 = Array.new(random.rand(0..30)) { random.rand(alphabet_size) }
36
+ s2 = Array.new(random.rand(0..30)) { random.rand(alphabet_size) }
37
+ assert_longest_common_subsequence(s1, s2)
38
+ end
39
+ end
40
+
41
+ def test_finds_longest_common_subsequences_in_a_larger_corpus
42
+ random = Random.new(20261108)
43
+
44
+ [0..100, 200..300, 400..500].each do |length_range|
45
+ 20.times do
46
+ alphabet_size = random.rand(2..50)
47
+ s1 = Array.new(random.rand(length_range)) { random.rand(alphabet_size) }
48
+ s2 = Array.new(random.rand(length_range)) { random.rand(alphabet_size) }
49
+ assert_longest_common_subsequence(s1, s2)
50
+ end
51
+ end
52
+ end
53
+
54
+ private def assert_longest_common_subsequence(s1, s2)
55
+ res = internal_lcs(s1, s2)
56
+
57
+ assert_correctly_maps_sequence(res, s1, s2)
58
+ assert_equal res.compact, res.compact.sort.uniq
59
+ assert_equal dynamic_programming_lcs_size(s1, s2), res.compact.size
60
+ assert_equal reference_lcs(s1, s2), res, "picked another LCS for #{s1.inspect} / #{s2.inspect}"
61
+ end
62
+
63
+ # Hunt-Szymanski as diff-lcs 2.0.0 runs it, without the shortcuts of `Internals.lcs`:
64
+ # among several longest common subsequences, the one both must return.
65
+ private def reference_lcs(a, b)
66
+ a_start = b_start = 0
67
+ a_finish = a.size - 1
68
+ b_finish = b.size - 1
69
+ vector = []
70
+
71
+ while (a_start <= a_finish) && (b_start <= b_finish) && (a[a_start] == b[b_start])
72
+ vector[a_start] = b_start
73
+ a_start += 1
74
+ b_start += 1
75
+ end
76
+
77
+ while (a_start <= a_finish) && (b_start <= b_finish) && (a[a_finish] == b[b_finish])
78
+ vector[a_finish] = b_finish
79
+ a_finish -= 1
80
+ b_finish -= 1
81
+ end
82
+
83
+ b_matches = Hash.new { |h, k| h[k] = [] }
84
+ (b_start..b_finish).each { b_matches[b[_1]] << _1 }
85
+ thresh = []
86
+ links = []
87
+
88
+ (a_start..a_finish).each do |i|
89
+ b_matches[a[i]].reverse_each do |j|
90
+ k = thresh.bsearch_index { _1 >= j } || thresh.size
91
+ next if thresh[k] == j
92
+
93
+ thresh[k] = j
94
+ links[k] = [k.positive? ? links[k - 1] : nil, i, j]
95
+ end
96
+ end
97
+
98
+ link = links[thresh.size - 1]
99
+ until link.nil?
100
+ vector[link[1]] = link[2]
101
+ link = link[0]
102
+ end
103
+ vector
104
+ end
105
+
106
+ private def dynamic_programming_lcs_size(s1, s2)
107
+ previous_row = Array.new(s2.size + 1, 0)
108
+ s1.each do |element|
109
+ row = [0]
110
+ s2.each_with_index do |other, index|
111
+ row << ((element == other) ? previous_row[index] + 1 : [previous_row[index + 1], row[index]].max)
112
+ end
113
+ previous_row = row
114
+ end
115
+ previous_row.last
116
+ end
117
+ end
118
+
119
+ # `diff` and `sdiff` with their default callbacks skip the traversals and build their
120
+ # results straight from the LCS; a callbacks instance still goes through the traversals.
121
+ class TestDefaultCallbacksShortcut < Minitest::Test
122
+ def test_diff_matches_traverse_sequences_with_diff_callbacks
123
+ each_random_pair do |s1, s2|
124
+ assert_equal diff(s1, s2, Diff::LCS::DiffCallbacks.new), diff(s1, s2),
125
+ "diff drifted from traverse_sequences for #{s1.inspect} / #{s2.inspect}"
126
+ end
127
+ end
128
+
129
+ def test_sdiff_matches_traverse_balanced_with_sdiff_callbacks
130
+ each_random_pair do |s1, s2|
131
+ assert_equal sdiff(s1, s2, Diff::LCS::SDiffCallbacks.new), sdiff(s1, s2),
132
+ "sdiff drifted from traverse_balanced for #{s1.inspect} / #{s2.inspect}"
133
+ end
134
+ end
135
+
136
+ def test_never_compares_callbacks_with_double_equals
137
+ callbacks = Class.new {
138
+ undef_method :==
139
+
140
+ def match(_event) = nil
141
+
142
+ def diffs = :traversed
143
+ }.new
144
+
145
+ assert_equal :traversed, sdiff([1], [1], callbacks)
146
+ assert_equal :traversed, diff([1], [1], callbacks)
147
+ end
148
+
149
+ def test_reads_string_subclasses_through_their_own_brackets
150
+ tagged = Class.new(String) { def [](...) = self.class.new(super) }
151
+
152
+ sdiff(tagged.new("ab"), tagged.new("ac")).each do |change|
153
+ [change.old_element, change.new_element].compact.each { assert_instance_of tagged, _1 }
154
+ end
155
+ end
156
+
157
+ private def each_random_pair
158
+ random = Random.new(20261108)
159
+
160
+ 1000.times do
161
+ alphabet = [nil, *0...random.rand(1..6)]
162
+ s1 = Array.new(random.rand(0..25)) { alphabet.sample(random:) }
163
+ s2 = Array.new(random.rand(0..25)) { alphabet.sample(random:) }
164
+ yield s1, s2
165
+
166
+ letters = "abcdef"[0, random.rand(1..6)].chars
167
+ t1 = Array.new(random.rand(0..25)) { letters.sample(random:) }.join
168
+ t2 = Array.new(random.rand(0..25)) { letters.sample(random:) }.join
169
+ yield t1, t2
170
+ end
171
+ end
25
172
  end
26
173
 
27
174
  class TestLCS < Minitest::Test
data/test/test_ldiff.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "test_helper"
4
+ require "diff/lcs/ldiff"
4
5
 
5
6
  class TestLdiff < Minitest::Test
6
7
  FIXTURES = [
@@ -10,14 +11,16 @@ class TestLdiff < Minitest::Test
10
11
  {name: "diff.issue95_trailing_context", left: "123_x", right: "456_x", diff: 1},
11
12
  {name: "diff.four_lines.vs.empty", left: "four_lines", right: "empty", diff: 1},
12
13
  {name: "diff.empty.vs.four_lines", left: "empty", right: "four_lines", diff: 1},
13
- {name: "diff.bin1", left: "file1.bin", right: "file1.bin", diff: 0},
14
- {name: "diff.bin2", left: "file1.bin", right: "file2.bin", diff: 1},
14
+ {name: "diff.bin1", left: "file1.bin", right: "file1.bin", diff: 0, flags: ["-q"]},
15
+ {name: "diff.bin2", left: "file1.bin", right: "file2.bin", diff: 1, flags: ["-q"]},
15
16
  {name: "diff.chef", left: "old-chef", right: "new-chef", diff: 1},
16
- {name: "diff.chef2", left: "old-chef2", right: "new-chef2", diff: 1}
17
- ].product([nil, "-c", "-u"]).map { |(fixture, flag)|
18
- fixture = fixture.dup
19
- fixture[:flag] = flag
20
- fixture
17
+ {name: "diff.chef2", left: "old-chef2", right: "new-chef2", diff: 1},
18
+ {name: "diff.hunks", left: "old-hunks", right: "new-hunks", diff: 1},
19
+ {name: "diff.same", left: "old-hunks", right: "old-hunks", diff: 0}
20
+ ].flat_map { |fixture|
21
+ [nil, "-c", "-u", *fixture[:flags]].map { |flag|
22
+ fixture.except(:flags).merge(flag: flag)
23
+ }
21
24
  }
22
25
 
23
26
  FIXTURES.each do |fixture|
@@ -33,8 +36,62 @@ class TestLdiff < Minitest::Test
33
36
  end
34
37
  end
35
38
 
39
+ def test_run_reads_file_names_from_args
40
+ status, output, error = run_in_process(%w[test/fixtures/aX test/fixtures/bXaX])
41
+
42
+ assert_equal 1, status
43
+ assert_equal "", error
44
+ assert_equal read_fixture({name: "diff", flag: nil}), output
45
+ end
46
+
47
+ def test_run_parses_options_from_args
48
+ status, output, error = run_in_process(%w[-u test/fixtures/aX test/fixtures/bXaX])
49
+
50
+ assert_equal 1, status
51
+ assert_equal "", error
52
+ assert_equal read_fixture({name: "diff", flag: "-u"}), clean_data(output, "-u")
53
+ end
54
+
55
+ def test_run_leaves_args_untouched
56
+ status, _output, error = run_in_process(%w[-u test/fixtures/aX test/fixtures/bXaX].freeze)
57
+
58
+ assert_equal 1, status
59
+ assert_equal "", error
60
+ end
61
+
62
+ def test_run_reads_dash_from_input
63
+ input = StringIO.new(::File.read("test/fixtures/aX"))
64
+ status, output, error = run_in_process(%w[- test/fixtures/bXaX], input: input)
65
+
66
+ assert_equal 1, status
67
+ assert_equal "", error
68
+ assert_equal read_fixture({name: "diff", flag: nil}), output
69
+ end
70
+
71
+ def test_run_reports_usage_for_missing_file_names
72
+ status, _output, error = run_in_process(%w[test/fixtures/aX])
73
+
74
+ assert_equal 127, status
75
+ assert_match(/\AUsage: #{Regexp.escape(File.basename($0))} \[options\] oldfile newfile$/, error)
76
+ end
77
+
78
+ def test_run_warns_about_invalid_options
79
+ status, output, error = run_in_process(%w[--bogus test/fixtures/aX test/fixtures/bXaX])
80
+
81
+ assert_equal 1, status
82
+ assert_equal "#{OptionParser.new.program_name}: invalid option: --bogus\n", error
83
+ assert_equal read_fixture({name: "diff", flag: nil}), output
84
+ end
85
+
36
86
  private
37
87
 
88
+ def run_in_process(args, input: StringIO.new)
89
+ output = StringIO.new
90
+ error = StringIO.new
91
+ status = Diff::LCS::Ldiff.run(args, input, output, error)
92
+ [status, output.string, error.string]
93
+ end
94
+
38
95
  def read_fixture(options, mode: "output", allow_missing: false)
39
96
  fixture = options.fetch(:name)
40
97
  flag = options.fetch(:flag)