File: map_spec.rb

package info (click to toggle)
ruby-hamster 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,932 kB
  • sloc: ruby: 16,915; makefile: 4
file content (52 lines) | stat: -rw-r--r-- 1,434 bytes parent folder | download | duplicates (2)
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
require "spec_helper"
require "hamster/vector"

describe Hamster::Vector do
  [:map, :collect].each do |method|
    describe "##{method}" do
      context "when empty" do
        let(:vector) { V.empty }

        it "returns self" do
          vector.send(method) {}.should equal(vector)
        end
      end

      context "when not empty" do
        let(:vector) { V["A", "B", "C"] }

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

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

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

      context "from a subclass" do
        it "returns an instance of the subclass" do
          subclass = Class.new(Hamster::Vector)
          instance = subclass[1,2,3]
          instance.map { |x| x + 1 }.class.should be(subclass)
        end
      end

      context "on a large vector" do
        it "works" do
          V.new(1..2000).map { |x| x * 2 }.should eql(V.new((1..2000).map { |x| x * 2}))
        end
      end
    end
  end
end