File: each_with_index_spec.rb

package info (click to toggle)
ruby-immutable-ruby 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 16,556; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 784 bytes parent folder | download
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
require 'spec_helper'

describe Immutable::List do
  describe '#each_with_index' do
    context 'with no block' do
      let(:list) { L['A', 'B', 'C'] }

      it 'returns an Enumerator' do
        list.each_with_index.class.should be(Enumerator)
        list.each_with_index.to_a.should == [['A', 0], ['B', 1], ['C', 2]]
      end
    end

    context 'with a block' do
      let(:list) { Immutable.interval(1, 1025) }

      it 'returns self' do
        list.each_with_index { |item, index| item }.should be(list)
      end

      it 'iterates over the items in order, yielding item and index' do
        yielded = []
        list.each_with_index { |item, index| yielded << [item, index] }
        yielded.should == (1..list.size).zip(0..list.size.pred)
      end
    end
  end
end