|
From: Steve F. <sm...@us...> - 2005-05-26 20:51:58
|
Update of /cvsroot/nmock/nmock2/src/NMock2.Test/Monitoring In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2.Test/Monitoring Added Files: ProxyInvokableAdapterTest.cs InvocationTest.cs ProxiedObjectIdentityTest.cs MockInvokable.cs ParameterListTest.cs InvokerTest.cs ParameterInfoStub.cs InvocationSemanticsTest.cs MultiInterfaceFactoryTest.cs MethodInfoStub.cs Log Message: first upload of nmock2 --- NEW FILE: MethodInfoStub.cs --- using System; using System.Globalization; using System.Reflection; namespace NMock2.Test.Monitoring { public class MethodInfoStub : MethodInfo { private readonly string name; private ParameterInfo[] parameters; public MethodInfoStub( string name, params ParameterInfo[] parameters ) { this.name = name; this.parameters = parameters; } public override object[] GetCustomAttributes(bool inherit) { throw new NotImplementedException(); } public override object[] GetCustomAttributes(Type attributeType, bool inherit) { throw new NotImplementedException(); } public override bool IsDefined(Type attributeType, bool inherit) { throw new NotImplementedException(); } public override string Name { get { return name; } } public override Type DeclaringType { get { throw new NotImplementedException(); } } public override ParameterInfo[] GetParameters() { return parameters; } public override MethodImplAttributes GetMethodImplementationFlags() { throw new NotImplementedException(); } public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) { throw new NotImplementedException(); } public override RuntimeMethodHandle MethodHandle { get { throw new NotImplementedException(); } } public override MethodInfo GetBaseDefinition() { throw new NotImplementedException(); } public Type StubReturnType = typeof(object); public override Type ReturnType { get { return StubReturnType; } } public override ICustomAttributeProvider ReturnTypeCustomAttributes { get { throw new NotImplementedException(); } } public override MethodAttributes Attributes { get { throw new NotImplementedException(); } } public override Type ReflectedType { get { throw new NotImplementedException(); } } } } --- NEW FILE: InvocationTest.cs --- using System; using System.Reflection; using NUnit.Framework; using NMock2.Monitoring; namespace NMock2.Test.Monitoring { [TestFixture] public class InvocationTest { object receiver; MethodInfoStub method; Invocation invocation; object result; Exception exception; [SetUp] public void SetUp() { receiver = "receiver"; result = "result"; method = new MethodInfoStub("method"); invocation = new Invocation(receiver,method,new object[0]); exception = new Exception(); } [Test] public void StoresResultToReturn() { invocation.Result = result; Assert.AreSame(result, invocation.Result, "should store result"); Assert.IsFalse(invocation.IsThrowing, "should not be throwing"); Assert.IsNull(invocation.Exception, "should not store an exception"); } [Test] public void StoresExceptionToThrow() { invocation.Exception = exception; Assert.AreSame(exception, invocation.Exception, "should store exception"); Assert.IsTrue(invocation.IsThrowing, "should be throwing"); Assert.IsNull(invocation.Result, "should not store a result"); } [Test] public void SettingResultClearsException() { invocation.Exception = exception; invocation.Result = result; Assert.AreSame(result, invocation.Result, "should store result"); Assert.IsFalse(invocation.IsThrowing, "should not be throwing"); Assert.IsNull(invocation.Exception, "should not store an exception"); } [Test] public void SettingExceptionClearsResult() { invocation.Result = result; invocation.Exception = exception; Assert.AreSame(exception, invocation.Exception, "should store exception"); Assert.IsTrue(invocation.IsThrowing, "should be throwing"); Assert.IsNull(invocation.Result, "should not store a result"); } [Test, ExpectedException(typeof(ArgumentNullException))] public void DoesNotAllowNullException() { invocation.Exception = null; } [Test, ExpectedException(typeof(ArgumentException))] public void DoesNotAllowSettingNonNullResultOfVoidMethod() { method.StubReturnType = typeof(void); invocation.Result = "some value"; } [Test] public void AllowsSettingNullResultOfVoidMethod() { method.StubReturnType = typeof(void); invocation.Result = null; } [Test, ExpectedException(typeof(ArgumentException))] public void DoesNotAllowNullResultForMethodThatReturnsAValueType() { method.StubReturnType = typeof(int); invocation.Result = null; } public class A {} public class B: A {} public class C {} [Test] public void DoesNotAllowSettingIncompatibleResultType() { method.StubReturnType = typeof(A); invocation.Result = new B(); try { invocation.Result = new C(); Assert.Fail("expected ArgumentException"); } catch(ArgumentException) { //expected } } public interface IFoo { string Foo(string input, out string output); } public static readonly MethodInfo FOO_METHOD = typeof(IFoo).GetMethod("Foo"); public class MockFoo : IFoo { public bool FooWasInvoked = false; public string Foo_ExpectedInput; public string Foo_Output = "output"; public string Foo_Result; public Exception Foo_Exception = null; public string Foo(string input, out string output) { FooWasInvoked = true; Assert.AreEqual(Foo_ExpectedInput, input, "input"); if (Foo_Exception != null) { throw Foo_Exception; } else { output = Foo_Output; return Foo_Result; } } } [Test] public void CanBeInvokedOnAnotherObject() { string input = "INPUT"; string output = "OUTPUT"; string result = "RESULT"; invocation = new Invocation(receiver, FOO_METHOD, new object[]{input, null}); MockFoo mockFoo = new MockFoo(); mockFoo.Foo_ExpectedInput = input; mockFoo.Foo_Output = output; mockFoo.Foo_Result = result; invocation.InvokeOn(mockFoo); Assert.IsTrue(mockFoo.FooWasInvoked, "Foo should have been invoked"); Assert.AreEqual(invocation.Result, result, "result"); Assert.AreEqual(invocation.Parameters[1], output, "output"); } [Test] public void TrapsExceptionsWhenInvokedOnAnotherObject() { Exception exception = new Exception("thrown from Foo"); invocation = new Invocation(receiver, FOO_METHOD, new object[]{"input",null}); MockFoo mockFoo = new MockFoo(); mockFoo.Foo_ExpectedInput = "input"; mockFoo.Foo_Exception = exception; invocation.InvokeOn(mockFoo); Assert.IsTrue(mockFoo.FooWasInvoked, "Foo should have been invoked"); Assert.AreSame(exception, invocation.Exception, "exception"); } public interface SugarMethods { int Property { get; set; } int this[string s, int i] { get; set; } event EventHandler Event; } private void HandleEvent(object sender, EventArgs args) { } [Test] public void DescriptionOfInvocationOfPropertyGetterDoesNotShowSugaredMethod() { MethodInfo getter = typeof(SugarMethods).GetMethod("get_Property", new Type[0]); invocation = new Invocation(receiver, getter, new object[0]); AssertDescription.IsEqual(invocation, "receiver.Property"); } [Test] public void DescriptionOfInvocationOfPropertySetterDoesNotShowSugaredMethod() { MethodInfo setter = typeof(SugarMethods).GetMethod("set_Property", new Type[] { typeof(int) }); invocation = new Invocation(receiver, setter, new object[] { 10 }); AssertDescription.IsEqual(invocation, "receiver.Property = <10>"); } [Test] public void DescriptionOfInvocationOfIndexerGetterDoesNotShowSugaredMethod() { MethodInfo getter = typeof(SugarMethods).GetMethod( "get_Item", new Type[]{typeof(string), typeof(int)}); invocation = new Invocation(receiver, getter, new object[]{"hello", 10}); AssertDescription.IsEqual(invocation, "receiver[\"hello\", <10>]"); } [Test] public void DescriptionOfInvocationOfIndexerSetterDoesNotShowSugaredMethod() { MethodInfo getter = typeof(SugarMethods).GetMethod( "set_Item", new Type[]{typeof(string), typeof(int), typeof(int)}); invocation = new Invocation(receiver, getter, new object[]{"hello", 10, 11}); AssertDescription.IsEqual(invocation, "receiver[\"hello\", <10>] = <11>"); } [Test] public void DescriptionOfEventAdderDoesNotShowSugaredMethod() { MethodInfo adder = typeof(SugarMethods).GetMethod( "add_Event", new Type[]{typeof(EventHandler)} ); Delegate handler = new EventHandler(HandleEvent); invocation = new Invocation(receiver, adder, new object[]{handler}); AssertDescription.IsEqual(invocation, "receiver += <System.EventHandler>"); } [Test] public void DescriptionOfEventRemoverDoesNotShowSugaredMethod() { MethodInfo adder = typeof(SugarMethods).GetMethod( "remove_Event", new Type[]{typeof(EventHandler)} ); Delegate handler = new EventHandler(HandleEvent); invocation = new Invocation(receiver, adder, new object[]{handler}); AssertDescription.IsEqual(invocation, "receiver -= <System.EventHandler>"); } } } --- NEW FILE: ParameterInfoStub.cs --- using System.Reflection; namespace NMock2.Test.Monitoring { public class ParameterInfoStub : ParameterInfo { public ParameterInfoStub( string name, ParameterAttributes attributes ) { this.NameImpl = name; this.AttrsImpl = attributes; } } } --- NEW FILE: ProxyInvokableAdapterTest.cs --- using System; using System.Reflection; using NUnit.Framework; using NMock2.Monitoring; namespace NMock2.Test.Monitoring { public interface ProxiedInterface { string Return(); void Throw(); void InOut( string inArg, ref string refArg, out string outArg ); } [TestFixture] public class ProxyInvokableAdapterTest { static readonly MethodInfo RETURN_METHOD = typeof(ProxiedInterface).GetMethod("Return"); static readonly MethodInfo INOUT_METHOD = typeof(ProxiedInterface).GetMethod("InOut"); MockInvokable invokable; ProxyInvokableAdapter adapter; ProxiedInterface transparentProxy; [SetUp] public void SetUp() { invokable = new MockInvokable(); adapter = new ProxyInvokableAdapter( typeof(ProxiedInterface), invokable ); transparentProxy = (ProxiedInterface)adapter.GetTransparentProxy(); } [Test] public void CapturesInvocationsOnTransparentProxyAndForwardsToInvokableObject() { invokable.Expected = new Invocation( transparentProxy, RETURN_METHOD, new object[0] ); transparentProxy.Return(); Assert.IsNotNull(invokable.Actual, "received invocation"); } [Test] public void ReturnsResultFromInvocationToCallerOfProxy() { invokable.ResultSetOnInvocation = "result"; Assert.AreEqual( invokable.ResultSetOnInvocation, transparentProxy.Return() ); } class TestException : Exception {} [Test, ExpectedException(typeof(TestException))] public void ThrowsExceptionFromInvocationToCallerOfProxy() { invokable.ExceptionSetOnInvocation = new TestException(); transparentProxy.Throw(); } [Test] public void ReturnsOutAndRefParametersFromInvocationToCallerOfProxy() { invokable.Expected = new Invocation(transparentProxy,INOUT_METHOD,new object[]{"inArg","refArg",null}); invokable.Outputs = new object[]{null, "returnedRefArg", "returnedOutArg"}; string refArg = "refArg"; string outArg; transparentProxy.InOut("inArg", ref refArg, out outArg); Assert.AreEqual("returnedRefArg", refArg); Assert.AreEqual("returnedOutArg", outArg); } [Test, ExpectedException(typeof(TestException))] public void PassesExceptionThrownByInvokableToCallerOfProxy() { invokable.ThrownException = new TestException(); transparentProxy.Throw(); } } } --- NEW FILE: MockInvokable.cs --- using System; using NUnit.Framework; using NMock2.Monitoring; namespace NMock2.Test.Monitoring { public class MockInvokable : IInvokable { public Invocation Expected; public Invocation Actual; public object[] Outputs; public object ResultSetOnInvocation = null; public Exception ExceptionSetOnInvocation = null; public Exception ThrownException = null; public bool expectNotCalled = false; public void ExpectNotCalled() { this.expectNotCalled = true; } public void Invoke(Invocation invocation) { Assert.IsFalse(expectNotCalled, "MockInvokable should not have been invoked"); Actual = invocation; if (Expected != null) Assert.AreEqual( Expected.Method, Actual.Method, "method"); if (Outputs != null) { for (int i = 0; i < Actual.Parameters.Count; i++) { if (!Actual.Method.GetParameters()[i].IsIn) { Actual.Parameters[i] = Outputs[i]; } } } if (ThrownException != null) throw ThrownException; if (ExceptionSetOnInvocation != null) { invocation.Exception = ExceptionSetOnInvocation; } else if(invocation.Method.ReturnType != typeof(void)) { invocation.Result = ResultSetOnInvocation; } } } } --- NEW FILE: InvokerTest.cs --- using System.Reflection; using NUnit.Framework; using NMock2.Monitoring; using NMock2.Test.Monitoring; namespace NMock2.Test.Monitoring { [TestFixture] public class InvokerTest { public interface IFoo { void Foo(); } public interface IOther { void Other(); } public class MockFoo : IFoo { public bool FooWasInvoked = false; public void Foo() { FooWasInvoked = true; } } public static readonly MethodInfo FOO_METHOD = typeof(IFoo).GetMethod("Foo"); public static readonly MethodInfo OTHER_METHOD = typeof(IOther).GetMethod("Other"); private object receiver; private MockFoo target; private MockInvokable next; private Invoker invoker; [SetUp] public void SetUp() { receiver = new object(); target = new MockFoo(); next = new MockInvokable(); invoker = new Invoker(typeof(IFoo), this.target, this.next); } [Test] public void InvokesMethodOnObjectIfMethodIsDeclaredInSpecifiedType() { Invocation invocation = new Invocation(receiver, FOO_METHOD, new object[0]); invoker.Invoke(invocation); Assert.IsTrue(target.FooWasInvoked, "Foo should have been invoked on target"); } [Test] public void ForwardsInvocationsToNextIfMethodIsNotDeclaredInSpecifiedType() { Invocation invocation = new Invocation(receiver, OTHER_METHOD, new object[0]); next.Expected = invocation; invoker.Invoke(invocation); Assert.IsFalse(target.FooWasInvoked, "should not have invoked method on target"); Assert.AreSame(invocation, next.Actual, "should have passed invocation to next in chain"); } } } --- NEW FILE: ParameterListTest.cs --- using System; using System.Reflection; using NUnit.Framework; using NMock2.Monitoring; namespace NMock2.Test.Monitoring { [TestFixture] public class ParameterListTest { static readonly object inValue = "inValue"; static readonly object refValue = "refValue"; const int IN_PARAMETER_INDEX = 0; const int REF_PARAMETER_INDEX = 1; const int OUT_PARAMETER_INDEX = 2; MethodInfo method; object[] parameterValues; ParameterList list; [SetUp] public void SetUp() { ParameterInfo inParam = new ParameterInfoStub("inParam", ParameterAttributes.In); ParameterInfo refParam = new ParameterInfoStub("refParam", ParameterAttributes.None); ParameterInfo outParam = new ParameterInfoStub("outParam", ParameterAttributes.Out); method = new MethodInfoStub("method", inParam, refParam, outParam); parameterValues = new object[]{inValue, refValue, null}; list = new ParameterList(method, parameterValues); } [Test] public void ReturnsNumberOfParameters() { Assert.AreEqual( parameterValues.Length, list.Count, "size" ); } [Test] public void ReturnsValuesOfInParameters() { Assert.IsTrue(list.IsValueSet(IN_PARAMETER_INDEX), "in parameter should be set"); Assert.AreSame(inValue, list[IN_PARAMETER_INDEX], "in value"); Assert.IsTrue(list.IsValueSet(REF_PARAMETER_INDEX), "ref parameter should be set"); Assert.AreSame(refValue, list[REF_PARAMETER_INDEX], "ref value"); } [Test, ExpectedException(typeof(InvalidOperationException))] public void DoesNotAllowAccessToValuesOfUnsetOutParameters() { Assert.IsFalse(list.IsValueSet(OUT_PARAMETER_INDEX), "out parameter is not set"); Ignore( list[OUT_PARAMETER_INDEX] ); } [Test] public void CanSetValuesOfOutAndRefParameters() { object newRefValue = "newRefValue"; object outValue = "outValue"; list[REF_PARAMETER_INDEX] = newRefValue; list[OUT_PARAMETER_INDEX] = outValue; Assert.AreSame(newRefValue, list[REF_PARAMETER_INDEX], "new ref value"); Assert.IsTrue(list.IsValueSet(OUT_PARAMETER_INDEX), "out parameter is set"); Assert.AreSame(outValue, list[OUT_PARAMETER_INDEX], "out value"); } [Test, ExpectedException(typeof(InvalidOperationException))] public void DoesNotAllowValuesOfInputParametersToBeChanged() { object newValue = "newValue"; list[IN_PARAMETER_INDEX] = newValue; } private void Ignore(object o) { // The things you have to do to ignore compiler warnings! object o2 = o; o = o2; } } } --- NEW FILE: ProxiedObjectIdentityTest.cs --- using System; using System.Reflection; using NUnit.Framework; using NMock2.Monitoring; using NMock2.Test.Monitoring; namespace NMock2.Test.Monitoring { [TestFixture] public class ProxiedObjectIdentityTest { private static readonly MethodInfo EQUALS_METHOD = typeof(object).GetMethod("Equals",new Type[]{typeof(object)}); private static readonly MethodInfo TOSRING_METHOD = typeof(object).GetMethod("ToString",new Type[0]); private static readonly MethodInfo GETHASHCODE_METHOD = typeof(object).GetMethod("GetHashCode",new Type[0]); private object receiver; private object identityProvider; private MockInvokable next; private IInvokable id; [SetUp] public void SetUp() { receiver = new NamedObject("receiver"); identityProvider = new NamedObject("identityProvider"); next = new MockInvokable(); id = new ProxiedObjectIdentity(identityProvider, next); } [Test] public void ImplementsEqualsByComparingInvocationReceiversForIdentity() { next.ExpectNotCalled(); Invocation equalInvocation = new Invocation(receiver, EQUALS_METHOD, new object[]{receiver}); id.Invoke(equalInvocation); Assert.IsTrue((bool)equalInvocation.Result, "should have returned true"); object other = new NamedObject("other"); Invocation notEqualInvocation = new Invocation(receiver, EQUALS_METHOD, new object[]{other}); id.Invoke(notEqualInvocation); Assert.IsFalse((bool)notEqualInvocation.Result, "should not have returned true"); } [Test] public void ForwardsToStringToIdentityProvider() { Invocation toStringInvocation = new Invocation(receiver, TOSRING_METHOD, new object[0]); id.Invoke(toStringInvocation); Assert.AreEqual(identityProvider.ToString(), toStringInvocation.Result, "ToString()"); } [Test] public void ForwardsGetHashCodeToIdentityProvider() { Invocation getHashCodeInvocation = new Invocation(receiver, GETHASHCODE_METHOD, new object[0]); id.Invoke(getHashCodeInvocation); Assert.AreEqual(identityProvider.GetHashCode(), getHashCodeInvocation.Result, "GetHashCode()"); } [Test] public void ForwardsInvocationsOfOtherMethodsToNextInChain() { Invocation invocation = new Invocation(receiver, typeof(ICloneable).GetMethod("Clone"), new object[0]); id.Invoke(invocation); Assert.AreSame(invocation, next.Actual, "should have forwarded invocation to next in chain"); } } } --- NEW FILE: InvocationSemanticsTest.cs --- using System; using NUnit.Framework; namespace NMock2.Test.Monitoring { [TestFixture] public class InvocationSemanticsTest { const int REF_PARAM_VALUE = 1; const int OUT_PARAM_VALUE = 2; [Test] public void OutParametersAreSetAfterExceptionThrown() { int refParam = 0; int outParam = 0; try { SetAndThrow(ref refParam, out outParam); } catch( TestException ) { } Assert.AreEqual( REF_PARAM_VALUE, refParam ); Assert.AreEqual( OUT_PARAM_VALUE, outParam ); } public void SetAndThrow( ref int refParam, out int outParam ) { refParam = REF_PARAM_VALUE; outParam = OUT_PARAM_VALUE; throw new TestException(); } } internal class TestException : ApplicationException { } } --- NEW FILE: MultiInterfaceFactoryTest.cs --- using System; using NUnit.Framework; using NMock2.Monitoring; namespace NMock2.Test.Monitoring { public interface A {} public interface B {} public interface C {} public interface A_B {} [TestFixture] public class MultiInterfaceFactoryTest { MultiInterfaceFactory factory; [SetUp] public void SetUp() { factory = new MultiInterfaceFactory("TestedMultiInterfaceFactory"); } [Test] public void CreatesTypeInfoObjectsThatRepresentAnInterfaceThatExtendsAllParameterInterfaces() { Type newType = factory.GetType(typeof(A),typeof(B),typeof(C)); Assert.IsTrue( typeof(A).IsAssignableFrom(newType), "new type is an A"); Assert.IsTrue( typeof(B).IsAssignableFrom(newType), "new type is an B"); Assert.IsTrue( typeof(C).IsAssignableFrom(newType), "new type is an C"); } [Test] public void ReturnsTheSameTypeWhenPassedTheSameArguments() { Type type = factory.GetType(typeof(A),typeof(B)); Type sameType = factory.GetType(typeof(A),typeof(B)); Type otherType = factory.GetType(typeof(A),typeof(C)); Assert.IsTrue( type == sameType, "should be same type"); Assert.IsTrue( type != otherType, "should be different type"); } [Test] public void ReturnsTheSameTypeWhenPassedTheSameArgumentsInDifferentOrder() { Type type = factory.GetType(typeof(A),typeof(B)); Type sameType = factory.GetType(typeof(B),typeof(A)); Assert.IsTrue( type == sameType, "should be same type"); } [Test] public void AvoidsNameClash() { Type type = factory.GetType(typeof(A), typeof(B), typeof(C)); Type otherType = factory.GetType(typeof(A_B), typeof(C)); Assert.IsTrue(type != otherType, "types should be different"); } } } |