File: Mock.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 (155 lines) | stat: -rw-r--r-- 3,352 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ****************************************************************
// 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 NUnit.Framework;

namespace NUnit.Mocks
{
	/// <summary>
	/// Summary description for MockObject.
	/// </summary>
	public class Mock : IMock
	{
		#region Private Fields

		private string name;

		private bool strict;

		private IDictionary methods = new Hashtable();

		private Exception lastException;

		#endregion

		#region Properties

		public Exception LastException
		{
			get { return lastException; }
		}

		#endregion

		#region Constructors

		public Mock() : this( "Mock" ) { }

		public Mock( string name )
		{
			this.name = name;
		}

		#endregion

		#region IMock Members

		public string Name
		{
			get { return name; }
		}

		public bool Strict
		{
			get { return strict; }
			set { strict = value; }
		}

		public void Expect( string methodName, params object[] args )
		{
			ExpectAndReturn( methodName, null, args );
		}

		public void Expect( string methodName )
		{
			ExpectAndReturn( methodName, null, null );
		}

		public void ExpectNoCall( string methodName )
		{
			methods[methodName] = new MockMethod( methodName, null, 
				new AssertionException("Unexpected call to method " + methodName) );
		}

		public void ExpectAndReturn( string methodName, object returnVal, params object[] args )
		{
			AddExpectedCall( methodName, returnVal, null, args );
		}

		public void ExpectAndThrow( string methodName, Exception exception, params object[] args )
		{
			AddExpectedCall( methodName, null, exception, args );
		}

		public void SetReturnValue( string methodName, object returnVal )
		{
			methods[methodName] = new MockMethod( methodName, returnVal );
		}

		#endregion

		#region IVerify Members

		public virtual void Verify()
		{
			foreach( IMethod method in methods.Values )
				method.Verify();
		}

		#endregion

		#region ICallHandler Members

		public virtual object Call( string methodName, params object[] args )
		{
			if ( methods.Contains( methodName ) )
			{
				try
				{
					IMethod method = (IMethod)methods[methodName];
					return method.Call( args );
				}
				catch( Exception exception )
				{
					// Save exception in case MO is running on a separate thread
					lastException = exception;
					throw;
				}
			}
			else // methodName is not listed in methods
			if ( Strict )
				Assert.Fail( "Unexpected call to " + methodName );
			
			// not listed but Strict is not specified
			return null;
		}

		#endregion
	
		#region Helper Methods

		private void AddExpectedCall( string methodName, object returnVal, Exception exception, object[] args )
		{
			IMethod method = (IMethod)methods[methodName];
			if ( method == null )
			{
				method = new MockMethod( methodName );
				methods[methodName] = method;
			}

			Type[] argTypes = MethodSignature.GetArgTypes( args );
			MethodSignature signature = new MethodSignature( this.Name, methodName, argTypes );

			method.Expect( new MockCall( signature, returnVal, exception, args ) );
		}

		#endregion
	}
}