memoizable 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require File.expand_path('../../fixtures/classes', __FILE__)
5
-
6
- describe Memoizable::MethodBuilder, '#call' do
7
- subject { object.call }
8
-
9
- let(:object) { described_class.new(descendant, method_name, freezer) }
10
- let(:freezer) { lambda { |object| object.freeze } }
11
- let(:instance) { descendant.new }
12
-
13
- let(:descendant) do
14
- Class.new do
15
- include Memoizable
16
-
17
- def public_method
18
- __method__.to_s
19
- end
20
-
21
- def protected_method
22
- __method__.to_s
23
- end
24
- protected :protected_method
25
-
26
- def private_method
27
- __method__.to_s
28
- end
29
- private :private_method
30
- end
31
- end
32
-
33
- shared_examples_for 'Memoizable::MethodBuilder#call' do
34
- it_should_behave_like 'a command method'
35
-
36
- it 'creates a method that is memoized' do
37
- subject
38
- expect(instance.send(method_name)).to be(instance.send(method_name))
39
- end
40
-
41
- it 'creates a method that returns the expected value' do
42
- subject
43
- expect(instance.send(method_name)).to eql(method_name.to_s)
44
- end
45
-
46
- it 'creates a method that returns a frozen value' do
47
- subject
48
- expect(descendant.new.send(method_name)).to be_frozen
49
- end
50
-
51
- it 'creates a method that does not accept a block' do
52
- subject
53
- expect { descendant.new.send(method_name) {} }.to raise_error(
54
- described_class::BlockNotAllowedError,
55
- "Cannot pass a block to #{descendant}##{method_name}, it is memoized"
56
- )
57
- end
58
- end
59
-
60
- context 'public method' do
61
- let(:method_name) { :public_method }
62
-
63
- it_should_behave_like 'Memoizable::MethodBuilder#call'
64
-
65
- it 'creates a public memoized method' do
66
- subject
67
- expect(descendant).to be_public_method_defined(method_name)
68
- end
69
- end
70
-
71
- context 'protected method' do
72
- let(:method_name) { :protected_method }
73
-
74
- it_should_behave_like 'Memoizable::MethodBuilder#call'
75
-
76
- it 'creates a protected memoized method' do
77
- subject
78
- expect(descendant).to be_protected_method_defined(method_name)
79
- end
80
-
81
- end
82
-
83
- context 'private method' do
84
- let(:method_name) { :private_method }
85
-
86
- it_should_behave_like 'Memoizable::MethodBuilder#call'
87
-
88
- it 'creates a private memoized method' do
89
- subject
90
- expect(descendant).to be_private_method_defined(method_name)
91
- end
92
- end
93
- end
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require File.expand_path('../../../fixtures/classes', __FILE__)
5
-
6
- describe Memoizable::MethodBuilder, '.new' do
7
- subject { described_class.new(descendant, method_name, freezer) }
8
-
9
- let(:descendant) { Fixture::Object }
10
- let(:freezer) { lambda { |object| object.freeze } }
11
-
12
- context 'with a zero arity method' do
13
- let(:method_name) { :zero_arity }
14
-
15
- it { should be_instance_of(described_class) }
16
-
17
- it 'sets the original method' do
18
- # original method is not memoized
19
- method = subject.original_method.bind(descendant.new)
20
- expect(method.call).to_not be(method.call)
21
- end
22
- end
23
-
24
- context 'with a one arity method' do
25
- let(:method_name) { :one_arity }
26
-
27
- it 'raises an exception' do
28
- expect { subject }.to raise_error(
29
- described_class::InvalidArityError,
30
- 'Cannot memoize Fixture::Object#one_arity, its arity is 1'
31
- )
32
- end
33
- end
34
- end
@@ -1,31 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Memoizable::MethodBuilder, '#original_method' do
6
- subject { object.original_method }
7
-
8
- let(:object) { described_class.new(descendant, method_name, freezer) }
9
- let(:method_name) { :foo }
10
- let(:freezer) { lambda { |object| object.freeze } }
11
-
12
- let(:descendant) do
13
- Class.new do
14
- def initialize
15
- @foo = 0
16
- end
17
-
18
- def foo
19
- @foo += 1
20
- end
21
- end
22
- end
23
-
24
- it { should be_instance_of(UnboundMethod) }
25
-
26
- it 'returns the original method' do
27
- # original method is not memoized
28
- method = subject.bind(descendant.new)
29
- expect(method.call).to_not be(method.call)
30
- end
31
- end
@@ -1,23 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Memoizable::ModuleMethods, '#included' do
6
- subject { descendant.instance_exec(object) { |mod| include mod } }
7
-
8
- let(:object) { Module.new.extend(described_class) }
9
- let(:descendant) { Class.new }
10
- let(:superclass) { Module }
11
-
12
- before do
13
- # Prevent Module.included from being called through inheritance
14
- allow(Memoizable).to receive(:included)
15
- end
16
-
17
- it_behaves_like 'it calls super', :included
18
-
19
- it 'includes Memoizable into the descendant' do
20
- subject
21
- expect(descendant.included_modules).to include(Memoizable)
22
- end
23
- end
@@ -1,123 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require File.expand_path('../../fixtures/classes', __FILE__)
5
-
6
- shared_examples_for 'memoizes method' do
7
- it 'memoizes the instance method' do
8
- subject
9
- instance = object.new
10
- expect(instance.send(method)).to be(instance.send(method))
11
- end
12
-
13
- it 'creates a zero arity method', :unless => RUBY_VERSION == '1.8.7' do
14
- subject
15
- expect(object.new.method(method).arity).to be_zero
16
- end
17
-
18
- context 'when the initializer calls the memoized method' do
19
- before do
20
- method = self.method
21
- object.send(:define_method, :initialize) { send(method) }
22
- end
23
-
24
- it 'allows the memoized method to be called within the initializer' do
25
- subject
26
- expect { object.new }.to_not raise_error
27
- end
28
- end
29
- end
30
-
31
- describe Memoizable::ModuleMethods, '#memoize' do
32
- subject { object.memoize(method) }
33
-
34
- let(:object) do
35
- stub_const 'TestClass', Class.new(Fixture::Object) {
36
- def some_state
37
- Object.new
38
- end
39
- }
40
- end
41
-
42
- context 'on method with required arguments' do
43
- let(:method) { :required_arguments }
44
-
45
- it 'should raise error' do
46
- expect { subject }.to raise_error(
47
- Memoizable::MethodBuilder::InvalidArityError,
48
- 'Cannot memoize TestClass#required_arguments, its arity is 1'
49
- )
50
- end
51
- end
52
-
53
- context 'on method with optional arguments' do
54
- let(:method) { :optional_arguments }
55
-
56
- it 'should raise error' do
57
- expect { subject }.to raise_error(
58
- Memoizable::MethodBuilder::InvalidArityError,
59
- 'Cannot memoize TestClass#optional_arguments, its arity is -1'
60
- )
61
- end
62
- end
63
-
64
- context 'memoized method that returns generated values' do
65
- let(:method) { :some_state }
66
-
67
- it_should_behave_like 'a command method'
68
- it_should_behave_like 'memoizes method'
69
-
70
- it 'creates a method that returns a frozen value' do
71
- subject
72
- expect(object.new.send(method)).to be_frozen
73
- end
74
- end
75
-
76
- context 'public method' do
77
- let(:method) { :public_method }
78
-
79
- it_should_behave_like 'a command method'
80
- it_should_behave_like 'memoizes method'
81
-
82
- it 'is still a public method' do
83
- should be_public_method_defined(method)
84
- end
85
-
86
- it 'creates a method that returns a frozen value' do
87
- subject
88
- expect(object.new.send(method)).to be_frozen
89
- end
90
- end
91
-
92
- context 'protected method' do
93
- let(:method) { :protected_method }
94
-
95
- it_should_behave_like 'a command method'
96
- it_should_behave_like 'memoizes method'
97
-
98
- it 'is still a protected method' do
99
- should be_protected_method_defined(method)
100
- end
101
-
102
- it 'creates a method that returns a frozen value' do
103
- subject
104
- expect(object.new.send(method)).to be_frozen
105
- end
106
- end
107
-
108
- context 'private method' do
109
- let(:method) { :private_method }
110
-
111
- it_should_behave_like 'a command method'
112
- it_should_behave_like 'memoizes method'
113
-
114
- it 'is still a private method' do
115
- should be_private_method_defined(method)
116
- end
117
-
118
- it 'creates a method that returns a frozen value' do
119
- subject
120
- expect(object.new.send(method)).to be_frozen
121
- end
122
- end
123
- end
@@ -1,28 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Memoizable::ModuleMethods, '#memoized?' do
6
- let(:object) do
7
- Class.new do
8
- include Memoizable
9
- def foo
10
- end
11
- memoize :foo
12
- end
13
- end
14
-
15
- subject { object.memoized?(name) }
16
-
17
- context 'with memoized method' do
18
- let(:name) { :foo }
19
-
20
- it { should be(true) }
21
- end
22
-
23
- context 'with non memoized method' do
24
- let(:name) { :bar }
25
-
26
- it { should be(false) }
27
- end
28
- end
@@ -1,43 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Memoizable::ModuleMethods, '#unmemoized_instance_method' do
6
- subject { object.unmemoized_instance_method(name) }
7
-
8
- let(:object) do
9
- Class.new do
10
- include Memoizable
11
-
12
- def initialize
13
- @foo = 0
14
- end
15
-
16
- def foo
17
- @foo += 1
18
- end
19
-
20
- memoize :foo
21
- end
22
- end
23
-
24
- context 'when the method was memoized' do
25
- let(:name) { :foo }
26
-
27
- it { should be_instance_of(UnboundMethod) }
28
-
29
- it 'returns the original method' do
30
- # original method is not memoized
31
- method = subject.bind(object.new)
32
- expect(method.call).to_not be(method.call)
33
- end
34
- end
35
-
36
- context 'when the method was not memoized' do
37
- let(:name) { :bar }
38
-
39
- it 'raises an exception' do
40
- expect { subject }.to raise_error(NameError, 'No method bar is memoized')
41
- end
42
- end
43
- end