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 (29 lines) | stat: -rw-r--r-- 958 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
29
require 'spec_helper'

describe Immutable::Hash do
  describe '#each_with_index' do
    let(:hash) { H['A' => 'aye', 'B' => 'bee', 'C' => 'see'] }

    describe 'with a block (internal iteration)' do
      it 'returns self' do
        hash.each_with_index {}.should be(hash)
      end

      it 'yields all key/value pairs with numeric indexes' do
        actual_pairs = {}
        indexes = []
        hash.each_with_index { |(key, value), index| actual_pairs[key] = value; indexes << index }
        actual_pairs.should == { 'A' => 'aye', 'B' => 'bee', 'C' => 'see' }
        indexes.sort.should == [0, 1, 2]
      end
    end

    describe 'with no block' do
      it 'returns an Enumerator' do
        hash.each_with_index.should be_kind_of(Enumerator)
        hash.each_with_index.to_a.map(&:first).sort.should eql([['A', 'aye'], ['B', 'bee'], ['C', 'see']])
        hash.each_with_index.to_a.map(&:last).should eql([0,1,2])
      end
    end
  end
end