1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
21 using NUnit
.Framework
;
23 using AspectSharp
.Lang
.AST
;
26 /// Summary description for ParserPointcutTestCase.
29 public class ParserPointcutTestCase
: ParserTestCaseBase
32 public void ParsingSimplePointcutDeclaration()
34 AspectParser parser
= CreateParser(
35 "aspect XPTO for MyNamespace.MyType \r\n" +
37 " pointcut method(*)" +
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
);
51 public void ParsingPropertyPointcutDeclaration()
53 AspectParser parser
= CreateParser(
54 "aspect XPTO for MyNamespace.MyType \r\n" +
56 " pointcut property(*)" +
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
);
70 public void ParsingMethodAndPropertyPointcutDeclaration()
72 AspectParser parser
= CreateParser(
73 "aspect XPTO for MyNamespace.MyType \r\n" +
75 " pointcut method|property(*)" +
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
);
89 public void ParsingMethodAndPropertyWritePointcutDeclaration()
91 AspectParser parser
= CreateParser(
92 "aspect XPTO for MyNamespace.MyType \r\n" +
94 " pointcut method|propertywrite(*)" +
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
);
108 public void ParsingPointcutDeclarationForMethodDoSomethingNoArgument()
110 AspectParser parser
= CreateParser(
111 "aspect XPTO for MyNamespace.MyType \r\n" +
113 " pointcut method(* DoSomething)" +
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
);
128 public void ParsingPointcutDeclarationForMethodDoSomethingAlsoWithNoArgument()
130 AspectParser parser
= CreateParser(
131 "aspect XPTO for MyNamespace.MyType \r\n" +
133 " pointcut method(* DoSomething())" +
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
);
148 public void ParsingPointcutDeclarationForMethodDoSomethingAllArgument()
150 AspectParser parser
= CreateParser(
151 "aspect XPTO for MyNamespace.MyType \r\n" +
153 " pointcut method(* DoSomething(*))" +
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
);
168 public void ParsingPointcutDeclarationForMethodDoSomethingAllArgumentsAndIntRetType()
170 AspectParser parser
= CreateParser(
171 "aspect XPTO for MyNamespace.MyType \r\n" +
173 " pointcut method(int DoSomething(*))" +
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
);
189 public void ParsingPointcutDeclarationForMethodDoSomethingWithTwoArgumentsAndRetType()
191 AspectParser parser
= CreateParser(
192 "aspect XPTO for MyNamespace.MyType \r\n" +
194 " pointcut method(int DoSomething(string, int))" +
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] );
212 public void ParsingPointcutDeclarationForMethodDoSomethingWithTwoArgumentsAndRegEx()
214 AspectParser parser
= CreateParser(
215 "aspect XPTO for MyNamespace.MyType \r\n" +
217 " pointcut method(int DoSomething(string, *))" +
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] );
235 public void ParsingPointcutDeclarationForMethodWithRegExp()
237 AspectParser parser
= CreateParser(
238 "aspect XPTO for MyNamespace.MyType \r\n" +
240 " pointcut method(* DoS.*(*))" +
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
);
255 public void ParsingPointcutDeclarationForAllMethodsReturningString()
257 AspectParser parser
= CreateParser(
258 "aspect XPTO for MyNamespace.MyType \r\n" +
260 " pointcut method(String (*))" +
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
);
276 public void ParsingPointcutDeclarationForMethodWithRegExpOnReturnType()
278 AspectParser parser
= CreateParser(
279 "aspect XPTO for MyNamespace.MyType \r\n" +
281 " pointcut method(strin.* DoSomething(*))" +
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
);
297 public void ParsingPointcutDeclarationForPropertyNameNoArguments()
299 AspectParser parser
= CreateParser(
300 "aspect XPTO for MyNamespace.MyType \r\n" +
302 " pointcut property(* Name)" +
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
);
317 public void ParsingInterceptorRefForProperty()
319 AspectParser parser
= CreateParser(
320 "aspect XPTO for MyNamespace.MyType \r\n" +
322 " pointcut property(* Name)" +
323 " advice(\"logger\")" +
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
);
337 public void ParsingInterceptorTypeForProperty()
339 AspectParser parser
= CreateParser(
340 "aspect XPTO for MyNamespace.MyType \r\n" +
342 " pointcut property(* Name)" +
343 " advice( My.NS.Interceptor in My.Assembly )" +
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
)
362 allparams
= "(" + param1
+ ")";
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" +
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];
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
);
388 Assert
.AreEqual(1, pc
.Method
.Arguments
.Length
, "1 Param: " + s
);
389 Assert
.AreEqual(param1
, pc
.Method
.Arguments
[0], "Param: " + s
);
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
);
411 [ExpectedException(typeof(MismatchedTokenException
))]
412 public void ParsingInvalidPointcutDeclaration()
414 AspectParser parser
= CreateParser(
415 "aspect XPTO for MyNamespace.MyType \r\n" +
417 " pointcut method|propertyread|property(*)" +