Added container accessor to Castle.Core
[castle.git] / AspectSharp / AspectSharp.Lang.Tests / Types / ResolveTypesStepTestCase.cs
blobd404e048f90cf0f399d32c266fea981dedcec733
1 // Copyright 2004-2007 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.Types
17 using System;
19 using NUnit.Framework;
21 using AspectSharp.Lang.AST;
22 using AspectSharp.Lang.Steps;
23 using AspectSharp.Lang.Steps.Semantic;
24 using AspectSharp.Lang.Steps.Types;
25 using AspectSharp.Lang.Tests.Types.Mixins;
26 using AspectSharp.Lang.Tests.Types.Matcher;
28 /// <summary>
29 /// Summary description for ResolveTypesStepTestCase.
30 /// </summary>
31 [TestFixture]
32 public class ResolveTypesStepTestCase : ParserTestCaseBase
34 [Test]
35 public void AssembliesResolved()
37 String content = "import System.Collection in System";
39 EngineConfiguration conf = ProcessContent(content);
40 Assert.IsFalse(_context.HasErrors);
42 foreach(ImportDirective import in conf.Imports)
44 Assert.IsNotNull( import.AssemblyReference );
45 Assert.IsNotNull( import.AssemblyReference.ResolvedAssembly );
49 [Test]
50 public void MixinResolvedFullTypeName()
52 String content = "mixins \r\n" +
53 "[" +
54 "\"customer\" : System.Collections.ArrayList" +
55 "]";
57 EngineConfiguration conf = ProcessContent(content);
58 Assert.IsFalse(_context.HasErrors);
59 MixinEntryDefinition mixin = conf.Mixins[0];
60 Assert.IsNotNull( mixin.Key );
61 Assert.IsNotNull( mixin.TypeReference );
62 Assert.IsNotNull( mixin.TypeReference.ResolvedType );
63 Assert.AreEqual( typeof(System.Collections.ArrayList), mixin.TypeReference.ResolvedType );
66 [Test]
67 public void MixinResolvedUsingImport()
69 String content = "import System.Collections " +
70 "mixins \r\n" +
71 "[" +
72 "\"customer\" : ArrayList" +
73 "]";
75 EngineConfiguration conf = ProcessContent(content);
76 Assert.IsFalse(_context.HasErrors);
77 MixinEntryDefinition mixin = conf.Mixins[0];
78 Assert.IsNotNull( mixin.Key );
79 Assert.IsNotNull( mixin.TypeReference );
80 Assert.IsNotNull( mixin.TypeReference.ResolvedType );
81 Assert.AreEqual( typeof(System.Collections.ArrayList), mixin.TypeReference.ResolvedType );
84 [Test]
85 public void MixinResolvedFullTypeNameAndAssembly()
87 String content = " " +
88 "mixins \r\n" +
89 "[" +
90 "\"customer\" : AspectSharp.Lang.Tests.Types.Mixins.MockMixin in AspectSharp.Lang.Tests" +
91 "]";
93 EngineConfiguration conf = ProcessContent(content);
94 Assert.IsFalse(_context.HasErrors);
95 MixinEntryDefinition mixin = conf.Mixins[0];
96 Assert.IsNotNull( mixin.Key );
97 Assert.IsNotNull( mixin.TypeReference );
98 Assert.IsNotNull( mixin.TypeReference.ResolvedType );
99 Assert.AreEqual( typeof(MockMixin), mixin.TypeReference.ResolvedType );
102 [Test]
103 public void GlobalsCorrectlyReferenced()
105 String content = " " +
106 "interceptors \r\n" +
107 "[" +
108 "\"customer\" : System.Collections.Hashtable" +
109 "]" +
110 "mixins \r\n" +
111 "[" +
112 "\"customer\" : AspectSharp.Lang.Tests.Types.Mixins.MockMixin in AspectSharp.Lang.Tests" +
113 "]" +
114 " " +
115 "aspect McBrother for System.Collections.ArrayList " +
116 " " +
117 " include \"customer\"" +
118 " " +
119 " pointcut method(*)" +
120 " advice(\"customer\")" +
121 " end" +
122 " " +
123 "end " +
124 " ";
126 EngineConfiguration conf = ProcessContent(content);
127 Assert.IsFalse(_context.HasErrors);
129 AspectDefinition aspect = conf.Aspects[0];
130 MixinDefinition mixin = aspect.Mixins[0];
131 InterceptorDefinition interceptor = aspect.PointCuts[0].Advices[0];
133 Assert.IsNotNull( mixin.TypeReference );
134 Assert.IsNotNull( mixin.TypeReference.ResolvedType );
135 Assert.AreEqual( typeof(MockMixin), mixin.TypeReference.ResolvedType );
137 Assert.IsNotNull( interceptor.TypeReference );
138 Assert.IsNotNull( interceptor.TypeReference.ResolvedType );
139 Assert.AreEqual( typeof(System.Collections.Hashtable), interceptor.TypeReference.ResolvedType );
142 [Test]
143 public void AspectTarget()
145 String content = "" +
146 "aspect McBrother for AspectSharp.Lang.Tests.Types.Mixins.MockMixin in AspectSharp.Lang.Tests " +
147 " " +
148 "end " +
149 " ";
151 EngineConfiguration conf = ProcessContent(content);
152 Assert.IsFalse(_context.HasErrors);
154 AspectDefinition aspect = conf.Aspects[0];
156 Assert.IsNotNull( aspect.TargetType.SingleType );
157 Assert.IsNotNull( aspect.TargetType.SingleType.ResolvedType );
158 Assert.AreEqual( typeof(MockMixin), aspect.TargetType.SingleType.ResolvedType );
161 [Test]
162 public void AspectTargetingNamespace()
164 String content = "" +
165 "aspect McBrother for [AspectSharp.Lang.Tests.Types.Mixins] " +
166 " " +
167 "end " +
168 " ";
170 EngineConfiguration conf = ProcessContent(content);
171 Assert.IsFalse(_context.HasErrors);
173 AspectDefinition aspect = conf.Aspects[0];
175 Assert.AreEqual( TargetStrategyEnum.Namespace, aspect.TargetType.TargetStrategy );
176 Assert.AreEqual( "AspectSharp.Lang.Tests.Types.Mixins", aspect.TargetType.NamespaceRoot );
177 Assert.AreEqual( 0, aspect.TargetType.Excludes.Count );
178 Assert.IsNull( aspect.TargetType.SingleType );
181 [Test]
182 public void AspectTargetingNamespaceWithExcludes()
184 String content = "import System.Collections " +
185 "aspect McBrother for [System.Collections excludes(Stack) ] " +
186 " " +
187 "end " +
188 " ";
190 EngineConfiguration conf = ProcessContent(content);
191 Assert.IsFalse(_context.HasErrors);
193 AspectDefinition aspect = conf.Aspects[0];
195 Assert.AreEqual( TargetStrategyEnum.Namespace, aspect.TargetType.TargetStrategy );
196 Assert.AreEqual( "System.Collections", aspect.TargetType.NamespaceRoot );
197 Assert.AreEqual( 1, aspect.TargetType.Excludes.Count );
199 TypeReference typeRef = aspect.TargetType.Excludes[0];
200 Assert.AreEqual( typeof(System.Collections.Stack), typeRef.ResolvedType );
203 [Test]
204 public void AspectTargetingNamespaceWithMoreExcludes()
206 String content = "import System.Collections " +
207 "aspect McBrother for [System.Collections excludes(Stack; ArrayList) ] " +
208 " " +
209 "end " +
210 " ";
212 EngineConfiguration conf = ProcessContent(content);
213 Assert.IsFalse(_context.HasErrors);
215 AspectDefinition aspect = conf.Aspects[0];
217 Assert.AreEqual( TargetStrategyEnum.Namespace, aspect.TargetType.TargetStrategy );
218 Assert.AreEqual( "System.Collections", aspect.TargetType.NamespaceRoot );
219 Assert.AreEqual( 2, aspect.TargetType.Excludes.Count );
221 TypeReference typeRef = aspect.TargetType.Excludes[0];
222 Assert.AreEqual( typeof(System.Collections.Stack), typeRef.ResolvedType );
223 typeRef = aspect.TargetType.Excludes[1];
224 Assert.AreEqual( typeof(System.Collections.ArrayList), typeRef.ResolvedType );
227 [Test]
228 public void AspectTargetingCustom()
230 String content = "" +
231 "aspect McBrother for [ customMatcher(AspectSharp.Lang.Tests.Types.Matcher.ValidMatcher) ] " +
232 " " +
233 "end " +
234 " ";
236 EngineConfiguration conf = ProcessContent(content);
237 Assert.IsFalse(_context.HasErrors);
239 AspectDefinition aspect = conf.Aspects[0];
241 Assert.AreEqual( TargetStrategyEnum.Custom, aspect.TargetType.TargetStrategy );
242 Assert.IsNotNull( aspect.TargetType.CustomMatcherType );
243 Assert.IsNotNull( aspect.TargetType.CustomMatcherType.ResolvedType );
244 Assert.AreEqual( typeof(ValidMatcher), aspect.TargetType.CustomMatcherType.ResolvedType );
247 [Test]
248 public void AspectTargetingAssignable()
250 String content = "" +
251 "aspect McBrother for [ assignableFrom(System.Collections.IList) ] " +
252 " " +
253 "end " +
254 " ";
256 EngineConfiguration conf = ProcessContent(content);
257 Assert.IsFalse(_context.HasErrors);
259 AspectDefinition aspect = conf.Aspects[0];
261 Assert.AreEqual( TargetStrategyEnum.Assignable, aspect.TargetType.TargetStrategy );
262 Assert.IsNotNull( aspect.TargetType.AssignType );
263 Assert.IsNotNull( aspect.TargetType.AssignType.ResolvedType );
264 Assert.AreEqual( typeof(System.Collections.IList), aspect.TargetType.AssignType.ResolvedType );
267 protected override void AddSteps(StepChainBuilder chain)
269 chain.AddStep(new SemanticAnalizerStep());
270 chain.AddStep(new ResolveTypesStep());