File: memoize_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 (56 lines) | stat: -rw-r--r-- 973 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
53
54
55
56
require "spec_helper"
require "hamster/immutable"

describe Hamster::Immutable do
  class Fixture
    include Hamster::Immutable

    def initialize(&block)
      @block = block
    end

    def call
      @block.call
    end
    memoize :call

    def copy
      transform {}
    end
  end

  let(:immutable) { Fixture.new { @count += 1 } }

  describe "#memoize" do
    before(:each) do
      @count = 0
      immutable.call
    end

    it "keeps the receiver frozen and immutable" do
      expect(immutable).to be_immutable
    end

    context "when called multiple times" do
      before(:each) do
        immutable.call
      end

      it "doesn't evaluate the memoized method more than once" do
        expect(@count).to eq(1)
      end
    end

    describe "when making a copy" do
      let(:copy) { immutable.copy }

      before(:each) do
        copy.call
      end

      it "clears all memory" do
        expect(@count).to eq(2)
      end
    end
  end
end