File: call_spec.rb

package info (click to toggle)
ruby-memoizable 0.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: ruby: 605; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download | duplicates (3)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# encoding: utf-8

require 'spec_helper'
require File.expand_path('../../fixtures/classes', __FILE__)

describe Memoizable::MethodBuilder, '#call' do
  subject { object.call }

  let(:object)   { described_class.new(descendant, method_name, freezer) }
  let(:freezer)  { lambda { |object| object.freeze }                     }
  let(:instance) { descendant.new                                        }

  let(:descendant) do
    Class.new do
      include Memoizable

      def public_method
        __method__.to_s
      end

      def protected_method
        __method__.to_s
      end
      protected :protected_method

      def private_method
        __method__.to_s
      end
      private :private_method
    end
  end

  shared_examples_for 'Memoizable::MethodBuilder#call' do
    it_should_behave_like 'a command method'

    it 'creates a method that is memoized' do
      subject
      expect(instance.send(method_name)).to be(instance.send(method_name))
    end

    it 'creates a method that returns the expected value' do
      subject
      expect(instance.send(method_name)).to eql(method_name.to_s)
    end

    it 'creates a method that returns a frozen value' do
      subject
      expect(descendant.new.send(method_name)).to be_frozen
    end

    it 'creates a method that does not accept a block' do
      subject
      expect { descendant.new.send(method_name) {} }.to raise_error(
        described_class::BlockNotAllowedError,
        "Cannot pass a block to #{descendant}##{method_name}, it is memoized"
      )
    end
  end

  context 'public method' do
    let(:method_name) { :public_method }

    it_should_behave_like 'Memoizable::MethodBuilder#call'

    it 'creates a public memoized method' do
      subject
      expect(descendant).to be_public_method_defined(method_name)
    end
  end

  context 'protected method' do
    let(:method_name) { :protected_method }

    it_should_behave_like 'Memoizable::MethodBuilder#call'

    it 'creates a protected memoized method' do
      subject
      expect(descendant).to be_protected_method_defined(method_name)
    end

  end

  context 'private method' do
    let(:method_name) { :private_method }

    it_should_behave_like 'Memoizable::MethodBuilder#call'

    it 'creates a private memoized method' do
      subject
      expect(descendant).to be_private_method_defined(method_name)
    end
  end
end