File: constructor_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (119 lines) | stat: -rw-r--r-- 4,120 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Hash.[]" do
  describe "passed zero arguments" do
    it "returns an empty hash" do
      hash_class[].should == new_hash
    end
  end

  it "creates a Hash; values can be provided as the argument list" do
    hash_class[:a, 1, :b, 2].should == new_hash(:a => 1, :b => 2)
    hash_class[].should == new_hash
    hash_class[:a, 1, :b, new_hash(:c => 2)].should ==
      new_hash(:a => 1, :b => new_hash(:c => 2))
  end

  it "creates a Hash; values can be provided as one single hash" do
    hash_class[:a => 1, :b => 2].should == new_hash(:a => 1, :b => 2)
    hash_class[new_hash(1 => 2, 3 => 4)].should == new_hash(1 => 2, 3 => 4)
    hash_class[new_hash].should == new_hash
  end

  ruby_version_is '1.8.7' do
    describe "passed an array" do
      it "treats elements that are 2 element arrays as key and value" do
        hash_class[[[:a, :b], [:c, :d]]].should == new_hash(:a => :b, :c => :d)
      end

      it "treats elements that are 1 element arrays as keys with value nil" do
        hash_class[[[:a]]].should == new_hash(:a => nil)
      end
    end

    ruby_bug "#1000 #1385", "1.8.7.167" do
      it "creates a Hash; values can be provided as a list of value-pairs in an array" do
        hash_class[[[:a, 1], [:b, 2]]].should == new_hash(:a => 1, :b => 2)
      end

      it "coerces a single argument which responds to #to_ary" do
        ary = mock('to_ary')
        ary.should_receive(:to_ary).and_return([[:a, :b]])

        hash_class[ary].should == new_hash(:a => :b)
      end
    end
  end

  it "ignores elements that are not arrays" do
    hash_class[[:a]].should == new_hash
    hash_class[[:nil]].should == new_hash
  end

  ruby_version_is '1.8.7'...'2.0' do
    it "ignores elements that are arrays of more than 2 elements" do
      hash_class[[[:a, :b, :c]]].should == new_hash
    end

    ruby_bug "#1000 #1385", "1.8.7.167" do
      it "creates a Hash; values can be provided as a list of value-invalid-pairs in an array" do
        hash_class[[[:a, 1], [:b], 42, [:d, 2], [:e, 2, 3], []]].should == new_hash(:a => 1, :b => nil, :d => 2)
      end
    end
  end

  ruby_version_is '2.0' do
    it "raises an ArgumentError for arrays of more than 2 elements" do
      lambda{ hash_class[[[:a, :b, :c]]].should == new_hash }.should raise_error(ArgumentError)
    end

    it "raises an ArgumentError when passed a list of value-invalid-pairs in an array" do
      lambda{ hash_class[[[:a, 1], [:b], 42, [:d, 2], [:e, 2, 3], []]] }.should raise_error(ArgumentError)
    end
  end

  ruby_version_is '1.8.7' do
    describe "passed a single argument which responds to #to_hash" do
      it "coerces it and returns a copy" do
        h = new_hash(:a => :b, :c => :d)
        to_hash = mock('to_hash')
        to_hash.should_receive(:to_hash).and_return(h)

        result = hash_class[to_hash]
        result.should == h
        result.should_not equal(h)
      end
    end
  end

  it "raises an ArgumentError when passed an odd number of arguments" do
    lambda { hash_class[1, 2, 3] }.should raise_error(ArgumentError)
    lambda { hash_class[1, 2, new_hash(3 => 4)] }.should raise_error(ArgumentError)
  end

  ruby_version_is '1.8.7' do
    it "calls to_hash" do
      obj = mock('x')
      def obj.to_hash() new_hash(1 => 2, 3 => 4) end
      hash_class[obj].should == new_hash(1 => 2, 3 => 4)
    end

    it "returns an instance of a subclass when passed an Array" do
      HashSpecs::MyHash[1,2,3,4].should be_kind_of(HashSpecs::MyHash)
    end
  end

  it "returns instances of subclasses" do
    HashSpecs::MyHash[].should be_an_instance_of(HashSpecs::MyHash)
  end

  it "returns an instance of the class it's called on" do
    hash_class[HashSpecs::MyHash[1, 2]].class.should == hash_class
    HashSpecs::MyHash[hash_class[1, 2]].should be_kind_of(HashSpecs::MyHash)
  end

  it "does not call #initialize on the subclass instance" do
    HashSpecs::MyInitializerHash[hash_class[1, 2]].should be_kind_of(HashSpecs::MyInitializerHash)
  end
end