diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | 95e8c48dd3348503a8c7db5d0498894a1b676395 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/set | |
parent | ed7d803500de38186c74bce94d233e85ef51e503 (diff) | |
download | ruby-95e8c48dd3348503a8c7db5d0498894a1b676395.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/set')
99 files changed, 1852 insertions, 0 deletions
diff --git a/spec/rubyspec/library/set/add_spec.rb b/spec/rubyspec/library/set/add_spec.rb new file mode 100644 index 0000000000..a7d6fb8a56 --- /dev/null +++ b/spec/rubyspec/library/set/add_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/add', __FILE__) + +describe "Set#add" do + it_behaves_like :set_add, :add +end + +describe "Set#add?" do + before :each do + @set = Set.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/rubyspec/library/set/append_spec.rb b/spec/rubyspec/library/set/append_spec.rb new file mode 100644 index 0000000000..b3097c0904 --- /dev/null +++ b/spec/rubyspec/library/set/append_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/add', __FILE__) + +describe "Set#<<" do + it_behaves_like :set_add, :<< +end diff --git a/spec/rubyspec/library/set/classify_spec.rb b/spec/rubyspec/library/set/classify_spec.rb new file mode 100644 index 0000000000..87e5b62f96 --- /dev/null +++ b/spec/rubyspec/library/set/classify_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#classify" do + before :each do + @set = Set["one", "two", "three", "four"] + end + + it "yields each Object in self" do + res = [] + @set.classify { |x| res << x } + res.sort.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 => Set["one", "two"], 4 => Set["four"], 5 => Set["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 => Set["one", "two"], 4 => Set["four"], 5 => Set["three"] } + end +end diff --git a/spec/rubyspec/library/set/clear_spec.rb b/spec/rubyspec/library/set/clear_spec.rb new file mode 100644 index 0000000000..b35ce4fec0 --- /dev/null +++ b/spec/rubyspec/library/set/clear_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#clear" do + before :each do + @set = Set["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/rubyspec/library/set/collect_spec.rb b/spec/rubyspec/library/set/collect_spec.rb new file mode 100644 index 0000000000..03c00c9794 --- /dev/null +++ b/spec/rubyspec/library/set/collect_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/collect', __FILE__) + +describe "Set#collect!" do + it_behaves_like :set_collect_bang, :collect! +end diff --git a/spec/rubyspec/library/set/constructor_spec.rb b/spec/rubyspec/library/set/constructor_spec.rb new file mode 100644 index 0000000000..75c7ba8bc8 --- /dev/null +++ b/spec/rubyspec/library/set/constructor_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set[]" do + it "returns a new Set populated with the passed Objects" do + set = Set[1, 2, 3] + + set.instance_of?(Set).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/rubyspec/library/set/delete_if_spec.rb b/spec/rubyspec/library/set/delete_if_spec.rb new file mode 100644 index 0000000000..b6bd28a59f --- /dev/null +++ b/spec/rubyspec/library/set/delete_if_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#delete_if" do + before :each do + @set = Set["one", "two", "three"] + end + + it "yields every element of self" do + ret = [] + @set.delete_if { |x| ret << x } + ret.sort.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/rubyspec/library/set/delete_spec.rb b/spec/rubyspec/library/set/delete_spec.rb new file mode 100644 index 0000000000..4f0326e37a --- /dev/null +++ b/spec/rubyspec/library/set/delete_spec.rb @@ -0,0 +1,37 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#delete" do + before :each do + @set = Set["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 "Set#delete?" do + before :each do + @set = Set["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/rubyspec/library/set/difference_spec.rb b/spec/rubyspec/library/set/difference_spec.rb new file mode 100644 index 0000000000..416dffe802 --- /dev/null +++ b/spec/rubyspec/library/set/difference_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/difference', __FILE__) + +describe "Set#difference" do + it_behaves_like :set_difference, :difference +end diff --git a/spec/rubyspec/library/set/divide_spec.rb b/spec/rubyspec/library/set/divide_spec.rb new file mode 100644 index 0000000000..930c69885f --- /dev/null +++ b/spec/rubyspec/library/set/divide_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#divide" do + it "divides self into a set of subsets based on the blocks return values" do + set = Set["one", "two", "three", "four", "five"].divide { |x| x.length } + set.map { |x| x.to_a.sort }.sort.should == [["five", "four"], ["one", "two"], ["three"]] + end + + it "yields each Object to the block" do + ret = [] + Set["one", "two", "three", "four", "five"].divide { |x| ret << x } + ret.sort.should == ["five", "four", "one", "three", "two"] + end + + # BUG: Does not raise a LocalJumpError, but a NoMethodError + # + # it "raises a LocalJumpError when not passed a block" do + # lambda { Set[1].divide }.should raise_error(LocalJumpError) + # end +end + +describe "Set#divide when passed a block with an arity of 2" do + it "divides self into a set of subsets based on the blocks return values" do + set = Set[1, 3, 4, 6, 9, 10, 11].divide { |x, y| (x - y).abs == 1 } + set.map{ |x| x.to_a.sort }.sort.should == [[1], [3, 4], [6], [9, 10, 11]] + end + + it "yields each two Object to the block" do + ret = [] + Set[1, 2].divide { |x, y| ret << [x, y] } + ret.sort.should == [[1, 1], [1, 2], [2, 1], [2, 2]] + end +end diff --git a/spec/rubyspec/library/set/each_spec.rb b/spec/rubyspec/library/set/each_spec.rb new file mode 100644 index 0000000000..867e94ff0b --- /dev/null +++ b/spec/rubyspec/library/set/each_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#each" do + before :each do + @set = Set[1, 2, 3] + end + + it "yields each Object in self" do + ret = [] + @set.each { |x| ret << x } + ret.sort.should == [1, 2, 3] + end + + it "returns self" do + @set.each { |x| x }.should equal(@set) + end + + it "returns an Enumerator when not passed a block" do + enum = @set.each + + ret = [] + enum.each { |x| ret << x } + ret.sort.should == [1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/empty_spec.rb b/spec/rubyspec/library/set/empty_spec.rb new file mode 100644 index 0000000000..044d58727c --- /dev/null +++ b/spec/rubyspec/library/set/empty_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#empty?" do + it "returns true if self is empty" do + Set[].empty?.should be_true + Set[1].empty?.should be_false + Set[1,2,3].empty?.should be_false + end +end diff --git a/spec/rubyspec/library/set/enumerable/to_set_spec.rb b/spec/rubyspec/library/set/enumerable/to_set_spec.rb new file mode 100644 index 0000000000..41ca039de6 --- /dev/null +++ b/spec/rubyspec/library/set/enumerable/to_set_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "Emumerable#to_set" do + it "returns a new Set created from self" do + [1, 2, 3].to_set.should == Set[1, 2, 3] + {a: 1, b: 2}.to_set.should == Set[[:b, 2], [:a, 1]] + end + + it "allows passing an alternate class for Set" do + sorted_set = [1, 2, 3].to_set(SortedSet) + sorted_set.should == SortedSet[1, 2, 3] + sorted_set.instance_of?(SortedSet).should == true + end + + it "passes down passed blocks" do + [1, 2, 3].to_set { |x| x * x }.should == Set[1, 4, 9] + end +end diff --git a/spec/rubyspec/library/set/eql_spec.rb b/spec/rubyspec/library/set/eql_spec.rb new file mode 100644 index 0000000000..ed62e20d7a --- /dev/null +++ b/spec/rubyspec/library/set/eql_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#eql?" do + it "returns true when the passed argument is a Set and contains the same elements" do + Set[].should eql(Set[]) + Set[1, 2, 3].should eql(Set[1, 2, 3]) + Set[1, 2, 3].should eql(Set[3, 2, 1]) + Set["a", :b, ?c].should eql(Set[?c, :b, "a"]) + + Set[1, 2, 3].should_not eql(Set[1.0, 2, 3]) + Set[1, 2, 3].should_not eql(Set[2, 3]) + Set[1, 2, 3].should_not eql(Set[]) + end +end diff --git a/spec/rubyspec/library/set/equal_value_spec.rb b/spec/rubyspec/library/set/equal_value_spec.rb new file mode 100644 index 0000000000..2e2c875407 --- /dev/null +++ b/spec/rubyspec/library/set/equal_value_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#==" do + it "returns true when the passed Object is a Set and self and the Object contain the same elements" do + Set[].should == Set[] + Set[1, 2, 3].should == Set[1, 2, 3] + Set["1", "2", "3"].should == Set["1", "2", "3"] + + Set[1, 2, 3].should_not == Set[1.0, 2, 3] + Set[1, 2, 3].should_not == [1, 2, 3] + end + + it "does not depend on the order of the elements" do + Set[1, 2, 3].should == Set[3, 2, 1] + Set[:a, "b", ?c].should == Set[?c, "b", :a] + end + + it "does not depend on the order of nested Sets" do + Set[Set[1], Set[2], Set[3]].should == Set[Set[3], Set[2], Set[1]] + + set1 = Set[Set["a", "b"], Set["c", "d"], Set["e", "f"]] + set2 = Set[Set["c", "d"], Set["a", "b"], Set["e", "f"]] + set1.should == set2 + end +end diff --git a/spec/rubyspec/library/set/exclusion_spec.rb b/spec/rubyspec/library/set/exclusion_spec.rb new file mode 100644 index 0000000000..d2923ba2d7 --- /dev/null +++ b/spec/rubyspec/library/set/exclusion_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#^" do + before :each do + @set = Set[1, 2, 3, 4] + end + + it "returns a new Set containing elements that are not in both self and the passed Enumberable" do + (@set ^ Set[3, 4, 5]).should == Set[1, 2, 5] + (@set ^ [3, 4, 5]).should == Set[1, 2, 5] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set ^ 3 }.should raise_error(ArgumentError) + lambda { @set ^ Object.new }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/flatten_merge_spec.rb b/spec/rubyspec/library/set/flatten_merge_spec.rb new file mode 100644 index 0000000000..bbddec7076 --- /dev/null +++ b/spec/rubyspec/library/set/flatten_merge_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#flatten_merge" do + it "is protected" do + Set.should have_protected_instance_method("flatten_merge") + end + + it "flattens the passed Set and merges it into self" do + set1 = Set[1, 2] + set2 = Set[3, 4, Set[5, 6]] + + set1.send(:flatten_merge, set2).should == Set[1, 2, 3, 4, 5, 6] + end + + it "raises an ArgumentError when trying to flatten a recursive Set" do + set1 = Set[1, 2, 3] + set2 = Set[5, 6, 7] + set2 << set2 + + lambda { set1.send(:flatten_merge, set2) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/flatten_spec.rb b/spec/rubyspec/library/set/flatten_spec.rb new file mode 100644 index 0000000000..2fa7f9f6e5 --- /dev/null +++ b/spec/rubyspec/library/set/flatten_spec.rb @@ -0,0 +1,40 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#flatten" do + it "returns a copy of self with each included Set flattened" do + set = Set[1, 2, Set[3, 4, Set[5, 6, Set[7, 8]]], 9, 10] + flattened_set = set.flatten + + flattened_set.should_not equal(set) + flattened_set.should == Set[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + end + + it "raises an ArgumentError when self is recursive" do + (set = Set[]) << set + lambda { set.flatten }.should raise_error(ArgumentError) + end +end + +describe "Set#flatten!" do + it "flattens self" do + set = Set[1, 2, Set[3, 4, Set[5, 6, Set[7, 8]]], 9, 10] + set.flatten! + set.should == Set[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + end + + it "returns self when self was modified" do + set = Set[1, 2, Set[3, 4]] + set.flatten!.should equal(set) + end + + it "returns nil when self was not modified" do + set = Set[1, 2, 3, 4] + set.flatten!.should be_nil + end + + it "raises an ArgumentError when self is recursive" do + (set = Set[]) << set + lambda { set.flatten! }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/hash_spec.rb b/spec/rubyspec/library/set/hash_spec.rb new file mode 100644 index 0000000000..ea39105937 --- /dev/null +++ b/spec/rubyspec/library/set/hash_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#hash" do + it "is static" do + Set[].hash.should == Set[].hash + Set[1, 2, 3].hash.should == Set[1, 2, 3].hash + Set[:a, "b", ?c].hash.should == Set[?c, "b", :a].hash + + Set[].hash.should_not == Set[1, 2, 3].hash + Set[1, 2, 3].hash.should_not == Set[:a, "b", ?c].hash + end +end diff --git a/spec/rubyspec/library/set/include_spec.rb b/spec/rubyspec/library/set/include_spec.rb new file mode 100644 index 0000000000..693b8b997f --- /dev/null +++ b/spec/rubyspec/library/set/include_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include', __FILE__) +require 'set' + +describe "Set#include?" do + it_behaves_like :set_include, :include? +end diff --git a/spec/rubyspec/library/set/initialize_spec.rb b/spec/rubyspec/library/set/initialize_spec.rb new file mode 100644 index 0000000000..e14cbaacc2 --- /dev/null +++ b/spec/rubyspec/library/set/initialize_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#initialize" do + it "is private" do + Set.should have_private_instance_method(:initialize) + end + + it "adds all elements of the passed Enumerable to self" do + s = Set.new([1, 2, 3]) + s.size.should eql(3) + s.should include(1) + s.should include(2) + s.should include(3) + end + + it "preprocesses all elements by a passed block before adding to self" do + s = Set.new([1, 2, 3]) { |x| x * x } + s.size.should eql(3) + s.should include(1) + s.should include(4) + s.should include(9) + end +end diff --git a/spec/rubyspec/library/set/inspect_spec.rb b/spec/rubyspec/library/set/inspect_spec.rb new file mode 100644 index 0000000000..8a6c565c2e --- /dev/null +++ b/spec/rubyspec/library/set/inspect_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#inspect" do + it "returns a String representation of self" do + Set[].inspect.should be_kind_of(String) + Set[nil, false, true].inspect.should be_kind_of(String) + Set[1, 2, 3].inspect.should be_kind_of(String) + Set["1", "2", "3"].inspect.should be_kind_of(String) + Set[:a, "b", Set[?c]].inspect.should be_kind_of(String) + end + + it "correctly handles self-references" do + (set = Set[]) << set + set.inspect.should be_kind_of(String) + set.inspect.should include("#<Set: {...}>") + end +end diff --git a/spec/rubyspec/library/set/intersection_spec.rb b/spec/rubyspec/library/set/intersection_spec.rb new file mode 100644 index 0000000000..c5adef5fc4 --- /dev/null +++ b/spec/rubyspec/library/set/intersection_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/intersection', __FILE__) +require 'set' + +describe "Set#intersection" do + it_behaves_like :set_intersection, :intersection +end + +describe "Set#&" do + it_behaves_like :set_intersection, :& +end diff --git a/spec/rubyspec/library/set/keep_if_spec.rb b/spec/rubyspec/library/set/keep_if_spec.rb new file mode 100644 index 0000000000..45f2cbf07a --- /dev/null +++ b/spec/rubyspec/library/set/keep_if_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#keep_if" do + before :each do + @set = Set["one", "two", "three"] + end + + it "yields every element of self" do + ret = [] + @set.keep_if { |x| ret << x } + ret.sort.should == ["one", "two", "three"].sort + end + + it "keeps every element from self for which the passed block returns true" do + @set.keep_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.keep_if {}.should equal(@set) + end + + it "returns an Enumerator when passed no block" do + enum = @set.keep_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/rubyspec/library/set/length_spec.rb b/spec/rubyspec/library/set/length_spec.rb new file mode 100644 index 0000000000..274abcde83 --- /dev/null +++ b/spec/rubyspec/library/set/length_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'set' + +describe "Set#length" do + it_behaves_like :set_length, :length +end diff --git a/spec/rubyspec/library/set/map_spec.rb b/spec/rubyspec/library/set/map_spec.rb new file mode 100644 index 0000000000..8610982ba9 --- /dev/null +++ b/spec/rubyspec/library/set/map_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/collect', __FILE__) + +describe "Set#map!" do + it_behaves_like :set_collect_bang, :map! +end diff --git a/spec/rubyspec/library/set/member_spec.rb b/spec/rubyspec/library/set/member_spec.rb new file mode 100644 index 0000000000..1a807c061b --- /dev/null +++ b/spec/rubyspec/library/set/member_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/include', __FILE__) +require 'set' + +describe "Set#member?" do + it_behaves_like :set_include, :member? +end diff --git a/spec/rubyspec/library/set/merge_spec.rb b/spec/rubyspec/library/set/merge_spec.rb new file mode 100644 index 0000000000..ecee51b951 --- /dev/null +++ b/spec/rubyspec/library/set/merge_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#merge" do + it "adds the elements of the passed Enumerable to self" do + Set[:a, :b].merge(Set[:b, :c, :d]).should == Set[:a, :b, :c, :d] + Set[1, 2].merge([3, 4]).should == Set[1, 2, 3, 4] + end + + it "returns self" do + set = Set[1, 2] + set.merge([3, 4]).should equal(set) + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { Set[1, 2].merge(1) }.should raise_error(ArgumentError) + lambda { Set[1, 2].merge(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/minus_spec.rb b/spec/rubyspec/library/set/minus_spec.rb new file mode 100644 index 0000000000..3b14d6649d --- /dev/null +++ b/spec/rubyspec/library/set/minus_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/difference', __FILE__) + +describe "Set#-" do + it_behaves_like :set_difference, :- +end diff --git a/spec/rubyspec/library/set/plus_spec.rb b/spec/rubyspec/library/set/plus_spec.rb new file mode 100644 index 0000000000..38b78b6330 --- /dev/null +++ b/spec/rubyspec/library/set/plus_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/union', __FILE__) +require 'set' + +describe "Set#+" do + it_behaves_like :set_union, :+ +end diff --git a/spec/rubyspec/library/set/pretty_print_cycle_spec.rb b/spec/rubyspec/library/set/pretty_print_cycle_spec.rb new file mode 100644 index 0000000000..a7eaab337b --- /dev/null +++ b/spec/rubyspec/library/set/pretty_print_cycle_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#pretty_print_cycle" do + it "passes the 'pretty print' representation of a self-referencing Set to the pretty print writer" do + pp = mock("PrettyPrint") + pp.should_receive(:text).with("#<Set: {...}>") + Set[1, 2, 3].pretty_print_cycle(pp) + end +end diff --git a/spec/rubyspec/library/set/pretty_print_spec.rb b/spec/rubyspec/library/set/pretty_print_spec.rb new file mode 100644 index 0000000000..60f3ba7d3d --- /dev/null +++ b/spec/rubyspec/library/set/pretty_print_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#pretty_print" do + it "passes the 'pretty print' representation of self to the pretty print writer" do + pp = mock("PrettyPrint") + set = Set[1, 2, 3] + + pp.should_receive(:text).with("#<Set: {") + pp.should_receive(:text).with("}>") + + pp.should_receive(:nest).with(1).and_yield + pp.should_receive(:seplist).with(set) + + set.pretty_print(pp) + end +end diff --git a/spec/rubyspec/library/set/proper_subset_spec.rb b/spec/rubyspec/library/set/proper_subset_spec.rb new file mode 100644 index 0000000000..1a1fa13c3c --- /dev/null +++ b/spec/rubyspec/library/set/proper_subset_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#proper_subset?" do + before :each do + @set = Set[1, 2, 3, 4] + end + + it "returns true if passed a Set that self is a proper subset of" do + Set[].proper_subset?(@set).should be_true + Set[].proper_subset?(Set[1, 2, 3]).should be_true + Set[].proper_subset?(Set["a", :b, ?c]).should be_true + + Set[1, 2, 3].proper_subset?(@set).should be_true + Set[1, 3].proper_subset?(@set).should be_true + Set[1, 2].proper_subset?(@set).should be_true + Set[1].proper_subset?(@set).should be_true + + Set[5].proper_subset?(@set).should be_false + Set[1, 5].proper_subset?(@set).should be_false + Set[nil].proper_subset?(@set).should be_false + Set["test"].proper_subset?(@set).should be_false + + @set.proper_subset?(@set).should be_false + Set[].proper_subset?(Set[]).should be_false + end + + it "raises an ArgumentError when passed a non-Set" do + lambda { Set[].proper_subset?([]) }.should raise_error(ArgumentError) + lambda { Set[].proper_subset?(1) }.should raise_error(ArgumentError) + lambda { Set[].proper_subset?("test") }.should raise_error(ArgumentError) + lambda { Set[].proper_subset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/proper_superset_spec.rb b/spec/rubyspec/library/set/proper_superset_spec.rb new file mode 100644 index 0000000000..61c7984a6b --- /dev/null +++ b/spec/rubyspec/library/set/proper_superset_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#proper_superset?" do + before :each do + @set = Set[1, 2, 3, 4] + end + + it "returns true if passed a Set that self is a proper superset of" do + @set.proper_superset?(Set[]).should be_true + Set[1, 2, 3].proper_superset?(Set[]).should be_true + Set["a", :b, ?c].proper_superset?(Set[]).should be_true + + @set.proper_superset?(Set[1, 2, 3]).should be_true + @set.proper_superset?(Set[1, 3]).should be_true + @set.proper_superset?(Set[1, 2]).should be_true + @set.proper_superset?(Set[1]).should be_true + + @set.proper_superset?(Set[5]).should be_false + @set.proper_superset?(Set[1, 5]).should be_false + @set.proper_superset?(Set[nil]).should be_false + @set.proper_superset?(Set["test"]).should be_false + + @set.proper_superset?(@set).should be_false + Set[].proper_superset?(Set[]).should be_false + end + + it "raises an ArgumentError when passed a non-Set" do + lambda { Set[].proper_superset?([]) }.should raise_error(ArgumentError) + lambda { Set[].proper_superset?(1) }.should raise_error(ArgumentError) + lambda { Set[].proper_superset?("test") }.should raise_error(ArgumentError) + lambda { Set[].proper_superset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/reject_spec.rb b/spec/rubyspec/library/set/reject_spec.rb new file mode 100644 index 0000000000..32d3a92801 --- /dev/null +++ b/spec/rubyspec/library/set/reject_spec.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#reject!" do + before :each do + @set = Set["one", "two", "three"] + end + + it "yields every element of self" do + ret = [] + @set.reject! { |x| ret << x } + ret.sort.should == ["one", "two", "three"].sort + end + + it "deletes every element from self for which the passed block returns true" do + @set.reject! { |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 when self was modified" do + @set.reject! { |x| true }.should equal(@set) + end + + it "returns nil when self was not modified" do + @set.reject! { |x| false }.should be_nil + end + + it "returns an Enumerator when passed no block" do + enum = @set.reject! + 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/rubyspec/library/set/replace_spec.rb b/spec/rubyspec/library/set/replace_spec.rb new file mode 100644 index 0000000000..6f0c45b7ed --- /dev/null +++ b/spec/rubyspec/library/set/replace_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#replace" do + before :each do + @set = Set[:a, :b, :c] + end + + it "replaces the contents with other and returns self" do + @set.replace(Set[1, 2, 3]).should == @set + @set.should == Set[1, 2, 3] + end + + it "accepts any enumerable as other" do + @set.replace([1, 2, 3]).should == Set[1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/select_spec.rb b/spec/rubyspec/library/set/select_spec.rb new file mode 100644 index 0000000000..34274a7c46 --- /dev/null +++ b/spec/rubyspec/library/set/select_spec.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#select!" do + before :each do + @set = Set["one", "two", "three"] + end + + it "yields every element of self" do + ret = [] + @set.select! { |x| ret << x } + ret.sort.should == ["one", "two", "three"].sort + end + + it "keeps every element from self for which the passed block returns true" do + @set.select! { |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 when self was modified" do + @set.select! { false }.should equal(@set) + end + + it "returns nil when self was not modified" do + @set.select! { true }.should be_nil + end + + it "returns an Enumerator when passed no block" do + enum = @set.select! + 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/rubyspec/library/set/shared/add.rb b/spec/rubyspec/library/set/shared/add.rb new file mode 100644 index 0000000000..9e797f5df9 --- /dev/null +++ b/spec/rubyspec/library/set/shared/add.rb @@ -0,0 +1,14 @@ +describe :set_add, shared: true do + before :each do + @set = Set.new + end + + it "adds the passed Object to self" do + @set.send(@method, "dog") + @set.should include("dog") + end + + it "returns self" do + @set.send(@method, "dog").should equal(@set) + end +end diff --git a/spec/rubyspec/library/set/shared/collect.rb b/spec/rubyspec/library/set/shared/collect.rb new file mode 100644 index 0000000000..bc58c231be --- /dev/null +++ b/spec/rubyspec/library/set/shared/collect.rb @@ -0,0 +1,20 @@ +describe :set_collect_bang, shared: true do + before :each do + @set = Set[1, 2, 3, 4, 5] + end + + it "yields each Object in self" do + res = [] + @set.send(@method) { |x| res << x } + res.sort.should == [1, 2, 3, 4, 5].sort + end + + it "returns self" do + @set.send(@method) { |x| x }.should equal(@set) + end + + it "replaces self with the return values of the block" do + @set.send(@method) { |x| x * 2 } + @set.should == Set[2, 4, 6, 8, 10] + end +end diff --git a/spec/rubyspec/library/set/shared/difference.rb b/spec/rubyspec/library/set/shared/difference.rb new file mode 100644 index 0000000000..9439715da7 --- /dev/null +++ b/spec/rubyspec/library/set/shared/difference.rb @@ -0,0 +1,15 @@ +describe :set_difference, shared: true do + before :each do + @set = Set[:a, :b, :c] + end + + it "returns a new Set containting self's elements excluding the elements in the passed Enumerable" do + @set.send(@method, Set[:a, :b]).should == Set[:c] + @set.send(@method, [:b, :c]).should == Set[:a] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/shared/include.rb b/spec/rubyspec/library/set/shared/include.rb new file mode 100644 index 0000000000..d41f8f4102 --- /dev/null +++ b/spec/rubyspec/library/set/shared/include.rb @@ -0,0 +1,7 @@ +describe :set_include, shared: true do + it "returns true when self contains the passed Object" do + set = Set[:a, :b, :c] + set.send(@method, :a).should be_true + set.send(@method, :e).should be_false + end +end diff --git a/spec/rubyspec/library/set/shared/intersection.rb b/spec/rubyspec/library/set/shared/intersection.rb new file mode 100644 index 0000000000..ed0db7457d --- /dev/null +++ b/spec/rubyspec/library/set/shared/intersection.rb @@ -0,0 +1,15 @@ +describe :set_intersection, shared: true do + before :each do + @set = Set[:a, :b, :c] + end + + it "returns a new Set containing only elements shared by self and the passed Enumerable" do + @set.send(@method, Set[:b, :c, :d, :e]).should == Set[:b, :c] + @set.send(@method, [:b, :c, :d]).should == Set[:b, :c] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/shared/length.rb b/spec/rubyspec/library/set/shared/length.rb new file mode 100644 index 0000000000..a8fcee9f39 --- /dev/null +++ b/spec/rubyspec/library/set/shared/length.rb @@ -0,0 +1,6 @@ +describe :set_length, shared: true do + it "returns the number of elements in the set" do + set = Set[:a, :b, :c] + set.send(@method).should == 3 + end +end diff --git a/spec/rubyspec/library/set/shared/union.rb b/spec/rubyspec/library/set/shared/union.rb new file mode 100644 index 0000000000..81920f5687 --- /dev/null +++ b/spec/rubyspec/library/set/shared/union.rb @@ -0,0 +1,15 @@ +describe :set_union, shared: true do + before :each do + @set = Set[:a, :b, :c] + end + + it "returns a new Set containing all elements of self and the passed Enumerable" do + @set.send(@method, Set[:b, :d, :e]).should == Set[:a, :b, :c, :d, :e] + @set.send(@method, [:b, :e]).should == Set[:a, :b, :c, :e] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/size_spec.rb b/spec/rubyspec/library/set/size_spec.rb new file mode 100644 index 0000000000..f33740c5cd --- /dev/null +++ b/spec/rubyspec/library/set/size_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'set' + +describe "Set#size" do + it_behaves_like :set_length, :size +end diff --git a/spec/rubyspec/library/set/sortedset/add_spec.rb b/spec/rubyspec/library/set/sortedset/add_spec.rb new file mode 100644 index 0000000000..bdc5c077d8 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/add_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/add', __FILE__) + +describe "SortedSet#add" do + it_behaves_like :sorted_set_add, :add + + it "takes only values which responds <=>" do + obj = mock('no_comparison_operator') + obj.should_receive(:respond_to?).with(:<=>).and_return(false) + lambda { SortedSet["hello"].add(obj) }.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/rubyspec/library/set/sortedset/append_spec.rb b/spec/rubyspec/library/set/sortedset/append_spec.rb new file mode 100644 index 0000000000..62933f3e42 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/append_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/add', __FILE__) + +describe "SortedSet#<<" do + it_behaves_like :sorted_set_add, :<< +end diff --git a/spec/rubyspec/library/set/sortedset/classify_spec.rb b/spec/rubyspec/library/set/sortedset/classify_spec.rb new file mode 100644 index 0000000000..1e8c814699 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/classify_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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/rubyspec/library/set/sortedset/clear_spec.rb b/spec/rubyspec/library/set/sortedset/clear_spec.rb new file mode 100644 index 0000000000..3a3277dd8a --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/clear_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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/rubyspec/library/set/sortedset/collect_spec.rb b/spec/rubyspec/library/set/sortedset/collect_spec.rb new file mode 100644 index 0000000000..18274e6353 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/collect_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/collect', __FILE__) + +describe "SortedSet#collect!" do + it_behaves_like :sorted_set_collect_bang, :collect! +end diff --git a/spec/rubyspec/library/set/sortedset/constructor_spec.rb b/spec/rubyspec/library/set/sortedset/constructor_spec.rb new file mode 100644 index 0000000000..45b6749e27 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/constructor_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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/rubyspec/library/set/sortedset/delete_if_spec.rb b/spec/rubyspec/library/set/sortedset/delete_if_spec.rb new file mode 100644 index 0000000000..c809ff75f0 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/delete_if_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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/rubyspec/library/set/sortedset/delete_spec.rb b/spec/rubyspec/library/set/sortedset/delete_spec.rb new file mode 100644 index 0000000000..7123f79bcf --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/delete_spec.rb @@ -0,0 +1,37 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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/rubyspec/library/set/sortedset/difference_spec.rb b/spec/rubyspec/library/set/sortedset/difference_spec.rb new file mode 100644 index 0000000000..bc3650c55c --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/difference_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/difference', __FILE__) + +describe "SortedSet#difference" do + it_behaves_like :sorted_set_difference, :difference +end diff --git a/spec/rubyspec/library/set/sortedset/divide_spec.rb b/spec/rubyspec/library/set/sortedset/divide_spec.rb new file mode 100644 index 0000000000..adb152b7e6 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/divide_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +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(LocalJumpError) + # end +end + +describe "SortedSet#divide when passed a block with an arity of 2" do + it "divides self into a set of subsets based on the blocks return values" do + set = SortedSet[1, 3, 4, 6, 9, 10, 11].divide { |x, y| (x - y).abs == 1 } + set.map { |x| x.to_a }.to_a.sort.should == [[1], [3, 4], [6], [9, 10, 11]] + end + + it "yields each two Objects to the block" do + ret = [] + SortedSet[1, 2].divide { |x, y| ret << [x, y] } + ret.should == [[1, 1], [1, 2], [2, 1], [2, 2]] + end +end diff --git a/spec/rubyspec/library/set/sortedset/each_spec.rb b/spec/rubyspec/library/set/sortedset/each_spec.rb new file mode 100644 index 0000000000..c715c403b2 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/each_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#each" do + before :each do + @set = SortedSet[1, 2, 3] + end + + it "yields each Object in self in sorted order" do + ret = [] + SortedSet["one", "two", "three"].each { |x| ret << x } + ret.should == ["one", "two", "three"].sort + end + + it "returns self" do + @set.each { |x| x }.should equal(@set) + end + + it "returns an Enumerator when not passed a block" do + enum = @set.each + + ret = [] + enum.each { |x| ret << x } + ret.sort.should == [1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/sortedset/empty_spec.rb b/spec/rubyspec/library/set/sortedset/empty_spec.rb new file mode 100644 index 0000000000..50d046e4c0 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/empty_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#empty?" do + it "returns true if self is empty" do + SortedSet[].empty?.should be_true + SortedSet[1].empty?.should be_false + SortedSet[1,2,3].empty?.should be_false + end +end diff --git a/spec/rubyspec/library/set/sortedset/eql_spec.rb b/spec/rubyspec/library/set/sortedset/eql_spec.rb new file mode 100644 index 0000000000..e7b3e7b624 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/eql_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#eql?" do + it "returns true when the passed argument is a SortedSet and contains the same elements" do + SortedSet[].should eql(SortedSet[]) + SortedSet[1, 2, 3].should eql(SortedSet[1, 2, 3]) + SortedSet[1, 2, 3].should eql(SortedSet[3, 2, 1]) + +# SortedSet["a", :b, ?c].should eql(SortedSet[?c, :b, "a"]) + + SortedSet[1, 2, 3].should_not eql(SortedSet[1.0, 2, 3]) + SortedSet[1, 2, 3].should_not eql(SortedSet[2, 3]) + SortedSet[1, 2, 3].should_not eql(SortedSet[]) + end +end diff --git a/spec/rubyspec/library/set/sortedset/equal_value_spec.rb b/spec/rubyspec/library/set/sortedset/equal_value_spec.rb new file mode 100644 index 0000000000..e358372aa4 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/equal_value_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#==" do + it "returns true when the passed Object is a SortedSet and self and the Object contain the same elements" do + SortedSet[].should == SortedSet[] + SortedSet[1, 2, 3].should == SortedSet[1, 2, 3] + SortedSet["1", "2", "3"].should == SortedSet["1", "2", "3"] + + SortedSet[1, 2, 3].should_not == SortedSet[1.0, 2, 3] + SortedSet[1, 2, 3].should_not == [1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/sortedset/exclusion_spec.rb b/spec/rubyspec/library/set/sortedset/exclusion_spec.rb new file mode 100644 index 0000000000..193dbb7725 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/exclusion_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#^" do + before :each do + @set = SortedSet[1, 2, 3, 4] + end + + it "returns a new SortedSet containing elements that are not in both self and the passed Enumberable" do + (@set ^ SortedSet[3, 4, 5]).should == SortedSet[1, 2, 5] + (@set ^ [3, 4, 5]).should == SortedSet[1, 2, 5] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set ^ 3 }.should raise_error(ArgumentError) + lambda { @set ^ Object.new }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/flatten_merge_spec.rb b/spec/rubyspec/library/set/sortedset/flatten_merge_spec.rb new file mode 100644 index 0000000000..2a2505a58b --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/flatten_merge_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#flatten_merge" do + it "is protected" do + SortedSet.should have_protected_instance_method("flatten_merge") + end +end diff --git a/spec/rubyspec/library/set/sortedset/flatten_spec.rb b/spec/rubyspec/library/set/sortedset/flatten_spec.rb new file mode 100644 index 0000000000..80d064b846 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/flatten_spec.rb @@ -0,0 +1,44 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +# Note: Flatten make little sens on sorted sets, because SortedSets are not (by default) +# comparable. For a SortedSet to be both valid and nested, we need to define a comparison operator: +module SortedSet_FlattenSpecs + class ComparableSortedSet < SortedSet + def <=>(other) + return puts "#{other} vs #{self}" unless other.is_a?(ComparableSortedSet) + to_a <=> other.to_a + end + end +end + +describe "SortedSet#flatten" do + it "returns a copy of self with each included SortedSet flattened" do + klass = SortedSet_FlattenSpecs::ComparableSortedSet + set = klass[klass[1,2], klass[3,4], klass[5,6,7], klass[8]] + flattened_set = set.flatten + + flattened_set.should_not equal(set) + flattened_set.should == klass[1, 2, 3, 4, 5, 6, 7, 8] + end +end + +describe "SortedSet#flatten!" do + it "flattens self" do + klass = SortedSet_FlattenSpecs::ComparableSortedSet + set = klass[klass[1,2], klass[3,4], klass[5,6,7], klass[8]] + set.flatten! + set.should == klass[1, 2, 3, 4, 5, 6, 7, 8] + end + + it "returns self when self was modified" do + klass = SortedSet_FlattenSpecs::ComparableSortedSet + set = klass[klass[1,2], klass[3,4]] + set.flatten!.should equal(set) + end + + it "returns nil when self was not modified" do + set = SortedSet[1, 2, 3, 4] + set.flatten!.should be_nil + end +end diff --git a/spec/rubyspec/library/set/sortedset/hash_spec.rb b/spec/rubyspec/library/set/sortedset/hash_spec.rb new file mode 100644 index 0000000000..176cb7e8dc --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/hash_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#hash" do + it "is static" do + SortedSet[].hash.should == SortedSet[].hash + SortedSet[1, 2, 3].hash.should == SortedSet[1, 2, 3].hash + SortedSet["a", "b", "c"].hash.should == SortedSet["c", "b", "a"].hash + + SortedSet[].hash.should_not == SortedSet[1, 2, 3].hash + SortedSet[1, 2, 3].hash.should_not == SortedSet["a", "b", "c"].hash + end +end diff --git a/spec/rubyspec/library/set/sortedset/include_spec.rb b/spec/rubyspec/library/set/sortedset/include_spec.rb new file mode 100644 index 0000000000..bb8ceda708 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/include_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/include', __FILE__) +require 'set' + +describe "SortedSet#include?" do + it_behaves_like :sorted_set_include, :include? +end diff --git a/spec/rubyspec/library/set/sortedset/initialize_spec.rb b/spec/rubyspec/library/set/sortedset/initialize_spec.rb new file mode 100644 index 0000000000..04ad908667 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/initialize_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#initialize" do + it "is private" do + SortedSet.should have_private_instance_method("initialize") + end + + it "adds all elements of the passed Enumerable to self" do + s = SortedSet.new([1, 2, 3]) + s.size.should eql(3) + s.should include(1) + s.should include(2) + s.should include(3) + end + + it "preprocesses all elements by a passed block before adding to self" do + s = SortedSet.new([1, 2, 3]) { |x| x * x } + s.size.should eql(3) + s.should include(1) + s.should include(4) + s.should include(9) + end +end diff --git a/spec/rubyspec/library/set/sortedset/inspect_spec.rb b/spec/rubyspec/library/set/sortedset/inspect_spec.rb new file mode 100644 index 0000000000..64b3f3d882 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/inspect_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#inspect" do + it "returns a String representation of self" do + SortedSet[].inspect.should be_kind_of(String) + SortedSet[1, 2, 3].inspect.should be_kind_of(String) + SortedSet["1", "2", "3"].inspect.should be_kind_of(String) + end +end diff --git a/spec/rubyspec/library/set/sortedset/intersection_spec.rb b/spec/rubyspec/library/set/sortedset/intersection_spec.rb new file mode 100644 index 0000000000..d3f5b49ceb --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/intersection_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/intersection', __FILE__) +require 'set' + +describe "SortedSet#intersection" do + it_behaves_like :sorted_set_intersection, :intersection +end + +describe "SortedSet#&" do + it_behaves_like :sorted_set_intersection, :& +end diff --git a/spec/rubyspec/library/set/sortedset/keep_if_spec.rb b/spec/rubyspec/library/set/sortedset/keep_if_spec.rb new file mode 100644 index 0000000000..7a117fbc87 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/keep_if_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#keep_if" do + before :each do + @set = SortedSet["one", "two", "three"] + end + + it "yields each Object in self in sorted order" do + ret = [] + @set.keep_if { |x| ret << x } + ret.should == ["one", "two", "three"].sort + end + + it "keeps every element from self for which the passed block returns true" do + @set.keep_if { |x| x.size != 3 } + @set.to_a.should == ["three"] + end + + it "returns self" do + @set.keep_if {}.should equal(@set) + end + + it "returns an Enumerator when passed no block" do + enum = @set.keep_if + enum.should be_an_instance_of(Enumerator) + + enum.each { |x| x.size != 3 } + @set.to_a.should == ["three"] + end +end diff --git a/spec/rubyspec/library/set/sortedset/length_spec.rb b/spec/rubyspec/library/set/sortedset/length_spec.rb new file mode 100644 index 0000000000..d829b3e08e --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/length_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'set' + +describe "SortedSet#length" do + it_behaves_like :sorted_set_length, :length +end diff --git a/spec/rubyspec/library/set/sortedset/map_spec.rb b/spec/rubyspec/library/set/sortedset/map_spec.rb new file mode 100644 index 0000000000..1f0828f347 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/map_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/collect', __FILE__) + +describe "SortedSet#map!" do + it_behaves_like :sorted_set_collect_bang, :map! +end diff --git a/spec/rubyspec/library/set/sortedset/member_spec.rb b/spec/rubyspec/library/set/sortedset/member_spec.rb new file mode 100644 index 0000000000..d64e318b83 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/member_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/include', __FILE__) +require 'set' + +describe "SortedSet#member?" do + it_behaves_like :sorted_set_include, :member? +end diff --git a/spec/rubyspec/library/set/sortedset/merge_spec.rb b/spec/rubyspec/library/set/sortedset/merge_spec.rb new file mode 100644 index 0000000000..c422fe9513 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/merge_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#merge" do + it "adds the elements of the passed Enumerable to self" do + SortedSet["a", "b"].merge(SortedSet["b", "c", "d"]).should == SortedSet["a", "b", "c", "d"] + SortedSet[1, 2].merge([3, 4]).should == SortedSet[1, 2, 3, 4] + end + + it "returns self" do + set = SortedSet[1, 2] + set.merge([3, 4]).should equal(set) + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { SortedSet[1, 2].merge(1) }.should raise_error(ArgumentError) + lambda { SortedSet[1, 2].merge(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/minus_spec.rb b/spec/rubyspec/library/set/sortedset/minus_spec.rb new file mode 100644 index 0000000000..1f56d57037 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/minus_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' +require File.expand_path('../shared/difference', __FILE__) + +describe "SortedSet#-" do + it_behaves_like :sorted_set_difference, :- +end diff --git a/spec/rubyspec/library/set/sortedset/plus_spec.rb b/spec/rubyspec/library/set/sortedset/plus_spec.rb new file mode 100644 index 0000000000..af9bdf82fa --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/plus_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/union', __FILE__) +require 'set' + +describe "SortedSet#+" do + it_behaves_like :sorted_set_union, :+ +end diff --git a/spec/rubyspec/library/set/sortedset/pretty_print_cycle_spec.rb b/spec/rubyspec/library/set/sortedset/pretty_print_cycle_spec.rb new file mode 100644 index 0000000000..6e79245e18 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/pretty_print_cycle_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#pretty_print_cycle" do + it "passes the 'pretty print' representation of a self-referencing SortedSet to the pretty print writer" do + pp = mock("PrettyPrint") + pp.should_receive(:text).with("#<SortedSet: {...}>") + SortedSet[1, 2, 3].pretty_print_cycle(pp) + end +end diff --git a/spec/rubyspec/library/set/sortedset/pretty_print_spec.rb b/spec/rubyspec/library/set/sortedset/pretty_print_spec.rb new file mode 100644 index 0000000000..5317357b8f --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/pretty_print_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#pretty_print" do + it "passes the 'pretty print' representation of self to the pretty print writer" do + pp = mock("PrettyPrint") + set = SortedSet[1, 2, 3] + + pp.should_receive(:text).with("#<SortedSet: {") + pp.should_receive(:text).with("}>") + + pp.should_receive(:nest).with(1).and_yield + pp.should_receive(:seplist).with(set) + + set.pretty_print(pp) + end +end diff --git a/spec/rubyspec/library/set/sortedset/proper_subset_spec.rb b/spec/rubyspec/library/set/sortedset/proper_subset_spec.rb new file mode 100644 index 0000000000..7e94774c1f --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/proper_subset_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#proper_subset?" do + before :each do + @set = SortedSet[1, 2, 3, 4] + end + + it "returns true if passed a SortedSet that self is a proper subset of" do + SortedSet[].proper_subset?(@set).should be_true + SortedSet[].proper_subset?(SortedSet[1, 2, 3]).should be_true + SortedSet[].proper_subset?(SortedSet["a", "b", "c"]).should be_true + + SortedSet[1, 2, 3].proper_subset?(@set).should be_true + SortedSet[1, 3].proper_subset?(@set).should be_true + SortedSet[1, 2].proper_subset?(@set).should be_true + SortedSet[1].proper_subset?(@set).should be_true + + SortedSet[5].proper_subset?(@set).should be_false + SortedSet[1, 5].proper_subset?(@set).should be_false + SortedSet["test"].proper_subset?(@set).should be_false + + @set.proper_subset?(@set).should be_false + SortedSet[].proper_subset?(SortedSet[]).should be_false + end + + it "raises an ArgumentError when passed a non-SortedSet" do + lambda { SortedSet[].proper_subset?([]) }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_subset?(1) }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_subset?("test") }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_subset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/proper_superset_spec.rb b/spec/rubyspec/library/set/sortedset/proper_superset_spec.rb new file mode 100644 index 0000000000..ccfa37988d --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/proper_superset_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#proper_superset?" do + before :each do + @set = SortedSet[1, 2, 3, 4] + end + + it "returns true if passed a SortedSet that self is a proper superset of" do + @set.proper_superset?(SortedSet[]).should be_true + SortedSet[1, 2, 3].proper_superset?(SortedSet[]).should be_true + SortedSet["a", "b", "c"].proper_superset?(SortedSet[]).should be_true + + @set.proper_superset?(SortedSet[1, 2, 3]).should be_true + @set.proper_superset?(SortedSet[1, 3]).should be_true + @set.proper_superset?(SortedSet[1, 2]).should be_true + @set.proper_superset?(SortedSet[1]).should be_true + + @set.proper_superset?(SortedSet[5]).should be_false + @set.proper_superset?(SortedSet[1, 5]).should be_false + @set.proper_superset?(SortedSet["test"]).should be_false + + @set.proper_superset?(@set).should be_false + SortedSet[].proper_superset?(SortedSet[]).should be_false + end + + it "raises an ArgumentError when passed a non-SortedSet" do + lambda { SortedSet[].proper_superset?([]) }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_superset?(1) }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_superset?("test") }.should raise_error(ArgumentError) + lambda { SortedSet[].proper_superset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/reject_spec.rb b/spec/rubyspec/library/set/sortedset/reject_spec.rb new file mode 100644 index 0000000000..e357d55052 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/reject_spec.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#reject!" do + before :each do + @set = SortedSet["one", "two", "three"] + end + + it "yields each Object in self in sorted order" do + res = [] + @set.reject! { |x| res << x } + res.should == ["one", "two", "three"].sort + end + + it "deletes every element from self for which the passed block returns true" do + @set.reject! { |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 when self was modified" do + @set.reject! { |x| true }.should equal(@set) + end + + it "returns nil when self was not modified" do + @set.reject! { |x| false }.should be_nil + end + + it "returns an Enumerator when passed no block" do + enum = @set.reject! + 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/rubyspec/library/set/sortedset/replace_spec.rb b/spec/rubyspec/library/set/sortedset/replace_spec.rb new file mode 100644 index 0000000000..a5bf333e87 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/replace_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#replace" do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "replaces the contents with other and returns self" do + @set.replace(SortedSet[1, 2, 3]).should == @set + @set.should == SortedSet[1, 2, 3] + end + + it "accepts any enumerable as other" do + @set.replace([1, 2, 3]).should == SortedSet[1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/sortedset/select_spec.rb b/spec/rubyspec/library/set/sortedset/select_spec.rb new file mode 100644 index 0000000000..3ca748350a --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/select_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#select!" do + before :each do + @set = SortedSet["one", "two", "three"] + end + + it "yields each Object in self in sorted order" do + res = [] + @set.select! { |x| res << x } + res.should == ["one", "two", "three"].sort + end + + it "keeps every element from self for which the passed block returns true" do + @set.select! { |x| x.size != 3 } + @set.to_a.should == ["three"] + end + + it "returns self when self was modified" do + @set.select! { false }.should equal(@set) + end + + it "returns nil when self was not modified" do + @set.select! { true }.should be_nil + end + + it "returns an Enumerator when passed no block" do + enum = @set.select! + enum.should be_an_instance_of(Enumerator) + + enum.each { |x| x.size != 3 } + @set.to_a.should == ["three"] + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/add.rb b/spec/rubyspec/library/set/sortedset/shared/add.rb new file mode 100644 index 0000000000..95ef1b090e --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/add.rb @@ -0,0 +1,14 @@ +describe :sorted_set_add, shared: true do + before :each do + @set = SortedSet.new + end + + it "adds the passed Object to self" do + @set.send(@method, "dog") + @set.should include("dog") + end + + it "returns self" do + @set.send(@method, "dog").should equal(@set) + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/collect.rb b/spec/rubyspec/library/set/sortedset/shared/collect.rb new file mode 100644 index 0000000000..e53304d427 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/collect.rb @@ -0,0 +1,20 @@ +describe :sorted_set_collect_bang, shared: true do + before :each do + @set = SortedSet[1, 2, 3, 4, 5] + end + + it "yields each Object in self in sorted order" do + res = [] + SortedSet["one", "two", "three"].send(@method) { |x| res << x; x } + res.should == ["one", "two", "three"].sort + end + + it "returns self" do + @set.send(@method) { |x| x }.should equal(@set) + end + + it "replaces self with the return values of the block" do + @set.send(@method) { |x| x * 2 } + @set.should == SortedSet[2, 4, 6, 8, 10] + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/difference.rb b/spec/rubyspec/library/set/sortedset/shared/difference.rb new file mode 100644 index 0000000000..ec57015ac2 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/difference.rb @@ -0,0 +1,15 @@ +describe :sorted_set_difference, shared: true do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "returns a new SortedSet containting self's elements excluding the elements in the passed Enumerable" do + @set.send(@method, SortedSet["a", "b"]).should == SortedSet["c"] + @set.send(@method, ["b", "c"]).should == SortedSet["a"] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/include.rb b/spec/rubyspec/library/set/sortedset/shared/include.rb new file mode 100644 index 0000000000..cd1758819d --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/include.rb @@ -0,0 +1,7 @@ +describe :sorted_set_include, shared: true do + it "returns true when self contains the passed Object" do + set = SortedSet["a", "b", "c"] + set.send(@method, "a").should be_true + set.send(@method, "e").should be_false + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/intersection.rb b/spec/rubyspec/library/set/sortedset/shared/intersection.rb new file mode 100644 index 0000000000..d3cfa96656 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/intersection.rb @@ -0,0 +1,15 @@ +describe :sorted_set_intersection, shared: true do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "returns a new SortedSet containing only elements shared by self and the passed Enumerable" do + @set.send(@method, SortedSet["b", "c", "d", "e"]).should == SortedSet["b", "c"] + @set.send(@method, ["b", "c", "d"]).should == SortedSet["b", "c"] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/length.rb b/spec/rubyspec/library/set/sortedset/shared/length.rb new file mode 100644 index 0000000000..d1dfee1cff --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/length.rb @@ -0,0 +1,6 @@ +describe :sorted_set_length, shared: true do + it "returns the number of elements in the set" do + set = SortedSet["a", "b", "c"] + set.send(@method).should == 3 + end +end diff --git a/spec/rubyspec/library/set/sortedset/shared/union.rb b/spec/rubyspec/library/set/sortedset/shared/union.rb new file mode 100644 index 0000000000..4ff07ef5cc --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/shared/union.rb @@ -0,0 +1,15 @@ +describe :sorted_set_union, shared: true do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "returns a new SortedSet containing all elements of self and the passed Enumerable" do + @set.send(@method, SortedSet["b", "d", "e"]).should == SortedSet["a", "b", "c", "d", "e"] + @set.send(@method, ["b", "e"]).should == SortedSet["a", "b", "c", "e"] + end + + it "raises an ArgumentError when passed a non-Enumerable" do + lambda { @set.send(@method, 1) }.should raise_error(ArgumentError) + lambda { @set.send(@method, Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/size_spec.rb b/spec/rubyspec/library/set/sortedset/size_spec.rb new file mode 100644 index 0000000000..dbcdc3ded3 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/size_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/length', __FILE__) +require 'set' + +describe "SortedSet#size" do + it_behaves_like :sorted_set_length, :size +end diff --git a/spec/rubyspec/library/set/sortedset/subset_spec.rb b/spec/rubyspec/library/set/sortedset/subset_spec.rb new file mode 100644 index 0000000000..81f938317c --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/subset_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#subset?" do + before :each do + @set = SortedSet[1, 2, 3, 4] + end + + it "returns true if passed a SortedSet that is equal to self or self is a subset of" do + @set.subset?(@set).should be_true + SortedSet[].subset?(SortedSet[]).should be_true + + SortedSet[].subset?(@set).should be_true + SortedSet[].subset?(SortedSet[1, 2, 3]).should be_true + SortedSet[].subset?(SortedSet["a", "b", "c"]).should be_true + + SortedSet[1, 2, 3].subset?(@set).should be_true + SortedSet[1, 3].subset?(@set).should be_true + SortedSet[1, 2].subset?(@set).should be_true + SortedSet[1].subset?(@set).should be_true + + SortedSet[5].subset?(@set).should be_false + SortedSet[1, 5].subset?(@set).should be_false + SortedSet["test"].subset?(@set).should be_false + end + + it "raises an ArgumentError when passed a non-SortedSet" do + lambda { SortedSet[].subset?([]) }.should raise_error(ArgumentError) + lambda { SortedSet[].subset?(1) }.should raise_error(ArgumentError) + lambda { SortedSet[].subset?("test") }.should raise_error(ArgumentError) + lambda { SortedSet[].subset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/subtract_spec.rb b/spec/rubyspec/library/set/sortedset/subtract_spec.rb new file mode 100644 index 0000000000..207748cfb9 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/subtract_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#subtract" do + before :each do + @set = SortedSet["a", "b", "c"] + end + + it "deletes any elements contained in other and returns self" do + @set.subtract(SortedSet["b", "c"]).should == @set + @set.should == SortedSet["a"] + end + + it "accepts any enumerable as other" do + @set.subtract(["c"]).should == SortedSet["a", "b"] + end +end diff --git a/spec/rubyspec/library/set/sortedset/superset_spec.rb b/spec/rubyspec/library/set/sortedset/superset_spec.rb new file mode 100644 index 0000000000..fc54e618a2 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/superset_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#superset?" do + before :each do + @set = SortedSet[1, 2, 3, 4] + end + + it "returns true if passed a SortedSet that equals self or self is a proper superset of" do + @set.superset?(@set).should be_true + SortedSet[].superset?(SortedSet[]).should be_true + + @set.superset?(SortedSet[]).should be_true + SortedSet[1, 2, 3].superset?(SortedSet[]).should be_true + SortedSet["a", "b", "c"].superset?(SortedSet[]).should be_true + + @set.superset?(SortedSet[1, 2, 3]).should be_true + @set.superset?(SortedSet[1, 3]).should be_true + @set.superset?(SortedSet[1, 2]).should be_true + @set.superset?(SortedSet[1]).should be_true + + @set.superset?(SortedSet[5]).should be_false + @set.superset?(SortedSet[1, 5]).should be_false + @set.superset?(SortedSet["test"]).should be_false + end + + it "raises an ArgumentError when passed a non-SortedSet" do + lambda { SortedSet[].superset?([]) }.should raise_error(ArgumentError) + lambda { SortedSet[].superset?(1) }.should raise_error(ArgumentError) + lambda { SortedSet[].superset?("test") }.should raise_error(ArgumentError) + lambda { SortedSet[].superset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/sortedset/to_a_spec.rb b/spec/rubyspec/library/set/sortedset/to_a_spec.rb new file mode 100644 index 0000000000..f288cfb9d2 --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/to_a_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'set' + +describe "SortedSet#to_a" do + it "returns an array containing elements of self" do + SortedSet[1, 2, 3].to_a.sort.should == [1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/sortedset/union_spec.rb b/spec/rubyspec/library/set/sortedset/union_spec.rb new file mode 100644 index 0000000000..c7255c3d2f --- /dev/null +++ b/spec/rubyspec/library/set/sortedset/union_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/union', __FILE__) +require 'set' + +describe "SortedSet#union" do + it_behaves_like :sorted_set_union, :union +end + +describe "SortedSet#|" do + it_behaves_like :sorted_set_union, :| +end diff --git a/spec/rubyspec/library/set/subset_spec.rb b/spec/rubyspec/library/set/subset_spec.rb new file mode 100644 index 0000000000..6503a7539f --- /dev/null +++ b/spec/rubyspec/library/set/subset_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#subset?" do + before :each do + @set = Set[1, 2, 3, 4] + end + + it "returns true if passed a Set that is equal to self or self is a subset of" do + @set.subset?(@set).should be_true + Set[].subset?(Set[]).should be_true + + Set[].subset?(@set).should be_true + Set[].subset?(Set[1, 2, 3]).should be_true + Set[].subset?(Set["a", :b, ?c]).should be_true + + Set[1, 2, 3].subset?(@set).should be_true + Set[1, 3].subset?(@set).should be_true + Set[1, 2].subset?(@set).should be_true + Set[1].subset?(@set).should be_true + + Set[5].subset?(@set).should be_false + Set[1, 5].subset?(@set).should be_false + Set[nil].subset?(@set).should be_false + Set["test"].subset?(@set).should be_false + end + + it "raises an ArgumentError when passed a non-Set" do + lambda { Set[].subset?([]) }.should raise_error(ArgumentError) + lambda { Set[].subset?(1) }.should raise_error(ArgumentError) + lambda { Set[].subset?("test") }.should raise_error(ArgumentError) + lambda { Set[].subset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/subtract_spec.rb b/spec/rubyspec/library/set/subtract_spec.rb new file mode 100644 index 0000000000..b0889bb675 --- /dev/null +++ b/spec/rubyspec/library/set/subtract_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#subtract" do + before :each do + @set = Set[:a, :b, :c] + end + + it "deletes any elements contained in other and returns self" do + @set.subtract(Set[:b, :c]).should == @set + @set.should == Set[:a] + end + + it "accepts any enumerable as other" do + @set.subtract([:c]).should == Set[:a, :b] + end +end diff --git a/spec/rubyspec/library/set/superset_spec.rb b/spec/rubyspec/library/set/superset_spec.rb new file mode 100644 index 0000000000..b7364f529e --- /dev/null +++ b/spec/rubyspec/library/set/superset_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#superset?" do + before :each do + @set = Set[1, 2, 3, 4] + end + + it "returns true if passed a Set that equals self or self is a proper superset of" do + @set.superset?(@set).should be_true + Set[].superset?(Set[]).should be_true + + @set.superset?(Set[]).should be_true + Set[1, 2, 3].superset?(Set[]).should be_true + Set["a", :b, ?c].superset?(Set[]).should be_true + + @set.superset?(Set[1, 2, 3]).should be_true + @set.superset?(Set[1, 3]).should be_true + @set.superset?(Set[1, 2]).should be_true + @set.superset?(Set[1]).should be_true + + @set.superset?(Set[5]).should be_false + @set.superset?(Set[1, 5]).should be_false + @set.superset?(Set[nil]).should be_false + @set.superset?(Set["test"]).should be_false + end + + it "raises an ArgumentError when passed a non-Set" do + lambda { Set[].superset?([]) }.should raise_error(ArgumentError) + lambda { Set[].superset?(1) }.should raise_error(ArgumentError) + lambda { Set[].superset?("test") }.should raise_error(ArgumentError) + lambda { Set[].superset?(Object.new) }.should raise_error(ArgumentError) + end +end diff --git a/spec/rubyspec/library/set/to_a_spec.rb b/spec/rubyspec/library/set/to_a_spec.rb new file mode 100644 index 0000000000..daee014e90 --- /dev/null +++ b/spec/rubyspec/library/set/to_a_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'set' + +describe "Set#to_a" do + it "returns an array containing elements of self" do + Set[1, 2, 3].to_a.sort.should == [1, 2, 3] + end +end diff --git a/spec/rubyspec/library/set/union_spec.rb b/spec/rubyspec/library/set/union_spec.rb new file mode 100644 index 0000000000..c705497928 --- /dev/null +++ b/spec/rubyspec/library/set/union_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/union', __FILE__) +require 'set' + +describe "Set#union" do + it_behaves_like :set_union, :union +end + +describe "Set#|" do + it_behaves_like :set_union, :| +end |