Skip to content

Commit

Permalink
🔀 Merge pull request #338 from ruby/backport/0.4/331/nicer-seqset-fro…
Browse files Browse the repository at this point in the history
…zen-error

🥅 Improve SequenceSet frozen errors [backport: #331]
  • Loading branch information
nevans authored Oct 13, 2024
2 parents e60cde6 + 0bb92a4 commit 2302839
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 @@ -682,6 +682,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 @@ -1317,6 +1318,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 @@ -1331,6 +1338,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 @@ -1367,6 +1375,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 2302839

Please sign in to comment.