Skip to content

Commit

Permalink
Merge pull request #1049 from herwinw/set_divide
Browse files Browse the repository at this point in the history
Extend specs of Set#divide
  • Loading branch information
andrykonchin authored Aug 9, 2023
2 parents 0bc7813 + e15116b commit 958e32b
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions library/set/divide_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,4 +31,29 @@
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

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

0 comments on commit 958e32b

Please sign in to comment.