2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
2019-01-20 15:38:57 -05:00
|
|
|
require_relative 'fixtures/set_like'
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
2019-01-20 15:38:57 -05:00
|
|
|
|
|
|
|
context "when Set contains a Set-like object" do
|
|
|
|
it "returns a copy of self with each included Set-like object flattened" do
|
|
|
|
Set[SetSpecs::SetLike.new([1])].flatten.should == Set[1]
|
|
|
|
end
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
2019-01-20 15:38:57 -05:00
|
|
|
|
|
|
|
context "when Set contains a Set-like object" do
|
|
|
|
it "flattens self, including Set-like objects" do
|
|
|
|
Set[SetSpecs::SetLike.new([1])].flatten!.should == Set[1]
|
|
|
|
end
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|