-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmemoizable_spec.rb
More file actions
308 lines (246 loc) · 7.24 KB
/
memoizable_spec.rb
File metadata and controls
308 lines (246 loc) · 7.24 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# frozen_string_literal: true
require "concurrent/atomic/atomic_fixnum"
require "dry/core/memoizable"
require "tempfile"
require_relative "../../support/memoized"
RSpec.describe Dry::Core::Memoizable do
before do
Dry::Core::Deprecations.set_logger!(Tempfile.new("dry_deprecations"))
end
before { Memoized.memoize_methods }
describe ".memoize" do
describe Object do
it_behaves_like "a memoizable class" do
context "frozen object" do
before { object.freeze }
it "works" do
expect(object.foo).to be(object.foo)
end
end
end
end
describe BasicObject do
it_behaves_like "a memoizable class"
end
describe Class.new(Object) do
it_behaves_like "a memoizable class"
end
describe Class.new(BasicObject) do
it_behaves_like "a memoizable class"
end
end
describe Memoized.new do
let(:block) { -> {} }
describe "test1" do
it_behaves_like "a memoized method" do
let(:new_meth) { described_class.method(:test1) }
it "does not raise an error" do
2.times do
new_meth.("a", kwarg1: "world", other: "test", &block)
end
end
end
end
describe "test2" do
it_behaves_like "a memoized method" do
let(:new_meth) { described_class.method(:test2) }
it "does not raise an error" do
2.times { new_meth.("a", &block) }
end
end
end
describe "test3" do
it_behaves_like "a memoized method" do
let(:new_meth) { described_class.method(:test3) }
it "does not raise an error" do
2.times { new_meth.(&block) }
end
end
end
describe "test4" do
it_behaves_like "a memoized method" do
before { described_class.test4 }
let(:new_meth) { described_class.method(:test4) }
it "does not raise an error" do
2.times { new_meth.call }
end
end
end
end
describe ".new" do
let(:args) { [double("arg")] }
let(:kwargs) { {key: double("value")} }
let(:block) { -> { double("block") } }
let(:object) do
Class.new do
include Dry::Core::Memoizable
attr_reader :args, :kwargs, :block
def initialize(*args, **kwargs, &block)
@args = args
@kwargs = kwargs
@block = block
end
end.new(*args, **kwargs, &block)
end
describe "#args" do
subject { object.args }
it { is_expected.to eq(args) }
end
describe "#kwargs" do
subject { object.kwargs }
it { is_expected.to eq(kwargs) }
end
describe "#block" do
subject { object.block }
it { is_expected.to eq(block) }
end
end
context "test calls" do
context "a class including memoizable directly" do
let(:klass) { Class.new.include(Dry::Core::Memoizable) }
let(:instance) { klass.new }
let(:counter) { Concurrent::AtomicFixnum.new }
context "no args" do
before do
counter = self.counter
klass.define_method(:meth) { counter.increment }
klass.memoize(:meth)
end
it "gets called only once" do
instance.meth
instance.meth
instance.meth
expect(counter.value).to eql(1)
end
end
context "pos arg" do
before do
counter = self.counter
klass.define_method(:meth) { |req| counter.increment }
klass.memoize(:meth)
end
it "memoizes results" do
instance.meth(1)
instance.meth(1)
instance.meth(2)
instance.meth(2)
expect(counter.value).to eql(2)
end
end
context "splat" do
before do
counter = self.counter
klass.define_method(:meth) { |v, *args| counter.increment }
klass.memoize(:meth)
end
it "memoizes results" do
instance.meth(1)
instance.meth(1)
expect(counter.value).to eql(1)
instance.meth(1, 2)
instance.meth(1, 2)
expect(counter.value).to eql(2)
instance.meth(1, 2, 3)
instance.meth(1, 2, 3)
expect(counter.value).to eql(3)
end
end
context "**kwargs" do
before do
counter = self.counter
klass.define_method(:meth) { |foo:, **kwargs| counter.increment }
klass.memoize(:meth)
end
it "memoizes results" do
instance.meth(foo: 1)
instance.meth(foo: 1)
expect(counter.value).to eql(1)
instance.meth(foo: 1, bar: 2)
instance.meth(foo: 1, bar: 2)
expect(counter.value).to eql(2)
instance.meth(foo: 1, baz: 2)
instance.meth(foo: 1, baz: 2)
expect(counter.value).to eql(3)
end
end
end
context "a class including a module including memoizable" do
let(:m0dule) do
Module.new.tap do |module_in_tap|
module_in_tap.include(Dry::Core::Memoizable)
# This will potentially prevent class/module including this module
# from setting up memoizable properly
def module_in_tap.included(_base)
super
end
end
end
let(:klass) { Class.new.include(m0dule) }
let(:instance) { klass.new }
let(:counter) { Concurrent::AtomicFixnum.new }
context "no args" do
before do
counter = self.counter
m0dule.define_method(:meth) { counter.increment }
m0dule.memoize(:meth)
end
it "gets called only once" do
instance.meth
instance.meth
instance.meth
expect(counter.value).to eql(1)
end
end
context "pos arg" do
before do
counter = self.counter
m0dule.define_method(:meth) { |req| counter.increment }
m0dule.memoize(:meth)
end
it "memoizes results" do
instance.meth(1)
instance.meth(1)
instance.meth(2)
instance.meth(2)
expect(counter.value).to eql(2)
end
end
context "splat" do
before do
counter = self.counter
m0dule.define_method(:meth) { |v, *args| counter.increment }
m0dule.memoize(:meth)
end
it "memoizes results" do
instance.meth(1)
instance.meth(1)
expect(counter.value).to eql(1)
instance.meth(1, 2)
instance.meth(1, 2)
expect(counter.value).to eql(2)
instance.meth(1, 2, 3)
instance.meth(1, 2, 3)
expect(counter.value).to eql(3)
end
end
context "**kwargs" do
before do
counter = self.counter
m0dule.define_method(:meth) { |foo:, **kwargs| counter.increment }
m0dule.memoize(:meth)
end
it "memoizes results" do
instance.meth(foo: 1)
instance.meth(foo: 1)
expect(counter.value).to eql(1)
instance.meth(foo: 1, bar: 2)
instance.meth(foo: 1, bar: 2)
expect(counter.value).to eql(2)
instance.meth(foo: 1, baz: 2)
instance.meth(foo: 1, baz: 2)
expect(counter.value).to eql(3)
end
end
end
end
end