Sha256: 63e92dc576b0233a080e49623ed81e4ee5072a041160bac1b8ea9dfbb4a01321

Contents?: true

Size: 1.76 KB

Versions: 58

Compression:

Stored size: 1.76 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Array#unshift" do
  it "prepends object to the original array" do
    a = [1, 2, 3]
    a.unshift("a").should equal(a)
    a.should == ['a', 1, 2, 3]
    a.unshift().should equal(a)
    a.should == ['a', 1, 2, 3]
    a.unshift(5, 4, 3)
    a.should == [5, 4, 3, 'a', 1, 2, 3]

    # shift all but one element
    a = [1, 2]
    a.shift
    a.unshift(3, 4)
    a.should == [3, 4, 2]

    # now shift all elements
    a.shift
    a.shift
    a.shift
    a.unshift(3, 4)
    a.should == [3, 4]
  end

  it "quietly ignores unshifting nothing" do
    [].unshift().should == []
    [].unshift(*[]).should == []
  end

  it "properly handles recursive arrays" do
    empty = ArraySpecs.empty_recursive_array
    empty.unshift(:new).should == [:new, empty]

    array = ArraySpecs.recursive_array
    array.unshift(:new)
    array[0..5].should == [:new, 1, 'two', 3.0, array, array]
  end

  ruby_version_is "" ... "1.9" do
    it "raises a TypeError on a frozen array" do
      lambda { ArraySpecs.frozen_array.unshift(1) }.should raise_error(TypeError)
    end
  end

  ruby_version_is "1.9" do
    it "raises a RuntimeError on a frozen array when the array is modified" do
      lambda { ArraySpecs.frozen_array.unshift(1) }.should raise_error(RuntimeError)
    end

    # see [ruby-core:23666]
    it "raises a RuntimeError on a frozen array when the array would not be modified" do
      lambda { ArraySpecs.frozen_array.unshift    }.should raise_error(RuntimeError)
    end
  end

  ruby_version_is ""..."1.9" do
    it "does not raise an exception on a frozen array if no modification takes place" do
      ArraySpecs.frozen_array.unshift.should == [1, 2, 3]
    end
  end
end

Version data entries

58 entries across 58 versions & 3 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/array/unshift_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/array/unshift_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/array/unshift_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/array/unshift_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/array/unshift_spec.rb
opal-0.4.4 spec/rubyspec/core/array/unshift_spec.rb
opal-0.4.3 spec/rubyspec/core/array/unshift_spec.rb
opal-0.4.2 spec/rubyspec/core/array/unshift_spec.rb