Skip to content

Commit ea6f3b3

Browse files
authored
🔀 Merge pull request #739 from OskarEichler/codex/clone-imap
Honor clone freeze keywords for Config and SequenceSet
2 parents 8fcbf99 + f4e5493 commit ea6f3b3

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎lib/net/imap/config/attr_accessors.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def freeze
5858

5959
private
6060

61-
def initialize_clone(other)
61+
def initialize_clone(other, **kwargs)
6262
super
63-
@data = other.data.clone
63+
@data = other.data.clone(**kwargs)
6464
end
6565

6666
def initialize_dup(other)

‎lib/net/imap/sequence_set.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,8 +2106,9 @@ def remain_frozen(set) frozen? ? set.freeze : set end
21062106
def remain_frozen_empty; frozen? ? SequenceSet.empty : SequenceSet.new end
21072107

21082108
# frozen clones are shallow copied
2109-
def initialize_clone(other)
2110-
@set_data = other.dup_set_data unless other.frozen?
2109+
def initialize_clone(other, freeze: nil)
2110+
@set_data = other.dup_set_data unless other.frozen? && freeze != false
2111+
freeze_set_data if freeze
21112112
super
21122113
end
21132114

‎test/net/imap/test_config.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@ def duck.to_r = 1/11111
312312
assert_equal 1, copy.open_timeout
313313
end
314314

315+
test "#clone(freeze:)" do
316+
original = Config.new(open_timeout: 1).freeze
317+
copy = original.clone(freeze: false)
318+
refute copy.frozen?
319+
copy.open_timeout = 2
320+
assert_equal 1, original.open_timeout
321+
assert_equal 2, copy.open_timeout
322+
323+
copy = Config.new(open_timeout: 1).clone(freeze: true)
324+
assert copy.frozen?
325+
assert_raise FrozenError do
326+
copy.open_timeout = 2
327+
end
328+
end
329+
315330
test "#inherited? and #reset" do
316331
base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15
317332
child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10

‎test/net/imap/test_sequence_set.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ def compare_to_reference_set(nums, set, seqset)
252252
end
253253
end
254254

255+
test "#clone(freeze:)" do
256+
original = SequenceSet["2:4,7:11,99,999"]
257+
copy = original.clone(freeze: false)
258+
refute copy.frozen?
259+
copy << 123
260+
assert copy.include?(123)
261+
assert !original.include?(123)
262+
263+
copy = SequenceSet.new("2:4,7:11,99,999").clone(freeze: true)
264+
assert copy.frozen?
265+
assert_raise FrozenError do
266+
copy << 123
267+
end
268+
end
269+
255270
if defined?(Ractor)
256271
test "#freeze makes ractor sharable (deeply frozen)" do
257272
assert Ractor.shareable? SequenceSet.new("1:9,99,999").freeze

0 commit comments

Comments
 (0)