1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
describe :array_slice, :shared => true do
it "returns the element at index with [index]" do
[ "a", "b", "c", "d", "e" ].send(@method, 1).should == "b"
a = [1, 2, 3, 4]
a.send(@method, 0).should == 1
a.send(@method, 1).should == 2
a.send(@method, 2).should == 3
a.send(@method, 3).should == 4
a.send(@method, 4).should == nil
a.send(@method, 10).should == nil
a.should == [1, 2, 3, 4]
end
it "returns the element at index from the end of the array with [-index]" do
[ "a", "b", "c", "d", "e" ].send(@method, -2).should == "d"
a = [1, 2, 3, 4]
a.send(@method, -1).should == 4
a.send(@method, -2).should == 3
a.send(@method, -3).should == 2
a.send(@method, -4).should == 1
a.send(@method, -5).should == nil
a.send(@method, -10).should == nil
a.should == [1, 2, 3, 4]
end
it "return count elements starting from index with [index, count]" do
[ "a", "b", "c", "d", "e" ].send(@method, 2, 3).should == ["c", "d", "e"]
a = [1, 2, 3, 4]
a.send(@method, 0, 0).should == []
a.send(@method, 0, 1).should == [1]
a.send(@method, 0, 2).should == [1, 2]
a.send(@method, 0, 4).should == [1, 2, 3, 4]
a.send(@method, 0, 6).should == [1, 2, 3, 4]
a.send(@method, 0, -1).should == nil
a.send(@method, 0, -2).should == nil
a.send(@method, 0, -4).should == nil
a.send(@method, 2, 0).should == []
a.send(@method, 2, 1).should == [3]
a.send(@method, 2, 2).should == [3, 4]
a.send(@method, 2, 4).should == [3, 4]
a.send(@method, 2, -1).should == nil
a.send(@method, 4, 0).should == []
a.send(@method, 4, 2).should == []
a.send(@method, 4, -1).should == nil
a.send(@method, 5, 0).should == nil
a.send(@method, 5, 2).should == nil
a.send(@method, 5, -1).should == nil
a.send(@method, 6, 0).should == nil
a.send(@method, 6, 2).should == nil
a.send(@method, 6, -1).should == nil
a.should == [1, 2, 3, 4]
end
it "returns count elements starting at index from the end of array with [-index, count]" do
[ "a", "b", "c", "d", "e" ].send(@method, -2, 2).should == ["d", "e"]
a = [1, 2, 3, 4]
a.send(@method, -1, 0).should == []
a.send(@method, -1, 1).should == [4]
a.send(@method, -1, 2).should == [4]
a.send(@method, -1, -1).should == nil
a.send(@method, -2, 0).should == []
a.send(@method, -2, 1).should == [3]
a.send(@method, -2, 2).should == [3, 4]
a.send(@method, -2, 4).should == [3, 4]
a.send(@method, -2, -1).should == nil
a.send(@method, -4, 0).should == []
a.send(@method, -4, 1).should == [1]
a.send(@method, -4, 2).should == [1, 2]
a.send(@method, -4, 4).should == [1, 2, 3, 4]
a.send(@method, -4, 6).should == [1, 2, 3, 4]
a.send(@method, -4, -1).should == nil
a.send(@method, -5, 0).should == nil
a.send(@method, -5, 1).should == nil
a.send(@method, -5, 10).should == nil
a.send(@method, -5, -1).should == nil
a.should == [1, 2, 3, 4]
end
it "returns the first count elements with [0, count]" do
[ "a", "b", "c", "d", "e" ].send(@method, 0, 3).should == ["a", "b", "c"]
end
it "returns the subarray which is independent to self with [index,count]" do
a = [1, 2, 3]
sub = a.send(@method, 1,2)
sub.replace([:a, :b])
a.should == [1, 2, 3]
end
it "tries to convert the passed argument to an Integer using #to_int" do
obj = mock('to_int')
obj.stub!(:to_int).and_return(2)
a = [1, 2, 3, 4]
a.send(@method, obj).should == 3
a.send(@method, obj, 1).should == [3]
a.send(@method, obj, obj).should == [3, 4]
a.send(@method, 0, obj).should == [1, 2]
end
it "returns the elements specified by Range indexes with [m..n]" do
[ "a", "b", "c", "d", "e" ].send(@method, 1..3).should == ["b", "c", "d"]
[ "a", "b", "c", "d", "e" ].send(@method, 4..-1).should == ['e']
[ "a", "b", "c", "d", "e" ].send(@method, 3..3).should == ['d']
[ "a", "b", "c", "d", "e" ].send(@method, 3..-2).should == ['d']
['a'].send(@method, 0..-1).should == ['a']
a = [1, 2, 3, 4]
a.send(@method, 0..-10).should == []
a.send(@method, 0..0).should == [1]
a.send(@method, 0..1).should == [1, 2]
a.send(@method, 0..2).should == [1, 2, 3]
a.send(@method, 0..3).should == [1, 2, 3, 4]
a.send(@method, 0..4).should == [1, 2, 3, 4]
a.send(@method, 0..10).should == [1, 2, 3, 4]
a.send(@method, 2..-10).should == []
a.send(@method, 2..0).should == []
a.send(@method, 2..2).should == [3]
a.send(@method, 2..3).should == [3, 4]
a.send(@method, 2..4).should == [3, 4]
a.send(@method, 3..0).should == []
a.send(@method, 3..3).should == [4]
a.send(@method, 3..4).should == [4]
a.send(@method, 4..0).should == []
a.send(@method, 4..4).should == []
a.send(@method, 4..5).should == []
a.send(@method, 5..0).should == nil
a.send(@method, 5..5).should == nil
a.send(@method, 5..6).should == nil
a.should == [1, 2, 3, 4]
end
it "returns elements specified by Range indexes except the element at index n with [m...n]" do
[ "a", "b", "c", "d", "e" ].send(@method, 1...3).should == ["b", "c"]
a = [1, 2, 3, 4]
a.send(@method, 0...-10).should == []
a.send(@method, 0...0).should == []
a.send(@method, 0...1).should == [1]
a.send(@method, 0...2).should == [1, 2]
a.send(@method, 0...3).should == [1, 2, 3]
a.send(@method, 0...4).should == [1, 2, 3, 4]
a.send(@method, 0...10).should == [1, 2, 3, 4]
a.send(@method, 2...-10).should == []
a.send(@method, 2...0).should == []
a.send(@method, 2...2).should == []
a.send(@method, 2...3).should == [3]
a.send(@method, 2...4).should == [3, 4]
a.send(@method, 3...0).should == []
a.send(@method, 3...3).should == []
a.send(@method, 3...4).should == [4]
a.send(@method, 4...0).should == []
a.send(@method, 4...4).should == []
a.send(@method, 4...5).should == []
a.send(@method, 5...0).should == nil
a.send(@method, 5...5).should == nil
a.send(@method, 5...6).should == nil
a.should == [1, 2, 3, 4]
end
it "returns elements that exist if range start is in the array but range end is not with [m..n]" do
[ "a", "b", "c", "d", "e" ].send(@method, 4..7).should == ["e"]
end
it "accepts Range instances having a negative m and both signs for n with [m..n] and [m...n]" do
a = [1, 2, 3, 4]
a.send(@method, -1..-1).should == [4]
a.send(@method, -1...-1).should == []
a.send(@method, -1..3).should == [4]
a.send(@method, -1...3).should == []
a.send(@method, -1..4).should == [4]
a.send(@method, -1...4).should == [4]
a.send(@method, -1..10).should == [4]
a.send(@method, -1...10).should == [4]
a.send(@method, -1..0).should == []
a.send(@method, -1..-4).should == []
a.send(@method, -1...-4).should == []
a.send(@method, -1..-6).should == []
a.send(@method, -1...-6).should == []
a.send(@method, -2..-2).should == [3]
a.send(@method, -2...-2).should == []
a.send(@method, -2..-1).should == [3, 4]
a.send(@method, -2...-1).should == [3]
a.send(@method, -2..10).should == [3, 4]
a.send(@method, -2...10).should == [3, 4]
a.send(@method, -4..-4).should == [1]
a.send(@method, -4..-2).should == [1, 2, 3]
a.send(@method, -4...-2).should == [1, 2]
a.send(@method, -4..-1).should == [1, 2, 3, 4]
a.send(@method, -4...-1).should == [1, 2, 3]
a.send(@method, -4..3).should == [1, 2, 3, 4]
a.send(@method, -4...3).should == [1, 2, 3]
a.send(@method, -4..4).should == [1, 2, 3, 4]
a.send(@method, -4...4).should == [1, 2, 3, 4]
a.send(@method, -4...4).should == [1, 2, 3, 4]
a.send(@method, -4..0).should == [1]
a.send(@method, -4...0).should == []
a.send(@method, -4..1).should == [1, 2]
a.send(@method, -4...1).should == [1]
a.send(@method, -5..-5).should == nil
a.send(@method, -5...-5).should == nil
a.send(@method, -5..-4).should == nil
a.send(@method, -5..-1).should == nil
a.send(@method, -5..10).should == nil
a.should == [1, 2, 3, 4]
end
it "returns the subarray which is independent to self with [m..n]" do
a = [1, 2, 3]
sub = a.send(@method, 1..2)
sub.replace([:a, :b])
a.should == [1, 2, 3]
end
it "tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]" do
from = mock('from')
to = mock('to')
# So we can construct a range out of them...
def from.<=>(o) 0 end
def to.<=>(o) 0 end
def from.to_int() 1 end
def to.to_int() -2 end
a = [1, 2, 3, 4]
a.send(@method, from..to).should == [2, 3]
a.send(@method, from...to).should == [2]
a.send(@method, 1..0).should == []
a.send(@method, 1...0).should == []
lambda { a.slice("a" .. "b") }.should raise_error(TypeError)
lambda { a.slice("a" ... "b") }.should raise_error(TypeError)
lambda { a.slice(from .. "b") }.should raise_error(TypeError)
lambda { a.slice(from ... "b") }.should raise_error(TypeError)
end
it "returns the same elements as [m..n] and [m...n] with Range subclasses" do
a = [1, 2, 3, 4]
range_incl = ArraySpecs::MyRange.new(1, 2)
range_excl = ArraySpecs::MyRange.new(-3, -1, true)
a[range_incl].should == [2, 3]
a[range_excl].should == [2, 3]
end
it "returns nil for a requested index not in the array with [index]" do
[ "a", "b", "c", "d", "e" ].send(@method, 5).should == nil
end
it "returns [] if the index is valid but length is zero with [index, length]" do
[ "a", "b", "c", "d", "e" ].send(@method, 0, 0).should == []
[ "a", "b", "c", "d", "e" ].send(@method, 2, 0).should == []
end
it "returns nil if length is zero but index is invalid with [index, length]" do
[ "a", "b", "c", "d", "e" ].send(@method, 100, 0).should == nil
[ "a", "b", "c", "d", "e" ].send(@method, -50, 0).should == nil
end
# This is by design. It is in the official documentation.
it "returns [] if index == array.size with [index, length]" do
%w|a b c d e|.send(@method, 5, 2).should == []
end
it "returns nil if index > array.size with [index, length]" do
%w|a b c d e|.send(@method, 6, 2).should == nil
end
it "returns nil if length is negative with [index, length]" do
%w|a b c d e|.send(@method, 3, -1).should == nil
%w|a b c d e|.send(@method, 2, -2).should == nil
%w|a b c d e|.send(@method, 1, -100).should == nil
end
it "returns nil if no requested index is in the array with [m..n]" do
[ "a", "b", "c", "d", "e" ].send(@method, 6..10).should == nil
end
it "returns nil if range start is not in the array with [m..n]" do
[ "a", "b", "c", "d", "e" ].send(@method, -10..2).should == nil
[ "a", "b", "c", "d", "e" ].send(@method, 10..12).should == nil
end
it "returns an empty array when m == n with [m...n]" do
[1, 2, 3, 4, 5].send(@method, 1...1).should == []
end
it "returns an empty array with [0...0]" do
[1, 2, 3, 4, 5].send(@method, 0...0).should == []
end
it "returns a subarray where m, n negatives and m < n with [m..n]" do
[ "a", "b", "c", "d", "e" ].send(@method, -3..-2).should == ["c", "d"]
end
it "returns an array containing the first element with [0..0]" do
[1, 2, 3, 4, 5].send(@method, 0..0).should == [1]
end
it "returns the entire array with [0..-1]" do
[1, 2, 3, 4, 5].send(@method, 0..-1).should == [1, 2, 3, 4, 5]
end
it "returns all but the last element with [0...-1]" do
[1, 2, 3, 4, 5].send(@method, 0...-1).should == [1, 2, 3, 4]
end
it "returns [3] for [2..-1] out of [1, 2, 3]" do
[1,2,3].send(@method, 2..-1).should == [3]
end
it "returns an empty array when m > n and m, n are positive with [m..n]" do
[1, 2, 3, 4, 5].send(@method, 3..2).should == []
end
it "returns an empty array when m > n and m, n are negative with [m..n]" do
[1, 2, 3, 4, 5].send(@method, -2..-3).should == []
end
it "does not expand array when the indices are outside of the array bounds" do
a = [1, 2]
a.send(@method, 4).should == nil
a.should == [1, 2]
a.send(@method, 4, 0).should == nil
a.should == [1, 2]
a.send(@method, 6, 1).should == nil
a.should == [1, 2]
a.send(@method, 8...8).should == nil
a.should == [1, 2]
a.send(@method, 10..10).should == nil
a.should == [1, 2]
end
describe "with a subclass of Array" do
before :each do
ScratchPad.clear
@array = ArraySpecs::MyArray[1, 2, 3, 4, 5]
end
it "returns a subclass instance with [n, m]" do
@array.send(@method, 0, 2).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n, m]" do
@array.send(@method, -3, 2).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [n..m]" do
@array.send(@method, 1..3).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [n...m]" do
@array.send(@method, 1...3).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n..-m]" do
@array.send(@method, -3..-1).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n...-m]" do
@array.send(@method, -3...-1).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns an empty array when m == n with [m...n]" do
@array.send(@method, 1...1).should == []
ScratchPad.recorded.should be_nil
end
it "returns an empty array with [0...0]" do
@array.send(@method, 0...0).should == []
ScratchPad.recorded.should be_nil
end
it "returns an empty array when m > n and m, n are positive with [m..n]" do
@array.send(@method, 3..2).should == []
ScratchPad.recorded.should be_nil
end
it "returns an empty array when m > n and m, n are negative with [m..n]" do
@array.send(@method, -2..-3).should == []
ScratchPad.recorded.should be_nil
end
it "returns [] if index == array.size with [index, length]" do
@array.send(@method, 5, 2).should == []
ScratchPad.recorded.should be_nil
end
it "returns [] if the index is valid but length is zero with [index, length]" do
@array.send(@method, 0, 0).should == []
@array.send(@method, 2, 0).should == []
ScratchPad.recorded.should be_nil
end
it "does not call #initialize on the subclass instance" do
@array.send(@method, 0, 3).should == [1, 2, 3]
ScratchPad.recorded.should be_nil
end
end
not_compliant_on :rubinius do
it "raises a RangeError when the start index is out of range of Fixnum" do
array = [1, 2, 3, 4, 5, 6]
obj = mock('large value')
obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
lambda { array.send(@method, obj) }.should raise_error(RangeError)
obj = 8e19
lambda { array.send(@method, obj) }.should raise_error(RangeError)
end
it "raises a RangeError when the length is out of range of Fixnum" do
array = [1, 2, 3, 4, 5, 6]
obj = mock('large value')
obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
lambda { array.send(@method, 1, obj) }.should raise_error(RangeError)
obj = 8e19
lambda { array.send(@method, 1, obj) }.should raise_error(RangeError)
end
end
deviates_on :rubinius do
it "raises a TypeError when the start index is out of range of Fixnum" do
array = [1, 2, 3, 4, 5, 6]
obj = mock('large value')
obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
lambda { array.send(@method, obj) }.should raise_error(TypeError)
obj = 8e19
lambda { array.send(@method, obj) }.should raise_error(TypeError)
end
it "raises a TypeError when the length is out of range of Fixnum" do
array = [1, 2, 3, 4, 5, 6]
obj = mock('large value')
obj.should_receive(:to_int).and_return(0x8000_0000_0000_0000_0000)
lambda { array.send(@method, 1, obj) }.should raise_error(TypeError)
obj = 8e19
lambda { array.send(@method, 1, obj) }.should raise_error(TypeError)
end
end
end
|