aspect4r 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/README.rdoc +23 -4
  2. data/VERSION +1 -1
  3. data/aspect4r.gemspec +11 -9
  4. data/examples/advices_on_class_method_example.rb +2 -2
  5. data/examples/around_example.rb +2 -2
  6. data/examples/combined_example.rb +2 -2
  7. data/examples/test_advices_example.rb +3 -4
  8. data/lib/aspect4r/base.rb +25 -23
  9. data/lib/aspect4r/helper.rb +34 -17
  10. data/lib/aspect4r/model/advice.rb +29 -4
  11. data/lib/aspect4r/model/aspect_data.rb +14 -5
  12. data/lib/aspect4r/model/method_matcher.rb +43 -0
  13. data/spec/aspect4r/advice_on_class_method_spec.rb +4 -4
  14. data/spec/aspect4r/advice_test_spec.rb +9 -20
  15. data/spec/aspect4r/around_spec.rb +23 -24
  16. data/spec/aspect4r/before_spec.rb +1 -1
  17. data/spec/aspect4r/class_inheritance_spec.rb +40 -6
  18. data/spec/aspect4r/classic_spec.rb +2 -2
  19. data/spec/aspect4r/helper_spec.rb +2 -2
  20. data/spec/aspect4r/inheritance_inclusion_combined_spec.rb +4 -4
  21. data/spec/aspect4r/method_added_spec.rb +2 -2
  22. data/spec/aspect4r/{advice_group_spec.rb → model/advice_group_spec.rb} +1 -1
  23. data/spec/aspect4r/model/advice_spec.rb +52 -0
  24. data/spec/aspect4r/model/method_matcher_spec.rb +23 -0
  25. data/spec/aspect4r/module_inclusion_spec.rb +8 -8
  26. data/spec/aspect4r/regexp_methods_spec.rb +47 -0
  27. data/spec/aspect4r/singleton_method_added_spec.rb +2 -2
  28. data/spec/aspect4r_spec.rb +58 -22
  29. data/test/around_test.rb +2 -2
  30. data/test/combined_test.rb +2 -2
  31. metadata +13 -11
  32. data/lib/aspect4r/model/advices_for_method.rb +0 -26
  33. data/spec/aspect4r/advice_spec.rb +0 -62
  34. data/spec/aspect4r/advices_for_method_spec.rb +0 -17
@@ -20,7 +20,7 @@ describe Aspect4r::Around do
20
20
 
21
21
  it "should run advice method instead of original method" do
22
22
  @klass.class_eval do
23
- def do_something orig, value
23
+ def do_something value
24
24
  raise 'error'
25
25
  end
26
26
 
@@ -35,7 +35,7 @@ describe Aspect4r::Around do
35
35
  i = 100
36
36
 
37
37
  @klass.class_eval do
38
- around :test do |orig, value|
38
+ around :test do |value|
39
39
  i = 200
40
40
  'around_block_return'
41
41
  end
@@ -53,7 +53,7 @@ describe Aspect4r::Around do
53
53
  i = 100
54
54
 
55
55
  @klass.class_eval do
56
- around [:test] do |orig, value|
56
+ around [:test] do |value|
57
57
  i = 200
58
58
  end
59
59
  end
@@ -66,7 +66,7 @@ describe Aspect4r::Around do
66
66
 
67
67
  it "should have access to instance variables inside advice block" do
68
68
  @klass.class_eval do
69
- around :test do |orig, value|
69
+ around :test do |value|
70
70
  @var = 1
71
71
  end
72
72
  end
@@ -77,13 +77,28 @@ describe Aspect4r::Around do
77
77
  o.instance_variable_get(:@var).should == 1
78
78
  end
79
79
 
80
+ it "should pass method name as first arg to advice block(or method) if method_name_arg is true" do
81
+ s = nil
82
+
83
+ @klass.class_eval do
84
+ around :test, :method_name_arg => true do |method, value, &block|
85
+ s = method
86
+ end
87
+ end
88
+
89
+ o = @klass.new
90
+ o.test('something')
91
+
92
+ s.should == 'test'
93
+ end
94
+
80
95
  it "should be able to invoke original method from advice block" do
81
96
  i = 100
82
97
 
83
98
  @klass.class_eval do
84
- around :test do |proxy, value|
99
+ around :test do |value, &block|
85
100
  i = 200
86
- a4r_invoke proxy, value
101
+ block.call value
87
102
  end
88
103
  end
89
104
 
@@ -97,8 +112,8 @@ describe Aspect4r::Around do
97
112
 
98
113
  it "should be able to invoke original method from advice method" do
99
114
  @klass.class_eval do
100
- def do_something proxy, value
101
- a4r_invoke proxy, value
115
+ def do_something value
116
+ yield value
102
117
  end
103
118
 
104
119
  around :test, :do_something
@@ -109,20 +124,4 @@ describe Aspect4r::Around do
109
124
 
110
125
  o.value.should == 'something'
111
126
  end
112
-
113
- it "should be able to access method name through proxy.name (this subjects to change)" do
114
- s = nil
115
-
116
- @klass.class_eval do
117
- around :test do |proxy, value|
118
- s = proxy.name
119
- a4r_invoke proxy, value
120
- end
121
- end
122
-
123
- o = @klass.new
124
- o.test('something')
125
-
126
- s.should == 'test'
127
- end
128
127
  end
@@ -4,7 +4,7 @@ describe Aspect4r::Before do
4
4
  before do
5
5
  @klass = Class.new do
6
6
  include Aspect4r::Before
7
-
7
+
8
8
  attr :value
9
9
 
10
10
  def initialize
@@ -15,9 +15,9 @@ describe Aspect4r do
15
15
  @value << "test"
16
16
  end
17
17
 
18
- around :test do |proxy|
18
+ around :test do |&block|
19
19
  @value << "around1"
20
- a4r_invoke proxy
20
+ block.call
21
21
  @value << "around2"
22
22
  end
23
23
 
@@ -200,9 +200,9 @@ describe Aspect4r do
200
200
  class Child1 < parent
201
201
  include Aspect4r
202
202
 
203
- around :test do |proxy|
203
+ around :test do |&block|
204
204
  @value << "around(before)"
205
- a4r_invoke proxy
205
+ block.call
206
206
  @value << "around(after)"
207
207
  end
208
208
  end
@@ -239,9 +239,9 @@ describe Aspect4r do
239
239
  class Child2 < parent
240
240
  include Aspect4r
241
241
 
242
- around :test do |proxy|
242
+ around :test do |&block|
243
243
  @value << "around(before)"
244
- a4r_invoke proxy
244
+ block.call
245
245
  @value << "around(after)"
246
246
  end
247
247
  end
@@ -251,4 +251,38 @@ describe Aspect4r do
251
251
 
252
252
  o.value.should == %w(around(before) before test after around(after))
253
253
  end
254
+
255
+ it "inherit advice from module" do
256
+ pending
257
+ mod = Module.new do
258
+ include Aspect4r
259
+
260
+ before :test, :inherit => true do
261
+ @value << "before"
262
+ end
263
+
264
+ after :test do
265
+ @value << "after"
266
+ end
267
+ end
268
+
269
+ klass = Class.new do
270
+ include mod
271
+
272
+ attr :value
273
+
274
+ def initialize
275
+ @value = []
276
+ end
277
+
278
+ def test
279
+ @value << "test"
280
+ end
281
+ end
282
+
283
+ o = klass.new
284
+ o.test
285
+
286
+ o.value.should == %w(before test)
287
+ end
254
288
  end
@@ -17,9 +17,9 @@ describe Aspect4r::Classic do
17
17
  @value << "test"
18
18
  end
19
19
 
20
- a4r_around :test do |proxy_method|
20
+ a4r_around :test do |&block|
21
21
  @value << "around1"
22
- a4r_invoke proxy_method
22
+ block.call
23
23
  @value << "around2"
24
24
  end
25
25
 
@@ -43,9 +43,9 @@ describe Aspect4r::Helper do
43
43
  result
44
44
  end
45
45
 
46
- def around_test proxy
46
+ def around_test
47
47
  @value << "around_test_before"
48
- result = a4r_invoke proxy
48
+ result = yield
49
49
  @value << "around_test_after"
50
50
  result
51
51
  end
@@ -9,9 +9,9 @@ describe Aspect4r do
9
9
  @value << "test"
10
10
  end
11
11
 
12
- around :test do |proxy|
12
+ around :test do |&block|
13
13
  @value << "around1"
14
- a4r_invoke proxy
14
+ block.call
15
15
  @value << "around2"
16
16
  end
17
17
 
@@ -52,9 +52,9 @@ describe Aspect4r do
52
52
  @value << "test(module)"
53
53
  end
54
54
 
55
- around :test do |proxy|
55
+ around :test do |&block|
56
56
  @value << "around1"
57
- a4r_invoke proxy
57
+ block.call
58
58
  @value << "around2"
59
59
  end
60
60
 
@@ -11,9 +11,9 @@ describe "method_added" do
11
11
  @value = []
12
12
  end
13
13
 
14
- around :test do |proxy|
14
+ around :test do |&block|
15
15
  @value << "around1"
16
- a4r_invoke proxy
16
+ block.call
17
17
  @value << "around2"
18
18
  end
19
19
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "Group of advices" do
4
4
  it "should work" do
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ module Aspect4r::Model
4
+ describe Advice do
5
+ it "before? returns true for before advice" do
6
+ advice = Advice.new Advice::BEFORE, MethodMatcher.new, :advice_method, :group
7
+ advice.before?.should be_true
8
+ end
9
+
10
+ it "before_filter? returns false for before advice" do
11
+ advice = Advice.new Advice::BEFORE, MethodMatcher.new, :advice_method, :group
12
+ advice.before_filter?.should be_false
13
+ end
14
+
15
+ it "before? returns false for before_filter advice" do
16
+ advice = Advice.new Advice::BEFORE, MethodMatcher.new, :advice_method, :group, :skip_if_false => true
17
+ advice.before?.should be_false
18
+ end
19
+
20
+ it "before_filter? returns true for before_filter advice" do
21
+ advice = Advice.new Advice::BEFORE, MethodMatcher.new, :advice_method, :group, :skip_if_false => true
22
+ advice.before_filter?.should be_true
23
+ end
24
+
25
+ it "invoke before advice" do
26
+ advice = Advice.new Advice::BEFORE, MethodMatcher.new, :advice_method, :group
27
+
28
+ o = Object.new
29
+ o.expects(:advice_method).with(1)
30
+
31
+ advice.invoke(o, 1)
32
+ end
33
+
34
+ it "invoke after advice" do
35
+ advice = Advice.new Advice::AFTER, MethodMatcher.new, :advice_method, :group
36
+
37
+ o = Object.new
38
+ o.expects(:advice_method).with(1)
39
+
40
+ advice.invoke(o, 1)
41
+ end
42
+
43
+ it "invoke around advice" do
44
+ advice = Advice.new Advice::AROUND, MethodMatcher.new, :advice_method, :group
45
+
46
+ o = Object.new
47
+ o.expects(:advice_method).with(1)
48
+
49
+ advice.invoke(o, 1)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ module Aspect4r
4
+ module Model
5
+ describe MethodMatcher do
6
+ it "should match String" do
7
+ MethodMatcher.new('test').match?('test').should_not be_nil
8
+ end
9
+
10
+ it "should match regular expression" do
11
+ MethodMatcher.new(/test/).match?('test').should_not be_nil
12
+ end
13
+
14
+ it "should return true if any item matches" do
15
+ MethodMatcher.new('test1', 'test2').match?('test2').should_not be_nil
16
+ end
17
+
18
+ it "should return nil if none matches" do
19
+ MethodMatcher.new('test1', 'test2').match?('test3').should be_nil
20
+ end
21
+ end
22
+ end
23
+ end
@@ -17,9 +17,9 @@ describe Aspect4r do
17
17
  @value << "test"
18
18
  end
19
19
 
20
- around :test do |proxy|
20
+ around :test do |&block|
21
21
  @value << "around1"
22
- a4r_invoke proxy
22
+ block.call
23
23
  @value << "around2"
24
24
  end
25
25
 
@@ -60,9 +60,9 @@ describe Aspect4r do
60
60
  @value << "test(module)"
61
61
  end
62
62
 
63
- around :test do |proxy|
63
+ around :test do |&block|
64
64
  @value << "around1"
65
- send proxy
65
+ block.call
66
66
  @value << "around2"
67
67
  end
68
68
 
@@ -125,9 +125,9 @@ describe Aspect4r do
125
125
  @value << "test(module)"
126
126
  end
127
127
 
128
- around :test do |proxy|
128
+ around :test do |&block|
129
129
  @value << "around1"
130
- a4r_invoke proxy
130
+ block.call
131
131
  @value << "around2"
132
132
  end
133
133
 
@@ -169,9 +169,9 @@ describe Aspect4r do
169
169
  @value << "test(module)"
170
170
  end
171
171
 
172
- around :test do |proxy|
172
+ around :test do |&block|
173
173
  @value << "around1"
174
- a4r_invoke proxy
174
+ block.call
175
175
  @value << "around2"
176
176
  end
177
177
 
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Advices matched by regular expression" do
4
+ it "should not apply to methods defined before advice" 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
+
18
+ before /test/ do
19
+ @value << "before"
20
+ end
21
+ end
22
+
23
+ @klass.new.test.should == %w"test"
24
+ end
25
+
26
+ it "should not apply to methods defined after advice" do
27
+ @klass = Class.new do
28
+ include Aspect4r
29
+
30
+ attr :value
31
+
32
+ def initialize
33
+ @value = []
34
+ end
35
+
36
+ before /test/ do
37
+ @value << "before"
38
+ end
39
+
40
+ def test
41
+ @value << "test"
42
+ end
43
+ end
44
+
45
+ @klass.new.test.should == %w"before test"
46
+ end
47
+ end
@@ -15,9 +15,9 @@ describe "singleton_method_added" do
15
15
  @value ||= []
16
16
  end
17
17
 
18
- around :test do |proxy|
18
+ around :test do |&block|
19
19
  value << "around1"
20
- a4r_invoke proxy
20
+ block.call
21
21
  value << "around2"
22
22
  end
23
23
 
@@ -1,5 +1,41 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ describe "Mixing regular expressions and string" do
4
+ it "should work" do
5
+ @klass = Class.new do
6
+ include Aspect4r
7
+
8
+ before /do_/, "test" do
9
+ @value << "before"
10
+ end
11
+
12
+ attr :value
13
+
14
+ def initialize
15
+ @value = []
16
+ end
17
+
18
+ def test
19
+ @value << "test"
20
+ end
21
+
22
+ def do_something
23
+ @value << "do_something"
24
+ end
25
+ end
26
+
27
+ o = @klass.new
28
+ o.test
29
+
30
+ o.value.should == %w(before test)
31
+
32
+ o = @klass.new
33
+ o.do_something
34
+
35
+ o.value.should == %w(before do_something)
36
+ end
37
+ end
38
+
3
39
  describe "Aspect4r execution order" do
4
40
  before do
5
41
  @klass = Class.new do
@@ -19,9 +55,9 @@ describe "Aspect4r execution order" do
19
55
 
20
56
  it "around + before + after" do
21
57
  @klass.class_eval do
22
- around :test do |proxy|
58
+ around :test do |&block|
23
59
  @value << "around1"
24
- a4r_invoke proxy
60
+ block.call
25
61
  @value << "around2"
26
62
  end
27
63
 
@@ -51,9 +87,9 @@ describe "Aspect4r execution order" do
51
87
  @value << "after"
52
88
  end
53
89
 
54
- around :test do |proxy|
90
+ around :test do |&block|
55
91
  @value << "around1"
56
- a4r_invoke proxy
92
+ block.call
57
93
  @value << "around2"
58
94
  end
59
95
  end
@@ -66,15 +102,15 @@ describe "Aspect4r execution order" do
66
102
 
67
103
  it "2 around + 2 before + 2 after" do
68
104
  @klass.class_eval do
69
- around :test do |proxy|
105
+ around :test do |&block|
70
106
  @value << "around11"
71
- a4r_invoke proxy
107
+ block.call
72
108
  @value << "around12"
73
109
  end
74
110
 
75
- around :test do |proxy|
111
+ around :test do |&block|
76
112
  @value << "around21"
77
- a4r_invoke proxy
113
+ block.call
78
114
  @value << "around22"
79
115
  end
80
116
 
@@ -111,9 +147,9 @@ describe "Aspect4r execution order" do
111
147
  @value << "after1"
112
148
  end
113
149
 
114
- around :test do |proxy|
150
+ around :test do |&block|
115
151
  @value << "around1"
116
- a4r_invoke proxy
152
+ block.call
117
153
  @value << "around2"
118
154
  end
119
155
 
@@ -142,8 +178,8 @@ describe "Aspect4r result handling" do
142
178
  "test"
143
179
  end
144
180
 
145
- around :test do |proxy|
146
- result = a4r_invoke proxy
181
+ around :test do |&block|
182
+ result = block.call
147
183
  "around1 #{result} around2"
148
184
  end
149
185
 
@@ -180,9 +216,9 @@ describe "Aspect4r chaining (add advice to advice method)" do
180
216
  @value << "process_result"
181
217
  end
182
218
 
183
- around :test do |proxy|
219
+ around :test do |&block|
184
220
  @value << "around11"
185
- a4r_invoke proxy
221
+ block.call
186
222
  @value << "around12"
187
223
  end
188
224
 
@@ -231,8 +267,8 @@ describe "Aspect4r chaining (add advice to advice method)" do
231
267
  "test"
232
268
  end
233
269
 
234
- def around_test proxy
235
- result = a4r_invoke proxy
270
+ def around_test
271
+ result = yield
236
272
  "around1 #{result} around2"
237
273
  end
238
274
 
@@ -265,8 +301,8 @@ describe "Aspect4r chaining (add advice to advice method)" do
265
301
  "test"
266
302
  end
267
303
 
268
- def around_test proxy
269
- result = a4r_invoke proxy
304
+ def around_test
305
+ result = yield
270
306
  "around1 #{result} around2"
271
307
  end
272
308
 
@@ -278,13 +314,13 @@ describe "Aspect4r chaining (add advice to advice method)" do
278
314
 
279
315
  after :test, :after_test
280
316
 
281
- around :after_test do |proxy, *args|
282
- result = a4r_invoke proxy, *args
317
+ around :after_test do |*args, &block|
318
+ result = block.call *args
283
319
  "around1(after_test) " + result + " around2(after_test)"
284
320
  end
285
321
 
286
- around :around_test do |proxy, *args|
287
- result = a4r_invoke proxy, *args
322
+ around :around_test do |*args, &block|
323
+ result = block.call *args
288
324
  "around1(around_test) " + result + " around2(around_test)"
289
325
  end
290
326
  end
data/test/around_test.rb CHANGED
@@ -12,8 +12,8 @@ class AroundTest < Test::Unit::TestCase
12
12
 
13
13
  def test; end
14
14
 
15
- def around_test proxy
16
- a4r_invoke proxy
15
+ def around_test
16
+ yield
17
17
  end
18
18
  end
19
19
 
@@ -24,8 +24,8 @@ class CombinedTest < Test::Unit::TestCase
24
24
 
25
25
  def after_test result; end
26
26
 
27
- def around_test proxy
28
- a4r_invoke proxy
27
+ def around_test
28
+ yield
29
29
  end
30
30
  end
31
31