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
.Tests
18 using AspectSharp
.Builder
;
19 using AspectSharp
.Tests
.Classes
;
20 using NUnit
.Framework
;
23 public class AspectEngineTestCase
25 public AspectEngineTestCase()
32 LogInvocationInterceptor
.Clear();
36 public void ClassWithConstructorArguments()
38 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
40 " aspect MyAspect for ComplexClass " +
42 " pointcut method|property(*)" +
43 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
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
);
68 public void ClassWithConstructorArgumentsAndNoAspects()
70 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
72 " \"key\" : DummyInterceptor " +
75 " \"key\" : DummyMixin " +
78 " aspect McBrother for DummyCustomer " +
81 " pointcut method(*)" +
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
);
99 public void InterfaceWrap()
101 String contents
= "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
103 " aspect MyAspect for [ assignableFrom(IPartiallyComplex) ] " +
105 " pointcut method|property(*)" +
106 " advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
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();
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
);