require 'spec_helper'

describe Array do

  describe '#in_size' do

    it 'truncates an array to the given size' do
      [1, 2, 3, 4, 5].in_size(3).should == [1, 2, 3]
    end

    it 'grows an array to the given size, filling empty slots with nil' do
      [1, 2, 3].in_size(5).should == [1, 2, 3, nil, nil]
    end

    it 'fills empty slots with a given value, if present' do
      [1, 2, 3].in_size(5, 'hello').should == [1, 2, 3, 'hello', 'hello']
    end

    it 'works when it already is of the expected size' do
      [1, 2, 3, 4, 5].in_size(5).should == [1, 2, 3, 4, 5]
    end

    it 'does not modify the array itself' do
      x = [1, 2, 3, 4, 5]

      # shrinking should work, but not modify x
      x.in_size(3).should == [1, 2, 3]
      x.should == [1, 2, 3, 4, 5]

      # growing should work, but not modify x
      x.in_size(7).should == [1, 2, 3, 4, 5, nil, nil]
      x.should == [1, 2, 3, 4, 5]
    end

  end

end
