File: MockInterfaceHandler.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (72 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************

using System;
using System.Collections;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Reflection;

namespace NUnit.Mocks
{
	/// <summary>
	/// Summary description for MockInterfaceHandler.
	/// </summary>
	public class MockInterfaceHandler : RealProxy
	{
		private ICallHandler callHandler;

		public MockInterfaceHandler( Type type, ICallHandler callHandler ) : base( type ) 
		{ 
			this.callHandler = callHandler;
		}

		public override IMessage Invoke( IMessage msg )
		{
			IMethodCallMessage call = (IMethodCallMessage)msg;
			IMethodReturnMessage result = null; 

			if ( call != null )
			{
				try
				{
					object ret = callHandler.Call( call.MethodName, call.Args );

					if ( ret == null )
					{
						MethodInfo info = call.MethodBase as MethodInfo;
						Type returnType = info.ReturnType;

						if( returnType == typeof( System.Boolean ) ) ret = false; 

						if( returnType == typeof( System.Byte    ) ) ret = (System.Byte)0;
						if( returnType == typeof( System.SByte   ) ) ret = (System.SByte)0;
						if( returnType == typeof( System.Decimal ) ) ret = (System.Decimal)0;
						if( returnType == typeof( System.Double  ) ) ret = (System.Double)0;
						if( returnType == typeof( System.Single  ) ) ret = (System.Single)0;
						if( returnType == typeof( System.Int32   ) ) ret = (System.Int32)0;
						if( returnType == typeof( System.UInt32  ) ) ret = (System.UInt32)0;
						if( returnType == typeof( System.Int64   ) ) ret = (System.Int64)0;
						if( returnType == typeof( System.UInt64  ) ) ret = (System.UInt64)0;
						if( returnType == typeof( System.Int16   ) ) ret = (System.Int16)0;
						if( returnType == typeof( System.UInt16  ) ) ret = (System.UInt16)0;

						if( returnType == typeof( System.Char	 ) ) ret = '?';
					}

					result = new ReturnMessage( ret, null, 0, null, call );
				} 
				catch( Exception e )
				{
					result = new ReturnMessage( e, call );
				}
			}

			return result;
		}
	}
}