File: flat_map_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 (35 lines) | stat: -rw-r--r-- 1,154 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
30
31
32
33
34
35
require 'spec_helper'

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

  describe '#flat_map' do
    it 'yields each key/val pair' do
      passed = []
      hash.flat_map { |pair| passed << pair }
      passed.sort.should == [['A', 'aye'], ['B', 'bee'], ['C', 'see']]
    end

    it 'returns the concatenation of block return values' do
      hash.flat_map { |k,v| [k,v] }.sort.should == ['A', 'B', 'C', 'aye', 'bee', 'see']
      hash.flat_map { |k,v| L[k,v] }.sort.should == ['A', 'B', 'C', 'aye', 'bee', 'see']
      hash.flat_map { |k,v| V[k,v] }.sort.should == ['A', 'B', 'C', 'aye', 'bee', 'see']
    end

    it "doesn't change the receiver" do
      hash.flat_map { |k,v| [k,v] }
      hash.should eql(H['A' => 'aye', 'B' => 'bee', 'C' => 'see'])
    end

    context 'with no block' do
      it 'returns an Enumerator' do
        hash.flat_map.class.should be(Enumerator)
        hash.flat_map.each { |k,v| [k] }.sort.should == ['A', 'B', 'C']
      end
    end

    it 'returns an empty array if only empty arrays are returned by block' do
      hash.flat_map { [] }.should eql([])
    end
  end
end