-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoization_spec.rb
64 lines (47 loc) · 1.29 KB
/
memoization_spec.rb
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
require 'spec_helper'
describe "Memoizer" do
it "calls the methods of the target class" do
string = "A string"
memoizer = Memoizer.new string
memoizer.length .should eq 8
end
it "raises NoMethodError when an unknown method is called" do
memoizer = Memoizer.new "A string"
-> { memoizer.no_such_string_method }.should raise_error NoMethodError
end
it "memoizes results" do
call_count = 0
object = Object.new
object.define_singleton_method :foo do
call_count += 1
:bar
end
memoized = Memoizer.new object
memoized.foo.should eq :bar
memoized.foo.should eq :bar
call_count.should eq 1
end
it "returns the actual class of the target object" do
Memoizer.new(Object.new).class.should eq Object
end
it "works with nil return values" do
call_count = 0
object = Object.new
object.define_singleton_method :foo do
call_count += 1
nil
end
memoized = Memoizer.new object
memoized.foo.should be_nil
memoized.foo.should be_nil
call_count.should eq 1
end
it 'works with blocks' do
array = [2]
block_call_count = 0
memoized = Memoizer.new array
memoized.each { block_call_count += 1 }
memoized.each { block_call_count += 1 }
block_call_count.should eq 2
end
end