diff --git a/lib/net/imap/config/attr_accessors.rb b/lib/net/imap/config/attr_accessors.rb index cef757e0..609da208 100644 --- a/lib/net/imap/config/attr_accessors.rb +++ b/lib/net/imap/config/attr_accessors.rb @@ -58,9 +58,9 @@ def freeze private - def initialize_clone(other) + def initialize_clone(other, **kwargs) super - @data = other.data.clone + @data = other.data.clone(**kwargs) end def initialize_dup(other) diff --git a/lib/net/imap/sequence_set.rb b/lib/net/imap/sequence_set.rb index a5dabfc7..b12461b3 100644 --- a/lib/net/imap/sequence_set.rb +++ b/lib/net/imap/sequence_set.rb @@ -1869,8 +1869,9 @@ def remain_frozen(set) frozen? ? set.freeze : set end def remain_frozen_empty; frozen? ? SequenceSet.empty : SequenceSet.new end # frozen clones are shallow copied - def initialize_clone(other) - @set_data = other.dup_set_data unless other.frozen? + def initialize_clone(other, freeze: nil) + @set_data = other.dup_set_data unless other.frozen? && freeze != false + freeze_set_data if freeze super end diff --git a/test/net/imap/test_config.rb b/test/net/imap/test_config.rb index 5c03adf6..4abb5350 100644 --- a/test/net/imap/test_config.rb +++ b/test/net/imap/test_config.rb @@ -312,6 +312,21 @@ def duck.to_r = 1/11111 assert_equal 1, copy.open_timeout end + test "#clone(freeze:)" do + original = Config.new(open_timeout: 1).freeze + copy = original.clone(freeze: false) + refute copy.frozen? + copy.open_timeout = 2 + assert_equal 1, original.open_timeout + assert_equal 2, copy.open_timeout + + copy = Config.new(open_timeout: 1).clone(freeze: true) + assert copy.frozen? + assert_raise FrozenError do + copy.open_timeout = 2 + end + end + test "#inherited? and #reset" do base = Config.new debug: false, open_timeout: 99, idle_response_timeout: 15 child = base.new debug: true, open_timeout: 15, idle_response_timeout: 10 diff --git a/test/net/imap/test_sequence_set.rb b/test/net/imap/test_sequence_set.rb index fd6efa12..a3131047 100644 --- a/test/net/imap/test_sequence_set.rb +++ b/test/net/imap/test_sequence_set.rb @@ -233,6 +233,21 @@ def compare_to_reference_set(nums, set, seqset) end end + test "#clone(freeze:)" do + original = SequenceSet["2:4,7:11,99,999"] + copy = original.clone(freeze: false) + refute copy.frozen? + copy << 123 + assert copy.include?(123) + assert !original.include?(123) + + copy = SequenceSet.new("2:4,7:11,99,999").clone(freeze: true) + assert copy.frozen? + assert_raise FrozenError do + copy << 123 + end + end + if defined?(Ractor) test "#freeze makes ractor sharable (deeply frozen)" do assert Ractor.shareable? SequenceSet.new("1:9,99,999").freeze