File: 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 (59 lines) | stat: -rw-r--r-- 1,692 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'spec_helper'

describe Immutable::Set do
  [:map, :collect].each do |method|
    describe "##{method}" do
      context 'when empty' do
        it 'returns self' do
          S.empty.send(method) {}.should equal(S.empty)
        end
      end

      context 'when not empty' do
        let(:set) { S['A', 'B', 'C'] }

        context 'with a block' do
          it 'preserves the original values' do
            set.send(method, &:downcase)
            set.should eql(S['A', 'B', 'C'])
          end

          it 'returns a new set with the mapped values' do
            set.send(method, &:downcase).should eql(S['a', 'b', 'c'])
          end
        end

        context 'with no block' do
          it 'returns an Enumerator' do
            set.send(method).class.should be(Enumerator)
            set.send(method).each(&:downcase).should == S['a', 'b', 'c']
          end
        end
      end

      context 'from a subclass' do
        it 'returns an instance of the subclass' do
          subclass = Class.new(Immutable::Set)
          instance = subclass['a', 'b']
          instance.map(&:upcase).class.should be(subclass)
        end
      end

      context 'when multiple items map to the same value' do
        it 'filters out the duplicates' do
          set = S.new('aa'..'zz')
          result = set.map { |s| s[0] }
          result.should eql(Immutable::Set.new('a'..'z'))
          result.size.should == 26
        end
      end

      it 'works on large sets' do
        set = S.new(1..1000)
        result = set.map { |x| x * 10 }
        result.size.should == 1000
        1.upto(1000) { |n| result.include?(n * 10).should == true }
      end
    end
  end
end