Sha256: edc9453a979013519bc9f3fe93802526e8e94bfec290d9d7827a2bd10c77a3bc

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'
require 'rext/array'

describe Array do
  describe "helpers" do
    
    before :each do
      @items = %w( some foo bar )  
    end
    
    describe "#sum" do
      it "should return the sum of the numbers in an array" do
        [1,2,3].sum.should == 6 
      end
      
      it "should return 0 when empty" do
        [].sum.should == 0
      end
    end
    
    describe "#avg" do
      it "should return the average of numbers in the array" do
        [3,3,6,6].avg.should == 4.5
      end
      
      it "should return 0 when empty" do
        [].avg.should == 0
      end
    end
    
    describe "#from" do
      it "should return elements after the position specified" do
        @items.from(1).should == %w( foo bar )
      end
    end
    
    describe "#to" do
      it "should return elements up to position" do
        @items.to(1).should == %w( some foo )
      end
    end
    
    describe "#chunk" do
      it "should split an array into several containing the length of n" do
        (1..9).to_a.chunk(3).should == [[1,2,3], [4,5,6], [7,8,9]]
      end
      
      it "should accept a block, yielding each chunk" do
        chunks = []
        (1..9).to_a.in_groups_of(3) do |chunk|
          chunks << chunk
        end
        chunks.should == [[1,2,3], [4,5,6], [7,8,9]]
      end
      
      it "should pad with nil by default" do
        (1..5).to_a.in_groups_of(4).should == [[1,2,3,4], [5,nil,nil,nil]]
      end
      
      it "should not padd when pad_with is false" do
        (1..6).to_a.in_groups_of(4, false).should == [[1,2,3,4], [5,6]]
      end
    end
    
    describe "#pad" do
      it "should pad with nil by default" do
        [1,2].pad(4).should == [1, 2, nil, nil]
      end
      
      it "should pad an with an object" do
        [1,2].pad(3 ,'x').should == [1, 2, 'x']
      end
      
      it "should do nothing when the array is of expected length" do
        [1,2].pad(2).should == [1,2]
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rext-0.7.0 spec/array_spec.rb