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

halostatue / diff-lcs / 14698439081

28 Apr 2025 01:39AM UTC coverage: 88.598%. Remained the same
14698439081

Pull #143

github

web-flow
Merge 58e56b158 into 288dfccd3
Pull Request #143: Bump astral-sh/setup-uv from 5.4.2 to 6.0.0

533 of 797 branches covered (66.88%)

676 of 763 relevant lines covered (88.6%)

288.92 hits per line

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

81.61
/lib/diff/lcs/change.rb
1
# frozen_string_literal: true
2

3
# Represents a simplistic (non-contextual) change. Represents the removal or
4
# addition of an element from either the old or the new sequenced
5
# enumerable.
6
class Diff::LCS::Change
2✔
7
  IntClass = 1.class # Fixnum is deprecated in Ruby 2.4 # standard:disable Naming/ConstantName
2✔
8

9
  # The only actions valid for changes are '+' (add), '-' (delete), '='
10
  # (no change), '!' (changed), '<' (tail changes from first sequence), or
11
  # '>' (tail changes from second sequence). The last two ('<>') are only
12
  # found with Diff::LCS::diff and Diff::LCS::sdiff.
13
  VALID_ACTIONS = %w[+ - = ! > <].freeze
2✔
14

15
  def self.valid_action?(action)
2✔
16
    VALID_ACTIONS.include? action
8,176✔
17
  end
18

19
  # Returns the action this Change represents.
20
  attr_reader :action
2✔
21

22
  # Returns the position of the Change.
23
  attr_reader :position
2✔
24
  # Returns the sequence element of the Change.
25
  attr_reader :element
2✔
26

27
  def initialize(*args)
2✔
28
    @action, @position, @element = *args
656✔
29

30
    fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
656!
31
    fail "Invalid Position Type" unless @position.is_a? IntClass
656!
32
  end
33

34
  def inspect(*_args)
2✔
35
    "#<#{self.class}: #{to_a.inspect}>"
×
36
  end
37

38
  def to_a
2✔
39
    [@action, @position, @element]
2✔
40
  end
41

42
  alias_method :to_ary, :to_a
2✔
43

44
  def self.from_a(arr)
2✔
45
    arr = arr.flatten(1)
1,908✔
46
    case arr.size
1,908!
47
    when 5
918✔
48
      Diff::LCS::ContextChange.new(*(arr[0...5]))
1,836✔
49
    when 3
36✔
50
      Diff::LCS::Change.new(*(arr[0...3]))
72✔
51
    else
×
52
      fail "Invalid change array format provided."
×
53
    end
54
  end
55

56
  include Comparable
2✔
57

58
  def ==(other)
2✔
59
    (self.class == other.class) and
72✔
60
      (action == other.action) and
72✔
61
      (position == other.position) and
72✔
62
      (element == other.element)
72✔
63
  end
64

65
  def <=>(other)
2✔
66
    r = action <=> other.action
×
67
    r = position <=> other.position if r.zero?
×
68
    r = element <=> other.element if r.zero?
×
69
    r
×
70
  end
71

72
  def adding?
2✔
73
    @action == "+"
54✔
74
  end
75

76
  def deleting?
2✔
77
    @action == "-"
54✔
78
  end
79

80
  def unchanged?
2✔
81
    @action == "="
916✔
82
  end
83

84
  def changed?
2✔
85
    @action == "!"
12✔
86
  end
87

88
  def finished_a?
2✔
89
    @action == ">"
12✔
90
  end
91

92
  def finished_b?
2✔
93
    @action == "<"
12✔
94
  end
95
end
96

97
# Represents a contextual change. Contains the position and values of the
98
# elements in the old and the new sequenced enumerables as well as the action
99
# taken.
100
class Diff::LCS::ContextChange < Diff::LCS::Change
2✔
101
  # We don't need these two values.
102
  undef :position
2✔
103
  undef :element
2✔
104

105
  # Returns the old position being changed.
106
  attr_reader :old_position
2✔
107
  # Returns the new position being changed.
108
  attr_reader :new_position
2✔
109
  # Returns the old element being changed.
110
  attr_reader :old_element
2✔
111
  # Returns the new element being changed.
112
  attr_reader :new_element
2✔
113

114
  def initialize(*args)
2✔
115
    @action, @old_position, @old_element, @new_position, @new_element = *args
6,924✔
116

117
    fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
6,924!
118
    fail "Invalid (Old) Position Type" unless @old_position.nil? || @old_position.is_a?(IntClass)
6,924!
119
    fail "Invalid (New) Position Type" unless @new_position.nil? || @new_position.is_a?(IntClass)
6,924!
120
  end
121

122
  def to_a
2✔
123
    [
124
      @action,
1,558✔
125
      [@old_position, @old_element],
126
      [@new_position, @new_element]
127
    ]
128
  end
129

130
  alias_method :to_ary, :to_a
2✔
131

132
  def self.from_a(arr)
2✔
133
    Diff::LCS::Change.from_a(arr)
1,836✔
134
  end
135

136
  # Simplifies a context change for use in some diff callbacks. '<' actions
137
  # are converted to '-' and '>' actions are converted to '+'.
138
  def self.simplify(event)
2✔
139
    ea = event.to_a
1,556✔
140

141
    case ea[0]
1,556!
142
    when "-"
171✔
143
      ea[2][1] = nil
342✔
144
    when "<"
×
145
      ea[0] = "-"
×
146
      ea[2][1] = nil
×
147
    when "+"
171✔
148
      ea[1][1] = nil
342✔
149
    when ">"
×
150
      ea[0] = "+"
×
151
      ea[1][1] = nil
×
152
    end
153

154
    Diff::LCS::ContextChange.from_a(ea)
1,556✔
155
  end
156

157
  def ==(other)
2✔
158
    (self.class == other.class) and
280✔
159
      (@action == other.action) and
280✔
160
      (@old_position == other.old_position) and
280✔
161
      (@new_position == other.new_position) and
280✔
162
      (@old_element == other.old_element) and
280✔
163
      (@new_element == other.new_element)
280✔
164
  end
165

166
  def <=>(other)
2✔
167
    r = @action <=> other.action
×
168
    r = @old_position <=> other.old_position if r.zero?
×
169
    r = @new_position <=> other.new_position if r.zero?
×
170
    r = @old_element <=> other.old_element if r.zero?
×
171
    r = @new_element <=> other.new_element if r.zero?
×
172
    r
×
173
  end
174
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