PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/mocks/MockMethod.cs

#
C# | 119 lines | 64 code | 25 blank | 30 comment | 9 complexity | 711338a49ee122d4bdd04050b0820c8a 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 NUnit.Framework;
 10
 11namespace NUnit.Mocks
 12{
 13	/// <summary>
 14	/// The MockMethod object represents one named method on a mock object.
 15	/// All overloads are represented by one MockMethod. A method may return
 16	/// a fixed value, throw a fixed exception or have an expected sequence
 17	/// of calls. If it has a call sequence, then the signature must match and
 18	/// each call provides it's own return value or exception.
 19	/// </summary>
 20	public class MockMethod : IMethod
 21	{
 22		#region Private Fields
 23
 24		/// <summary>
 25		/// Name of this method
 26		/// </summary>
 27		private string methodName;
 28		
 29		/// <summary>
 30		/// Fixed return value
 31		/// </summary>
 32		private object returnVal;
 33		
 34		/// <summary>
 35		/// Exception to be thrown
 36		/// </summary>
 37		private Exception exception;
 38
 39		/// <summary>
 40		/// Expected call sequence. If null, this method has no expectations
 41		/// and simply provides a fixed return value or exception.
 42		/// </summary>
 43		private ArrayList expectedCalls = null;
 44		
 45		/// <summary>
 46		/// Actual sequence of calls... currently not used
 47		/// </summary>
 48		//private ArrayList actualCalls = null;
 49
 50		#endregion
 51
 52		#region Constructors
 53
 54		public MockMethod( string methodName ) 
 55			: this( methodName, null, null ) { }
 56
 57		public MockMethod( string methodName, object returnVal ) 
 58			: this( methodName, returnVal, null ) { }
 59
 60		public MockMethod( string methodName, object returnVal, Exception exception )
 61		{
 62			this.methodName = methodName;
 63			this.returnVal = returnVal;
 64			this.exception = exception;
 65		}
 66
 67		#endregion
 68
 69		#region IMethod Members
 70
 71		public string Name
 72		{
 73			get { return methodName; }
 74		}
 75
 76		public void Expect( ICall call )
 77		{
 78			if ( expectedCalls == null )
 79				expectedCalls = new ArrayList();
 80
 81			expectedCalls.Add( call );
 82		}
 83
 84		#endregion
 85
 86		#region ICall Members
 87
 88		public object Call( object[] args )
 89		{
 90			if ( expectedCalls == null )
 91			{
 92				if ( exception != null )
 93					throw exception;
 94
 95				return returnVal;
 96			}
 97			else
 98			{
 99				//actualCalls.Add( new MethodCall( methodName, null, null, args ) );
100				Assert.IsTrue( expectedCalls.Count > 0, "Too many calls to " + Name );
101				MockCall mockCall = (MockCall)expectedCalls[0];
102				expectedCalls.RemoveAt( 0 );
103				return mockCall.Call( args );
104			}
105		}
106
107		#endregion
108
109		#region IVerify Members
110
111		public void Verify()
112		{
113			if ( expectedCalls != null )
114				Assert.IsTrue( expectedCalls.Count == 0, "Not all methods were called" );
115		}
116
117		#endregion
118	}
119}