File: initialize_spec.rb

package info (click to toggle)
ruby2.7 2.7.4-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,576 kB
  • sloc: ruby: 849,454; ansic: 697,834; yacc: 45,100; xml: 25,367; pascal: 10,051; javascript: 6,575; sh: 3,848; makefile: 759; cpp: 713; asm: 333; python: 295; lisp: 97; sed: 94; perl: 62; awk: 36
file content (63 lines) | stat: -rw-r--r-- 1,883 bytes parent folder | download
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
57
58
59
60
61
62
63
# -*- encoding: us-ascii -*-

require_relative '../../../spec_helper'

describe "Enumerator::Lazy#initialize" do
  before :each do
    @receiver = receiver = Object.new

    def receiver.each
      yield 0
      yield 1
      yield 2
    end

    @uninitialized = Enumerator::Lazy.allocate
  end

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

  it "returns self" do
    @uninitialized.send(:initialize, @receiver) {}.should equal(@uninitialized)
  end

  describe "when the returned lazy enumerator is evaluated by Enumerable#first" do
    it "stops after specified times" do
      @uninitialized.send(:initialize, @receiver) do |yielder, *values|
        yielder.<<(*values)
      end.first(2).should == [0, 1]
    end
  end

  it "sets #size to nil if not given a size" do
    @uninitialized.send(:initialize, @receiver) {}.size.should be_nil
  end

  it "sets #size to nil if given size is nil" do
    @uninitialized.send(:initialize, @receiver, nil) {}.size.should be_nil
  end

  it "sets given size to own size if the given size is Float::INFINITY" do
    @uninitialized.send(:initialize, @receiver, Float::INFINITY) {}.size.should equal(Float::INFINITY)
  end

  it "sets given size to own size if the given size is a Fixnum" do
    @uninitialized.send(:initialize, @receiver, 100) {}.size.should == 100
  end

  it "sets given size to own size if the given size is a Proc" do
    @uninitialized.send(:initialize, @receiver, -> { 200 }) {}.size.should == 200
  end

  it "raises an ArgumentError when block is not given" do
    -> {  @uninitialized.send :initialize, @receiver }.should raise_error(ArgumentError)
  end

  describe "on frozen instance" do
    it "raises a RuntimeError" do
      -> {  @uninitialized.freeze.send(:initialize, @receiver) {} }.should raise_error(RuntimeError)
    end
  end
end