File: reduce_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,027 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
  [:reduce, :inject].each do |method|
    describe "##{method}" do
      context 'when empty' do
        it 'returns the memo' do
          H.empty.send(method, 'ABC') {}.should == 'ABC'
        end
      end

      context 'when not empty' do
        let(:hash) { H['A' => 'aye', 'B' => 'bee', 'C' => 'see'] }

        context 'with a block' do
          it 'returns the final memo' do
            hash.send(method, 0) { |memo, key, value| memo + 1 }.should == 3
          end
        end

        context 'with no block' do
          let(:hash) { H[a: 1, b: 2] }

          it 'uses a passed string as the name of a method to use instead' do
            [[:a, 1, :b, 2], [:b, 2, :a, 1]].include?(hash.send(method, '+')).should == true
          end

          it 'uses a passed symbol as the name of a method to use instead' do
            [[:a, 1, :b, 2], [:b, 2, :a, 1]].include?(hash.send(method, :+)).should == true
          end
        end
      end
    end
  end
end