/*************************************************************************** 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.Collections.Generic; using System.Diagnostics.CodeAnalysis; [module: SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", Scope = "namespace", Target = "Microsoft.VsSDK.UnitTestLibrary")] [module: SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Scope = "namespace", Target = "Microsoft.VsSDK.UnitTestLibrary")] namespace Microsoft.VsSDK.UnitTestLibrary { /// /// Arguments passed to the callback functions used by the GenericMockFactory /// [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] public class CallbackArgs : EventArgs { private object[] parameters; private object returnValue; /// /// Builds a new CallbackArgs using an array of objects as values for the parameters. /// public CallbackArgs(object[] parameters) { this.parameters = parameters; } /// /// Get the value of a specific parameter. /// public object GetParameter(int index) { return this.parameters[index]; } /// /// Set the value of a parameter. /// public void SetParameter(int index, object value) { this.parameters[index] = value; } /// /// The return value of the method. /// public object ReturnValue { get { return this.returnValue; } set { this.returnValue = value; } } } /// /// Base class for dynamicaly generated mock objects. /// [System.Runtime.InteropServices.ComVisible(true)] public abstract class BaseMock { [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] protected Dictionary> callbacks; [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")] protected Dictionary returnValues; private Dictionary data; private Dictionary callsCount; protected BaseMock() { this.callbacks = new Dictionary>(); this.returnValues = new Dictionary(); this.data = new Dictionary(); this.callsCount = new Dictionary(); } #region Public methods for Initialization /// /// 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. /// /// Name of the method the values are for. Case sensitive. /// List of objects to return. /// Index 0 is the return value while higher indexes are used for ref/out parameters. [SuppressMessage("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames")] public void AddMethodReturnValues(string methodName, object[] returnValues) { if (this.returnValues.ContainsKey(methodName)) { this.returnValues.Remove(methodName); } if (returnValues != null) { this.returnValues.Add(methodName, returnValues); } } /// /// 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. /// /// Name of the method for which the callback is provided /// Method to call when methodName is called on the mock object public void AddMethodCallback(string methodName, EventHandler callback) { if (this.callbacks.ContainsKey(methodName)) { this.callbacks.Remove(methodName); } if (null != callback) { this.callbacks.Add(methodName, callback); } } #endregion /// /// Any data that is needed in the implementation of the callback /// can be saved here. /// /// /// public object this[string name] { get { return this.data[name]; } set { this.data[name] = value; } } #region Functions to handle the conters of funtion calls. /// /// Returns the number of times a function is called. /// /// Function name. public int FunctionCalls(string name) { if (this.callsCount.ContainsKey(name)) { return this.callsCount[name]; } return 0; } /// /// Returns the sum of the number of times each function exposed by this /// object was called. /// public int TotalCallsAllFunctions() { var total = 0; foreach (var i in this.callsCount.Values) { total += i; } return total; } /// /// This function is called by the code generated by the GenericMockFactory /// when a function is called. /// /// Full name of the function. protected void IncrementFunctionCalls(string name) { // A function name can not be empty. if (string.IsNullOrEmpty(name)) return; if (this.callsCount.ContainsKey(name)) { this.callsCount[name] += 1; } else { this.callsCount[name] = 1; } } /// /// Clears all the data about the number of times aech function is called. /// public void ResetAllFunctionCalls() { this.callsCount.Clear(); } /// /// Clears the data about the number of times a specific function is called. /// /// Function name. public void ResetFunctionCalls(string name) { if (!string.IsNullOrEmpty(name) && this.callsCount.ContainsKey(name)) { this.callsCount.Remove(name); } } #endregion } }