1 shared :array_slice do |cmd|
2 describe "Array##{cmd}" do
3 it "returns the element at index with [index]" do
4 [ "a", "b", "c", "d", "e" ].send(cmd, 1).should == "b"
8 a.send(cmd, 0).should == 1
9 a.send(cmd, 1).should == 2
10 a.send(cmd, 2).should == 3
11 a.send(cmd, 3).should == 4
12 a.send(cmd, 4).should == nil
13 a.send(cmd, 10).should == nil
15 a.should == [1, 2, 3, 4]
18 it "returns the element at index from the end of the array with [-index]" do
19 [ "a", "b", "c", "d", "e" ].send(cmd, -2).should == "d"
23 a.send(cmd, -1).should == 4
24 a.send(cmd, -2).should == 3
25 a.send(cmd, -3).should == 2
26 a.send(cmd, -4).should == 1
27 a.send(cmd, -5).should == nil
28 a.send(cmd, -10).should == nil
30 a.should == [1, 2, 3, 4]
33 it "return count elements starting from index with [index, count]" do
34 [ "a", "b", "c", "d", "e" ].send(cmd, 2, 3).should == ["c", "d", "e"]
38 a.send(cmd, 0, 0).should == []
39 a.send(cmd, 0, 1).should == [1]
40 a.send(cmd, 0, 2).should == [1, 2]
41 a.send(cmd, 0, 4).should == [1, 2, 3, 4]
42 a.send(cmd, 0, 6).should == [1, 2, 3, 4]
43 a.send(cmd, 0, -1).should == nil
44 a.send(cmd, 0, -2).should == nil
45 a.send(cmd, 0, -4).should == nil
47 a.send(cmd, 2, 0).should == []
48 a.send(cmd, 2, 1).should == [3]
49 a.send(cmd, 2, 2).should == [3, 4]
50 a.send(cmd, 2, 4).should == [3, 4]
51 a.send(cmd, 2, -1).should == nil
53 a.send(cmd, 4, 0).should == []
54 a.send(cmd, 4, 2).should == []
55 a.send(cmd, 4, -1).should == nil
57 a.send(cmd, 5, 0).should == nil
58 a.send(cmd, 5, 2).should == nil
59 a.send(cmd, 5, -1).should == nil
61 a.send(cmd, 6, 0).should == nil
62 a.send(cmd, 6, 2).should == nil
63 a.send(cmd, 6, -1).should == nil
65 a.should == [1, 2, 3, 4]
68 it "returns count elements starting at index from the end of array with [-index, count]" do
69 [ "a", "b", "c", "d", "e" ].send(cmd, -2, 2).should == ["d", "e"]
73 a.send(cmd, -1, 0).should == []
74 a.send(cmd, -1, 1).should == [4]
75 a.send(cmd, -1, 2).should == [4]
76 a.send(cmd, -1, -1).should == nil
78 a.send(cmd, -2, 0).should == []
79 a.send(cmd, -2, 1).should == [3]
80 a.send(cmd, -2, 2).should == [3, 4]
81 a.send(cmd, -2, 4).should == [3, 4]
82 a.send(cmd, -2, -1).should == nil
84 a.send(cmd, -4, 0).should == []
85 a.send(cmd, -4, 1).should == [1]
86 a.send(cmd, -4, 2).should == [1, 2]
87 a.send(cmd, -4, 4).should == [1, 2, 3, 4]
88 a.send(cmd, -4, 6).should == [1, 2, 3, 4]
89 a.send(cmd, -4, -1).should == nil
91 a.send(cmd, -5, 0).should == nil
92 a.send(cmd, -5, 1).should == nil
93 a.send(cmd, -5, 10).should == nil
94 a.send(cmd, -5, -1).should == nil
96 a.should == [1, 2, 3, 4]
99 it "returns the first count elements with [0, count]" do
100 [ "a", "b", "c", "d", "e" ].send(cmd, 0, 3).should == ["a", "b", "c"]
103 it "tries to convert the passed argument to an Integer using #to_int" do
105 obj.stub!(:to_int).and_return(2)
108 a.send(cmd, obj).should == 3
109 a.send(cmd, obj, 1).should == [3]
110 a.send(cmd, obj, obj).should == [3, 4]
111 a.send(cmd, 0, obj).should == [1, 2]
114 ruby_version_is "" ... "1.8.7" do
115 it "checks whether index and count respond to #to_int with [index, count]" do
116 obj = mock('method_missing to_int')
117 obj.should_receive(:respond_to?).with(:to_int).any_number_of_times.and_return(true)
118 obj.should_receive(:method_missing).with(:to_int).and_return(2, 2)
119 [1, 2, 3, 4].send(cmd, obj, obj).should == [3, 4]
123 ruby_version_is "1.8.7" do
124 it "checks whether index and count respond to #to_int (including private methods) with [index, count]" do
125 obj = mock('method_missing to_int')
126 obj.should_receive(:respond_to?).with(:to_int, true).any_number_of_times.and_return(true)
127 obj.should_receive(:method_missing).with(:to_int).and_return(2, 2)
128 [1, 2, 3, 4].send(cmd, obj, obj).should == [3, 4]
132 it "returns the elements specified by Range indexes with [m..n]" do
133 [ "a", "b", "c", "d", "e" ].send(cmd, 1..3).should == ["b", "c", "d"]
134 [ "a", "b", "c", "d", "e" ].send(cmd, 4..-1).should == ['e']
135 [ "a", "b", "c", "d", "e" ].send(cmd, 3..3).should == ['d']
136 [ "a", "b", "c", "d", "e" ].send(cmd, 3..-2).should == ['d']
137 ['a'].send(cmd, 0..-1).should == ['a']
141 a.send(cmd, 0..-10).should == []
142 a.send(cmd, 0..0).should == [1]
143 a.send(cmd, 0..1).should == [1, 2]
144 a.send(cmd, 0..2).should == [1, 2, 3]
145 a.send(cmd, 0..3).should == [1, 2, 3, 4]
146 a.send(cmd, 0..4).should == [1, 2, 3, 4]
147 a.send(cmd, 0..10).should == [1, 2, 3, 4]
149 a.send(cmd, 2..-10).should == []
150 a.send(cmd, 2..0).should == []
151 a.send(cmd, 2..2).should == [3]
152 a.send(cmd, 2..3).should == [3, 4]
153 a.send(cmd, 2..4).should == [3, 4]
155 a.send(cmd, 3..0).should == []
156 a.send(cmd, 3..3).should == [4]
157 a.send(cmd, 3..4).should == [4]
159 a.send(cmd, 4..0).should == []
160 a.send(cmd, 4..4).should == []
161 a.send(cmd, 4..5).should == []
163 a.send(cmd, 5..0).should == nil
164 a.send(cmd, 5..5).should == nil
165 a.send(cmd, 5..6).should == nil
167 a.should == [1, 2, 3, 4]
170 it "returns elements specified by Range indexes except the element at index n with [m...n]" do
171 [ "a", "b", "c", "d", "e" ].send(cmd, 1...3).should == ["b", "c"]
175 a.send(cmd, 0...-10).should == []
176 a.send(cmd, 0...0).should == []
177 a.send(cmd, 0...1).should == [1]
178 a.send(cmd, 0...2).should == [1, 2]
179 a.send(cmd, 0...3).should == [1, 2, 3]
180 a.send(cmd, 0...4).should == [1, 2, 3, 4]
181 a.send(cmd, 0...10).should == [1, 2, 3, 4]
183 a.send(cmd, 2...-10).should == []
184 a.send(cmd, 2...0).should == []
185 a.send(cmd, 2...2).should == []
186 a.send(cmd, 2...3).should == [3]
187 a.send(cmd, 2...4).should == [3, 4]
189 a.send(cmd, 3...0).should == []
190 a.send(cmd, 3...3).should == []
191 a.send(cmd, 3...4).should == [4]
193 a.send(cmd, 4...0).should == []
194 a.send(cmd, 4...4).should == []
195 a.send(cmd, 4...5).should == []
197 a.send(cmd, 5...0).should == nil
198 a.send(cmd, 5...5).should == nil
199 a.send(cmd, 5...6).should == nil
201 a.should == [1, 2, 3, 4]
204 it "returns elements that exist if range start is in the array but range end is not with [m..n]" do
205 [ "a", "b", "c", "d", "e" ].send(cmd, 4..7).should == ["e"]
208 it "accepts Range instances having a negative m and both signs for n with [m..n] and [m...n]" do
211 a.send(cmd, -1..-1).should == [4]
212 a.send(cmd, -1...-1).should == []
213 a.send(cmd, -1..3).should == [4]
214 a.send(cmd, -1...3).should == []
215 a.send(cmd, -1..4).should == [4]
216 a.send(cmd, -1...4).should == [4]
217 a.send(cmd, -1..10).should == [4]
218 a.send(cmd, -1...10).should == [4]
219 a.send(cmd, -1..0).should == []
220 a.send(cmd, -1..-4).should == []
221 a.send(cmd, -1...-4).should == []
222 a.send(cmd, -1..-6).should == []
223 a.send(cmd, -1...-6).should == []
225 a.send(cmd, -2..-2).should == [3]
226 a.send(cmd, -2...-2).should == []
227 a.send(cmd, -2..-1).should == [3, 4]
228 a.send(cmd, -2...-1).should == [3]
229 a.send(cmd, -2..10).should == [3, 4]
230 a.send(cmd, -2...10).should == [3, 4]
232 a.send(cmd, -4..-4).should == [1]
233 a.send(cmd, -4..-2).should == [1, 2, 3]
234 a.send(cmd, -4...-2).should == [1, 2]
235 a.send(cmd, -4..-1).should == [1, 2, 3, 4]
236 a.send(cmd, -4...-1).should == [1, 2, 3]
237 a.send(cmd, -4..3).should == [1, 2, 3, 4]
238 a.send(cmd, -4...3).should == [1, 2, 3]
239 a.send(cmd, -4..4).should == [1, 2, 3, 4]
240 a.send(cmd, -4...4).should == [1, 2, 3, 4]
241 a.send(cmd, -4...4).should == [1, 2, 3, 4]
242 a.send(cmd, -4..0).should == [1]
243 a.send(cmd, -4...0).should == []
244 a.send(cmd, -4..1).should == [1, 2]
245 a.send(cmd, -4...1).should == [1]
247 a.send(cmd, -5..-5).should == nil
248 a.send(cmd, -5...-5).should == nil
249 a.send(cmd, -5..-4).should == nil
250 a.send(cmd, -5..-1).should == nil
251 a.send(cmd, -5..10).should == nil
253 a.should == [1, 2, 3, 4]
256 it "tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]" do
260 # So we can construct a range out of them...
261 def from.<=>(o) 0 end
264 def from.to_int() 1 end
265 def to.to_int() -2 end
269 a.send(cmd, from..to).should == [2, 3]
270 a.send(cmd, from...to).should == [2]
271 a.send(cmd, 1..0).should == []
272 a.send(cmd, 1...0).should == []
274 lambda { a.slice("a" .. "b") }.should raise_error(TypeError)
275 lambda { a.slice("a" ... "b") }.should raise_error(TypeError)
276 lambda { a.slice(from .. "b") }.should raise_error(TypeError)
277 lambda { a.slice(from ... "b") }.should raise_error(TypeError)
280 ruby_version_is "" ... "1.8.7" do
281 it "checks whether the Range elements respond to #to_int with [m..n] and [m...n]" do
285 def from.<=>(o) 0 end
288 from.should_receive(:respond_to?).with(:to_int).any_number_of_times.and_return(true)
289 from.should_receive(:method_missing).with(:to_int).and_return(1)
291 to.should_receive(:respond_to?).with(:to_int).any_number_of_times.and_return(true)
292 to.should_receive(:method_missing).with(:to_int).and_return(-2)
294 [1, 2, 3, 4].send(cmd, from..to).should == [2, 3]
298 ruby_version_is "1.8.7" do
299 it "checks whether the Range elements respond to #to_int (including private methods) with [m..n] and [m...n]" do
303 def from.<=>(o) 0 end
306 from.should_receive(:respond_to?).with(:to_int, true).any_number_of_times.and_return(true)
307 from.should_receive(:method_missing).with(:to_int).and_return(1)
309 to.should_receive(:respond_to?).with(:to_int, true).any_number_of_times.and_return(true)
310 to.should_receive(:method_missing).with(:to_int).and_return(-2)
312 [1, 2, 3, 4].send(cmd, from..to).should == [2, 3]
316 it "returns the same elements as [m..n] and [m...n] with Range subclasses" do
318 range_incl = ArraySpecs::MyRange.new(1, 2)
319 range_excl = ArraySpecs::MyRange.new(-3, -1, true)
321 a[range_incl].should == [2, 3]
322 a[range_excl].should == [2, 3]
325 it "returns nil for a requested index not in the array with [index]" do
326 [ "a", "b", "c", "d", "e" ].send(cmd, 5).should == nil
329 it "returns [] if the index is valid but length is zero with [index, length]" do
330 [ "a", "b", "c", "d", "e" ].send(cmd, 0, 0).should == []
331 [ "a", "b", "c", "d", "e" ].send(cmd, 2, 0).should == []
334 it "returns nil if length is zero but index is invalid with [index, length]" do
335 [ "a", "b", "c", "d", "e" ].send(cmd, 100, 0).should == nil
336 [ "a", "b", "c", "d", "e" ].send(cmd, -50, 0).should == nil
339 # This is by design. It is in the official documentation.
340 it "returns [] if index == array.size with [index, length]" do
341 %w|a b c d e|.send(cmd, 5, 2).should == []
344 it "returns nil if index > array.size with [index, length]" do
345 %w|a b c d e|.send(cmd, 6, 2).should == nil
348 it "returns nil if length is negative with [index, length]" do
349 %w|a b c d e|.send(cmd, 3, -1).should == nil
350 %w|a b c d e|.send(cmd, 2, -2).should == nil
351 %w|a b c d e|.send(cmd, 1, -100).should == nil
354 it "returns nil if no requested index is in the array with [m..n]" do
355 [ "a", "b", "c", "d", "e" ].send(cmd, 6..10).should == nil
358 it "returns nil if range start is not in the array with [m..n]" do
359 [ "a", "b", "c", "d", "e" ].send(cmd, -10..2).should == nil
360 [ "a", "b", "c", "d", "e" ].send(cmd, 10..12).should == nil
363 it "returns an empty array when m == n with [m...n]" do
364 [1, 2, 3, 4, 5].send(cmd, 1...1).should == []
367 it "returns an empty array with [0...0]" do
368 [1, 2, 3, 4, 5].send(cmd, 0...0).should == []
371 it "returns a subarray where m, n negatives and m < n with [m..n]" do
372 [ "a", "b", "c", "d", "e" ].send(cmd, -3..-2).should == ["c", "d"]
375 it "returns an array containing the first element with [0..0]" do
376 [1, 2, 3, 4, 5].send(cmd, 0..0).should == [1]
379 it "returns the entire array with [0..-1]" do
380 [1, 2, 3, 4, 5].send(cmd, 0..-1).should == [1, 2, 3, 4, 5]
383 it "returns all but the last element with [0...-1]" do
384 [1, 2, 3, 4, 5].send(cmd, 0...-1).should == [1, 2, 3, 4]
387 it "returns [3] for [2..-1] out of [1, 2, 3] <Specifies bug found by brixen, Defiler, mae>" do
388 [1,2,3].send(cmd, 2..-1).should == [3]
391 it "returns an empty array when m > n and m, n are positive with [m..n]" do
392 [1, 2, 3, 4, 5].send(cmd, 3..2).should == []
395 it "returns an empty array when m > n and m, n are negative with [m..n]" do
396 [1, 2, 3, 4, 5].send(cmd, -2..-3).should == []
399 it "does not expand array when the indices are outside of the array bounds" do
401 a.send(cmd, 4).should == nil
403 a.send(cmd, 4, 0).should == nil
405 a.send(cmd, 6, 1).should == nil
407 a.send(cmd, 8...8).should == nil
409 a.send(cmd, 10..10).should == nil
413 it "returns a subclass instance when called on a subclass of Array" do
414 ary = ArraySpecs::MyArray[1, 2, 3]
415 ary.send(cmd, 0, 0).class.should == ArraySpecs::MyArray
416 ary.send(cmd, 0, 2).class.should == ArraySpecs::MyArray
417 ary.send(cmd, 0..10).class.should == ArraySpecs::MyArray
420 not_compliant_on :rubinius do
421 it "raises a RangeError when the start index is out of range of Fixnum" do
422 array = [1, 2, 3, 4, 5, 6]
423 obj = mock('large value')
424 obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
425 lambda { array.send(cmd, obj) }.should raise_error(RangeError)
428 lambda { array.send(cmd, obj) }.should raise_error(RangeError)
431 it "raises a RangeError when the length is out of range of Fixnum" do
432 array = [1, 2, 3, 4, 5, 6]
433 obj = mock('large value')
434 obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
435 lambda { array.send(cmd, 1, obj) }.should raise_error(RangeError)
438 lambda { array.send(cmd, 1, obj) }.should raise_error(RangeError)
442 deviates_on :rubinius do
443 it "raises a TypeError when the start index is out of range of Fixnum" do
444 array = [1, 2, 3, 4, 5, 6]
445 obj = mock('large value')
446 obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
447 lambda { array.send(cmd, obj) }.should raise_error(TypeError)
450 lambda { array.send(cmd, obj) }.should raise_error(TypeError)
453 it "raises a TypeError when the length is out of range of Fixnum" do
454 array = [1, 2, 3, 4, 5, 6]
455 obj = mock('large value')
456 obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
457 lambda { array.send(cmd, 1, obj) }.should raise_error(TypeError)
460 lambda { array.send(cmd, 1, obj) }.should raise_error(TypeError)