Skip to content

Commit

Permalink
🥅 Improve SequenceSet frozen errors
Browse files Browse the repository at this point in the history
Previously, SequenceSet modification methods would raise a frozen error
when it updated its internal data structure (currently implemented as an
array of arrays), which was "can't modify frozen Array: [int, int]".
This was unexpected, confusing, and misleading.

This changes the SequenceSet modification methods to raise the expected
FrozenError _prior_ to attempting to modify the internal data structure.
  • Loading branch information
nevans committed Sep 26, 2024
1 parent 2813b4c commit cf7e62c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/net/imap/sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ def add(object)
# Unlike #add, #merge, or #union, the new value is appended to #string.
# This may result in a #string which has duplicates or is out-of-order.
def append(object)
modifying!
tuple = input_to_tuple object
entry = tuple_to_str tuple
tuple_add tuple
Expand Down Expand Up @@ -1310,6 +1311,12 @@ def intersect_tuple?((min, max))
range.include?(min) || range.include?(max) || (min..max).cover?(range)
end

def modifying!
if frozen?
raise FrozenError, "can't modify frozen #{self.class}: %p" % [self]
end
end

def tuples_add(tuples) tuples.each do tuple_add _1 end; self end
def tuples_subtract(tuples) tuples.each do tuple_subtract _1 end; self end

Expand All @@ -1324,6 +1331,7 @@ def tuples_subtract(tuples) tuples.each do tuple_subtract _1 end; self end
# ---------??===lower==|--|==|----|===upper===|-- join until upper
# ---------??===lower==|--|==|--|=====upper===|-- join to upper
def tuple_add(tuple)
modifying!
min, max = tuple
lower, lower_idx = tuple_gte_with_index(min - 1)
if lower.nil? then tuples << tuple
Expand Down Expand Up @@ -1360,6 +1368,7 @@ def tuple_coalesce(lower, lower_idx, min, max)
# -------??=====lower====|--|====|---|====upper====|-- 7. delete until
# -------??=====lower====|--|====|--|=====upper====|-- 8. delete and trim
def tuple_subtract(tuple)
modifying!
min, max = tuple
lower, idx = tuple_gte_with_index(min)
if lower.nil? then nil # case 1.
Expand Down
26 changes: 26 additions & 0 deletions test/net/imap/test_sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ def compare_to_reference_set(nums, set, seqset)
assert_equal set, set.freeze
end

data "#clear", :clear
data "#replace seq", ->{ _1.replace SequenceSet[1] }
data "#replace num", ->{ _1.replace 1 }
data "#replace str", ->{ _1.replace ?1 }
data "#string=", ->{ _1.string = ?1 }
data "#add", ->{ _1.add 1 }
data "#add?", ->{ _1.add? 1 }
data "#<<", ->{ _1 << 1 }
data "#append", ->{ _1.append 1 }
data "#delete", ->{ _1.delete 3 }
data "#delete?", ->{ _1.delete? 3 }
data "#delete_at", ->{ _1.delete_at 3 }
data "#slice!", ->{ _1.slice! 1 }
data "#merge", ->{ _1.merge 1 }
data "#subtract", ->{ _1.subtract 1 }
data "#limit!", ->{ _1.limit! max: 10 }
data "#complement!", :complement!
data "#normalize!", :normalize!
test "frozen error message" do |modification|
set = SequenceSet["2:4,7:11,99,999"]
msg = "can't modify frozen Net::IMAP::SequenceSet: %p" % [set]
assert_raise_with_message FrozenError, msg do
modification.to_proc.(set)
end
end

%i[clone dup].each do |method|
test "##{method}" do
orig = SequenceSet.new "2:4,7:11,99,999"
Expand Down

0 comments on commit cf7e62c

Please sign in to comment.