BaseMock.cs :  » Development » StyleCop » Microsoft » VsSDK » UnitTestLibrary » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » StyleCop 
StyleCop » Microsoft » VsSDK » UnitTestLibrary » BaseMock.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;
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{
  /// <summary>
  /// Arguments passed to the callback functions used by the GenericMockFactory
  /// </summary>
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
  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>
    [System.Runtime.InteropServices.ComVisible(true)]
  public abstract class BaseMock
  {
        [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
    protected Dictionary<string, EventHandler<CallbackArgs>> callbacks;

        [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
    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>
        [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);
      }
    }

    /// <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
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.