Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / AspectSharp / AspectSharp.Lang.Tests / ParserPointcutTestCase.cs
blobc4fcc7bc84c14963744f0e7c8b250d706752d28f
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace AspectSharp.Lang.Tests
17 using System;
19 using antlr;
21 using NUnit.Framework;
23 using AspectSharp.Lang.AST;
25 /// <summary>
26 /// Summary description for ParserPointcutTestCase.
27 /// </summary>
28 [TestFixture]
29 public class ParserPointcutTestCase : ParserTestCaseBase
31 [Test]
32 public void ParsingSimplePointcutDeclaration()
34 AspectParser parser = CreateParser(
35 "aspect XPTO for MyNamespace.MyType \r\n" +
36 " " +
37 " pointcut method(*)" +
38 " end" +
39 " " +
40 "end");
41 EngineConfiguration conf = parser.Parse();
42 AspectDefinition def = conf.Aspects[0];
43 Assert.AreEqual(1, def.PointCuts.Count);
44 PointCutDefinition pointcut = def.PointCuts[0];
45 Assert.IsNotNull( pointcut );
46 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
47 Assert.AreEqual( AllMethodSignature.Instance, pointcut.Method );
50 [Test]
51 public void ParsingPropertyPointcutDeclaration()
53 AspectParser parser = CreateParser(
54 "aspect XPTO for MyNamespace.MyType \r\n" +
55 " " +
56 " pointcut property(*)" +
57 " end" +
58 " " +
59 "end");
60 EngineConfiguration conf = parser.Parse();
61 AspectDefinition def = conf.Aspects[0];
62 Assert.AreEqual(1, def.PointCuts.Count);
63 PointCutDefinition pointcut = def.PointCuts[0];
64 Assert.IsNotNull( pointcut );
65 Assert.AreEqual( PointCutFlags.Property, pointcut.Flags );
66 Assert.AreEqual( AllMethodSignature.Instance, pointcut.Method );
69 [Test]
70 public void ParsingMethodAndPropertyPointcutDeclaration()
72 AspectParser parser = CreateParser(
73 "aspect XPTO for MyNamespace.MyType \r\n" +
74 " " +
75 " pointcut method|property(*)" +
76 " end" +
77 " " +
78 "end");
79 EngineConfiguration conf = parser.Parse();
80 AspectDefinition def = conf.Aspects[0];
81 Assert.AreEqual(1, def.PointCuts.Count);
82 PointCutDefinition pointcut = def.PointCuts[0];
83 Assert.IsNotNull( pointcut );
84 Assert.AreEqual( PointCutFlags.Property | PointCutFlags.Method, pointcut.Flags );
85 Assert.AreEqual( AllMethodSignature.Instance, pointcut.Method );
88 [Test]
89 public void ParsingMethodAndPropertyWritePointcutDeclaration()
91 AspectParser parser = CreateParser(
92 "aspect XPTO for MyNamespace.MyType \r\n" +
93 " " +
94 " pointcut method|propertywrite(*)" +
95 " end" +
96 " " +
97 "end");
98 EngineConfiguration conf = parser.Parse();
99 AspectDefinition def = conf.Aspects[0];
100 Assert.AreEqual(1, def.PointCuts.Count);
101 PointCutDefinition pointcut = def.PointCuts[0];
102 Assert.IsNotNull( pointcut );
103 Assert.AreEqual( PointCutFlags.PropertyWrite | PointCutFlags.Method, pointcut.Flags );
104 Assert.AreEqual( AllMethodSignature.Instance, pointcut.Method );
107 [Test]
108 public void ParsingPointcutDeclarationForMethodDoSomethingNoArgument()
110 AspectParser parser = CreateParser(
111 "aspect XPTO for MyNamespace.MyType \r\n" +
112 " " +
113 " pointcut method(* DoSomething)" +
114 " end" +
115 " " +
116 "end");
117 EngineConfiguration conf = parser.Parse();
118 AspectDefinition def = conf.Aspects[0];
119 PointCutDefinition pointcut = def.PointCuts[0];
120 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
121 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
122 Assert.AreEqual( 0, pointcut.Method.Arguments.Length );
123 Assert.IsTrue( pointcut.Method.AllRetTypes );
124 Assert.IsTrue( !pointcut.Method.AllArguments );
127 [Test]
128 public void ParsingPointcutDeclarationForMethodDoSomethingAlsoWithNoArgument()
130 AspectParser parser = CreateParser(
131 "aspect XPTO for MyNamespace.MyType \r\n" +
132 " " +
133 " pointcut method(* DoSomething())" +
134 " end" +
135 " " +
136 "end");
137 EngineConfiguration conf = parser.Parse();
138 AspectDefinition def = conf.Aspects[0];
139 PointCutDefinition pointcut = def.PointCuts[0];
140 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
141 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
142 Assert.AreEqual( 0, pointcut.Method.Arguments.Length );
143 Assert.IsTrue( pointcut.Method.AllRetTypes );
144 Assert.IsTrue( !pointcut.Method.AllArguments );
147 [Test]
148 public void ParsingPointcutDeclarationForMethodDoSomethingAllArgument()
150 AspectParser parser = CreateParser(
151 "aspect XPTO for MyNamespace.MyType \r\n" +
152 " " +
153 " pointcut method(* DoSomething(*))" +
154 " end" +
155 " " +
156 "end");
157 EngineConfiguration conf = parser.Parse();
158 AspectDefinition def = conf.Aspects[0];
159 PointCutDefinition pointcut = def.PointCuts[0];
160 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
161 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
162 Assert.AreEqual( 1, pointcut.Method.Arguments.Length );
163 Assert.IsTrue( pointcut.Method.AllRetTypes );
164 Assert.IsTrue( pointcut.Method.AllArguments );
167 [Test]
168 public void ParsingPointcutDeclarationForMethodDoSomethingAllArgumentsAndIntRetType()
170 AspectParser parser = CreateParser(
171 "aspect XPTO for MyNamespace.MyType \r\n" +
172 " " +
173 " pointcut method(int DoSomething(*))" +
174 " end" +
175 " " +
176 "end");
177 EngineConfiguration conf = parser.Parse();
178 AspectDefinition def = conf.Aspects[0];
179 PointCutDefinition pointcut = def.PointCuts[0];
180 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
181 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
182 Assert.AreEqual( 1, pointcut.Method.Arguments.Length );
183 Assert.IsTrue( !pointcut.Method.AllRetTypes );
184 Assert.AreEqual( "int", pointcut.Method.RetType );
185 Assert.IsTrue( pointcut.Method.AllArguments );
188 [Test]
189 public void ParsingPointcutDeclarationForMethodDoSomethingWithTwoArgumentsAndRetType()
191 AspectParser parser = CreateParser(
192 "aspect XPTO for MyNamespace.MyType \r\n" +
193 " " +
194 " pointcut method(int DoSomething(string, int))" +
195 " end" +
196 " " +
197 "end");
198 EngineConfiguration conf = parser.Parse();
199 AspectDefinition def = conf.Aspects[0];
200 PointCutDefinition pointcut = def.PointCuts[0];
201 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
202 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
203 Assert.AreEqual( 2, pointcut.Method.Arguments.Length );
204 Assert.IsTrue( !pointcut.Method.AllRetTypes );
205 Assert.AreEqual( "int", pointcut.Method.RetType );
206 Assert.IsTrue( !pointcut.Method.AllArguments );
207 Assert.AreEqual( "string", pointcut.Method.Arguments[0] );
208 Assert.AreEqual( "int", pointcut.Method.Arguments[1] );
211 [Test]
212 public void ParsingPointcutDeclarationForMethodDoSomethingWithTwoArgumentsAndRegEx()
214 AspectParser parser = CreateParser(
215 "aspect XPTO for MyNamespace.MyType \r\n" +
216 " " +
217 " pointcut method(int DoSomething(string, *))" +
218 " end" +
219 " " +
220 "end");
221 EngineConfiguration conf = parser.Parse();
222 AspectDefinition def = conf.Aspects[0];
223 PointCutDefinition pointcut = def.PointCuts[0];
224 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
225 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
226 Assert.AreEqual( 2, pointcut.Method.Arguments.Length );
227 Assert.IsTrue( !pointcut.Method.AllRetTypes );
228 Assert.AreEqual( "int", pointcut.Method.RetType );
229 Assert.IsTrue( !pointcut.Method.AllArguments );
230 Assert.AreEqual( "string", pointcut.Method.Arguments[0] );
231 Assert.AreEqual( "*", pointcut.Method.Arguments[1] );
234 [Test]
235 public void ParsingPointcutDeclarationForMethodWithRegExp()
237 AspectParser parser = CreateParser(
238 "aspect XPTO for MyNamespace.MyType \r\n" +
239 " " +
240 " pointcut method(* DoS.*(*))" +
241 " end" +
242 " " +
243 "end");
244 EngineConfiguration conf = parser.Parse();
245 AspectDefinition def = conf.Aspects[0];
246 PointCutDefinition pointcut = def.PointCuts[0];
247 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
248 Assert.AreEqual( "DoS.*", pointcut.Method.MethodName );
249 Assert.AreEqual( 1, pointcut.Method.Arguments.Length );
250 Assert.IsTrue( pointcut.Method.AllRetTypes );
251 Assert.IsTrue( pointcut.Method.AllArguments );
254 [Test]
255 public void ParsingPointcutDeclarationForAllMethodsReturningString()
257 AspectParser parser = CreateParser(
258 "aspect XPTO for MyNamespace.MyType \r\n" +
259 " " +
260 " pointcut method(String (*))" +
261 " end" +
262 " " +
263 "end");
264 EngineConfiguration conf = parser.Parse();
265 AspectDefinition def = conf.Aspects[0];
266 PointCutDefinition pointcut = def.PointCuts[0];
267 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
268 Assert.AreEqual( ".*", pointcut.Method.MethodName );
269 Assert.AreEqual( 1, pointcut.Method.Arguments.Length );
270 Assert.IsTrue( !pointcut.Method.AllRetTypes );
271 Assert.IsTrue( pointcut.Method.AllArguments );
272 Assert.AreEqual( "string", pointcut.Method.RetType );
275 [Test]
276 public void ParsingPointcutDeclarationForMethodWithRegExpOnReturnType()
278 AspectParser parser = CreateParser(
279 "aspect XPTO for MyNamespace.MyType \r\n" +
280 " " +
281 " pointcut method(strin.* DoSomething(*))" +
282 " end" +
283 " " +
284 "end");
285 EngineConfiguration conf = parser.Parse();
286 AspectDefinition def = conf.Aspects[0];
287 PointCutDefinition pointcut = def.PointCuts[0];
288 Assert.AreEqual( PointCutFlags.Method, pointcut.Flags );
289 Assert.AreEqual( "DoSomething", pointcut.Method.MethodName );
290 Assert.AreEqual( 1, pointcut.Method.Arguments.Length );
291 Assert.IsTrue( !pointcut.Method.AllRetTypes );
292 Assert.IsTrue( pointcut.Method.AllArguments );
293 Assert.AreEqual( "strin.*", pointcut.Method.RetType );
296 [Test]
297 public void ParsingPointcutDeclarationForPropertyNameNoArguments()
299 AspectParser parser = CreateParser(
300 "aspect XPTO for MyNamespace.MyType \r\n" +
301 " " +
302 " pointcut property(* Name)" +
303 " end" +
304 " " +
305 "end");
306 EngineConfiguration conf = parser.Parse();
307 AspectDefinition def = conf.Aspects[0];
308 PointCutDefinition pointcut = def.PointCuts[0];
309 Assert.AreEqual( PointCutFlags.Property, pointcut.Flags );
310 Assert.AreEqual( "Name", pointcut.Method.MethodName );
311 Assert.AreEqual( 0, pointcut.Method.Arguments.Length );
312 Assert.IsTrue( pointcut.Method.AllRetTypes );
313 Assert.IsTrue( !pointcut.Method.AllArguments );
316 [Test]
317 public void ParsingInterceptorRefForProperty()
319 AspectParser parser = CreateParser(
320 "aspect XPTO for MyNamespace.MyType \r\n" +
321 " " +
322 " pointcut property(* Name)" +
323 " advice(\"logger\")" +
324 " end" +
325 " " +
326 "end");
327 EngineConfiguration conf = parser.Parse();
328 AspectDefinition def = conf.Aspects[0];
329 PointCutDefinition pointcut = def.PointCuts[0];
330 Assert.AreEqual(1, pointcut.Advices.Count);
331 InterceptorDefinition adv = pointcut.Advices[0];
332 Assert.IsNotNull( adv );
333 Assert.AreEqual( "logger", adv.TypeReference.LinkRef );
336 [Test]
337 public void ParsingInterceptorTypeForProperty()
339 AspectParser parser = CreateParser(
340 "aspect XPTO for MyNamespace.MyType \r\n" +
341 " " +
342 " pointcut property(* Name)" +
343 " advice( My.NS.Interceptor in My.Assembly )" +
344 " end" +
345 " " +
346 "end");
347 EngineConfiguration conf = parser.Parse();
348 AspectDefinition def = conf.Aspects[0];
349 PointCutDefinition pointcut = def.PointCuts[0];
350 Assert.AreEqual(1, pointcut.Advices.Count);
351 InterceptorDefinition adv = pointcut.Advices[0];
352 Assert.IsNotNull( adv );
353 Assert.AreEqual( TargetTypeEnum.Type, adv.TypeReference.TargetType );
354 Assert.AreEqual( "My.NS.Interceptor", adv.TypeReference.TypeName );
355 Assert.AreEqual( "My.Assembly", adv.TypeReference.AssemblyReference.AssemblyName );
358 protected void DoParsing(string method, string access, string type, string name, string param1)
360 string allparams;
361 if (param1 != null)
362 allparams = "(" + param1 + ")";
363 else
364 allparams = string.Empty;
366 AspectParser parser = CreateParser(string.Format(
367 "aspect XPTO for MyNamespace.MyType \n\n" +
368 " pointcut {0}({1} {2} {3}{4}) \n" +
369 " advice(My.NS.Interceptor in My.Assembly ) \n" +
370 " end \n" +
371 "end", method, access, type, name, allparams));
372 string s = string.Format("{0}({1} {2} {3}{4})", method, access, type, name, allparams);
373 EngineConfiguration conf = parser.Parse();
374 AspectDefinition def = conf.Aspects[0];
375 PointCutDefinition pc = def.PointCuts[0];
376 if (name == "*")
377 name = ".*";
378 Assert.AreEqual(string.Format("({0} {1} {2}({3}))", access == "" ? "*" : access, type, name, param1 == null ? "" : param1), pc.Method.ToString());
379 Assert.AreEqual(method == "method", ((pc.Flags & PointCutFlags.Method) != 0), "method check: " + s);
380 Assert.AreEqual(access == "*" || access == "", pc.Method.AllAccess, "AllAccess: " + s);
381 Assert.AreEqual(type == "*", pc.Method.AllRetTypes, "AllRetTypes: " + s);
382 Assert.AreEqual(name, pc.Method.MethodName, "MethodName: " + s);
383 Assert.AreEqual(access == "" ? "*" : access, pc.Method.Access, "Access: " + s);
384 Assert.AreEqual(type, pc.Method.RetType, "RetType: " + s);
385 if (param1 == null || param1.Length == 0)
386 Assert.AreEqual(0, pc.Method.Arguments.Length, "No Params: " + s);
387 else {
388 Assert.AreEqual(1, pc.Method.Arguments.Length, "1 Param: " + s);
389 Assert.AreEqual(param1, pc.Method.Arguments[0], "Param: " + s);
393 [Test]
394 public void TestParsingWithAccess()
396 string[] methods = new string[] { "method", "property" };
397 string[] accesses = new string[] { "public", "protected", "*", "" };
398 string[] types = new string[] { "*", "string" };
399 string[] names = new string[] { "*", "Run.*", "Do_Something" };
400 string[] param = new string[] { null, "*", "", "int" };
402 foreach (string m in methods)
403 foreach (string a in accesses)
404 foreach (string t in types)
405 foreach (string n in names)
406 foreach (string p in param)
407 DoParsing(m, a, t, n, p);
410 [Test]
411 [ExpectedException(typeof(MismatchedTokenException))]
412 public void ParsingInvalidPointcutDeclaration()
414 AspectParser parser = CreateParser(
415 "aspect XPTO for MyNamespace.MyType \r\n" +
416 " " +
417 " pointcut method|propertyread|property(*)" +
418 " end" +
419 " " +
420 "end");
421 parser.Parse();