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
@@ -17,9 +17,9 @@ class Diff::LCS::Ldiff # :nodoc:
17
17
  COPYRIGHT
18
18
 
19
19
  InputInfo = Struct.new(:filename, :data, :mtime) do
20
- def initialize(filename)
20
+ def initialize(filename, input = $stdin)
21
21
  if filename == "-"
22
- super("<stdin>", $stdin.read, Time.now)
22
+ super("<stdin>", input.read, Time.now)
23
23
  return
24
24
  end
25
25
 
@@ -41,8 +41,8 @@ class Diff::LCS::Ldiff # :nodoc:
41
41
  @lines = 0
42
42
  end
43
43
 
44
- def run(args, _input = $stdin, output = $stdout, error = $stderr) # :nodoc:
45
- args.options do |o|
44
+ def run(args, input = $stdin, output = $stdout, error = $stderr) # :nodoc:
45
+ parser = OptionParser.new do |o|
46
46
  o.banner = "Usage: #{File.basename($0)} [options] oldfile newfile"
47
47
  o.separator ""
48
48
  o.on(
@@ -81,11 +81,18 @@ class Diff::LCS::Ldiff # :nodoc:
81
81
  end
82
82
  o.on_tail ""
83
83
  o.on_tail 'By default, runs produces an "old-style" diff, with output like UNIX diff.'
84
- o.parse!
84
+ end
85
+ # parse! on a copy rather than parse: after an invalid option, the copy
86
+ # still holds the arguments left to read, as ARGV did before.
87
+ args = args.dup
88
+ begin
89
+ parser.parse!(args)
90
+ rescue OptionParser::ParseError => e
91
+ error << "#{parser.program_name}: #{e.message}\n"
85
92
  end
86
93
 
87
94
  unless args.size == 2
88
- error << args.options
95
+ error << parser
89
96
  return 127
90
97
  end
91
98
 
@@ -93,10 +100,10 @@ class Diff::LCS::Ldiff # :nodoc:
93
100
  @format ||= :old
94
101
  @lines ||= 0
95
102
 
96
- file_old, file_new = *ARGV
103
+ file_old, file_new = *args
97
104
  diff?(
98
- InputInfo.new(file_old),
99
- InputInfo.new(file_new),
105
+ InputInfo.new(file_old, input),
106
+ InputInfo.new(file_new, input),
100
107
  @format,
101
108
  output,
102
109
  binary: @binary,
@@ -114,10 +121,6 @@ class Diff::LCS::Ldiff # :nodoc:
114
121
  char_new = "+" * 3
115
122
  end
116
123
 
117
- # After we've read up to a certain point in each file, the number of items we've read
118
- # from each file will differ by FLD (could be 0).
119
- file_length_difference = 0
120
-
121
124
  # Test binary status
122
125
  if binary.nil?
123
126
  old_bin = info_old.data[0, 4096].include?("\0")
@@ -125,21 +128,17 @@ class Diff::LCS::Ldiff # :nodoc:
125
128
  binary = old_bin || new_bin
126
129
  end
127
130
 
128
- # diff yields lots of pieces, each of which is basically a Block object
129
131
  if binary
130
- has_diffs = (info_old.data != info_new.data)
132
+ return false if info_old.data == info_new.data
133
+
131
134
  if format != :report
132
- if has_diffs
133
- output << "Binary files #{info_old.filename} and #{info_new.filename} differ\n"
134
- return true
135
- end
136
- return false
135
+ output << "Binary files #{info_old.filename} and #{info_new.filename} differ\n"
136
+ return true
137
137
  end
138
138
  else
139
139
  data_old = info_old.data.lines.to_a
140
140
  data_new = info_new.data.lines.to_a
141
- diffs = Diff::LCS.diff(data_old, data_new)
142
- return false if diffs.empty?
141
+ return false if data_old == data_new
143
142
  end
144
143
 
145
144
  case format
@@ -151,31 +150,10 @@ class Diff::LCS::Ldiff # :nodoc:
151
150
  output << "#{char_old} #{info_old.filename}\t#{ft}\n"
152
151
  ft = info_new.mtime.localtime.strftime("%Y-%m-%d %H:%M:%S.000000000 %z")
153
152
  output << "#{char_new} #{info_new.filename}\t#{ft}\n"
154
- when :ed
155
- output = []
156
- end
157
-
158
- # Loop over hunks. If a hunk overlaps with the last hunk, join them. Otherwise, print
159
- # out the old one.
160
- oldhunk = hunk = nil
161
- diffs.each do |piece|
162
- hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, lines, file_length_difference)
163
- file_length_difference = hunk.file_length_difference
164
-
165
- next unless oldhunk
166
- next if lines.positive? && hunk.merge(oldhunk)
167
-
168
- output << oldhunk.diff(format)
169
- output << "\n" if format == :unified
170
- ensure
171
- oldhunk = hunk
172
153
  end
173
154
 
174
- last = oldhunk.diff(format, true)
175
- last << "\n" unless last.is_a?(Diff::LCS::Hunk) || last.empty? || last.end_with?("\n")
176
-
177
- output << last
155
+ output << Diff::LCS::Hunk.diff(data_old, data_new, format, lines)
178
156
 
179
- 1
157
+ true
180
158
  end
181
159
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class String
3
+ warn 'diff/lcs/string: Automatically extending String with Diff::LCS is deprecated'
4
+
5
+ class ::String # :nodoc:
4
6
  include Diff::LCS
5
7
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Diff
4
4
  module LCS
5
- VERSION = "2.0.0"
5
+ VERSION = "2.1.0"
6
6
  end
7
7
  end
data/lib/diff/lcs.rb CHANGED
@@ -89,24 +89,24 @@ module Diff::LCS
89
89
  Diff::LCS.traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
90
90
 
91
91
  # Attempts to patch `self` with the provided `patchset`. A new sequence based on `self`
92
- # and the `patchset` will be created. See Diff::LCS.patch. Attempts to autodiscover the
92
+ # and the `patchset` will be created. See Diff::LCS.patch. Attempts to auto-discover the
93
93
  # direction of the patch.
94
94
  def patch(patchset) = Diff::LCS.patch(self, patchset)
95
95
  alias_method :unpatch, :patch
96
96
 
97
97
  # Attempts to patch `self` with the provided `patchset`. A new sequence based on `self`
98
98
  # and the `patchset` will be created. See Diff::LCS.patch!. Does no patch direction
99
- # autodiscovery.
99
+ # auto-discovery.
100
100
  def patch!(patchset) = Diff::LCS.patch!(self, patchset)
101
101
 
102
- # Attempts to unpatch `self` with the provided `patchset`. A new sequence based on
102
+ # Attempts to un-patch `self` with the provided `patchset`. A new sequence based on
103
103
  # `self` and the `patchset` will be created. See Diff::LCS.unpatch!. Does no patch
104
- # direction autodiscovery.
104
+ # direction auto-discovery.
105
105
  def unpatch!(patchset) = Diff::LCS.unpatch!(self, patchset)
106
106
 
107
107
  # Attempts to patch `self` with the provided `patchset`, using #patch!. If the sequence
108
108
  # this is used on supports #replace, the value of `self` will be replaced. See
109
- # Diff::LCS.patch!. Does no patch direction autodiscovery.
109
+ # Diff::LCS.patch!. Does no patch direction auto-discovery.
110
110
  def patch_me(patchset)
111
111
  if respond_to? :replace
112
112
  replace(patch!(patchset))
@@ -115,9 +115,9 @@ module Diff::LCS
115
115
  end
116
116
  end
117
117
 
118
- # Attempts to unpatch `self` with the provided `patchset`, using #unpatch!. If the
118
+ # Attempts to un-patch `self` with the provided `patchset`, using #unpatch!. If the
119
119
  # sequence this is used on supports #replace, the value of `self` will be replaced. See
120
- # Diff::LCS#unpatch. Does no patch direction autodiscovery.
120
+ # Diff::LCS#unpatch. Does no patch direction auto-discovery.
121
121
  def unpatch_me(patchset)
122
122
  if respond_to? :replace
123
123
  replace(unpatch!(patchset))
@@ -138,6 +138,8 @@ module Diff::LCS
138
138
  # > O.new('a').eql?(O.new('a')) == true && O.new('a').hash == O.new('a').hash
139
139
  # > ```
140
140
  def self.lcs(seq1, seq2, &block) # :yields: seq1[i] for each matched
141
+ seq1 = Diff::LCS::Internals.indexable(seq1)
142
+ seq2 = Diff::LCS::Internals.indexable(seq2)
141
143
  matches = Diff::LCS::Internals.lcs(seq1, seq2)
142
144
  [].tap { |result|
143
145
  matches.each_index do
@@ -161,7 +163,7 @@ module Diff::LCS
161
163
  diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
162
164
 
163
165
  # `sdiff` computes all necessary components to show two sequences and their minimized
164
- # differences side by side, just like the Unix utility _sdiff_ does:
166
+ # differences side by side, just like the Unix utility `sdiff` does:
165
167
  #
166
168
  #
167
169
  # ```
@@ -267,6 +269,8 @@ module Diff::LCS
267
269
  # will be called after the end of the sequence is reached, if `a` has not yet reached
268
270
  # the end of `A` or `b` has not yet reached the end of `B`.
269
271
  def self.traverse_sequences(seq1, seq2, callbacks = nil) # :yields: change events
272
+ seq1 = Diff::LCS::Internals.indexable(seq1)
273
+ seq2 = Diff::LCS::Internals.indexable(seq2)
270
274
  callbacks ||= Diff::LCS::SequenceCallbacks
271
275
  matches = Diff::LCS::Internals.lcs(seq1, seq2)
272
276
 
@@ -448,7 +452,10 @@ module Diff::LCS
448
452
  # Note that `i` and `j` may not be the same index position, even if `a` and `b` are
449
453
  # considered to be pointing to matching or changed elements.
450
454
  def self.traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
455
+ seq1 = Diff::LCS::Internals.indexable(seq1)
456
+ seq2 = Diff::LCS::Internals.indexable(seq2)
451
457
  matches = Diff::LCS::Internals.lcs(seq1, seq2)
458
+ change_callback = callbacks.respond_to?(:change)
452
459
  a_size = seq1.size
453
460
  b_size = seq2.size
454
461
  a_i = b_j = m_b = 0
@@ -471,9 +478,8 @@ module Diff::LCS
471
478
  a_x = seq1[a_i]
472
479
  b_x = seq2[b_j]
473
480
 
474
- case [(a_i < m_a), (b_j < m_b)]
475
- when [true, true]
476
- if callbacks.respond_to?(:change)
481
+ if (a_i < m_a) && (b_j < m_b)
482
+ if change_callback
477
483
  event = Diff::LCS::ContextChange.new("!", a_i, a_x, b_j, b_x)
478
484
  event = yield event if block_given?
479
485
  callbacks.change(event)
@@ -490,12 +496,12 @@ module Diff::LCS
490
496
  end
491
497
 
492
498
  b_j += 1
493
- when [true, false]
499
+ elsif a_i < m_a
494
500
  event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
495
501
  event = yield event if block_given?
496
502
  callbacks.discard_a(event)
497
503
  a_i += 1
498
- when [false, true]
504
+ else
499
505
  event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
500
506
  event = yield event if block_given?
501
507
  callbacks.discard_b(event)
@@ -517,9 +523,8 @@ module Diff::LCS
517
523
  a_x = seq1[a_i]
518
524
  b_x = seq2[b_j]
519
525
 
520
- case [(a_i < a_size), (b_j < b_size)]
521
- when [true, true]
522
- if callbacks.respond_to?(:change)
526
+ if (a_i < a_size) && (b_j < b_size)
527
+ if change_callback
523
528
  event = Diff::LCS::ContextChange.new("!", a_i, a_x, b_j, b_x)
524
529
  event = yield event if block_given?
525
530
  callbacks.change(event)
@@ -536,12 +541,12 @@ module Diff::LCS
536
541
  end
537
542
 
538
543
  b_j += 1
539
- when [true, false]
544
+ elsif a_i < a_size
540
545
  event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
541
546
  event = yield event if block_given?
542
547
  callbacks.discard_a(event)
543
548
  a_i += 1
544
- when [false, true]
549
+ else
545
550
  event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
546
551
  event = yield event if block_given?
547
552
  callbacks.discard_b(event)
@@ -586,12 +591,12 @@ module Diff::LCS
586
591
  # patchset.empty? or patchset.flatten(1).all? { |change| change.unchanged? }
587
592
  # ```
588
593
  #
589
- # ### Patchsets
594
+ # ### Patch Sets
590
595
  #
591
596
  # A `patchset` is always an enumerable sequence of changes, hunks of changes, or a mix
592
597
  # of the two. A hunk of changes is an enumerable sequence of changes:
593
598
  #
594
- # ```
599
+ # ```ruby
595
600
  # [ # patchset
596
601
  # # change
597
602
  # [ # hunk
@@ -605,7 +610,6 @@ module Diff::LCS
605
610
  # objects. Prior to application, array representations of Diff::LCS::Change objects will
606
611
  # be reified.
607
612
  def self.patch(src, patchset, direction = nil)
608
- # Normalize the patchset.
609
613
  has_changes, patchset = Diff::LCS::Internals.analyze_patchset(patchset)
610
614
 
611
615
  return src.respond_to?(:dup) ? src.dup : src unless has_changes
@@ -654,9 +658,8 @@ module Diff::LCS
654
658
  res << el
655
659
  b_j += 1
656
660
  when "="
657
- # This only appears in sdiff output with the SDiff callback.
658
- # Therefore, we only need to worry about dealing with a single
659
- # element.
661
+ # This only appears in `sdiff` output with the `SDiff` callback. Therefore, we
662
+ # only need to worry about dealing with a single element.
660
663
  res << el
661
664
 
662
665
  a_i += 1
@@ -705,11 +708,11 @@ module Diff::LCS
705
708
  res
706
709
  end
707
710
 
708
- # Given a patchset, convert the current version to the prior version. Does no
711
+ # Given a `patchset`, convert the current version to the prior version. Does no
709
712
  # auto-discovery.
710
713
  def self.unpatch!(src, patchset) = patch(src, patchset, :unpatch)
711
714
 
712
- # Given a patchset, convert the current version to the next version. Does no
715
+ # Given a `patchset`, convert the current version to the next version. Does no
713
716
  # auto-discovery.
714
717
  def self.patch!(src, patchset) = patch(src, patchset, :patch)
715
718
  end
data/licences/MIT.txt ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2004-2026 Austin Ziegler and Diff::LCS Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
+ software and associated documentation files (the "Software"), to deal in the Software
7
+ without restriction, including without limitation the rights to use, copy, modify, merge,
8
+ publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
+ to whom the Software is furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or
12
+ substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17
+ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
data/spec/ldiff_spec.rb CHANGED
@@ -13,14 +13,16 @@ RSpec.describe "bin/ldiff" do
13
13
  {:name => "diff.issue95_trailing_context", :left => "123_x", :right => "456_x", :diff => 1},
14
14
  {:name => "diff.four_lines.vs.empty", :left => "four_lines", :right => "empty", :diff => 1},
15
15
  {:name => "diff.empty.vs.four_lines", :left => "empty", :right => "four_lines", :diff => 1},
16
- {:name => "diff.bin1", :left => "file1.bin", :right => "file1.bin", :diff => 0},
17
- {:name => "diff.bin2", :left => "file1.bin", :right => "file2.bin", :diff => 1},
16
+ {:name => "diff.bin1", :left => "file1.bin", :right => "file1.bin", :diff => 0, :flags => ["-q"]},
17
+ {:name => "diff.bin2", :left => "file1.bin", :right => "file2.bin", :diff => 1, :flags => ["-q"]},
18
18
  {:name => "diff.chef", :left => "old-chef", :right => "new-chef", :diff => 1},
19
- {:name => "diff.chef2", :left => "old-chef2", :right => "new-chef2", :diff => 1}
20
- ].product([nil, "-c", "-u"]).map { |(fixture, flag)|
21
- fixture = fixture.dup
22
- fixture[:flag] = flag
23
- fixture
19
+ {:name => "diff.chef2", :left => "old-chef2", :right => "new-chef2", :diff => 1},
20
+ {:name => "diff.hunks", :left => "old-hunks", :right => "new-hunks", :diff => 1},
21
+ {:name => "diff.same", :left => "old-hunks", :right => "old-hunks", :diff => 0}
22
+ ].flat_map { |fixture|
23
+ [nil, "-c", "-u", *fixture[:flags]].map { |flag|
24
+ fixture.except(:flags).merge(flag: flag)
25
+ }
24
26
  }
25
27
  # standard:enable Style/HashSyntax
26
28
 
File without changes
@@ -0,0 +1 @@
1
+ Files test/fixtures/file1.bin and test/fixtures/file2.bin differ
@@ -0,0 +1,9 @@
1
+ 3c3
2
+ < 3
3
+ ---
4
+ > x
5
+ 26c26,27
6
+ < 26
7
+ ---
8
+ > y
9
+ > z
@@ -0,0 +1,35 @@
1
+ *** test/fixtures/old-hunks 2026-11-10 12:00:00.000000000 +0000
2
+ --- test/fixtures/new-hunks 2026-11-10 12:00:00.000000000 +0000
3
+ ***************
4
+ *** 1,6 ****
5
+ 1
6
+ 2
7
+ ! 3
8
+ 4
9
+ 5
10
+ 6
11
+ --- 1,6 ----
12
+ 1
13
+ 2
14
+ ! x
15
+ 4
16
+ 5
17
+ 6
18
+ ***************
19
+ *** 23,29 ****
20
+ 23
21
+ 24
22
+ 25
23
+ ! 26
24
+ 27
25
+ 28
26
+ 29
27
+ --- 23,30 ----
28
+ 23
29
+ 24
30
+ 25
31
+ ! y
32
+ ! z
33
+ 27
34
+ 28
35
+ 29
@@ -0,0 +1,20 @@
1
+ --- test/fixtures/old-hunks 2026-11-10 12:00:00.000000000 +0000
2
+ +++ test/fixtures/new-hunks 2026-11-10 12:00:00.000000000 +0000
3
+ @@ -1,6 +1,6 @@
4
+ 1
5
+ 2
6
+ -3
7
+ +x
8
+ 4
9
+ 5
10
+ 6
11
+ @@ -23,7 +23,8 @@
12
+ 23
13
+ 24
14
+ 25
15
+ -26
16
+ +y
17
+ +z
18
+ 27
19
+ 28
20
+ 29
File without changes
File without changes
File without changes
@@ -0,0 +1,31 @@
1
+ 1
2
+ 2
3
+ x
4
+ 4
5
+ 5
6
+ 6
7
+ 7
8
+ 8
9
+ 9
10
+ 10
11
+ 11
12
+ 12
13
+ 13
14
+ 14
15
+ 15
16
+ 16
17
+ 17
18
+ 18
19
+ 19
20
+ 20
21
+ 21
22
+ 22
23
+ 23
24
+ 24
25
+ 25
26
+ y
27
+ z
28
+ 27
29
+ 28
30
+ 29
31
+ 30
@@ -0,0 +1,30 @@
1
+ 1
2
+ 2
3
+ 3
4
+ 4
5
+ 5
6
+ 6
7
+ 7
8
+ 8
9
+ 9
10
+ 10
11
+ 11
12
+ 12
13
+ 13
14
+ 14
15
+ 15
16
+ 16
17
+ 17
18
+ 18
19
+ 19
20
+ 20
21
+ 21
22
+ 22
23
+ 23
24
+ 24
25
+ 25
26
+ 26
27
+ 27
28
+ 28
29
+ 29
30
+ 30
data/test/test_change.rb CHANGED
@@ -4,11 +4,11 @@ require "test_helper"
4
4
 
5
5
  class TestChange < Minitest::Test
6
6
  def test_invalid_change
7
- assert_raises RuntimeError, /Invalid CHange Action/ do
7
+ assert_raises_with_message RuntimeError, /Invalid Change Action/ do
8
8
  change("~", 0, "element")
9
9
  end
10
10
 
11
- assert_raises RuntimeError, /Invalid Position Type/ do
11
+ assert_raises_with_message RuntimeError, /Invalid Position Type/ do
12
12
  change("+", 3.5, "element")
13
13
  end
14
14
  end
@@ -21,11 +21,11 @@ class TestChange < Minitest::Test
21
21
  assert_kind_of Diff::LCS::ContextChange,
22
22
  change_from_a(["!", [1, "old_element"], [2, "new_element"]], Diff::LCS::ContextChange)
23
23
 
24
- assert_raises RuntimeError, "Invalid change array format provided." do
24
+ assert_raises_with_message RuntimeError, "Invalid change array format provided." do
25
25
  change_from_a(["+", 0])
26
26
  end
27
27
 
28
- assert_raises RuntimeError, "Invalid change array format provided." do
28
+ assert_raises_with_message RuntimeError, "Invalid change array format provided." do
29
29
  change_from_a(["+", 0], Diff::LCS::ContextChange)
30
30
  end
31
31
  end
@@ -148,15 +148,15 @@ class TestContextChange < Minitest::Test
148
148
  private def simplify(...) = Diff::LCS::ContextChange.simplify(change(...))
149
149
 
150
150
  def test_invalid_change
151
- assert_raises RuntimeError, /Invalid CHange Action/ do
151
+ assert_raises_with_message RuntimeError, /Invalid Change Action/ do
152
152
  change("~", 0, "old", 0, "new")
153
153
  end
154
154
 
155
- assert_raises RuntimeError, /Invalid Position Type/ do
155
+ assert_raises_with_message RuntimeError, "Invalid (Old) Position Type" do
156
156
  change("+", 3.5, "old", 0, "new")
157
157
  end
158
158
 
159
- assert_raises RuntimeError, /Invalid Position Type/ do
159
+ assert_raises_with_message RuntimeError, "Invalid (New) Position Type" do
160
160
  change("+", 0, "old", 3.5, "new")
161
161
  end
162
162
  end
@@ -215,20 +215,24 @@ class TestContextChange < Minitest::Test
215
215
  end
216
216
 
217
217
  def test_simplify
218
- simplify("-", 5, "old", 10, "new") => {action:, new_element:}
219
- assert_equal action, "-"
220
- assert_nil new_element
218
+ assert_equal change("-", 5, "old", 10, nil), simplify("-", 5, "old", 10, "new")
219
+ assert_equal change("-", 5, "old", 10, nil), simplify("<", 5, "old", 10, "new")
220
+ assert_equal change("+", 5, nil, 10, "new"), simplify("+", 5, "old", 10, "new")
221
+ assert_equal change("+", 5, nil, 10, "new"), simplify(">", 5, "old", 10, "new")
222
+ end
223
+
224
+ def test_simplify_returns_unchanged_and_changed_events_as_they_are
225
+ %w[= !].each do |action|
226
+ event = change(action, 5, "old", 10, "new")
221
227
 
222
- simplify("<", 5, "old", 10, "new") => {action:, new_element:}
223
- assert_equal action, "-"
224
- assert_nil new_element
228
+ assert_same event, Diff::LCS::ContextChange.simplify(event)
229
+ end
230
+ end
225
231
 
226
- simplify("+", 5, "old", 10, "new") => {action:, old_element:}
227
- assert_equal action, "+"
228
- assert_nil old_element
232
+ def test_simplify_keeps_the_class_of_the_event
233
+ subclass = Class.new(Diff::LCS::ContextChange)
229
234
 
230
- simplify(">", 5, "old", 10, "new") => {action:, old_element:}
231
- assert_equal action, "+"
232
- assert_nil old_element
235
+ assert_instance_of subclass, Diff::LCS::ContextChange.simplify(subclass.new("<", 5, "old", 10, "new"))
236
+ assert_instance_of subclass, Diff::LCS::ContextChange.simplify(subclass.new(">", 5, "old", 10, "new"))
233
237
  end
234
238
  end