summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerator/product/initialize_copy_spec.rb
blob: 46e8421322a5632cbea88034c894a65f56a7ddbc (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
require_relative '../../../spec_helper'

ruby_version_is "3.2" do
  describe "Enumerator::Product#initialize_copy" do
    it "replaces content of the receiver with content of the other object" do
      enum = Enumerator::Product.new([true, false])
      enum2 = Enumerator::Product.new([1, 2], [:a, :b])

      enum.send(:initialize_copy, enum2)
      enum.each.to_a.should == [[1, :a], [1, :b], [2, :a], [2, :b]]
    end

    it "returns self" do
      enum = Enumerator::Product.new([true, false])
      enum2 = Enumerator::Product.new([1, 2], [:a, :b])

      enum.send(:initialize_copy, enum2).should.equal?(enum)
    end

    it "is a private method" do
      Enumerator::Product.should have_private_instance_method(:initialize_copy, false)
    end

    it "does nothing if the argument is the same as the receiver" do
      enum = Enumerator::Product.new(1..2)
      enum.send(:initialize_copy, enum).should.equal?(enum)

      enum.freeze
      enum.send(:initialize_copy, enum).should.equal?(enum)
    end

    it "raises FrozenError if the receiver is frozen" do
      enum = Enumerator::Product.new(1..2)
      enum2 = Enumerator::Product.new(3..4)

      -> { enum.freeze.send(:initialize_copy, enum2) }.should raise_error(FrozenError)
    end

    it "raises TypeError if the objects are of different class" do
      enum = Enumerator::Product.new(1..2)
      enum2 = Class.new(Enumerator::Product).new(3..4)

      -> { enum.send(:initialize_copy, enum2) }.should raise_error(TypeError, 'initialize_copy should take same class object')
      -> { enum2.send(:initialize_copy, enum) }.should raise_error(TypeError, 'initialize_copy should take same class object')
    end

    it "raises ArgumentError if the argument is not initialized yet" do
      enum = Enumerator::Product.new(1..2)
      enum2 = Enumerator::Product.allocate

      -> { enum.send(:initialize_copy, enum2) }.should raise_error(ArgumentError, 'uninitialized product')
    end
  end
end