File: initialize_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (62 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (4)
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
require_relative '../../spec_helper'

describe "Queue#initialize" do
  it "can be passed no arguments for an empty Queue" do
    q = Queue.new
    q.size.should == 0
    q.should.empty?
  end

  it "is a private method" do
    Queue.private_instance_methods.include?(:initialize).should == true
  end

  ruby_version_is '3.1' do
    it "adds all elements of the passed Enumerable to self" do
      q = Queue.new([1, 2, 3])
      q.size.should == 3
      q.should_not.empty?
      q.pop.should == 1
      q.pop.should == 2
      q.pop.should == 3
      q.should.empty?
    end

    describe "converts the given argument to an Array using #to_a" do
      it "uses #to_a on the provided Enumerable" do
        enumerable = MockObject.new('mock-enumerable')
        enumerable.should_receive(:to_a).and_return([1, 2, 3])
        q = Queue.new(enumerable)
        q.size.should == 3
        q.should_not.empty?
        q.pop.should == 1
        q.pop.should == 2
        q.pop.should == 3
        q.should.empty?
      end

      it "raises a TypeError if the given argument can't be converted to an Array" do
        -> { Queue.new(42) }.should raise_error(TypeError)
        -> { Queue.new(:abc) }.should raise_error(TypeError)
      end

      it "raises a NoMethodError if the given argument raises a NoMethodError during type coercion to an Array" do
        enumerable = MockObject.new('mock-enumerable')
        enumerable.should_receive(:to_a).and_raise(NoMethodError)
        -> { Queue.new(enumerable) }.should raise_error(NoMethodError)
      end
    end

    it "raises TypeError if the provided Enumerable does not respond to #to_a" do
      enumerable = MockObject.new('mock-enumerable')
      -> { Queue.new(enumerable) }.should raise_error(TypeError, "can't convert MockObject into Array")
    end

    it "raises TypeError if #to_a does not return Array" do
      enumerable = MockObject.new('mock-enumerable')
      enumerable.should_receive(:to_a).and_return("string")

      -> { Queue.new(enumerable) }.should raise_error(TypeError, "can't convert MockObject to Array (MockObject#to_a gives String)")
    end
  end
end