PageRenderTime 25ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/mocks/MockInterfaceHandler.cs

#
C# | 72 lines | 52 code | 12 blank | 8 comment | 30 complexity | 24d87a5a1a48c9933baf4b4a8f6b28b7 MD5 | raw file
 1// ****************************************************************
 2// Copyright 2007, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org
 5// ****************************************************************
 6
 7using System;
 8using System.Collections;
 9using System.Runtime.Remoting.Proxies;
10using System.Runtime.Remoting.Messaging;
11using System.Reflection;
12
13namespace NUnit.Mocks
14{
15	/// <summary>
16	/// Summary description for MockInterfaceHandler.
17	/// </summary>
18	public class MockInterfaceHandler : RealProxy
19	{
20		private ICallHandler callHandler;
21
22		public MockInterfaceHandler( Type type, ICallHandler callHandler ) : base( type ) 
23		{ 
24			this.callHandler = callHandler;
25		}
26
27		public override IMessage Invoke( IMessage msg )
28		{
29			IMethodCallMessage call = (IMethodCallMessage)msg;
30			IMethodReturnMessage result = null; 
31
32			if ( call != null )
33			{
34				try
35				{
36					object ret = callHandler.Call( call.MethodName, call.Args );
37
38					if ( ret == null )
39					{
40						MethodInfo info = call.MethodBase as MethodInfo;
41						Type returnType = info.ReturnType;
42
43						if( returnType == typeof( System.Boolean ) ) ret = false; 
44
45						if( returnType == typeof( System.Byte    ) ) ret = (System.Byte)0;
46						if( returnType == typeof( System.SByte   ) ) ret = (System.SByte)0;
47						if( returnType == typeof( System.Decimal ) ) ret = (System.Decimal)0;
48						if( returnType == typeof( System.Double  ) ) ret = (System.Double)0;
49						if( returnType == typeof( System.Single  ) ) ret = (System.Single)0;
50						if( returnType == typeof( System.Int32   ) ) ret = (System.Int32)0;
51						if( returnType == typeof( System.UInt32  ) ) ret = (System.UInt32)0;
52						if( returnType == typeof( System.Int64   ) ) ret = (System.Int64)0;
53						if( returnType == typeof( System.UInt64  ) ) ret = (System.UInt64)0;
54						if( returnType == typeof( System.Int16   ) ) ret = (System.Int16)0;
55						if( returnType == typeof( System.UInt16  ) ) ret = (System.UInt16)0;
56
57						if( returnType == typeof( System.Char	 ) ) ret = '?';
58					}
59
60					result = new ReturnMessage( ret, null, 0, null, call );
61				} 
62				catch( Exception e )
63				{
64					result = new ReturnMessage( e, call );
65				}
66			}
67
68			return result;
69		}
70	}
71}
72