aspect4r 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.document +5 -0
  2. data/.gitignore +22 -0
  3. data/LICENSE +20 -0
  4. data/NOTES.rdoc +42 -0
  5. data/README.rdoc +70 -0
  6. data/Rakefile +45 -0
  7. data/VERSION +1 -0
  8. data/aspect4r.gemspec +108 -0
  9. data/examples/after_example.rb +30 -0
  10. data/examples/around_example.rb +29 -0
  11. data/examples/before_example.rb +62 -0
  12. data/examples/combined_example.rb +39 -0
  13. data/lib/aspect4r/after.rb +29 -0
  14. data/lib/aspect4r/around.rb +29 -0
  15. data/lib/aspect4r/base.rb +32 -0
  16. data/lib/aspect4r/before.rb +37 -0
  17. data/lib/aspect4r/classic.rb +12 -0
  18. data/lib/aspect4r/errors.rb +5 -0
  19. data/lib/aspect4r/extensions/class_extension.rb +20 -0
  20. data/lib/aspect4r/extensions/module_extension.rb +30 -0
  21. data/lib/aspect4r/helper.rb +171 -0
  22. data/lib/aspect4r/model/advice.rb +29 -0
  23. data/lib/aspect4r/model/advice_metadata.rb +27 -0
  24. data/lib/aspect4r/model/advices_for_method.rb +45 -0
  25. data/lib/aspect4r/model/aspect_data.rb +19 -0
  26. data/lib/aspect4r/return_this.rb +9 -0
  27. data/lib/aspect4r.rb +10 -0
  28. data/spec/aspect4r/after_spec.rb +121 -0
  29. data/spec/aspect4r/around_spec.rb +128 -0
  30. data/spec/aspect4r/before_spec.rb +148 -0
  31. data/spec/aspect4r/class_inheritance_spec.rb +254 -0
  32. data/spec/aspect4r/classic_spec.rb +44 -0
  33. data/spec/aspect4r/helper_spec.rb +117 -0
  34. data/spec/aspect4r/inheritance_inclusion_combined_spec.rb +98 -0
  35. data/spec/aspect4r/module_inclusion_spec.rb +208 -0
  36. data/spec/aspect4r/super_in_method_spec.rb +118 -0
  37. data/spec/aspect4r_spec.rb +295 -0
  38. data/spec/spec.opts +1 -0
  39. data/spec/spec_helper.rb +11 -0
  40. data/test/after_test.rb +30 -0
  41. data/test/around_test.rb +28 -0
  42. data/test/before_test.rb +28 -0
  43. data/test/combined_test.rb +40 -0
  44. data/test/method_invocation_test.rb +28 -0
  45. data/test/test_helper.rb +9 -0
  46. metadata +147 -0
@@ -0,0 +1,118 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "super in method body" do
4
+ it "advice in child class and no advice in parent class" do
5
+ parent = Class.new do
6
+ def test
7
+ @value << "parent"
8
+ end
9
+ end
10
+
11
+ klass = Class.new(parent) do
12
+ include Aspect4r
13
+
14
+ attr :value
15
+
16
+ def initialize
17
+ @value = []
18
+ end
19
+
20
+ before :test do
21
+ @value << "before"
22
+ end
23
+
24
+ def test
25
+ super
26
+ @value << "self"
27
+ end
28
+ end
29
+
30
+ o = klass.new
31
+ o.test
32
+
33
+ o.value.should == %w(before parent self)
34
+ end
35
+
36
+ it "no advice in child class and advice in parent class" do
37
+ parent = Class.new do
38
+ include Aspect4r
39
+
40
+ before :test do
41
+ @value << "before"
42
+ end
43
+
44
+ def test
45
+ @value << "parent"
46
+ end
47
+ end
48
+
49
+ klass = Class.new(parent) do
50
+ attr :value
51
+
52
+ def initialize
53
+ @value = []
54
+ end
55
+
56
+ def test
57
+ super
58
+ @value << "self"
59
+ end
60
+ end
61
+
62
+ o = klass.new
63
+ o.test
64
+
65
+ o.value.should == %w(before parent self)
66
+ end
67
+
68
+ it "advices in grand parent and parent and self" do
69
+ grand_parent = Class.new do
70
+ include Aspect4r
71
+
72
+ before :test do
73
+ @value << "before(grand_parent)"
74
+ end
75
+
76
+ def test
77
+ @value << "test(grand_parent)"
78
+ end
79
+ end
80
+
81
+ parent = Class.new(grand_parent) do
82
+ include Aspect4r
83
+
84
+ before :test do
85
+ @value << "before(parent)"
86
+ end
87
+
88
+ def test
89
+ super
90
+ @value << "test(parent)"
91
+ end
92
+ end
93
+
94
+ klass = Class.new(parent) do
95
+ include Aspect4r
96
+
97
+ attr :value
98
+
99
+ def initialize
100
+ @value = []
101
+ end
102
+
103
+ before :test do
104
+ @value << "before"
105
+ end
106
+
107
+ def test
108
+ super
109
+ @value << "test"
110
+ end
111
+ end
112
+
113
+ o = klass.new
114
+ o.test
115
+
116
+ o.value.should == %w(before before(parent) before(grand_parent) test(grand_parent) test(parent) test)
117
+ end
118
+ end
@@ -0,0 +1,295 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Aspect4r execution order" do
4
+ before do
5
+ @klass = Class.new do
6
+ include Aspect4r
7
+
8
+ attr :value
9
+
10
+ def initialize
11
+ @value = []
12
+ end
13
+
14
+ def test
15
+ @value << "test"
16
+ end
17
+ end
18
+ end
19
+
20
+ it "around + before + after" do
21
+ @klass.class_eval do
22
+ around :test do |proxy|
23
+ @value << "around1"
24
+ a4r_invoke proxy
25
+ @value << "around2"
26
+ end
27
+
28
+ before :test do
29
+ @value << "before"
30
+ end
31
+
32
+ after :test do |result|
33
+ @value << "after"
34
+ end
35
+ end
36
+
37
+ o = @klass.new
38
+ o.test
39
+
40
+ o.value.should == %w(before around1 test around2 after)
41
+ end
42
+
43
+ it "before + after + around" do
44
+ @klass.class_eval do
45
+
46
+ before :test do
47
+ @value << "before"
48
+ end
49
+
50
+ after :test do |result|
51
+ @value << "after"
52
+ end
53
+
54
+ around :test do |proxy|
55
+ @value << "around1"
56
+ a4r_invoke proxy
57
+ @value << "around2"
58
+ end
59
+ end
60
+
61
+ o = @klass.new
62
+ o.test
63
+
64
+ o.value.should == %w(around1 before test after around2)
65
+ end
66
+
67
+ it "2 around + 2 before + 2 after" do
68
+ @klass.class_eval do
69
+ around :test do |proxy|
70
+ @value << "around11"
71
+ a4r_invoke proxy
72
+ @value << "around12"
73
+ end
74
+
75
+ around :test do |proxy|
76
+ @value << "around21"
77
+ a4r_invoke proxy
78
+ @value << "around22"
79
+ end
80
+
81
+ before :test do
82
+ @value << "before1"
83
+ end
84
+
85
+ before :test do
86
+ @value << "before2"
87
+ end
88
+
89
+ after :test do |result|
90
+ @value << "after1"
91
+ end
92
+
93
+ after :test do
94
+ @value << "after2"
95
+ end
96
+ end
97
+
98
+ o = @klass.new
99
+ o.test
100
+
101
+ o.value.should == %w(before1 before2 around21 around11 test around12 around22 after1 after2)
102
+ end
103
+
104
+ it "before + after + around + before + after" do
105
+ @klass.class_eval do
106
+ before :test do
107
+ @value << "before1"
108
+ end
109
+
110
+ after :test do |result|
111
+ @value << "after1"
112
+ end
113
+
114
+ around :test do |proxy|
115
+ @value << "around1"
116
+ a4r_invoke proxy
117
+ @value << "around2"
118
+ end
119
+
120
+ before :test do
121
+ @value << "before2"
122
+ end
123
+
124
+ after :test do |result|
125
+ @value << "after2"
126
+ end
127
+ end
128
+
129
+ o = @klass.new
130
+ o.test
131
+
132
+ o.value.should == %w(before2 around1 before1 test after1 around2 after2)
133
+ end
134
+ end
135
+
136
+ describe "Aspect4r result handling" do
137
+ it "should return correct result" do
138
+ @klass = Class.new do
139
+ include Aspect4r
140
+
141
+ def test
142
+ "test"
143
+ end
144
+
145
+ around :test do |proxy|
146
+ result = a4r_invoke proxy
147
+ "around1 #{result} around2"
148
+ end
149
+
150
+ after :test do |result|
151
+ result + " after"
152
+ end
153
+ end
154
+
155
+ o = @klass.new
156
+ o.test.should == "around1 test around2 after"
157
+ end
158
+ end
159
+
160
+ describe "Aspect4r chaining (add advice to advice method)" do
161
+ it "execution order" do
162
+ @klass = Class.new do
163
+ include Aspect4r
164
+
165
+ attr :value
166
+
167
+ def initialize
168
+ @value = []
169
+ end
170
+
171
+ def test
172
+ @value << "test"
173
+ end
174
+
175
+ def do_something
176
+ @value << "do_something"
177
+ end
178
+
179
+ def process_result result
180
+ @value << "process_result"
181
+ end
182
+
183
+ around :test do |proxy|
184
+ @value << "around11"
185
+ a4r_invoke proxy
186
+ @value << "around12"
187
+ end
188
+
189
+ before :test do
190
+ @value << "before1"
191
+ end
192
+
193
+ before :test, :do_something
194
+
195
+ before :do_something do
196
+ @value << "before(do_something)"
197
+ end
198
+
199
+ after :do_something do
200
+ @value << "after(do_something)"
201
+ end
202
+
203
+ after :test do |result|
204
+ @value << "after1"
205
+ end
206
+
207
+ after :test, :process_result
208
+
209
+ before :process_result do
210
+ @value << "before(process_result)"
211
+ end
212
+
213
+ after :process_result do
214
+ @value << "after(process_result)"
215
+ end
216
+ end
217
+
218
+ o = @klass.new
219
+ o.test
220
+
221
+ o.value.should == %w(before1 before(do_something) do_something after(do_something)
222
+ around11 test around12
223
+ after1 before(process_result) process_result after(process_result))
224
+ end
225
+
226
+ it "should return correct result (after advice on advices)" do
227
+ @klass = Class.new do
228
+ include Aspect4r
229
+
230
+ def test
231
+ "test"
232
+ end
233
+
234
+ def around_test proxy
235
+ result = a4r_invoke proxy
236
+ "around1 #{result} around2"
237
+ end
238
+
239
+ def after_test result
240
+ result + " after_test"
241
+ end
242
+
243
+ around :test, :around_test
244
+
245
+ after :test, :after_test
246
+
247
+ after :after_test do |result, *args|
248
+ result + " after(after_test)"
249
+ end
250
+
251
+ after :around_test do |result, *args|
252
+ result + " after(around_test)"
253
+ end
254
+ end
255
+
256
+ o = @klass.new
257
+ o.test.should == "around1 test around2 after(around_test) after_test after(after_test)"
258
+ end
259
+
260
+ it "should return correct result (around advice on advices)" do
261
+ @klass = Class.new do
262
+ include Aspect4r
263
+
264
+ def test
265
+ "test"
266
+ end
267
+
268
+ def around_test proxy
269
+ result = a4r_invoke proxy
270
+ "around1 #{result} around2"
271
+ end
272
+
273
+ def after_test result
274
+ result + " after_test"
275
+ end
276
+
277
+ around :test, :around_test
278
+
279
+ after :test, :after_test
280
+
281
+ around :after_test do |proxy, *args|
282
+ result = a4r_invoke proxy, *args
283
+ "around1(after_test) " + result + " around2(after_test)"
284
+ end
285
+
286
+ around :around_test do |proxy, *args|
287
+ result = a4r_invoke proxy, *args
288
+ "around1(around_test) " + result + " around2(around_test)"
289
+ end
290
+ end
291
+
292
+ o = @klass.new
293
+ o.test.should == "around1(after_test) around1(around_test) around1 test around2 around2(around_test) after_test around2(after_test)"
294
+ end
295
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'aspect4r'
5
+
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :mocha
11
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class AfterTest < Test::Unit::TestCase
4
+ include RubyProf::Test
5
+
6
+ class Test
7
+ include Aspect4r
8
+
9
+ after :test, :after_test
10
+
11
+ def test_no_aspect
12
+ result = nil
13
+
14
+ after_test result
15
+ end
16
+
17
+ def test; end
18
+
19
+ def after_test result; end
20
+ end
21
+
22
+ def setup
23
+ @obj = Test.new
24
+ end
25
+
26
+ def test_after
27
+ @obj.test_no_aspect
28
+ @obj.test
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class AroundTest < Test::Unit::TestCase
4
+ include RubyProf::Test
5
+
6
+ class Test
7
+ include Aspect4r
8
+
9
+ around :test, :around_test
10
+
11
+ def test_no_aspect; end
12
+
13
+ def test; end
14
+
15
+ def around_test proxy
16
+ a4r_invoke proxy
17
+ end
18
+ end
19
+
20
+ def setup
21
+ @obj = Test.new
22
+ end
23
+
24
+ def test_around
25
+ @obj.test_no_aspect
26
+ @obj.test
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class BeforeTest < Test::Unit::TestCase
4
+ include RubyProf::Test
5
+
6
+ class Test
7
+ include Aspect4r
8
+
9
+ before :test, :before_test
10
+
11
+ def test_no_aspect
12
+ before_test
13
+ end
14
+
15
+ def test; end
16
+
17
+ def before_test; end
18
+ end
19
+
20
+ def setup
21
+ @obj = Test.new
22
+ end
23
+
24
+ def test_before
25
+ @obj.test_no_aspect
26
+ @obj.test
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class CombinedTest < Test::Unit::TestCase
4
+ include RubyProf::Test
5
+
6
+ class Test
7
+ include Aspect4r
8
+
9
+ before :test, :before_test
10
+ after :test, :after_test
11
+ around :test, :around_test
12
+
13
+ def test_no_aspect
14
+ before_test
15
+
16
+ result = nil
17
+
18
+ after_test result
19
+ end
20
+
21
+ def test; end
22
+
23
+ def before_test; end
24
+
25
+ def after_test result; end
26
+
27
+ def around_test proxy
28
+ a4r_invoke proxy
29
+ end
30
+ end
31
+
32
+ def setup
33
+ @obj = Test.new
34
+ end
35
+
36
+ def test_combined
37
+ @obj.test_no_aspect
38
+ @obj.test
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class MethodInvokationTest < Test::Unit::TestCase
4
+ include RubyProf::Test
5
+
6
+ class Test
7
+ def do_something; end
8
+
9
+ def test
10
+ do_something
11
+ end
12
+
13
+ do_something_method = instance_method(:do_something)
14
+
15
+ define_method :test_with_method_object do
16
+ do_something_method.bind(self).call
17
+ end
18
+ end
19
+
20
+ def setup
21
+ @obj = Test.new
22
+ end
23
+
24
+ def test_method_invocation
25
+ @obj.test
26
+ @obj.test_with_method_object
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'ruby-prof/test'
6
+ require 'aspect4r'
7
+
8
+ RubyProf::Test::PROFILE_OPTIONS[:count ] = 10000
9
+ RubyProf::Test::PROFILE_OPTIONS[:output_dir] = File.dirname(__FILE__) + "/output"