summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerator/product/size_spec.rb
blob: 46958b1a229544f66496379ac6977a45509de38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require_relative '../../../spec_helper'

ruby_version_is "3.2" do
  describe "Enumerator::Product#size" do
    it "returns the total size of the enumerator product calculated by multiplying the sizes of enumerables in the product" do
      product = Enumerator::Product.new(1..2, 1..3, 1..4)
      product.size.should == 24 # 2 * 3 * 4
    end

    it "returns nil if any enumerable reports its size as nil" do
      enum = Object.new
      def enum.size; nil; end

      product = Enumerator::Product.new(1..2, enum)
      product.size.should == nil
    end

    it "returns Float::INFINITY if any enumerable reports its size as Float::INFINITY" do
      enum = Object.new
      def enum.size; Float::INFINITY; end

      product = Enumerator::Product.new(1..2, enum)
      product.size.should == Float::INFINITY
    end

    it "returns nil if any enumerable reports its size as Float::NAN" do
      enum = Object.new
      def enum.size; Float::NAN; end

      product = Enumerator::Product.new(1..2, enum)
      product.size.should == nil
    end

    it "returns nil if any enumerable doesn't respond to #size" do
      enum = Object.new
      product = Enumerator::Product.new(1..2, enum)
      product.size.should == nil
    end

    it "returns nil if any enumerable reports a not-convertible to Integer" do
      enum = Object.new
      def enum.size; :symbol; end

      product = Enumerator::Product.new(1..2, enum)
      product.size.should == nil
    end

    it "returns nil if any enumerable reports a non-Integer but convertible to Integer size" do
      enum = Object.new
      def enum.size; 1.0; end

      product = Enumerator::Product.new(1..2, enum)
      product.size.should == nil
    end
  end
end