diff options
author | Jean Boussier <byroot@ruby-lang.org> | 2023-03-30 16:23:14 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2023-04-15 16:29:46 +0200 |
commit | 52449b5b75068872ce568ed00c4c7fb8e6c28072 (patch) | |
tree | 15c4ee12b472b863609d04344473986a79343608 /spec/ruby/core | |
parent | 4dc2e5a850e6b44e715ca8fa31d21dddf5ec1491 (diff) | |
download | ruby-52449b5b75068872ce568ed00c4c7fb8e6c28072.tar.gz |
Implement ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete
[Feature #19561]
It's useful to be able to remove references from weak maps.
Diffstat (limited to 'spec/ruby/core')
-rw-r--r-- | spec/ruby/core/objectspace/weakkeymap/delete_spec.rb | 40 | ||||
-rw-r--r-- | spec/ruby/core/objectspace/weakmap/delete_spec.rb | 30 |
2 files changed, 70 insertions, 0 deletions
diff --git a/spec/ruby/core/objectspace/weakkeymap/delete_spec.rb b/spec/ruby/core/objectspace/weakkeymap/delete_spec.rb new file mode 100644 index 0000000000..6e534b8ea8 --- /dev/null +++ b/spec/ruby/core/objectspace/weakkeymap/delete_spec.rb @@ -0,0 +1,40 @@ +require_relative '../../../spec_helper' + +ruby_version_is '3.3' do + describe "ObjectSpace::WeakKeyMap#delete" do + it "removes the entry and returns the deleted value" do + m = ObjectSpace::WeakKeyMap.new + key = Object.new + value = Object.new + m[key] = value + + m.delete(key).should == value + m.key?(key).should == false + end + + it "uses equality semantic" do + m = ObjectSpace::WeakKeyMap.new + key = "foo".upcase + value = Object.new + m[key] = value + + m.delete("foo".upcase).should == value + m.key?(key).should == false + end + + it "calls supplied block if the key is not found" do + key = Object.new + m = ObjectSpace::WeakKeyMap.new + return_value = m.delete(key) do |yielded_key| + yielded_key.should == key + 5 + end + return_value.should == 5 + end + + it "returns nil if the key is not found when no block is given" do + m = ObjectSpace::WeakMap.new + m.delete(Object.new).should == nil + end + end +end diff --git a/spec/ruby/core/objectspace/weakmap/delete_spec.rb b/spec/ruby/core/objectspace/weakmap/delete_spec.rb new file mode 100644 index 0000000000..302de264fb --- /dev/null +++ b/spec/ruby/core/objectspace/weakmap/delete_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../../spec_helper' + +ruby_version_is '3.3' do + describe "ObjectSpace::WeakMap#delete" do + it "removes the entry and returns the deleted value" do + m = ObjectSpace::WeakMap.new + key = Object.new + value = Object.new + m[key] = value + + m.delete(key).should == value + m.key?(key).should == false + end + + it "calls supplied block if the key is not found" do + key = Object.new + m = ObjectSpace::WeakMap.new + return_value = m.delete(key) do |yielded_key| + yielded_key.should == key + 5 + end + return_value.should == 5 + end + + it "returns nil if the key is not found when no block is given" do + m = ObjectSpace::WeakMap.new + m.delete(Object.new).should == nil + end + end +end |