cache_annotations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,209 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class A
4
+ include CacheAnnotation
5
+
6
+ def a
7
+ end
8
+
9
+ cached
10
+ def b
11
+ "b"
12
+ end
13
+
14
+ cached :in => "@c_cache"
15
+ def c
16
+ "c"
17
+ end
18
+
19
+ cached
20
+ def d(arg0)
21
+ arg0.to_s
22
+ end
23
+
24
+ cached :in => "@e_cache"
25
+ def e(arg0)
26
+ arg0.to_s
27
+ end
28
+ end
29
+
30
+ class TestCacheAnnotation < Test::Unit::TestCase
31
+
32
+ def setup
33
+ @a = A.new
34
+ end
35
+
36
+ def test_basics
37
+ self.methods.select { |name| name =~ /^basic_.*/ }.each do | name |
38
+ setup
39
+ self.send name
40
+ end
41
+ end
42
+
43
+ def test_meta_properties
44
+ assert_nothing_raised "including CacheAnnotation twice fails" do
45
+ class << A
46
+ include CacheAnnotation
47
+ end
48
+ end
49
+ test_basics
50
+ assert_nothing_raised "including CacheAnnotation twice fails" do
51
+ class << A
52
+ cached
53
+ def a
54
+ "a"
55
+ end
56
+ end
57
+ end
58
+ test_basics
59
+ end
60
+
61
+ def basic_simple_cached_method
62
+ assert_equal "b", @a.b, "First access to cached method failed"
63
+ assert_equal "b", @a.b, "Second access to cached method failed"
64
+ assert_equal "b", @a.b, "Repeated access to cached method failed"
65
+ assert_equal "b", @a.instance_variable_get(:@b), "Value was not " +
66
+ "stored in the right instance variable"
67
+ @a.instance_variable_set(:@b, "b2")
68
+ assert_equal "b2", @a.b, "Instance variable not used for cache"
69
+ end
70
+
71
+ def basic_simple_cached_method_with_costum_cache
72
+ assert_equal "c", @a.c, "First access to cached method failed"
73
+ assert_equal "c", @a.c, "Second access to cached method failed"
74
+ assert_equal "c", @a.c, "Repeated access to cached method failed"
75
+ assert_equal "c", @a.instance_variable_get(:@c_cache), "Value was not " +
76
+ "stored in the right instance variable"
77
+ @a.instance_variable_set(:@c_cache, "c2")
78
+ assert_equal "c2", @a.c, "Instance variable not used for cache"
79
+ end
80
+
81
+ def basic_cached_method
82
+ assert_equal "d", @a.d("d"), "First access to cached method failed"
83
+ assert_equal "d", @a.d("d"), "Second access to cached method failed"
84
+ assert_equal "d", @a.d("d"), "Repeated access to cached method failed"
85
+ assert_equal "d", @a.instance_variable_get(:@d)["d"], "Value was not " +
86
+ "stored in the right instance variable"
87
+
88
+ assert_equal "d1", @a.d("d1"), "First access to cached method failed"
89
+ assert_equal "d1", @a.d("d1"), "Second access to cached method failed"
90
+ assert_equal "d1", @a.d("d1"), "Repeated access to cached method failed"
91
+ assert_equal "d1", @a.instance_variable_get(:@d)["d1"], "Value was not " +
92
+ "stored in the right instance variable"
93
+
94
+ @a.instance_variable_get(:@d)["d"] = "d2"
95
+ assert_equal "d2", @a.d("d"), "Instance variable not used for cache"
96
+ end
97
+
98
+ def basic_cached_method_with_costum_cache
99
+ assert_equal "e", @a.e("e"), "First access to cached method failed"
100
+ assert_equal "e", @a.e("e"), "Second access to cached method failed"
101
+ assert_equal "e", @a.e("e"), "Repeated access to cached method failed"
102
+ assert_equal "e", @a.instance_variable_get(:@e_cache)["e"], "Value " +
103
+ "was not stored in the right instance variable"
104
+
105
+ assert_equal "e1", @a.e("e1"), "First access to cached method failed"
106
+ assert_equal "e1", @a.e("e1"), "Second access to cached method failed"
107
+ assert_equal "e1", @a.e("e1"), "Repeated access to cached method failed"
108
+ assert_equal "e1", @a.instance_variable_get(:@e_cache)["e1"], "Value " +
109
+ "was not stored in the right instance variable"
110
+
111
+ @a.instance_variable_get(:@e_cache)["e"] = "e2"
112
+ assert_equal "e2", @a.e("e"), "Instance variable not used for cache"
113
+ end
114
+ end
115
+
116
+ class B
117
+ module ClassMethods
118
+ include CacheAnnotation
119
+
120
+ def a
121
+ "a"
122
+ end
123
+
124
+ cached
125
+ def b
126
+ "b"
127
+ end
128
+
129
+ cached :in => "@c_cache"
130
+ def c
131
+ "c"
132
+ end
133
+
134
+ cached
135
+ def d(arg0)
136
+ arg0.to_s
137
+ end
138
+
139
+ cached :in => "@e_cache"
140
+ def e(arg0)
141
+ arg0.to_s
142
+ end
143
+ end
144
+ self.extend(ClassMethods)
145
+ end
146
+
147
+ class TestCacheAnnotationOnClassSide < Test::Unit::TestCase
148
+
149
+ def test_basics
150
+ self.methods.select { |name| name =~ /^basic_.*/ }.each do | name |
151
+ setup
152
+ self.send name
153
+ end
154
+ end
155
+
156
+ def basic_simple_cached_method
157
+ assert_equal "b", B.b, "First access to cached method failed"
158
+ assert_equal "b", B.b, "Second access to cached method failed"
159
+ assert_equal "b", B.b, "Repeated access to cached method failed"
160
+ assert_equal "b", B.instance_variable_get(:@b), "Value was not " +
161
+ "stored in the right instance variable"
162
+ B.instance_variable_set(:@b, "b2")
163
+ assert_equal "b2", B.b, "Instance variable not used for cache"
164
+ end
165
+
166
+ def basic_simple_cached_method_with_costum_cache
167
+ assert_equal "c", B.c, "First access to cached method failed"
168
+ assert_equal "c", B.c, "Second access to cached method failed"
169
+ assert_equal "c", B.c, "Repeated access to cached method failed"
170
+ assert_equal "c", B.instance_variable_get(:@c_cache), "Value was not " +
171
+ "stored in the right instance variable"
172
+ B.instance_variable_set(:@c_cache, "c2")
173
+ assert_equal "c2", B.c, "Instance variable not used for cache"
174
+ end
175
+
176
+ def basic_cached_method
177
+ assert_equal "d", B.d("d"), "First access to cached method failed"
178
+ assert_equal "d", B.d("d"), "Second access to cached method failed"
179
+ assert_equal "d", B.d("d"), "Repeated access to cached method failed"
180
+ assert_equal "d", B.instance_variable_get(:@d)["d"], "Value was not " +
181
+ "stored in the right instance variable"
182
+
183
+ assert_equal "d1", B.d("d1"), "First access to cached method failed"
184
+ assert_equal "d1", B.d("d1"), "Second access to cached method failed"
185
+ assert_equal "d1", B.d("d1"), "Repeated access to cached method failed"
186
+ assert_equal "d1", B.instance_variable_get(:@d)["d1"], "Value was not " +
187
+ "stored in the right instance variable"
188
+
189
+ B.instance_variable_get(:@d)["d"] = "d2"
190
+ assert_equal "d2", B.d("d"), "Instance variable not used for cache"
191
+ end
192
+
193
+ def basic_cached_method_with_costum_cache
194
+ assert_equal "e", B.e("e"), "First access to cached method failed"
195
+ assert_equal "e", B.e("e"), "Second access to cached method failed"
196
+ assert_equal "e", B.e("e"), "Repeated access to cached method failed"
197
+ assert_equal "e", B.instance_variable_get(:@e_cache)["e"], "Value " +
198
+ "was not stored in the right instance variable"
199
+
200
+ assert_equal "e1", B.e("e1"), "First access to cached method failed"
201
+ assert_equal "e1", B.e("e1"), "Second access to cached method failed"
202
+ assert_equal "e1", B.e("e1"), "Repeated access to cached method failed"
203
+ assert_equal "e1", B.instance_variable_get(:@e_cache)["e1"], "Value " +
204
+ "was not stored in the right instance variable"
205
+
206
+ B.instance_variable_get(:@e_cache)["e"] = "e2"
207
+ assert_equal "e2", B.e("e"), "Instance variable not used for cache"
208
+ end
209
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/cache_annotations'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: cache_annotations
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-05-28 00:00:00 +02:00
8
+ summary: Cache Annotations provides an Annotation like interface to mark methods as functional. These are then cached automagically. This leads to cleaner code without performance implications.
9
+ require_paths:
10
+ - lib
11
+ email: ruby@schmidtwisser.de
12
+ homepage: http://contextr.rubyforge.org
13
+ rubyforge_project: contextr
14
+ description: Cache Annotations provides an Annotation like interface to mark methods as functional. These are then cached automagically. This leads to cleaner code without performance implications.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Gregor Schmidt
31
+ files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - examples/fibonacci.rb
38
+ - lib/cache_annotations.rb
39
+ - lib/cache_annotations/version.rb
40
+ - scripts/txt2html
41
+ - setup.rb
42
+ - test/test_cache_annotations.rb
43
+ - test/test_helper.rb
44
+ test_files:
45
+ - test/test_cache_annotations.rb
46
+ - test/test_helper.rb
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ extra_rdoc_files:
51
+ - History.txt
52
+ - License.txt
53
+ - Manifest.txt
54
+ - README.txt
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ requirements: []
60
+
61
+ dependencies: []
62
+