|
| 1 | +require_relative '../../spec_helper' |
| 2 | +require_relative '../enumerable/fixtures/classes' |
| 3 | + |
| 4 | +describe "Enumerable#to_set" do |
| 5 | + it "returns a new Set created from self" do |
| 6 | + (1..4).to_set.should == Set[1, 2, 3, 4] |
| 7 | + (1...4).to_set.should == Set[1, 2, 3] |
| 8 | + end |
| 9 | + |
| 10 | + it "passes down passed blocks" do |
| 11 | + (1..3).to_set { |x| x * x }.should == Set[1, 4, 9] |
| 12 | + end |
| 13 | + |
| 14 | + ruby_version_is "4.0" do |
| 15 | + it "instantiates an object of provided as the first argument set class" do |
| 16 | + set = nil |
| 17 | + proc{set = (1..3).to_set(EnumerableSpecs::SetSubclass)}.should complain(/Enumerable#to_set/) |
| 18 | + set.should be_kind_of(EnumerableSpecs::SetSubclass) |
| 19 | + set.to_a.sort.should == [1, 2, 3] |
| 20 | + end |
| 21 | + |
| 22 | + it "raises a RangeError if the range is infinite" do |
| 23 | + -> { (1..).to_set }.should raise_error(RangeError, "cannot convert endless range to a set") |
| 24 | + -> { (1...).to_set }.should raise_error(RangeError, "cannot convert endless range to a set") |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + ruby_version_is ""..."4.0" do |
| 29 | + it "instantiates an object of provided as the first argument set class" do |
| 30 | + set = (1..3).to_set(EnumerableSpecs::SetSubclass) |
| 31 | + set.should be_kind_of(EnumerableSpecs::SetSubclass) |
| 32 | + set.to_a.sort.should == [1, 2, 3] |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it "does not need explicit `require 'set'`" do |
| 37 | + output = ruby_exe(<<~RUBY, options: '--disable-gems', args: '2>&1') |
| 38 | + puts (1..3).to_set.to_a.inspect |
| 39 | + RUBY |
| 40 | + |
| 41 | + output.chomp.should == "[1, 2, 3]" |
| 42 | + end |
| 43 | +end |
0 commit comments