Added container accessor to Castle.Core
[castle.git] / AspectSharp / AspectSharp.Tests / AspectEngineTestCase.cs
blob5f20f7f6023267247c41eb84edd3f419a6fd938c
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.Tests
17 using System;
18 using AspectSharp.Builder;
19 using AspectSharp.Tests.Classes;
20 using NUnit.Framework;
22 [TestFixture]
23 public class AspectEngineTestCase
25 public AspectEngineTestCase()
29 [SetUp]
30 public void Init()
32 LogInvocationInterceptor.Clear();
35 [Test]
36 public void ClassWithConstructorArguments()
38 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
39 " " +
40 " aspect MyAspect for ComplexClass " +
41 " " +
42 " pointcut method|property(*)" +
43 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
44 " end" +
45 " " +
46 " end ";
48 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
49 AspectEngine engine = builder.Build();
51 ComplexClass instance = null;
53 instance = engine.WrapClass(typeof(ComplexClass), "Eric Cartman") as ComplexClass;
54 Assert.AreEqual("Eric Cartman", instance.Name);
55 Assert.IsFalse(instance.Started);
56 InvokeAndAssert(instance);
58 instance = engine.WrapClass(typeof(ComplexClass), "Kenny McKormick", true) as ComplexClass;
59 Assert.AreEqual("Kenny McKormick", instance.Name);
60 Assert.IsTrue(instance.Started);
61 InvokeAndAssert(instance);
63 String[] messages = LogInvocationInterceptor.Messages;
64 Assert.AreEqual( 20, messages.Length );
67 [Test]
68 public void ClassWithConstructorArgumentsAndNoAspects()
70 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
71 " interceptors [" +
72 " \"key\" : DummyInterceptor " +
73 " ]" +
74 " mixins [" +
75 " \"key\" : DummyMixin " +
76 " ]" +
77 " " +
78 " aspect McBrother for DummyCustomer " +
79 " include \"key\"" +
80 " " +
81 " pointcut method(*)" +
82 " advice(\"key\")" +
83 " end" +
84 " " +
85 " end ";
87 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
88 AspectEngine engine = builder.Build();
90 ComplexClass instance = null;
92 instance = engine.WrapClass(typeof(ComplexClass), "Eric Cartman") as ComplexClass;
93 Assert.AreEqual("Eric Cartman", instance.Name);
94 Assert.IsFalse(instance.Started);
98 [Test]
99 public void InterfaceWrap()
101 String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
102 " " +
103 " aspect MyAspect for [ assignableFrom(IPartiallyComplex) ] " +
104 " " +
105 " pointcut method|property(*)" +
106 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
107 " end" +
108 " " +
109 " end ";
111 AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
112 AspectEngine engine = builder.Build();
114 IPartiallyComplex instance = null;
116 instance = engine.WrapInterface(typeof(IPartiallyComplex), new ComplexClass()) as IPartiallyComplex;
118 instance.DoNothing();
119 instance.DoSomething();
121 String[] messages = LogInvocationInterceptor.Messages;
122 Assert.AreEqual( 2, messages.Length );
125 private static void InvokeAndAssert(ComplexClass instance)
127 instance.DoNothing();
128 instance.DoSomething();
129 int arg = 1;
131 instance.DoSomething(arg);
132 instance.DoSomething(arg, "hiya");
134 //TODO: Intercept by ref calls.
135 //Assert.AreEqual(arg, instance.Pong(ref arg));
137 instance.Name = "John Johnson";
138 Assert.AreEqual( "John Johnson", instance.Name );
139 instance.Started = true;
140 Assert.IsTrue( instance.Started );