|
1 | 1 | require_relative '../../spec_helper' |
2 | 2 | require_relative 'fixtures/classes' |
3 | 3 |
|
4 | | -describe "Kernel" do |
5 | | - it "has private instance method Array()" do |
6 | | - Kernel.private_instance_methods(false).should.include?(:Array) |
7 | | - end |
8 | | -end |
9 | | - |
10 | | -describe :kernel_Array, shared: true do |
| 4 | +describe "Kernel#Array" do |
11 | 5 | before :each do |
12 | 6 | @array = [1, 2, 3] |
13 | 7 | end |
14 | 8 |
|
| 9 | + it "is a private method" do |
| 10 | + Kernel.private_instance_methods(false).should.include?(:Array) |
| 11 | + end |
| 12 | + |
15 | 13 | it "does not call #to_ary on an Array" do |
16 | 14 | @array.should_not_receive(:to_ary) |
17 | | - @object.send(@method, @array).should == @array |
| 15 | + Array(@array).should == @array |
18 | 16 | end |
19 | 17 |
|
20 | 18 | it "calls #to_ary to convert the argument to an Array" do |
21 | 19 | obj = mock("Array([1,2,3])") |
22 | 20 | obj.should_receive(:to_ary).and_return(@array) |
23 | 21 | obj.should_not_receive(:to_a) |
24 | 22 |
|
25 | | - @object.send(@method, obj).should == @array |
| 23 | + Array(obj).should == @array |
26 | 24 | end |
27 | 25 |
|
28 | 26 | it "does not call #to_a on an Array" do |
29 | 27 | @array.should_not_receive(:to_a) |
30 | | - @object.send(@method, @array).should == @array |
| 28 | + Array(@array).should == @array |
31 | 29 | end |
32 | 30 |
|
33 | 31 | it "calls #to_a if the argument does not respond to #to_ary" do |
34 | 32 | obj = mock("Array([1,2,3])") |
35 | 33 | obj.should_receive(:to_a).and_return(@array) |
36 | 34 |
|
37 | | - @object.send(@method, obj).should == @array |
| 35 | + Array(obj).should == @array |
38 | 36 | end |
39 | 37 |
|
40 | 38 | it "calls #to_a if #to_ary returns nil" do |
41 | 39 | obj = mock("Array([1,2,3])") |
42 | 40 | obj.should_receive(:to_ary).and_return(nil) |
43 | 41 | obj.should_receive(:to_a).and_return(@array) |
44 | 42 |
|
45 | | - @object.send(@method, obj).should == @array |
| 43 | + Array(obj).should == @array |
46 | 44 | end |
47 | 45 |
|
48 | 46 | it "returns an Array containing the argument if #to_a returns nil" do |
49 | 47 | obj = mock("Array([1,2,3])") |
50 | 48 | obj.should_receive(:to_a).and_return(nil) |
51 | 49 |
|
52 | | - @object.send(@method, obj).should == [obj] |
| 50 | + Array(obj).should == [obj] |
53 | 51 | end |
54 | 52 |
|
55 | 53 | it "calls #to_ary first, even if it's private" do |
56 | 54 | obj = KernelSpecs::PrivateToAry.new |
57 | 55 |
|
58 | | - @object.send(@method, obj).should == [1, 2] |
| 56 | + Array(obj).should == [1, 2] |
59 | 57 | end |
60 | 58 |
|
61 | 59 | it "calls #to_a if #to_ary is not defined, even if it's private" do |
62 | 60 | obj = KernelSpecs::PrivateToA.new |
63 | 61 |
|
64 | | - @object.send(@method, obj).should == [3, 4] |
| 62 | + Array(obj).should == [3, 4] |
65 | 63 | end |
66 | 64 |
|
67 | 65 | it "returns an Array containing the argument if it responds to neither #to_ary nor #to_a" do |
68 | 66 | obj = mock("Array(x)") |
69 | | - @object.send(@method, obj).should == [obj] |
| 67 | + Array(obj).should == [obj] |
70 | 68 | end |
71 | 69 |
|
72 | 70 | it "returns an empty Array when passed nil" do |
73 | | - @object.send(@method, nil).should == [] |
| 71 | + Array(nil).should == [] |
74 | 72 | end |
75 | 73 |
|
76 | 74 | it "raises a TypeError if #to_ary does not return an Array" do |
77 | 75 | obj = mock("Array() string") |
78 | 76 | obj.should_receive(:to_ary).and_return("string") |
79 | 77 |
|
80 | | - -> { @object.send(@method, obj) }.should.raise(TypeError) |
| 78 | + -> { Array(obj) }.should.raise(TypeError) |
81 | 79 | end |
82 | 80 |
|
83 | 81 | it "raises a TypeError if #to_a does not return an Array" do |
84 | 82 | obj = mock("Array() string") |
85 | 83 | obj.should_receive(:to_a).and_return("string") |
86 | 84 |
|
87 | | - -> { @object.send(@method, obj) }.should.raise(TypeError) |
| 85 | + -> { Array(obj) }.should.raise(TypeError) |
88 | 86 | end |
89 | 87 | end |
90 | 88 |
|
91 | 89 | describe "Kernel.Array" do |
92 | | - it_behaves_like :kernel_Array, :Array_method, KernelSpecs |
93 | | -end |
94 | | - |
95 | | -describe "Kernel#Array" do |
96 | | - it_behaves_like :kernel_Array, :Array_function, KernelSpecs |
| 90 | + it "is a public method" do |
| 91 | + Kernel.public_methods(false).should.include?(:Array) |
| 92 | + end |
97 | 93 | end |
0 commit comments