memoizable 0.4.2 → 0.5.0

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,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