• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

halostatue / diff-lcs / 15813454968

23 Jun 2025 01:57AM UTC coverage: 88.366%. Remained the same
15813454968

Pull #159

github

web-flow
Merge d45cbd309 into fbabd3a7f
Pull Request #159: Bump ruby/setup-ruby from 1.244.0 to 1.245.0

533 of 797 branches covered (66.88%)

676 of 765 relevant lines covered (88.37%)

288.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.93
/lib/diff/lcs/callbacks.rb
1
# frozen_string_literal: true
2

3
require "diff/lcs/change"
2✔
4

5
module Diff::LCS
2✔
6
  # This callback object implements the default set of callback events,
7
  # which only returns the event itself. Note that #finished_a and
8
  # #finished_b are not implemented -- I haven't yet figured out where they
9
  # would be useful.
10
  #
11
  # Note that this is intended to be called as is, e.g.,
12
  #
13
  #     Diff::LCS.LCS(seq1, seq2, Diff::LCS::DefaultCallbacks)
14
  class DefaultCallbacks
2✔
15
    class << self
2✔
16
      # Called when two items match.
17
      def match(event)
2✔
18
        event
×
19
      end
20

21
      # Called when the old value is discarded in favour of the new value.
22
      def discard_a(event)
2✔
23
        event
×
24
      end
25

26
      # Called when the new value is discarded in favour of the old value.
27
      def discard_b(event)
2✔
28
        event
×
29
      end
30

31
      # Called when both the old and new values have changed.
32
      def change(event)
2✔
33
        event
×
34
      end
35

36
      private :new
2✔
37
    end
38
  end
39

40
  # An alias for DefaultCallbacks that is used in
41
  # Diff::LCS#traverse_sequences.
42
  #
43
  #     Diff::LCS.LCS(seq1, seq2, Diff::LCS::SequenceCallbacks)
44
  SequenceCallbacks = DefaultCallbacks
2✔
45

46
  # An alias for DefaultCallbacks that is used in
47
  # Diff::LCS#traverse_balanced.
48
  #
49
  #     Diff::LCS.LCS(seq1, seq2, Diff::LCS::BalancedCallbacks)
50
  BalancedCallbacks = DefaultCallbacks
2✔
51

52
  def self.callbacks_for(callbacks)
2✔
53
    callbacks.new
332✔
54
  rescue
55
    callbacks
×
56
  end
57
end
58

59
# This will produce a compound array of simple diff change objects. Each
60
# element in the #diffs array is a +hunk+ or +hunk+ array, where each
61
# element in each +hunk+ array is a single Change object representing the
62
# addition or removal of a single element from one of the two tested
63
# sequences. The +hunk+ provides the full context for the changes.
64
#
65
#     diffs = Diff::LCS.diff(seq1, seq2)
66
#       # This example shows a simplified array format.
67
#       # [ [ [ '-',  0, 'a' ] ],   # 1
68
#       #   [ [ '+',  2, 'd' ] ],   # 2
69
#       #   [ [ '-',  4, 'h' ],     # 3
70
#       #     [ '+',  4, 'f' ] ],
71
#       #   [ [ '+',  6, 'k' ] ],   # 4
72
#       #   [ [ '-',  8, 'n' ],     # 5
73
#       #     [ '-',  9, 'p' ],
74
#       #     [ '+',  9, 'r' ],
75
#       #     [ '+', 10, 's' ],
76
#       #     [ '+', 11, 't' ] ] ]
77
#
78
# There are five hunks here. The first hunk says that the +a+ at position 0
79
# of the first sequence should be deleted (<tt>'-'</tt>). The second hunk
80
# says that the +d+ at position 2 of the second sequence should be inserted
81
# (<tt>'+'</tt>). The third hunk says that the +h+ at position 4 of the
82
# first sequence should be removed and replaced with the +f+ from position 4
83
# of the second sequence. The other two hunks are described similarly.
84
#
85
# === Use
86
#
87
# This callback object must be initialised and is used by the Diff::LCS#diff
88
# method.
89
#
90
#     cbo = Diff::LCS::DiffCallbacks.new
91
#     Diff::LCS.LCS(seq1, seq2, cbo)
92
#     cbo.finish
93
#
94
# Note that the call to #finish is absolutely necessary, or the last set of
95
# changes will not be visible. Alternatively, can be used as:
96
#
97
#     cbo = Diff::LCS::DiffCallbacks.new { |tcbo| Diff::LCS.LCS(seq1, seq2, tcbo) }
98
#
99
# The necessary #finish call will be made.
100
#
101
# === Simplified Array Format
102
#
103
# The simplified array format used in the example above can be obtained
104
# with:
105
#
106
#     require 'pp'
107
#     pp diffs.map { |e| e.map { |f| f.to_a } }
108
class Diff::LCS::DiffCallbacks
2✔
109
  # Returns the difference set collected during the diff process.
110
  attr_reader :diffs
2✔
111

112
  def initialize # :yields: self
2✔
113
    @hunk = []
204✔
114
    @diffs = []
204✔
115

116
    return unless block_given?
204!
117

118
    begin
119
      yield self
×
120
    ensure
121
      finish
×
122
    end
123
  end
124

125
  # Finalizes the diff process. If an unprocessed hunk still exists, then it
126
  # is appended to the diff list.
127
  def finish
2✔
128
    finish_hunk
204✔
129
  end
130

131
  def match(_event)
2✔
132
    finish_hunk
1,412✔
133
  end
134

135
  def discard_a(event)
2✔
136
    @hunk << Diff::LCS::Change.new("-", event.old_position, event.old_element)
250✔
137
  end
138

139
  def discard_b(event)
2✔
140
    @hunk << Diff::LCS::Change.new("+", event.new_position, event.new_element)
260✔
141
  end
142

143
  def finish_hunk
2✔
144
    @diffs << @hunk unless @hunk.empty?
1,616✔
145
    @hunk = []
1,616✔
146
  end
147
  private :finish_hunk
2✔
148
end
149

150
# This will produce a compound array of contextual diff change objects. Each
151
# element in the #diffs array is a "hunk" array, where each element in each
152
# "hunk" array is a single change. Each change is a Diff::LCS::ContextChange
153
# that contains both the old index and new index values for the change. The
154
# "hunk" provides the full context for the changes. Both old and new objects
155
# will be presented for changed objects. +nil+ will be substituted for a
156
# discarded object.
157
#
158
#     seq1 = %w(a b c e h j l m n p)
159
#     seq2 = %w(b c d e f j k l m r s t)
160
#
161
#     diffs = Diff::LCS.diff(seq1, seq2, Diff::LCS::ContextDiffCallbacks)
162
#       # This example shows a simplified array format.
163
#       # [ [ [ '-', [  0, 'a' ], [  0, nil ] ] ],   # 1
164
#       #   [ [ '+', [  3, nil ], [  2, 'd' ] ] ],   # 2
165
#       #   [ [ '-', [  4, 'h' ], [  4, nil ] ],     # 3
166
#       #     [ '+', [  5, nil ], [  4, 'f' ] ] ],
167
#       #   [ [ '+', [  6, nil ], [  6, 'k' ] ] ],   # 4
168
#       #   [ [ '-', [  8, 'n' ], [  9, nil ] ],     # 5
169
#       #     [ '+', [  9, nil ], [  9, 'r' ] ],
170
#       #     [ '-', [  9, 'p' ], [ 10, nil ] ],
171
#       #     [ '+', [ 10, nil ], [ 10, 's' ] ],
172
#       #     [ '+', [ 10, nil ], [ 11, 't' ] ] ] ]
173
#
174
# The five hunks shown are comprised of individual changes; if there is a
175
# related set of changes, they are still shown individually.
176
#
177
# This callback can also be used with Diff::LCS#sdiff, which will produce
178
# results like:
179
#
180
#     diffs = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextCallbacks)
181
#       # This example shows a simplified array format.
182
#       # [ [ [ "-", [  0, "a" ], [  0, nil ] ] ],  # 1
183
#       #   [ [ "+", [  3, nil ], [  2, "d" ] ] ],  # 2
184
#       #   [ [ "!", [  4, "h" ], [  4, "f" ] ] ],  # 3
185
#       #   [ [ "+", [  6, nil ], [  6, "k" ] ] ],  # 4
186
#       #   [ [ "!", [  8, "n" ], [  9, "r" ] ],    # 5
187
#       #     [ "!", [  9, "p" ], [ 10, "s" ] ],
188
#       #     [ "+", [ 10, nil ], [ 11, "t" ] ] ] ]
189
#
190
# The five hunks are still present, but are significantly shorter in total
191
# presentation, because changed items are shown as changes ("!") instead of
192
# potentially "mismatched" pairs of additions and deletions.
193
#
194
# The result of this operation is similar to that of
195
# Diff::LCS::SDiffCallbacks. They may be compared as:
196
#
197
#     s = Diff::LCS.sdiff(seq1, seq2).reject { |e| e.action == "=" }
198
#     c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten(1)
199
#
200
#     s == c # -> true
201
#
202
# === Use
203
#
204
# This callback object must be initialised and can be used by the
205
# Diff::LCS#diff or Diff::LCS#sdiff methods.
206
#
207
#     cbo = Diff::LCS::ContextDiffCallbacks.new
208
#     Diff::LCS.LCS(seq1, seq2, cbo)
209
#     cbo.finish
210
#
211
# Note that the call to #finish is absolutely necessary, or the last set of
212
# changes will not be visible. Alternatively, can be used as:
213
#
214
#     cbo = Diff::LCS::ContextDiffCallbacks.new { |tcbo| Diff::LCS.LCS(seq1, seq2, tcbo) }
215
#
216
# The necessary #finish call will be made.
217
#
218
# === Simplified Array Format
219
#
220
# The simplified array format used in the example above can be obtained
221
# with:
222
#
223
#     require 'pp'
224
#     pp diffs.map { |e| e.map { |f| f.to_a } }
225
class Diff::LCS::ContextDiffCallbacks < Diff::LCS::DiffCallbacks
2✔
226
  def discard_a(event)
2✔
227
    @hunk << Diff::LCS::ContextChange.simplify(event)
132✔
228
  end
229

230
  def discard_b(event)
2✔
231
    @hunk << Diff::LCS::ContextChange.simplify(event)
132✔
232
  end
233

234
  def change(event)
2✔
235
    @hunk << Diff::LCS::ContextChange.simplify(event)
48✔
236
  end
237
end
238

239
# This will produce a simple array of diff change objects. Each element in
240
# the #diffs array is a single ContextChange. In the set of #diffs provided
241
# by SDiffCallbacks, both old and new objects will be presented for both
242
# changed <strong>and unchanged</strong> objects. +nil+ will be substituted
243
# for a discarded object.
244
#
245
# The diffset produced by this callback, when provided to Diff::LCS#sdiff,
246
# will compute and display the necessary components to show two sequences
247
# and their minimized differences side by side, just like the Unix utility
248
# +sdiff+.
249
#
250
#     same             same
251
#     before     |     after
252
#     old        <     -
253
#     -          >     new
254
#
255
#     seq1 = %w(a b c e h j l m n p)
256
#     seq2 = %w(b c d e f j k l m r s t)
257
#
258
#     diffs = Diff::LCS.sdiff(seq1, seq2)
259
#       # This example shows a simplified array format.
260
#       # [ [ "-", [  0, "a"], [  0, nil ] ],
261
#       #   [ "=", [  1, "b"], [  0, "b" ] ],
262
#       #   [ "=", [  2, "c"], [  1, "c" ] ],
263
#       #   [ "+", [  3, nil], [  2, "d" ] ],
264
#       #   [ "=", [  3, "e"], [  3, "e" ] ],
265
#       #   [ "!", [  4, "h"], [  4, "f" ] ],
266
#       #   [ "=", [  5, "j"], [  5, "j" ] ],
267
#       #   [ "+", [  6, nil], [  6, "k" ] ],
268
#       #   [ "=", [  6, "l"], [  7, "l" ] ],
269
#       #   [ "=", [  7, "m"], [  8, "m" ] ],
270
#       #   [ "!", [  8, "n"], [  9, "r" ] ],
271
#       #   [ "!", [  9, "p"], [ 10, "s" ] ],
272
#       #   [ "+", [ 10, nil], [ 11, "t" ] ] ]
273
#
274
# The result of this operation is similar to that of
275
# Diff::LCS::ContextDiffCallbacks. They may be compared as:
276
#
277
#     s = Diff::LCS.sdiff(seq1, seq2).reject { |e| e.action == "=" }
278
#     c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten(1)
279
#
280
#     s == c # -> true
281
#
282
# === Use
283
#
284
# This callback object must be initialised and is used by the Diff::LCS#sdiff
285
# method.
286
#
287
#     cbo = Diff::LCS::SDiffCallbacks.new
288
#     Diff::LCS.LCS(seq1, seq2, cbo)
289
#
290
# As with the other initialisable callback objects,
291
# Diff::LCS::SDiffCallbacks can be initialised with a block. As there is no
292
# "fininishing" to be done, this has no effect on the state of the object.
293
#
294
#     cbo = Diff::LCS::SDiffCallbacks.new { |tcbo| Diff::LCS.LCS(seq1, seq2, tcbo) }
295
#
296
# === Simplified Array Format
297
#
298
# The simplified array format used in the example above can be obtained
299
# with:
300
#
301
#     require 'pp'
302
#     pp diffs.map { |e| e.to_a }
303
class Diff::LCS::SDiffCallbacks
2✔
304
  # Returns the difference set collected during the diff process.
305
  attr_reader :diffs
2✔
306

307
  def initialize # :yields: self
2✔
308
    @diffs = []
128✔
309
    yield self if block_given?
128!
310
  end
311

312
  def match(event)
2✔
313
    @diffs << Diff::LCS::ContextChange.simplify(event)
744✔
314
  end
315

316
  def discard_a(event)
2✔
317
    @diffs << Diff::LCS::ContextChange.simplify(event)
210✔
318
  end
319

320
  def discard_b(event)
2✔
321
    @diffs << Diff::LCS::ContextChange.simplify(event)
210✔
322
  end
323

324
  def change(event)
2✔
325
    @diffs << Diff::LCS::ContextChange.simplify(event)
80✔
326
  end
327
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc