Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend specs of Set#divide #1049

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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