From: Griffin C. <gc...@us...> - 2006-06-06 03:07:32
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv6076/DotNetMock/Dynamic Modified Files: ExpectationMethod.cs IMethodCallExpectation.cs MethodCall.cs MethodSignature.cs Log Message: - Initial conversion to .NET 2.0 and VIsual Studio 2005 Index: MethodCall.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/MethodCall.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** MethodCall.cs 11 May 2005 05:42:24 -0000 1.6 --- MethodCall.cs 6 Jun 2006 03:06:52 -0000 1.7 *************** *** 1,177 **** #region License // Copyright (c) 2004 Griffin Caprio, Roman V. Gavrilov & Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Reflection; #endregion namespace DotNetMock.Dynamic { ! /// <summary> ! /// Method call. ! /// </summary> ! /// <author>Roman V. Gavrilov</author> ! /// <author>Choy Rim</author> ! public class MethodCall ! { ! #region Private Instance Fields ! private MethodInfo _method; ! private object[] _arguments; ! #endregion ! /// <summary> ! /// Create new method call instance. ! /// </summary> ! /// <param name="method"> ! /// <see cref="MethodInfo"/> of method called ! /// </param> ! /// <param name="arguments">Arguments of the method call.</param> ! public MethodCall(MethodInfo method, params object[] arguments) ! { ! if (arguments == null) ! { ! arguments = new object[0]; ! } ! int expectedArgumentCount = method.GetParameters().Length; ! if (expectedArgumentCount != arguments.Length) ! { ! throw new InvalidOperationException(String.Format( ! "Method {0} takes {1} arguments but received {2}.", ! method.Name, ! expectedArgumentCount, ! arguments.Length ! )); ! } ! _method = method; ! _arguments = arguments; ! } ! /// <summary> ! /// Name of method or property. ! /// </summary> ! /// <remarks> ! /// In the case of a property, we strip the "get_" or "set_" ! /// prefix. ! /// </remarks> ! public string MethodName ! { ! get ! { ! string name = _method.Name; ! if ( IsPropertyAccess ) ! { ! return name.Substring(4); ! } ! return name; ! } ! } ! /// <summary> ! /// Are we accessing a property or calling a typical method? ! /// </summary> ! public bool IsPropertyAccess ! { ! get ! { ! if ( ! _method.IsSpecialName ) ! { ! return false; ! } ! string name = _method.Name; ! return name.StartsWith("get_") || name.StartsWith("set_"); ! } ! } ! /// <summary> ! /// Check if given and this object represent the same method call. ! /// </summary> ! public override bool Equals(object obj) ! { ! MethodCall that = obj as MethodCall; ! if ( object.ReferenceEquals(that, null) ) ! { ! return false; ! } ! if ( that.Method!=this.Method ) ! { ! return false; ! } ! if (that.Arguments.Length != this.Arguments.Length) ! { ! return false; ! } ! for (int i = 0; i < that.Arguments.Length; ++i) ! { ! if (!that.Arguments[i].Equals(this.Arguments[i])) return false; ! } ! return true; ! } ! /// <summary> ! /// Get object's hash code. ! /// </summary> ! /// <returns>Object's hash code.</returns> ! public override int GetHashCode() ! { ! int hashCode = Method.GetHashCode(); ! foreach (object argument in Arguments) ! { ! hashCode ^= argument.GetHashCode(); ! } ! return hashCode; ! } ! /// <summary> ! /// String representation of method call. ! /// </summary> ! /// <returns> ! /// String of the form: ! /// <i>method-name</i>(<i>argument-name</i>=<i>argument-value</i>, ...) ! /// </returns> ! public override string ToString() ! { ! ParameterInfo[] pis = Method.GetParameters(); ! string[] argumentTexts = new string[Arguments.Length]; ! for ( int i = 0; i<Arguments.Length; ++i ) ! { ! object argument = Arguments[i]; ! string argumentName = pis[i].Name; ! string argumentValue; ! if ( object.ReferenceEquals(argument, null) ) ! { ! argumentValue = "null"; ! } ! else if( argument is string ) ! { ! argumentValue = "\""+argument.ToString()+"\""; ! } ! else ! { ! try ! { ! argumentValue = argument.ToString(); ! } ! catch ! { ! argumentValue = "N/A"; ! } ! } ! argumentTexts[i] = String.Format( ! "{0}={1}", ! argumentName, ! argumentValue ! ); ! } ! return String.Format( ! "{0}({1})", ! Method.Name, ! String.Join(", ", argumentTexts) ! ); ! } ! /// <summary> ! /// <see cref="MethodInfo"/> of method in method call. ! /// </summary> ! public MethodInfo Method { get { return _method; } } ! /// <summary> ! /// Array of all argument values for method call. ! /// </summary> ! public object[] Arguments { get { return _arguments; } } ! } ! } --- 1,196 ---- #region License + // Copyright (c) 2004 Griffin Caprio, Roman V. Gavrilov & Choy Rim. All rights reserved. + #endregion + #region Imports + using System; using System.Reflection; + #endregion namespace DotNetMock.Dynamic { ! /// <summary> ! /// Method call. ! /// </summary> ! /// <author>Roman V. Gavrilov</author> ! /// <author>Choy Rim</author> ! public class MethodCall ! { ! #region Private Instance Fields ! private MethodInfo _method; ! private object[] _arguments; ! #endregion ! ! /// <summary> ! /// Create new method call instance. ! /// </summary> ! /// <param name="method"> ! /// <see cref="MethodInfo"/> of method called ! /// </param> ! /// <param name="arguments">Arguments of the method call.</param> ! public MethodCall(MethodInfo method, params object[] arguments) ! { ! if (arguments == null) ! { ! arguments = new object[0]; ! } ! int expectedArgumentCount = method.GetParameters().Length; ! if (expectedArgumentCount != arguments.Length) ! { ! throw new InvalidOperationException(String.Format( ! "Method {0} takes {1} arguments but received {2}.", ! method.Name, ! expectedArgumentCount, ! arguments.Length ! )); ! } ! _method = method; ! _arguments = arguments; ! } ! ! /// <summary> ! /// Name of method or property. ! /// </summary> ! /// <remarks> ! /// In the case of a property, we strip the "get_" or "set_" ! /// prefix. ! /// </remarks> ! public string MethodName ! { ! get ! { ! string name = _method.Name; ! if (IsPropertyAccess) ! { ! return name.Substring(4); ! } ! return name; ! } ! } ! ! /// <summary> ! /// Are we accessing a property or calling a typical method? ! /// </summary> ! public bool IsPropertyAccess ! { ! get ! { ! if (! _method.IsSpecialName) ! { ! return false; ! } ! string name = _method.Name; ! return name.StartsWith("get_") || name.StartsWith("set_"); ! } ! } ! ! /// <summary> ! /// Check if given and this object represent the same method call. ! /// </summary> ! public override bool Equals(object obj) ! { ! MethodCall that = obj as MethodCall; ! if (ReferenceEquals(that, null)) ! { ! return false; ! } ! if (that.Method != Method) ! { ! return false; ! } ! if (that.Arguments.Length != Arguments.Length) ! { ! return false; ! } ! for (int i = 0; i < that.Arguments.Length; ++i) ! { ! if (!that.Arguments[i].Equals(Arguments[i])) return false; ! } ! return true; ! } ! ! /// <summary> ! /// Get object's hash code. ! /// </summary> ! /// <returns>Object's hash code.</returns> ! public override int GetHashCode() ! { ! int hashCode = Method.GetHashCode(); ! foreach (object argument in Arguments) ! { ! hashCode ^= argument.GetHashCode(); ! } ! return hashCode; ! } ! ! /// <summary> ! /// String representation of method call. ! /// </summary> ! /// <returns> ! /// String of the form: ! /// <i>method-name</i>(<i>argument-name</i>=<i>argument-value</i>, ...) ! /// </returns> ! public override string ToString() ! { ! ParameterInfo[] pis = Method.GetParameters(); ! string[] argumentTexts = new string[Arguments.Length]; ! for (int i = 0; i < Arguments.Length; ++i) ! { ! object argument = Arguments[i]; ! string argumentName = pis[i].Name; ! string argumentValue; ! if (ReferenceEquals(argument, null)) ! { ! argumentValue = "null"; ! } ! else if (argument is string) ! { ! argumentValue = "\"" + argument.ToString() + "\""; ! } ! else ! { ! try ! { ! argumentValue = argument.ToString(); ! } ! catch ! { ! argumentValue = "N/A"; ! } ! } ! argumentTexts[i] = String.Format( ! "{0}={1}", ! argumentName, ! argumentValue ! ); ! } ! return String.Format( ! "{0}({1})", ! Method.Name, ! String.Join(", ", argumentTexts) ! ); ! } ! ! /// <summary> ! /// <see cref="MethodInfo"/> of method in method call. ! /// </summary> ! public MethodInfo Method ! { ! get { return _method; } ! } ! ! /// <summary> ! /// Array of all argument values for method call. ! /// </summary> ! public object[] Arguments ! { ! get { return _arguments; } ! } ! } ! } \ No newline at end of file Index: IMethodCallExpectation.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/IMethodCallExpectation.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IMethodCallExpectation.cs 6 Mar 2006 02:24:58 -0000 1.3 --- IMethodCallExpectation.cs 6 Jun 2006 03:06:52 -0000 1.4 *************** *** 1,20 **** #region License // Copyright (c) 2005 Choy Rim. All rights reserved. ! #endregion ! #region Imports ! using System; #endregion namespace DotNetMock.Dynamic { ! /// <summary> ! /// Interface for expectations on method calls.. ! /// </summary> ! public interface IMethodCallExpectation ! { ! /// <summary> ! /// Expected method name. ! /// </summary> ! string ExpectedMethodName { get; } /// <summary> --- 1,19 ---- #region License + // Copyright (c) 2005 Choy Rim. All rights reserved. ! #endregion namespace DotNetMock.Dynamic { ! /// <summary> ! /// Interface for expectations on method calls.. ! /// </summary> ! public interface IMethodCallExpectation ! { ! /// <summary> ! /// Expected method name. ! /// </summary> ! string ExpectedMethodName { get; } /// <summary> *************** *** 22,36 **** /// </summary> ExpectationMethodType ExpectationType { get; } ! ! /// <summary> ! /// Check actual incoming method call and return expected outgoing response. ! /// </summary> ! /// <param name="call">incoming call</param> ! /// <returns>expected return value</returns> ! /// <remarks> ! /// The outgoing response may be an exception or the modification ! /// of ref/out parameters. ! /// </remarks> ! object CheckCallAndSendResponse(MethodCall call); ! } ! } --- 21,35 ---- /// </summary> ExpectationMethodType ExpectationType { get; } ! ! /// <summary> ! /// Check actual incoming method call and return expected outgoing response. ! /// </summary> ! /// <param name="call">incoming call</param> ! /// <returns>expected return value</returns> ! /// <remarks> ! /// The outgoing response may be an exception or the modification ! /// of ref/out parameters. ! /// </remarks> ! object CheckCallAndSendResponse(MethodCall call); ! } ! } \ No newline at end of file Index: MethodSignature.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/MethodSignature.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MethodSignature.cs 23 Apr 2005 21:53:33 -0000 1.2 --- MethodSignature.cs 6 Jun 2006 03:06:52 -0000 1.3 *************** *** 1,143 **** using System; using System.Reflection; namespace DotNetMock.Dynamic { ! /// <summary> ! /// Class encapsulates a method signature of a single method. ! /// </summary> ! /// <author>Roman V. Gavrilov</author> ! /// <author>Griffin Caprio</author> ! public class MethodSignature ! { ! private Type[] _paramTypes; ! private string _methodName; ! private Type _returnType; ! #region Constructors ! /// <summary> ! /// Create new instance based on given information. ! /// </summary> ! /// <param name="methodName">Name of the method.</param> ! /// <param name="returnType">Method return type.</param> ! /// <param name="paramTypes">Parameter types.</param> ! public MethodSignature( string methodName, Type returnType, params Type[] paramTypes ) ! { ! _methodName = methodName; ! _returnType = returnType; ! _paramTypes = ( Type[] )paramTypes.Clone( ); ! } ! /// <summary> ! /// Create new instance of method signature from MethodInfo. ! /// </summary> ! /// <param name="methodInfo">MethodInfo to use for method signature instantiation.</param> ! public MethodSignature( MethodInfo methodInfo ) ! { ! ParameterInfo[] paramInfos = methodInfo.GetParameters( ); ! Type[] paramTypes = new Type[paramInfos.Length]; ! foreach ( ParameterInfo paramInfo in paramInfos ) ! { ! paramTypes[ paramInfo.Position ] = paramInfo.ParameterType; ! } ! _methodName = methodInfo.Name; ! _returnType = methodInfo.ReturnType; ! _paramTypes = paramTypes; ! } ! #endregion ! #region Public Properties ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s parameter types. ! /// </summary> ! public Type[] ParamTypes ! { ! get ! { ! return _paramTypes; ! } ! } ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s method name. ! /// </summary> ! public string MethodName ! { ! get ! { ! return _methodName; ! } ! } ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s return type. ! /// </summary> ! public Type ReturnType ! { ! get ! { ! return _returnType; ! } ! } ! #endregion ! /// <summary> ! /// Compare two signatures by content. ! /// </summary> ! public override bool Equals( object obj ) ! { ! if ( obj == null ) ! { ! return false; ! } ! if ( !( obj is MethodSignature ) ) ! { ! return false; ! } ! MethodSignature that = ( MethodSignature )obj; ! if ( that.MethodName.CompareTo( this.MethodName ) != 0 ) ! { ! return false; ! } ! if ( !that.ReturnType.Equals( this.ReturnType ) ) ! { ! return false; ! } ! if ( that.ParamTypes.Length != this.ParamTypes.Length ) ! { ! return false; ! } ! for ( int idx = 0; idx < that.ParamTypes.Length; ++idx ) ! { ! if ( !that.ParamTypes[ idx ].Equals( this.ParamTypes[ idx ] ) ) ! { ! return false; ! } ! } ! return true; ! } ! /// <summary> ! /// Returns string representation of the instance of <see cref="DotNetMock.Dynamic.MethodSignature"/> ! /// </summary> ! /// <returns>string representation of the instance of <see cref="DotNetMock.Dynamic.MethodSignature"/></returns> ! public override string ToString( ) ! { ! string paramTypesString = null; ! foreach ( Type paramType in _paramTypes ) ! { ! if ( paramTypesString != null ) ! { ! paramTypesString += ", "; ! } ! paramTypesString += paramType.ToString( ); ! } ! string str = string.Format( "{0} {1}({2})", _returnType.ToString( ), _methodName, paramTypesString ); ! return str; ! } ! /// <summary> ! /// Returns hashcode of base class ! /// </summary> ! /// <returns></returns> ! public override int GetHashCode( ) ! { ! return base.GetHashCode( ); ! } ! } } \ No newline at end of file --- 1,134 ---- using System; using System.Reflection; + namespace DotNetMock.Dynamic { ! /// <summary> ! /// Class encapsulates a method signature of a single method. ! /// </summary> ! /// <author>Roman V. Gavrilov</author> ! /// <author>Griffin Caprio</author> ! public class MethodSignature ! { ! private Type[] _paramTypes; ! private string _methodName; ! private Type _returnType; ! #region Constructors ! /// <summary> ! /// Create new instance based on given information. ! /// </summary> ! /// <param name="methodName">Name of the method.</param> ! /// <param name="returnType">Method return type.</param> ! /// <param name="paramTypes">Parameter types.</param> ! public MethodSignature(string methodName, Type returnType, params Type[] paramTypes) ! { ! _methodName = methodName; ! _returnType = returnType; ! _paramTypes = (Type[]) paramTypes.Clone(); ! } ! /// <summary> ! /// Create new instance of method signature from MethodInfo. ! /// </summary> ! /// <param name="methodInfo">MethodInfo to use for method signature instantiation.</param> ! public MethodSignature(MethodInfo methodInfo) ! { ! ParameterInfo[] paramInfos = methodInfo.GetParameters(); ! Type[] paramTypes = new Type[paramInfos.Length]; ! foreach (ParameterInfo paramInfo in paramInfos) ! { ! paramTypes[paramInfo.Position] = paramInfo.ParameterType; ! } ! _methodName = methodInfo.Name; ! _returnType = methodInfo.ReturnType; ! _paramTypes = paramTypes; ! } ! #endregion ! ! #region Public Properties ! ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s parameter types. ! /// </summary> ! public Type[] ParamTypes ! { ! get { return _paramTypes; } ! } ! ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s method name. ! /// </summary> ! public string MethodName ! { ! get { return _methodName; } ! } ! ! /// <summary> ! /// Returns <see cref="DotNetMock.Dynamic.MethodSignature"/>s return type. ! /// </summary> ! public Type ReturnType ! { ! get { return _returnType; } ! } ! ! #endregion ! ! /// <summary> ! /// Compare two signatures by content. ! /// </summary> ! public override bool Equals(object obj) ! { ! if (obj == null) ! { ! return false; ! } ! if (!(obj is MethodSignature)) ! { ! return false; ! } ! MethodSignature that = (MethodSignature) obj; ! if (that.MethodName.CompareTo(MethodName) != 0) ! { ! return false; ! } ! if (!that.ReturnType.Equals(ReturnType)) ! { ! return false; ! } ! if (that.ParamTypes.Length != ParamTypes.Length) ! { ! return false; ! } ! for (int idx = 0; idx < that.ParamTypes.Length; ++idx) ! { ! if (!that.ParamTypes[idx].Equals(ParamTypes[idx])) ! { ! return false; ! } ! } ! return true; ! } ! ! /// <summary> ! /// Returns string representation of the instance of <see cref="DotNetMock.Dynamic.MethodSignature"/> ! /// </summary> ! /// <returns>string representation of the instance of <see cref="DotNetMock.Dynamic.MethodSignature"/></returns> ! public override string ToString() ! { ! string paramTypesString = null; ! foreach (Type paramType in _paramTypes) ! { ! if (paramTypesString != null) ! { ! paramTypesString += ", "; ! } ! paramTypesString += paramType.ToString(); ! } ! string str = string.Format("{0} {1}({2})", _returnType.ToString(), _methodName, paramTypesString); ! return str; ! } ! } } \ No newline at end of file Index: ExpectationMethod.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/ExpectationMethod.cs,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** ExpectationMethod.cs 6 Mar 2006 02:24:58 -0000 1.27 --- ExpectationMethod.cs 6 Jun 2006 03:06:52 -0000 1.28 *************** *** 27,94 **** /// </summary> NoCall ! }; ! ! ! /// <summary> ! /// Expected method call used for building dynamic mocks ! /// </summary> ! public class ExpectationMethod : IMethodCallExpectation ! { ! /// <summary> ! /// Expected Method Name ! /// </summary> ! private string _expectedMethodName; ! /// <summary> ! /// Expectations on the arguments of the expected method call. ! /// </summary> ! private object[] _argumentExpectations; ! /// <summary> ! /// Expected return value for this method call if any. ! /// </summary> ! private object _expectedReturnValue; ! /// <summary> ! /// Exception to throw when the method is called ! /// </summary> ! private Exception _expectedException; /// <summary> /// The type of expectation, call or no call /// </summary> private ExpectationMethodType _expectationType; ! ! /// <summary> ! /// Actual <see cref="MethodCall"/>. ! /// </summary> ! private MethodCall _actualMethodCall = null; ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! public ExpectationMethod( string methodName ) ! : this( methodName, null, null, null ) ! { ! } ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! /// <param name="returnValue">return value when expectation is called</param> ! public ExpectationMethod( string methodName, object returnValue ) ! : this( methodName, returnValue, null, null ) ! { ! } ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! /// <param name="returnValue">return value when expectation is called</param> ! /// <param name="argumentExpectations">Expectations on the arguments</param> ! public ExpectationMethod( ! string methodName, ! object returnValue, ! object[] argumentExpectations ! ) ! : this( methodName, returnValue, argumentExpectations, null ) ! { ! } /// <summary> /// Default Constructor --- 27,102 ---- /// </summary> NoCall ! } ; ! ! ! /// <summary> ! /// Expected method call used for building dynamic mocks ! /// </summary> ! public class ExpectationMethod : IMethodCallExpectation ! { ! /// <summary> ! /// Expected Method Name ! /// </summary> ! private string _expectedMethodName; ! ! /// <summary> ! /// Expectations on the arguments of the expected method call. ! /// </summary> ! private object[] _argumentExpectations; ! ! /// <summary> ! /// Expected return value for this method call if any. ! /// </summary> ! private object _expectedReturnValue; ! ! /// <summary> ! /// Exception to throw when the method is called ! /// </summary> ! private Exception _expectedException; ! /// <summary> /// The type of expectation, call or no call /// </summary> private ExpectationMethodType _expectationType; ! ! /// <summary> ! /// Actual <see cref="MethodCall"/>. ! /// </summary> ! private MethodCall _actualMethodCall = null; ! ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! public ExpectationMethod(string methodName) ! : this(methodName, null, null, null) ! { ! } ! ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! /// <param name="returnValue">return value when expectation is called</param> ! public ExpectationMethod(string methodName, object returnValue) ! : this(methodName, returnValue, null, null) ! { ! } ! ! /// <summary> ! /// Default Constructor ! /// </summary> ! /// <param name="methodName">Method name to expect</param> ! /// <param name="returnValue">return value when expectation is called</param> ! /// <param name="argumentExpectations">Expectations on the arguments</param> ! public ExpectationMethod( ! string methodName, ! object returnValue, ! object[] argumentExpectations ! ) ! : this(methodName, returnValue, argumentExpectations, null) ! { ! } ! /// <summary> /// Default Constructor *************** *** 106,112 **** : this(methodName, returnValue, argumentExpectations, expectedException, ExpectationMethodType.Call) { ! ! } ! /// <summary> /// Default Constructor /// </summary> --- 114,120 ---- : this(methodName, returnValue, argumentExpectations, expectedException, ExpectationMethodType.Call) { ! } ! ! /// <summary> /// Default Constructor /// </summary> *************** *** 130,133 **** --- 138,142 ---- _expectationType = expectationType; } + /// <summary> /// Expected Method Name *************** *** 135,357 **** public string ExpectedMethodName { ! get ! { ! return _expectedMethodName; ! } ! } ! /// <summary> /// Expected Method Type /// </summary> public ExpectationMethodType ExpectationType { get { ! return _expectationType; } } ! /// <summary> ! /// Expected Return Value ! /// </summary> ! public object ReturnValue ! { ! get ! { ! return _expectedReturnValue; ! } ! } ! /// <summary> ! /// True if actual method call was set. ! /// <seealso cref="ActualMethodCall"/> ! /// </summary> ! public bool ActualMethodCallWasSet ! { ! get ! { ! return _actualMethodCall != null; ! } ! } ! /// <summary> ! /// <see cref="MethodInfo"/> of actual method called. ! /// </summary> ! public MethodInfo ActualMethod ! { ! get ! { ! return ActualMethodCall.Method; ! } ! } ! /// <summary> ! /// Name of actual method called. ! /// </summary> ! public string ActualMethodName ! { ! get ! { ! return ActualMethodCall.MethodName; ! } ! } ! /// <summary> ! /// Actual method call. ! /// </summary> ! public MethodCall ActualMethodCall ! { ! get ! { ! if ( ! ActualMethodCallWasSet ) ! { ! throw new InvalidOperationException( ! "Cannot get property ActualMethodCall " + ! "before setting it." ! ); ! } ! return _actualMethodCall; ! } ! set ! { ! if ( value == null ) ! { ! throw new ArgumentNullException( ! "Cannot set ActualMethodCall property to null" ! ); ! } ! if ( ActualMethodCallWasSet ) ! { ! throw new InvalidOperationException( ! "Cannot set property ActualMethodCall " + ! "more than once." ! ); ! } ! _actualMethodCall = value; ! } ! } ! //TODO: Refactor methods ! private string argsToString( object[] args ) ! { ! if ( args != null && args.Length > 0 ) ! { ! string[] argText = new string[args.Length]; ! for ( int i = 0; i < args.Length; ++i ) ! { ! object arg = args[ i ]; ! if ( object.ReferenceEquals( arg, null ) ) ! { ! argText[ i ] = "null"; ! } ! else if ( arg is string ) ! { ! argText[ i ] = "\"" + arg.ToString( ) + "\""; ! } ! else ! { ! argText[ i ] = arg.ToString( ); ! } ! } ! return String.Join( ", ", argText ); ! } ! return String.Empty; ! } ! /// <summary> ! /// Verifies this expectation, when the method is called ! /// </summary> ! public void Verify( ) ! { ! if ( ! ActualMethodCallWasSet ) ! { ! Assertion.Fail( String.Format( ! "{0}({1}) expected but never called.", ! ExpectedMethodName, ! argsToString( _argumentExpectations ) ! ) ); ! } if (ExpectedMethodName != ActualMethodName && _expectationType == ExpectationMethodType.Call) { Assertion.Fail(String.Format( ! "{0}({1}) expected, but {2} called.", ! ExpectedMethodName, argsToString(_argumentExpectations), ! ActualMethodCall ! )); } if (ExpectedMethodName == ActualMethodName && _expectationType == ExpectationMethodType.NoCall) { Assertion.Fail(String.Format( ! "{0}({1}) was not expected.", ! ExpectedMethodName, argsToString(_argumentExpectations) ! )); } if (_argumentExpectations == null) ! { ! return; ! } ! object[] actualArguments = ActualMethodCall.Arguments; ! // actual arguments must be equal to expectations ! if ( actualArguments.Length != _argumentExpectations.Length ) ! { ! Assertion.Fail( String.Format( ! "Expected {0} arguments but received {1} " + ! "in method call {2}.", ! _argumentExpectations.Length, ! actualArguments.Length, ! ActualMethodCall ! ) ); ! } ! // assert that each passed in arg is validated by the appropriate predicate. ! for ( int i = 0; i < _argumentExpectations.Length; i++ ) ! { ! object argumentExpectation = _argumentExpectations[ i ]; ! object actualArgument = actualArguments[ i ]; ! // evaluate whether input expectations have been met ! IPredicate predicate = ! PredicateUtils.ConvertFrom( argumentExpectation ); ! bool isPredicateSatisfied = ! predicate.Eval( actualArgument ); ! if ( ! isPredicateSatisfied ) ! { ! Assertion.Fail( String.Format( ! "Failed to satisfy '{0}' on argument[{1}] " + ! "of method call {2}", ! predicate, ! i, ! ActualMethodCall ! ) ); ! } ! // return output expectations if specified ! IArgumentMutator mutator = ! argumentExpectation as IArgumentMutator; ! if ( mutator != null ) ! { ! mutator.Mutate( ref actualArguments[ i ] ); ! } ! } ! if ( _expectedReturnValue!=null ) ! { ! // handle return values that are ValueTypes and ensure they can be casted ! // in the IL that unboxes the return value. ! Type expectedReturnType = _expectedReturnValue.GetType(); ! Type returnType = ActualMethod.ReturnType; ! if ( returnType!=typeof(void) && returnType.IsValueType ) ! { ! if ( returnType!=expectedReturnType ) ! { ! _expectedReturnValue = Convert.ChangeType(_expectedReturnValue, returnType); ! } ! } ! } ! // if exception setup to be thrown, throw it ! if ( _expectedException != null ) ! { ! throw _expectedException; ! } ! } ! /// <summary> ! /// Check actual incoming method call and return expected outgoing response. ! /// <see cref="IMethodCallExpectation.CheckCallAndSendResponse"/> ! /// </summary> ! public object CheckCallAndSendResponse( MethodCall call ) ! { ! _actualMethodCall = call; ! this.Verify( ); ! return _expectedReturnValue; ! } ! } } \ No newline at end of file --- 144,357 ---- public string ExpectedMethodName { ! get { return _expectedMethodName; } ! } ! ! /// <summary> /// Expected Method Type /// </summary> public ExpectationMethodType ExpectationType { + get { return _expectationType; } + } + + /// <summary> + /// Expected Return Value + /// </summary> + public object ReturnValue + { + get { return _expectedReturnValue; } + } + + /// <summary> + /// True if actual method call was set. + /// <seealso cref="ActualMethodCall"/> + /// </summary> + public bool ActualMethodCallWasSet + { + get { return _actualMethodCall != null; } + } + + /// <summary> + /// <see cref="MethodInfo"/> of actual method called. + /// </summary> + public MethodInfo ActualMethod + { + get { return ActualMethodCall.Method; } + } + + /// <summary> + /// Name of actual method called. + /// </summary> + public string ActualMethodName + { + get { return ActualMethodCall.MethodName; } + } + + /// <summary> + /// Actual method call. + /// </summary> + public MethodCall ActualMethodCall + { get { ! if (! ActualMethodCallWasSet) ! { ! throw new InvalidOperationException( ! "Cannot get property ActualMethodCall " + ! "before setting it." ! ); ! } ! return _actualMethodCall; ! } ! set ! { ! if (value == null) ! { ! throw new ArgumentNullException( ! "Cannot set ActualMethodCall property to null" ! ); ! } ! if (ActualMethodCallWasSet) ! { ! throw new InvalidOperationException( ! "Cannot set property ActualMethodCall " + ! "more than once." ! ); ! } ! _actualMethodCall = value; } } ! ! //TODO: Refactor methods ! private string argsToString(object[] args) ! { ! if (args != null && args.Length > 0) ! { ! string[] argText = new string[args.Length]; ! for (int i = 0; i < args.Length; ++i) ! { ! object arg = args[i]; ! if (ReferenceEquals(arg, null)) ! { ! argText[i] = "null"; ! } ! else if (arg is string) ! { ! argText[i] = "\"" + arg.ToString() + "\""; ! } ! else ! { ! argText[i] = arg.ToString(); ! } ! } ! return String.Join(", ", argText); ! } ! return String.Empty; ! } ! ! /// <summary> ! /// Verifies this expectation, when the method is called ! /// </summary> ! public void Verify() ! { ! if (! ActualMethodCallWasSet) ! { ! Assertion.Fail(String.Format( ! "{0}({1}) expected but never called.", ! ExpectedMethodName, ! argsToString(_argumentExpectations) ! )); ! } if (ExpectedMethodName != ActualMethodName && _expectationType == ExpectationMethodType.Call) { Assertion.Fail(String.Format( ! "{0}({1}) expected, but {2} called.", ! ExpectedMethodName, argsToString(_argumentExpectations), ! ActualMethodCall ! )); } if (ExpectedMethodName == ActualMethodName && _expectationType == ExpectationMethodType.NoCall) { Assertion.Fail(String.Format( ! "{0}({1}) was not expected.", ! ExpectedMethodName, argsToString(_argumentExpectations) ! )); } if (_argumentExpectations == null) ! { ! return; ! } ! object[] actualArguments = ActualMethodCall.Arguments; ! // actual arguments must be equal to expectations ! if (actualArguments.Length != _argumentExpectations.Length) ! { ! Assertion.Fail(String.Format( ! "Expected {0} arguments but received {1} " + ! "in method call {2}.", ! _argumentExpectations.Length, ! actualArguments.Length, ! ActualMethodCall ! )); ! } ! // assert that each passed in arg is validated by the appropriate predicate. ! for (int i = 0; i < _argumentExpectations.Length; i++) ! { ! object argumentExpectation = _argumentExpectations[i]; ! object actualArgument = actualArguments[i]; ! // evaluate whether input expectations have been met ! IPredicate predicate = ! PredicateUtils.ConvertFrom(argumentExpectation); ! bool isPredicateSatisfied = ! predicate.Eval(actualArgument); ! if (! isPredicateSatisfied) ! { ! Assertion.Fail(String.Format( ! "Failed to satisfy '{0}' on argument[{1}] " + ! "of method call {2}", ! predicate, ! i, ! ActualMethodCall ! )); ! } ! // return output expectations if specified ! IArgumentMutator mutator = ! argumentExpectation as IArgumentMutator; ! if (mutator != null) ! { ! mutator.Mutate(ref actualArguments[i]); ! } ! } ! if (_expectedReturnValue != null) ! { ! // handle return values that are ValueTypes and ensure they can be casted ! // in the IL that unboxes the return value. ! Type expectedReturnType = _expectedReturnValue.GetType(); ! Type returnType = ActualMethod.ReturnType; ! if (returnType != typeof (void) && returnType.IsValueType) ! { ! if (returnType != expectedReturnType) ! { ! _expectedReturnValue = Convert.ChangeType(_expectedReturnValue, returnType); ! } ! } ! } ! // if exception setup to be thrown, throw it ! if (_expectedException != null) ! { ! throw _expectedException; ! } ! } ! ! /// <summary> ! /// Check actual incoming method call and return expected outgoing response. ! /// <see cref="IMethodCallExpectation.CheckCallAndSendResponse"/> ! /// </summary> ! public object CheckCallAndSendResponse(MethodCall call) ! { ! _actualMethodCall = call; ! Verify(); ! return _expectedReturnValue; ! } ! } } \ No newline at end of file |