From: Choy R. <ch...@us...> - 2005-05-11 05:17:28
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Tests/Dynamic In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1535/DotNetMock.Tests/Dynamic Modified Files: AbstractDynamicMockTests.cs ExpectationMethodTests.cs Log Message: BUG [ 1199222 ] Does not cast return value well * Do a simple conversion using Convert.ChangeType on the return value if necessary. It is necessary if the return type is a value type and the return type of the method is not the same as the type of the specified return value. Index: ExpectationMethodTests.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock.Tests/Dynamic/ExpectationMethodTests.cs,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** ExpectationMethodTests.cs 8 Apr 2005 02:57:09 -0000 1.15 --- ExpectationMethodTests.cs 11 May 2005 05:17:14 -0000 1.16 *************** *** 26,29 **** --- 26,30 ---- void Method4(int x); void Method4(int x, int y); + long Method5(); } *************** *** 39,43 **** --- 40,55 ---- new Type[] { typeof(int), typeof(int) } ); + static readonly MethodInfo method5 = typeof(IMethods).GetMethod("Method5"); + [Test] public void ProvideReturnValueWithWrongType() + { + methodCallExpectation = new ExpectationMethod( + method5.Name, + 123 + ); + MethodCall call = new MethodCall(method5); + object returnValue = methodCallExpectation.CheckCallAndSendResponse(call); + methodCallExpectation.Verify(); + } [ExpectedException( typeof(AssertionException), Index: AbstractDynamicMockTests.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock.Tests/Dynamic/AbstractDynamicMockTests.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** AbstractDynamicMockTests.cs 8 Apr 2005 02:57:09 -0000 1.4 --- AbstractDynamicMockTests.cs 11 May 2005 05:17:14 -0000 1.5 *************** *** 33,36 **** --- 33,40 ---- new Type[] { typeof(int), typeof(int) } ); + protected static readonly MethodInfo METHOD3 = + typeof(IBlah).GetMethod("Method3"); + protected static readonly MethodInfo METHOD4 = + typeof(IBlah).GetMethod("Method4"); *************** *** 60,63 **** --- 64,85 ---- void Method2(int a); void Method2(int a, int b); + + long Method3(); + int Method4(); + } + + protected struct CanBeCastToLongExplicitly + { + public static explicit operator long(CanBeCastToLongExplicitly v) + { + return 0; + } + } + protected struct CanBeCastToLongImplicitly + { + public static implicit operator long(CanBeCastToLongImplicitly v) + { + return 0; + } } *************** *** 74,77 **** --- 96,153 ---- } + [Test] public void ReturnDifferentReferenceType() + { + IDynamicMock mock = newDynamicMock(typeof(IBlah)); + IBlah blah = (IBlah) mock.Object; + mock.ExpectAndReturn("Method1", + 123, + 1 + ); + + object r = blah.Method1(1); + Assert.AreEqual(123, r); + mock.Verify(); + } + + [ExpectedException(typeof(InvalidCastException))] + [Test] public void ReturnWrongImplicitlyCastableValueType() + { + IDynamicMock mock = newDynamicMock(typeof(IBlah)); + IBlah blah = (IBlah) mock.Object; + mock.ExpectAndReturn("Method3", + new CanBeCastToLongImplicitly() + ); + + long r = blah.Method3(); + Assert.AreEqual(0, r); + mock.Verify(); + } + [ExpectedException(typeof(InvalidCastException))] + [Test] public void ReturnWrongExplicitlyCastableValueType() + { + IDynamicMock mock = newDynamicMock(typeof(IBlah)); + IBlah blah = (IBlah) mock.Object; + mock.ExpectAndReturn("Method3", + new CanBeCastToLongExplicitly() + ); + + long r = blah.Method3(); + Assert.AreEqual(0, r); + mock.Verify(); + } + + [Test] public void ReturnWrongValueType() + { + IDynamicMock mock = newDynamicMock(typeof(IBlah)); + IBlah blah = (IBlah) mock.Object; + mock.ExpectAndReturn("Method3", + 123 + ); + + long r = blah.Method3(); + Assert.AreEqual(123, r); + mock.Verify(); + } + [ExpectedException(typeof(AssertionException))] [Test] public void FailsIfPassedTooManyArguments() *************** *** 289,296 **** mock.ExpectAndReturn("myMethod", something); mock.ExpectAndReturn("myMethod", anotherthing); ! mock.ExpectAndReturn("myMethod", x); Assert.AreEqual(something, mock.Call(myMethod)); Assert.AreEqual(anotherthing, mock.Call(myMethod)); ! Assert.AreEqual(x, mock.Call(myMethod)); mock.Verify(); } --- 365,372 ---- mock.ExpectAndReturn("myMethod", something); mock.ExpectAndReturn("myMethod", anotherthing); ! mock.ExpectAndReturn("Method4", x); Assert.AreEqual(something, mock.Call(myMethod)); Assert.AreEqual(anotherthing, mock.Call(myMethod)); ! Assert.AreEqual(x, mock.Call(METHOD4)); mock.Verify(); } |