1 // Copyright 2004-2007 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 ParserAspectTestCase.
29 public class ParserAspectTestCase
: ParserTestCaseBase
32 public void ParsingAspectEmptyDeclaration()
34 AspectParser parser
= CreateParser(
35 "aspect XPTO for MyNamespace.MyType \r\n" +
37 EngineConfiguration conf
= parser
.Parse();
38 Assert
.IsNotNull(conf
);
39 Assert
.IsNotNull(conf
.Aspects
);
40 Assert
.AreEqual(1, conf
.Aspects
.Count
);
41 AspectDefinition def
= conf
.Aspects
[0];
42 Assert
.IsNotNull(def
);
43 Assert
.AreEqual("XPTO", def
.Name
);
44 Assert
.AreEqual("MyNamespace.MyType", def
.TargetType
.SingleType
.TypeName
);
45 Assert
.AreEqual(TargetStrategyEnum
.SingleType
, def
.TargetType
.TargetStrategy
);
49 public void ParsingAspectForAssignable()
51 AspectParser parser
= CreateParser(
52 "aspect XPTO for [ assignableFrom(Customer) ] \r\n" +
54 EngineConfiguration conf
= parser
.Parse();
55 Assert
.IsNotNull(conf
);
56 Assert
.IsNotNull(conf
.Aspects
);
57 Assert
.AreEqual(1, conf
.Aspects
.Count
);
58 AspectDefinition def
= conf
.Aspects
[0];
59 Assert
.IsNotNull(def
);
60 Assert
.AreEqual("XPTO", def
.Name
);
61 Assert
.AreEqual(TargetStrategyEnum
.Assignable
, def
.TargetType
.TargetStrategy
);
62 Assert
.AreEqual("Customer", def
.TargetType
.AssignType
.TypeName
);
66 [ExpectedException(typeof (MismatchedTokenException
))]
67 public void InvalidAspectForAssignable()
69 AspectParser parser
= CreateParser(
70 "aspect XPTO for [ assignableFrom() ] \r\n" +
72 EngineConfiguration conf
= parser
.Parse();
76 public void ParsingAspectForCustomMatcher()
78 AspectParser parser
= CreateParser(
79 "aspect XPTO for [ customMatcher(MyMatcher) ] \r\n" +
81 EngineConfiguration conf
= parser
.Parse();
82 Assert
.IsNotNull(conf
);
83 Assert
.IsNotNull(conf
.Aspects
);
84 Assert
.AreEqual(1, conf
.Aspects
.Count
);
85 AspectDefinition def
= conf
.Aspects
[0];
86 Assert
.IsNotNull(def
);
87 Assert
.AreEqual("XPTO", def
.Name
);
88 Assert
.AreEqual(TargetStrategyEnum
.Custom
, def
.TargetType
.TargetStrategy
);
89 Assert
.AreEqual("MyMatcher", def
.TargetType
.CustomMatcherType
.TypeName
);
93 public void ParsingAspectForNamespace()
95 AspectParser parser
= CreateParser(
96 "aspect XPTO for [ my.namespace.types ] \r\n" +
98 EngineConfiguration conf
= parser
.Parse();
99 Assert
.IsNotNull(conf
);
100 Assert
.IsNotNull(conf
.Aspects
);
101 Assert
.AreEqual(1, conf
.Aspects
.Count
);
102 AspectDefinition def
= conf
.Aspects
[0];
103 Assert
.IsNotNull(def
);
104 Assert
.AreEqual("XPTO", def
.Name
);
105 Assert
.AreEqual(TargetStrategyEnum
.Namespace
, def
.TargetType
.TargetStrategy
);
106 Assert
.AreEqual("my.namespace.types", def
.TargetType
.NamespaceRoot
);
107 Assert
.IsFalse(def
.TargetType
.IncludeSubNamespace
);
111 public void ParsingAspectForNamespaceWithExcludes()
113 AspectParser parser
= CreateParser(
114 "aspect XPTO for [ my.namespace.types excludes(Customer;Author) ] \r\n" +
116 EngineConfiguration conf
= parser
.Parse();
117 Assert
.IsNotNull(conf
);
118 Assert
.IsNotNull(conf
.Aspects
);
119 Assert
.AreEqual(1, conf
.Aspects
.Count
);
120 AspectDefinition def
= conf
.Aspects
[0];
121 Assert
.IsNotNull(def
);
122 Assert
.AreEqual("XPTO", def
.Name
);
123 Assert
.AreEqual(TargetStrategyEnum
.Namespace
, def
.TargetType
.TargetStrategy
);
124 Assert
.AreEqual("my.namespace.types", def
.TargetType
.NamespaceRoot
);
125 Assert
.IsFalse(def
.TargetType
.IncludeSubNamespace
);
126 Assert
.AreEqual(2, def
.TargetType
.Excludes
.Count
);
130 [ExpectedException(typeof (MismatchedTokenException
))]
131 public void InvalidAspectForCustomMatcher()
133 AspectParser parser
= CreateParser(
134 "aspect XPTO for [ customMatcher() ] \r\n" +
136 EngineConfiguration conf
= parser
.Parse();
140 public void ParsingAspectEmptyDeclarationWithFullType()
142 AspectParser parser
= CreateParser(
143 "aspect XPTO for MyNamespace.MyType in My.New.Assembly \r\n" +
145 EngineConfiguration conf
= parser
.Parse();
146 Assert
.IsNotNull(conf
);
147 Assert
.IsNotNull(conf
.Aspects
);
148 Assert
.AreEqual(1, conf
.Aspects
.Count
);
149 AspectDefinition def
= conf
.Aspects
[0];
150 Assert
.IsNotNull(def
);
151 Assert
.AreEqual("XPTO", def
.Name
);
152 Assert
.AreEqual("MyNamespace.MyType", def
.TargetType
.SingleType
.TypeName
);
153 Assert
.AreEqual("My.New.Assembly", def
.TargetType
.SingleType
.AssemblyReference
.AssemblyName
);
157 public void ParsingAspectWithMixinRefDeclaration()
159 AspectParser parser
= CreateParser(
160 "aspect XPTO for MyNamespace.MyType \r\n" +
162 " include \"customer\"" +
166 EngineConfiguration conf
= parser
.Parse();
167 AspectDefinition def
= conf
.Aspects
[0];
168 Assert
.AreEqual(1, def
.Mixins
.Count
);
170 MixinDefinition typeName
= def
.Mixins
[0];
171 Assert
.AreEqual(TargetTypeEnum
.Link
, typeName
.TypeReference
.TargetType
);
172 Assert
.AreEqual("customer", typeName
.TypeReference
.LinkRef
);
176 public void ParsingAspectWithSingleMixinDeclaration()
178 AspectParser parser
= CreateParser(
179 "aspect XPTO for MyNamespace.MyType \r\n" +
181 " include MyNamespace.Type in MyAssembly " +
185 EngineConfiguration conf
= parser
.Parse();
186 AspectDefinition def
= conf
.Aspects
[0];
187 Assert
.AreEqual(1, def
.Mixins
.Count
);
189 MixinDefinition typeName
= def
.Mixins
[0];
190 Assert
.AreEqual(TargetTypeEnum
.Type
, typeName
.TypeReference
.TargetType
);
191 Assert
.AreEqual("MyNamespace.Type", typeName
.TypeReference
.TypeName
);
192 Assert
.AreEqual("MyAssembly", typeName
.TypeReference
.AssemblyReference
.AssemblyName
);
196 public void ParsingAspectWithAFewMixinDeclarations()
198 AspectParser parser
= CreateParser(
199 "aspect XPTO for MyNamespace.MyType \r\n" +
201 " include MyNamespace.Type1 in MyAssembly1 " +
202 " include MyNamespace.Type2 in MyAssembly2 " +
203 " include MyNamespace.Type3 in MyAssembly3 " +
207 EngineConfiguration conf
= parser
.Parse();
208 AspectDefinition def
= conf
.Aspects
[0];
209 Assert
.AreEqual(3, def
.Mixins
.Count
);
211 MixinDefinition typeName
= def
.Mixins
[0];
212 Assert
.AreEqual(TargetTypeEnum
.Type
, typeName
.TypeReference
.TargetType
);
213 Assert
.AreEqual("MyNamespace.Type1", typeName
.TypeReference
.TypeName
);
214 Assert
.AreEqual("MyAssembly1", typeName
.TypeReference
.AssemblyReference
.AssemblyName
);
216 typeName
= def
.Mixins
[1];
217 Assert
.AreEqual(TargetTypeEnum
.Type
, typeName
.TypeReference
.TargetType
);
218 Assert
.AreEqual("MyNamespace.Type2", typeName
.TypeReference
.TypeName
);
219 Assert
.AreEqual("MyAssembly2", typeName
.TypeReference
.AssemblyReference
.AssemblyName
);
221 typeName
= def
.Mixins
[2];
222 Assert
.AreEqual(TargetTypeEnum
.Type
, typeName
.TypeReference
.TargetType
);
223 Assert
.AreEqual("MyNamespace.Type3", typeName
.TypeReference
.TypeName
);
224 Assert
.AreEqual("MyAssembly3", typeName
.TypeReference
.AssemblyReference
.AssemblyName
);
228 [ExpectedException(typeof (NoViableAltException
))]
229 public void ParsingInvalidAspectEmptyDeclaration()
231 AspectParser parser
= CreateParser(
232 "aspect XPTO for MyNamespace.MyType \r\n" +