Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 206 lines (168 sloc) 6.434 kb
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/classes'
3
4 describe "Array#flatten" do
5 it "returns a one-dimensional flattening recursively" do
6 [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []].flatten.should == [1, 2, 3, 2, 3, 4, 4, 5, 5, 1, 2, 3, 4]
7 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
8
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
9 ruby_version_is "1.8.7" do
10 it "takes an optional argument that determines the level of recursion" do
11 [ 1, 2, [3, [4, 5] ] ].flatten(1).should == [1, 2, 3, [4, 5]]
12 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
13
056a162 @ujihisa Fixing Array#flatten for 1.9.2 * See [ruby-core:23168]
ujihisa authored
14 ruby_version_is ""..."1.9.2" do
15 it "returns self when the level of recursion is 0" do
16 a = [ 1, 2, [3, [4, 5] ] ]
17 a.flatten(0).should equal(a)
18 end
19 end
20
21 ruby_version_is "1.9.2" do
22 it "returns dup when the level of recursion is 0" do
23 a = [ 1, 2, [3, [4, 5] ] ]
24 a.flatten(0).should == a
25 a.flatten(0).should_not equal(a)
26 end
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
27 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
28
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
29 it "ignores negative levels" do
30 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten(-1).should == [1, 2, 3, 4, 5, 6]
31 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten(-10).should == [1, 2, 3, 4, 5, 6]
32 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
33
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
34 it "tries to convert passed Objects to Integers using #to_int" do
35 obj = mock("Converted to Integer")
36 obj.should_receive(:to_int).and_return(1)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
37
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
38 [ 1, 2, [3, [4, 5] ] ].flatten(obj).should == [1, 2, 3, [4, 5]]
39 end
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
40
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
41 it "raises a TypeError when the passed Object can't be converted to an Integer" do
42 obj = mock("Not converted")
43 lambda { [ 1, 2, [3, [4, 5] ] ].flatten(obj) }.should raise_error(TypeError)
44 end
45 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
46
47 it "does not call flatten on elements" do
48 obj = mock('[1,2]')
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
49 obj.should_not_receive(:flatten)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
50 [obj, obj].flatten.should == [obj, obj]
51
52 obj = [5, 4]
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
53 obj.should_not_receive(:flatten)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
54 [obj, obj].flatten.should == [5, 4, 5, 4]
55 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
56
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
57 it "raises an ArgumentError on recursive arrays" do
58 x = []
59 x << x
60 lambda { x.flatten }.should raise_error(ArgumentError)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
61
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
62 x = []
63 y = []
64 x << y
65 y << x
66 lambda { x.flatten }.should raise_error(ArgumentError)
67 end
738df6b @mernen Added specs for Array#flatten flattening anything that responds to #to_a...
mernen authored
68
69 it "flattens any element which responds to #to_ary, using the return value of said method" do
70 x = mock("[3,4]")
71 x.should_receive(:to_ary).at_least(:once).and_return([3, 4])
72 [1, 2, x, 5].flatten.should == [1, 2, 3, 4, 5]
73
74 y = mock("MyArray[]")
75 y.should_receive(:to_ary).at_least(:once).and_return(ArraySpecs::MyArray[])
76 [y].flatten.should == []
77
78 z = mock("[2,x,y,5]")
79 z.should_receive(:to_ary).and_return([2, x, y, 5])
80 [1, z, 6].flatten.should == [1, 2, 3, 4, 5, 6]
81 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
82
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
83 it "returns subclass instance for Array subclasses" do
84 ArraySpecs::MyArray[].flatten.class.should == ArraySpecs::MyArray
85 ArraySpecs::MyArray[1, 2, 3].flatten.class.should == ArraySpecs::MyArray
86 ArraySpecs::MyArray[1, [2], 3].flatten.class.should == ArraySpecs::MyArray
87 [ArraySpecs::MyArray[1, 2, 3]].flatten.class.should == Array
88 end
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
89
90 it "is not destructive" do
91 ary = [1, [2, 3]]
92 ary.flatten
93 ary.should == [1, [2, 3]]
94 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
95 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
96
97 describe "Array#flatten!" do
98 it "modifies array to produce a one-dimensional flattening recursively" do
99 a = [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []]
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
100 a.flatten!
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
101 a.should == [1, 2, 3, 2, 3, 4, 4, 5, 5, 1, 2, 3, 4]
102 end
103
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
104 it "returns self if made some modifications" do
105 a = [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []]
106 a.flatten!.should equal(a)
107 end
108
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
109 it "returns nil if no modifications took place" do
110 a = [1, 2, 3]
111 a.flatten!.should == nil
17f9fd5 Adding all relevant missing items from JRuby's test/test_array.rb test.
Charles Oliver Nutter authored
112 a = [1, [2, 3]]
113 a.flatten!.should_not == nil
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
114 end
115
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
116 ruby_version_is "1.8.7" do
117 it "takes an optional argument that determines the level of recursion" do
118 [ 1, 2, [3, [4, 5] ] ].flatten!(1).should == [1, 2, 3, [4, 5]]
119 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
120
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
121 # NOTE: This is inconsistent behaviour, it should return nil
122 it "returns self when the level of recursion is 0" do
123 a = [ 1, 2, [3, [4, 5] ] ]
124 a.flatten!(0).should equal(a)
125 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
126
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
127 it "ignores negative levels" do
128 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten!(-1).should == [1, 2, 3, 4, 5, 6]
129 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten!(-10).should == [1, 2, 3, 4, 5, 6]
130 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
131
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
132 it "tries to convert passed Objects to Integers using #to_int" do
133 obj = mock("Converted to Integer")
134 obj.should_receive(:to_int).and_return(1)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
135
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
136 [ 1, 2, [3, [4, 5] ] ].flatten!(obj).should == [1, 2, 3, [4, 5]]
137 end
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
138
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
139 it "raises a TypeError when the passed Object can't be converted to an Integer" do
140 obj = mock("Not converted")
141 lambda { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should raise_error(TypeError)
142 end
143 end
144
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
145 it "does not call flatten! on elements" do
146 obj = mock('[1,2]')
147 obj.should_not_receive(:flatten!)
148 [obj, obj].flatten!.should == nil
149
150 obj = [5, 4]
151 obj.should_not_receive(:flatten!)
152 [obj, obj].flatten!.should == [5, 4, 5, 4]
153 end
154
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
155 it "raises an ArgumentError on recursive arrays" do
156 x = []
157 x << x
158 lambda { x.flatten! }.should raise_error(ArgumentError)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
159
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
160 x = []
161 y = []
162 x << y
163 y << x
164 lambda { x.flatten! }.should raise_error(ArgumentError)
165 end
166
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
167 it "flattens any elements which responds to #to_ary, using the return value of said method" do
168 x = mock("[3,4]")
169 x.should_receive(:to_ary).at_least(:once).and_return([3, 4])
170 [1, 2, x, 5].flatten!.should == [1, 2, 3, 4, 5]
171
172 y = mock("MyArray[]")
173 y.should_receive(:to_ary).at_least(:once).and_return(ArraySpecs::MyArray[])
174 [y].flatten!.should == []
175
176 z = mock("[2,x,y,5]")
177 z.should_receive(:to_ary).and_return([2, x, y, 5])
178 [1, z, 6].flatten!.should == [1, 2, 3, 4, 5, 6]
179
180 ary = [ArraySpecs::MyArray[1, 2, 3]]
181 ary.flatten!
182 ary.class.should == Array
183 ary.should == [1, 2, 3]
184 end
185
326a538 @brixen Fixed use of guards in the specs.
brixen authored
186 ruby_version_is '' ... '1.9' do
187 it "raises a TypeError on frozen arrays when modification would take place" do
188 nested_ary = [1, 2, []]
189 nested_ary.freeze
190 lambda { nested_ary.flatten! }.should raise_error(TypeError)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
191 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
192 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
193
326a538 @brixen Fixed use of guards in the specs.
brixen authored
194 ruby_version_is '1.9' do
195 it "raises a RuntimeError on frozen arrays when modification would take place" do
196 nested_ary = [1, 2, []]
197 nested_ary.freeze
198 lambda { nested_ary.flatten! }.should raise_error(RuntimeError)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
199 end
200 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
201
202 it "does not raise on frozen arrays when no modification would take place" do
203 ArraySpecs.frozen_array.flatten!.should be_nil
204 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
205 end
Something went wrong with that request. Please try again.