Skip to content

HTTPS clone URL

Subversion checkout URL

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