A
download BaseMock.cs
Language: C#
License: MSVSSDK
LOC: 114
Project Info
C# Common.UnitTest
Server: Visual Studio SDK
Type: filesystem
...mon\Source\CSharp\UnitTest\
   BaseMock.cs
   ConnectionPointHelper.cs
   FileGenerator.cs
   GenericMockFactory.cs
   LocalRegistryMock.cs
   ....UnitTestLibrary.csproj
   ...SDK.UnitTestLibrary.sln
   MockBuildEngine.cs
   OleServiceProvider.cs
   OutputWindowPane.cs
   OutputWindowService.cs
   RegistrationContextMock.cs
   RegistrationKeyMock.cs
   TextWriterMock.cs

/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using System.Reflection;
using System.Collections.Generic;

namespace Microsoft.VsSDK.UnitTestLibrary
{
	/// <summary>
	/// Arguments passed to the callback functions used by the GenericMockFactory
	/// </summary>
	public class CallbackArgs : EventArgs
	{
		private object[] parameters;
		private object returnValue;

		/// <summary>
		/// Builds a new CallbackArgs using an array of objects as values for the parameters.
		/// </summary>
		public CallbackArgs(object[] parameters)
		{
			this.parameters = parameters;
		}

		/// <summary>
		/// Get the value of a specific parameter.
		/// </summary>
		public object GetParameter(int index)
		{
			return parameters[index];
		}

		/// <summary>
		/// Set the value of a parameter.
		/// </summary>
		public void SetParameter(int index, object value)
		{
			parameters[index] = value;
		}

		/// <summary>
		/// The return value of the method.
		/// </summary>
		public object ReturnValue
		{
			get { return returnValue; }
			set { returnValue = value; }
		}
	}

	/// <summary>
	/// Base class for dynamicaly generated mock objects.
	/// </summary>
	public abstract class BaseMock
	{
		protected Dictionary<string, EventHandler<CallbackArgs>> callbacks;
		protected Dictionary<string, object[]> returnValues;
		private Dictionary<string, object> data;
        private Dictionary<string, int> callsCount;

		protected BaseMock()
		{
			callbacks = new Dictionary<string, EventHandler<CallbackArgs>>();
			returnValues = new Dictionary<string, object[]>();
			data = new Dictionary<string, object>();
            callsCount = new Dictionary<string, int>();
		}

		#region Public methods for Initialization
		/// <summary>
		/// Provide an array of values that will be used as return values in the 
		/// mock object method implementation. Index 0 being the return value index 1
		/// the value assigned to the first parameter (assuming it is ref/out),...
		/// To remove an entry, pass a null ArrayList.
		/// </summary>
		/// <param name="methodName">Name of the method the values are for. Case sensitive.</param>
		/// <param name="valuesToReturn">List of objects to return.
		/// Index 0 is the return value while higher indexes are used for ref/out parameters.</param>
		public void AddMethodReturnValues(string methodName, object[] returnValues)
		{
			if (this.returnValues.ContainsKey(methodName))
			{
				this.returnValues.Remove(methodName);
			}
			if (returnValues != null)
			{
				this.returnValues.Add(methodName, returnValues);
			}
		}

		/// <summary>
		/// Provide a call back method that the mock object will call when
		/// methodName is called on the mock object.
		/// As long as no value were specified for AddMethodReturnValues,
		/// the callBackMethod can set the value in the array list to set
		/// which value should be returned (return value and ref/out parameters).
		/// To remove an entry pass null as the callBackMethod.
		/// </summary>
		/// <param name="methodName">Name of the method for which the callback is provided</param>
		/// <param name="callBackMethod">Method to call when methodName is called on the mock object</param>
		public void AddMethodCallback(string methodName, EventHandler<CallbackArgs> callback)
		{
			if (callbacks.ContainsKey(methodName))
			{
				callbacks.Remove(methodName);
			}
			if (null != callback)
			{
				callbacks.Add(methodName, callback);
			}
		}

		#endregion

        /// <summary>
        /// Any data that is needed in the implementation of the callback
        /// can be saved here.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object this[string name]
        {
            get { return data[name]; }
            set { this.data[name] = value; }
        }

        #region Functions to handle the conters of funtion calls.
        /// <summary>
        /// Returns the number of times a function is called.
        /// </summary>
        /// <param name="name">Function name.</param>
        public int FunctionCalls(string name)
        {
            if (callsCount.ContainsKey(name))
            {
                return callsCount[name];
            }
            return 0;
        }

        /// <summary>
        /// Returns the sum of the number of times each function exposed by this
        /// object was called.
        /// </summary>
        public int TotalCallsAllFunctions()
        {
            int total = 0;
            foreach (int i in callsCount.Values)
            {
                total += i;
            }
            return total;
        }

        /// <summary>
        /// This function is called by the code generated by the GenericMockFactory
        /// when a function is called.
        /// </summary>
        /// <param name="name">Full name of the function.</param>
        protected void IncrementFunctionCalls(string name)
        {
            // A function name can not be empty.
            if (string.IsNullOrEmpty(name))
                return;

            if (callsCount.ContainsKey(name))
            {
                callsCount[name] += 1;
            }
            else
            {
                callsCount[name] = 1;
            }
        }

        /// <summary>
        /// Clears all the data about the number of times aech function is called.
        /// </summary>
        public void ResetAllFunctionCalls()
        {
            callsCount.Clear();
        }

        /// <summary>
        /// Clears the data about the number of times a specific function is called.
        /// </summary>
        /// <param name="name">Function name.</param>
        public void ResetFunctionCalls(string name)
        {
            if (!string.IsNullOrEmpty(name) && callsCount.ContainsKey(name))
            {
                callsCount.Remove(name);
            }
        }
        #endregion
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us