ruby-changes:63958
From: Benoit <ko1@a...>
Date: Sat, 5 Dec 2020 19:42:11 +0900 (JST)
Subject: [ruby-changes:63958] d0bd43c332 (master): Revert "SortedSet was removed at a3db08d7b6ff119223f77e3df00b4f6deac971e2"
https://git.ruby-lang.org/ruby.git/commit/?id=d0bd43c332 From d0bd43c332f95e5f227ffcd4eb0e6f7bfa942b2a Mon Sep 17 00:00:00 2001 From: Benoit Daloze <eregontp@g...> Date: Sat, 5 Dec 2020 11:16:32 +0100 Subject: Revert "SortedSet was removed at a3db08d7b6ff119223f77e3df00b4f6deac971e2" * This reverts commit b06ffce4aef002dc47c3c5968181230e7ab8d7cc. * Do not revert specs, wrap them with `ruby_version_is` (tool for that in next commit). diff --git a/spec/ruby/library/set/sortedset/add_spec.rb b/spec/ruby/library/set/sortedset/add_spec.rb new file mode 100644 index 0000000..5f8bde0 --- /dev/null +++ b/spec/ruby/library/set/sortedset/add_spec.rb @@ -0,0 +1,39 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/add_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' +require_relative 'shared/add' + +describe "SortedSet#add" do + it_behaves_like :sorted_set_add, :add + + it "takes only values which responds <=>" do + obj = mock('no_comparison_operator') + obj.stub!(:respond_to?).with(:<=>).and_return(false) + -> { SortedSet["hello"].add(obj) }.should raise_error(ArgumentError) + end + + it "raises on incompatible <=> comparison" do + # Use #to_a here as elements are sorted only when needed. + # Therefore the <=> incompatibility is only noticed on sorting. + -> { SortedSet['1', '2'].add(3).to_a }.should raise_error(ArgumentError) + end +end + +describe "SortedSet#add?" do + before :each do + @set = SortedSet.new + end + + it "adds the passed Object to self" do + @set.add?("cat") + @set.should include("cat") + end + + it "returns self when the Object has not yet been added to self" do + @set.add?("cat").should equal(@set) + end + + it "returns nil when the Object has already been added to self" do + @set.add?("cat") + @set.add?("cat").should be_nil + end +end diff --git a/spec/ruby/library/set/sortedset/append_spec.rb b/spec/ruby/library/set/sortedset/append_spec.rb new file mode 100644 index 0000000..ebcceba --- /dev/null +++ b/spec/ruby/library/set/sortedset/append_spec.rb @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/append_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' +require_relative 'shared/add' + +describe "SortedSet#<<" do + it_behaves_like :sorted_set_add, :<< +end diff --git a/spec/ruby/library/set/sortedset/case_equality_spec.rb b/spec/ruby/library/set/sortedset/case_equality_spec.rb new file mode 100644 index 0000000..48e3735 --- /dev/null +++ b/spec/ruby/library/set/sortedset/case_equality_spec.rb @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/case_equality_spec.rb#L1 +require_relative '../../../spec_helper' +require_relative 'shared/include' +require 'set' + +describe "SortedSet#===" do + it_behaves_like :sorted_set_include, :=== +end diff --git a/spec/ruby/library/set/sortedset/classify_spec.rb b/spec/ruby/library/set/sortedset/classify_spec.rb new file mode 100644 index 0000000..62b26d5 --- /dev/null +++ b/spec/ruby/library/set/sortedset/classify_spec.rb @@ -0,0 +1,27 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/classify_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet#classify" do + before :each do + @set = SortedSet["one", "two", "three", "four"] + end + + it "yields each Object in self in sorted order" do + res = [] + @set.classify { |x| res << x } + res.should == ["one", "two", "three", "four"].sort + end + + it "returns an Enumerator when passed no block" do + enum = @set.classify + enum.should be_an_instance_of(Enumerator) + + classified = enum.each { |x| x.length } + classified.should == { 3 => SortedSet["one", "two"], 4 => SortedSet["four"], 5 => SortedSet["three"] } + end + + it "classifies the Objects in self based on the block's return value" do + classified = @set.classify { |x| x.length } + classified.should == { 3 => SortedSet["one", "two"], 4 => SortedSet["four"], 5 => SortedSet["three"] } + end +end diff --git a/spec/ruby/library/set/sortedset/clear_spec.rb b/spec/ruby/library/set/sortedset/clear_spec.rb new file mode 100644 index 0000000..11b5db2 --- /dev/null +++ b/spec/ruby/library/set/sortedset/clear_spec.rb @@ -0,0 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/clear_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet#clear" do + before :each do + @set = SortedSet["one", "two", "three", "four"] + end + + it "removes all elements from self" do + @set.clear + @set.should be_empty + end + + it "returns self" do + @set.clear.should equal(@set) + end +end diff --git a/spec/ruby/library/set/sortedset/collect_spec.rb b/spec/ruby/library/set/sortedset/collect_spec.rb new file mode 100644 index 0000000..21ead4f --- /dev/null +++ b/spec/ruby/library/set/sortedset/collect_spec.rb @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/collect_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' +require_relative 'shared/collect' + +describe "SortedSet#collect!" do + it_behaves_like :sorted_set_collect_bang, :collect! +end diff --git a/spec/ruby/library/set/sortedset/constructor_spec.rb b/spec/ruby/library/set/sortedset/constructor_spec.rb new file mode 100644 index 0000000..953144d --- /dev/null +++ b/spec/ruby/library/set/sortedset/constructor_spec.rb @@ -0,0 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/constructor_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet[]" do + it "returns a new SortedSet populated with the passed Objects" do + set = SortedSet[1, 2, 3] + + set.instance_of?(SortedSet).should be_true + set.size.should eql(3) + + set.should include(1) + set.should include(2) + set.should include(3) + end +end diff --git a/spec/ruby/library/set/sortedset/delete_if_spec.rb b/spec/ruby/library/set/sortedset/delete_if_spec.rb new file mode 100644 index 0000000..1ff6893 --- /dev/null +++ b/spec/ruby/library/set/sortedset/delete_if_spec.rb @@ -0,0 +1,38 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/delete_if_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet#delete_if" do + before :each do + @set = SortedSet["one", "two", "three"] + end + + it "yields each Object in self in sorted order" do + ret = [] + @set.delete_if { |x| ret << x } + ret.should == ["one", "two", "three"].sort + end + + it "deletes every element from self for which the passed block returns true" do + @set.delete_if { |x| x.size == 3 } + @set.size.should eql(1) + + @set.should_not include("one") + @set.should_not include("two") + @set.should include("three") + end + + it "returns self" do + @set.delete_if { |x| x }.should equal(@set) + end + + it "returns an Enumerator when passed no block" do + enum = @set.delete_if + enum.should be_an_instance_of(Enumerator) + + enum.each { |x| x.size == 3 } + + @set.should_not include("one") + @set.should_not include("two") + @set.should include("three") + end +end diff --git a/spec/ruby/library/set/sortedset/delete_spec.rb b/spec/ruby/library/set/sortedset/delete_spec.rb new file mode 100644 index 0000000..71583c7 --- /dev/null +++ b/spec/ruby/library/set/sortedset/delete_spec.rb @@ -0,0 +1,37 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/delete_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet#delete" do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "deletes the passed Object from self" do + @set.delete("a") + @set.should_not include("a") + end + + it "returns self" do + @set.delete("a").should equal(@set) + @set.delete("x").should equal(@set) + end +end + +describe "SortedSet#delete?" do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "deletes the passed Object from self" do + @set.delete?("a") + @set.should_not include("a") + end + + it "returns self when the passed Object is in self" do + @set.delete?("a").should equal(@set) + end + + it "returns nil when the passed Object is not in self" do + @set.delete?("x").should be_nil + end +end diff --git a/spec/ruby/library/set/sortedset/difference_spec.rb b/spec/ruby/library/set/sortedset/difference_spec.rb new file mode 100644 index 0000000..c3d679a --- /dev/null +++ b/spec/ruby/library/set/sortedset/difference_spec.rb @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/difference_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' +require_relative 'shared/difference' + +describe "SortedSet#difference" do + it_behaves_like :sorted_set_difference, :difference +end diff --git a/spec/ruby/library/set/sortedset/divide_spec.rb b/spec/ruby/library/set/sortedset/divide_spec.rb new file mode 100644 index 0000000..4b2135a --- /dev/null +++ b/spec/ruby/library/set/sortedset/divide_spec.rb @@ -0,0 +1,34 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/set/sortedset/divide_spec.rb#L1 +require_relative '../../../spec_helper' +require 'set' + +describe "SortedSet#divide" do + it "divides self into a set of subsets based on the blocks return values" do + set = SortedSet["one", "two", "three", "four", "five"].divide { |x| x.length } + set.map { |x| x.to_a }.to_a.sort.should == [["five", "four"], ["one", "two"], ["three"]] + end + + it "yields each Object in self in sorted order" do + ret = [] + SortedSet["one", "two", "three", "four", "five"].divide { |x| ret << x } + ret.should == ["one", "two", "three", "four", "five"].sort + end + + # BUG: Does not raise a LocalJumpError, but a NoMethodError + # + # it "raises a LocalJumpError when not passed a block" do + # lambda { SortedSet[1].divide }.should raise_error(LocalJu (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/