From 70ab5c9fc54eccd1b8bb0abd84caf538d2876206 Mon Sep 17 00:00:00 2001 From: Herwin Date: Sat, 5 Aug 2023 19:35:47 +0200 Subject: [PATCH 1/2] Add spec for Set#divide without block The description in the docs state "Returns an enumerator if no block is given", added in Ruby 2.4. --- library/set/divide_spec.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/library/set/divide_spec.rb b/library/set/divide_spec.rb index fdd8cd9622..da9d2c1ae1 100644 --- a/library/set/divide_spec.rb +++ b/library/set/divide_spec.rb @@ -13,11 +13,11 @@ 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 + it "returns an enumerator when not passed a block" do + ret = Set[1, 2, 3, 4].divide + ret.should be_kind_of(Enumerator) + ret.each(&:even?).should == Set[Set[1, 3], Set[2, 4]] + end end describe "Set#divide when passed a block with an arity of 2" do @@ -31,4 +31,10 @@ Set[1, 2].divide { |x, y| ret << [x, y] } ret.sort.should == [[1, 1], [1, 2], [2, 1], [2, 2]] end + + it "returns an enumerator when not passed a block" do + ret = Set[1, 2, 3, 4].divide + ret.should be_kind_of(Enumerator) + ret.each { |a, b| (a + b).even? }.should == Set[Set[1, 3], Set[2, 4]] + end end From e15116b337f3629e1d11a9345be4d98d6162bce6 Mon Sep 17 00:00:00 2001 From: Herwin Date: Sat, 5 Aug 2023 19:42:53 +0200 Subject: [PATCH 2/2] Add spec for Set#divide for arity other than 1 and 2 This is undocumented behaviour. --- library/set/divide_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/set/divide_spec.rb b/library/set/divide_spec.rb index da9d2c1ae1..998a1b292c 100644 --- a/library/set/divide_spec.rb +++ b/library/set/divide_spec.rb @@ -38,3 +38,22 @@ ret.each { |a, b| (a + b).even? }.should == Set[Set[1, 3], Set[2, 4]] end end + +describe "Set#divide when passed a block with an arity of > 2" do + it "only uses the first element if the arity > 2" do + set = Set["one", "two", "three", "four", "five"].divide do |x, y, z| + y.should be_nil + z.should be_nil + x.length + end + set.map { |x| x.to_a.sort }.sort.should == [["five", "four"], ["one", "two"], ["three"]] + end + + it "only uses the first element if the arity = -1" do + set = Set["one", "two", "three", "four", "five"].divide do |*xs| + xs.size.should == 1 + xs.first.length + end + set.map { |x| x.to_a.sort }.sort.should == [["five", "four"], ["one", "two"], ["three"]] + end +end