Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 213 lines (174 sloc) 6.743 kB
957c2c9 @brixen Standardize require lines.
brixen authored
1 require File.expand_path('../../../spec_helper', __FILE__)
2 require File.expand_path('../fixtures/classes', __FILE__)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
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
fd45f5e @brixen Do not use 1.9.2 in version strings.
brixen authored
14 ruby_version_is ""..."1.9" do
056a162 @ujihisa Fixing Array#flatten for 1.9.2 * See [ruby-core:23168]
ujihisa authored
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
fd45f5e @brixen Do not use 1.9.2 in version strings.
brixen authored
21 ruby_version_is "1.9" do
056a162 @ujihisa Fixing Array#flatten for 1.9.2 * See [ruby-core:23168]
ujihisa authored
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 #t…
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
3ccbfd1 @BanzaiMan Avoid result.class.should == Class
BanzaiMan authored
84 ArraySpecs::MyArray[].flatten.should be_kind_of(ArraySpecs::MyArray)
85 ArraySpecs::MyArray[1, 2, 3].flatten.should be_kind_of(ArraySpecs::MyArray)
86 ArraySpecs::MyArray[1, [2], 3].flatten.should be_kind_of(ArraySpecs::MyArray)
87 [ArraySpecs::MyArray[1, 2, 3]].flatten.should be_kind_of(Array)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
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
7958b29 @ujihisa Bug fix by r23361
ujihisa authored
121 ruby_bug "redmine #1440", "1.9.1" do
8d5f92c @marcandre 1.8.7: Array#flatten! now reflects redmine bug #1440
marcandre authored
122 it "returns nil when the level of recursion is 0" do
123 a = [ 1, 2, [3, [4, 5] ] ]
124 a.flatten!(0).should == nil
125 end
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
126 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
127
1306fa2 @ujihisa Fixed ambiguous spec
ujihisa authored
128 it "treats negative levels as no arguments" do
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
129 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten!(-1).should == [1, 2, 3, 4, 5, 6]
130 [ 1, 2, [ 3, 4, [5, 6] ] ].flatten!(-10).should == [1, 2, 3, 4, 5, 6]
131 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
132
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
133 it "tries to convert passed Objects to Integers using #to_int" do
134 obj = mock("Converted to Integer")
135 obj.should_receive(:to_int).and_return(1)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
136
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
137 [ 1, 2, [3, [4, 5] ] ].flatten!(obj).should == [1, 2, 3, [4, 5]]
138 end
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
139
12e32c8 @arthurschreiber Updated Array#flatten and Array#flatten specs for Ruby 1.8.7
arthurschreiber authored
140 it "raises a TypeError when the passed Object can't be converted to an Integer" do
141 obj = mock("Not converted")
142 lambda { [ 1, 2, [3, [4, 5] ] ].flatten!(obj) }.should raise_error(TypeError)
143 end
144 end
145
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
146 it "does not call flatten! on elements" do
147 obj = mock('[1,2]')
148 obj.should_not_receive(:flatten!)
149 [obj, obj].flatten!.should == nil
150
151 obj = [5, 4]
152 obj.should_not_receive(:flatten!)
153 [obj, obj].flatten!.should == [5, 4, 5, 4]
154 end
155
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
156 it "raises an ArgumentError on recursive arrays" do
157 x = []
158 x << x
159 lambda { x.flatten! }.should raise_error(ArgumentError)
326a538 @brixen Fixed use of guards in the specs.
brixen authored
160
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
161 x = []
162 y = []
163 x << y
164 y << x
165 lambda { x.flatten! }.should raise_error(ArgumentError)
166 end
167
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
168 it "flattens any elements which responds to #to_ary, using the return value of said method" do
169 x = mock("[3,4]")
170 x.should_receive(:to_ary).at_least(:once).and_return([3, 4])
171 [1, 2, x, 5].flatten!.should == [1, 2, 3, 4, 5]
172
173 y = mock("MyArray[]")
174 y.should_receive(:to_ary).at_least(:once).and_return(ArraySpecs::MyArray[])
175 [y].flatten!.should == []
176
177 z = mock("[2,x,y,5]")
178 z.should_receive(:to_ary).and_return([2, x, y, 5])
179 [1, z, 6].flatten!.should == [1, 2, 3, 4, 5, 6]
180
181 ary = [ArraySpecs::MyArray[1, 2, 3]]
182 ary.flatten!
3ccbfd1 @BanzaiMan Avoid result.class.should == Class
BanzaiMan authored
183 ary.should be_kind_of(Array)
43da578 @yugui * adds 1.9 feature to the spec for Array#flatten and #flatten!.
yugui authored
184 ary.should == [1, 2, 3]
185 end
186
fd45f5e @brixen Do not use 1.9.2 in version strings.
brixen authored
187 ruby_version_is ""..."1.9" do
188 it "raises a TypeError on frozen arrays when the array is modified" do
326a538 @brixen Fixed use of guards in the specs.
brixen authored
189 nested_ary = [1, 2, []]
190 nested_ary.freeze
191 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
192 end
fd45f5e @brixen Do not use 1.9.2 in version strings.
brixen authored
193
194 it "does not raise on frozen arrays when the array would not be modified" do
1614ccc @runpaint Array#flatten! Guard frozen behav. [core:23663]
runpaint authored
195 ArraySpecs.frozen_array.flatten!.should be_nil
196 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
197 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
198
fd45f5e @brixen Do not use 1.9.2 in version strings.
brixen authored
199 ruby_version_is "1.9" do
200 it "raises a RuntimeError on frozen arrays when the array is modified" do
201 nested_ary = [1, 2, []]
202 nested_ary.freeze
203 lambda { nested_ary.flatten! }.should raise_error(RuntimeError)
204 end
205
206 # see [ruby-core:23663]
207 it "raises a RuntimeError on frozen arrays when the array would not be modified" do
208 lambda { ArraySpecs.frozen_array.flatten! }.should raise_error(RuntimeError)
f6ebbda @marcandre Add tests for mutative methods and empty frozen arrays.
marcandre authored
209 lambda { ArraySpecs.empty_frozen_array.flatten! }.should raise_error(RuntimeError)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
210 end
211 end
212 end
Something went wrong with that request. Please try again.