PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/mocks/Mock.cs

#
C# | 163 lines | 115 code | 38 blank | 10 comment | 6 complexity | 0ce57299995fb48e273a61f4b25b720c 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 NUnit.Framework;
 12
 13namespace NUnit.Mocks
 14{
 15	/// <summary>
 16	/// Summary description for MockObject.
 17	/// </summary>
 18	public class Mock : IMock
 19	{
 20		#region Private Fields
 21
 22		private string name;
 23
 24		private bool strict;
 25
 26		private IDictionary methods = new Hashtable();
 27
 28		private Exception lastException;
 29
 30        private ArrayList unexpected = new ArrayList();
 31
 32		#endregion
 33
 34		#region Properties
 35
 36		public Exception LastException
 37		{
 38			get { return lastException; }
 39		}
 40
 41		#endregion
 42
 43		#region Constructors
 44
 45		public Mock() : this( "Mock" ) { }
 46
 47		public Mock( string name )
 48		{
 49			this.name = name;
 50		}
 51
 52		#endregion
 53
 54		#region IMock Members
 55
 56		public string Name
 57		{
 58			get { return name; }
 59		}
 60
 61		public bool Strict
 62		{
 63			get { return strict; }
 64			set { strict = value; }
 65		}
 66
 67		public void Expect( string methodName, params object[] args )
 68		{
 69			ExpectAndReturn( methodName, null, args );
 70		}
 71
 72		public void Expect( string methodName )
 73		{
 74			ExpectAndReturn( methodName, null, null );
 75		}
 76
 77		public void ExpectNoCall( string methodName )
 78		{
 79			methods[methodName] = new MockMethod( methodName, null, 
 80				new AssertionException("Unexpected call to method " + methodName) );
 81		}
 82
 83		public void ExpectAndReturn( string methodName, object returnVal, params object[] args )
 84		{
 85			AddExpectedCall( methodName, returnVal, null, args );
 86		}
 87
 88		public void ExpectAndThrow( string methodName, Exception exception, params object[] args )
 89		{
 90			AddExpectedCall( methodName, null, exception, args );
 91		}
 92
 93		public void SetReturnValue( string methodName, object returnVal )
 94		{
 95			methods[methodName] = new MockMethod( methodName, returnVal );
 96		}
 97
 98		#endregion
 99
100		#region IVerify Members
101
102        public virtual void Verify()
103        {
104            foreach (IMethod method in methods.Values)
105                method.Verify();
106
107            if (unexpected.Count > 0)
108                Assert.Fail("Unexpected call to " + (string)unexpected[0]);
109        }
110
111		#endregion
112
113		#region ICallHandler Members
114
115		public virtual object Call( string methodName, params object[] args )
116		{
117			if ( methods.Contains( methodName ) )
118			{
119				try
120				{
121					IMethod method = (IMethod)methods[methodName];
122					return method.Call( args );
123				}
124				catch( Exception exception )
125				{
126					// Save exception in case MO is running on a separate thread
127					lastException = exception;
128					throw;
129				}
130			}
131			else // methodName is not listed in methods
132                if (Strict)
133                {
134                    unexpected.Add(methodName);
135                    Assert.Fail("Unexpected call to " + methodName);
136                }
137			
138			// not listed but Strict is not specified
139			return null;
140		}
141
142		#endregion
143	
144		#region Helper Methods
145
146		private void AddExpectedCall( string methodName, object returnVal, Exception exception, object[] args )
147		{
148			IMethod method = (IMethod)methods[methodName];
149			if ( method == null )
150			{
151				method = new MockMethod( methodName );
152				methods[methodName] = method;
153			}
154
155			Type[] argTypes = MethodSignature.GetArgTypes( args );
156			MethodSignature signature = new MethodSignature( this.Name, methodName, argTypes );
157
158			method.Expect( new MockCall( signature, returnVal, exception, args ) );
159		}
160
161		#endregion
162	}
163}