From: Griffin C. <gc...@us...> - 2005-04-25 03:26:42
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5075/DotNetMock/Dynamic Modified Files: ExpectationMethod.cs MethodCall.cs Log Message: - Minor refactorings Index: MethodCall.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/MethodCall.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** MethodCall.cs 8 Apr 2005 01:21:51 -0000 1.3 --- MethodCall.cs 25 Apr 2005 03:26:31 -0000 1.4 *************** *** 16,19 **** --- 16,23 ---- public class MethodCall { + #region Private Instance Fields + private MethodInfo _method; + private object[] _arguments; + #endregion /// <summary> /// Create new method call instance. *************** *** 156,163 **** public object[] Arguments { get { return _arguments; } } - #region Private Instance Fields - private MethodInfo _method; - private object[] _arguments; - #endregion } } --- 160,163 ---- Index: ExpectationMethod.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/ExpectationMethod.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** ExpectationMethod.cs 8 Apr 2005 02:57:09 -0000 1.23 --- ExpectationMethod.cs 25 Apr 2005 03:26:31 -0000 1.24 *************** *** 1,10 **** #region License // Copyright (c) 2004 Griffin Caprio & Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Reflection; - using DotNetMock.Dynamic.Predicates; #endregion --- 1,13 ---- #region License + // Copyright (c) 2004 Griffin Caprio & Choy Rim. All rights reserved. + #endregion + #region Imports + using System; using System.Reflection; #endregion *************** *** 14,19 **** /// Expected method call used for building dynamic mocks /// </summary> ! public class ExpectationMethod : ! IMethodCallExpectation { /// <summary> --- 17,21 ---- /// Expected method call used for building dynamic mocks /// </summary> ! public class ExpectationMethod : IMethodCallExpectation { /// <summary> *************** *** 42,46 **** /// <param name="methodName">Method name to expect</param> public ExpectationMethod( string methodName ) ! : this(methodName, null, null, null) { } --- 44,48 ---- /// <param name="methodName">Method name to expect</param> public ExpectationMethod( string methodName ) ! : this( methodName, null, null, null ) { } *************** *** 80,88 **** object[] argumentExpectations, Exception expectedException ! ) { _expectedMethodName = methodName; _argumentExpectations = argumentExpectations; - _expectedReturnValue = returnValue; _expectedException = expectedException; --- 82,89 ---- object[] argumentExpectations, Exception expectedException ! ) { _expectedMethodName = methodName; _argumentExpectations = argumentExpectations; _expectedReturnValue = returnValue; _expectedException = expectedException; *************** *** 93,97 **** public string ExpectedMethodName { ! get { return _expectedMethodName; } } /// <summary> --- 94,101 ---- public string ExpectedMethodName { ! get ! { ! return _expectedMethodName; ! } } /// <summary> *************** *** 100,104 **** public object ReturnValue { ! get { return _expectedReturnValue; } } /// <summary> --- 104,111 ---- public object ReturnValue { ! get ! { ! return _expectedReturnValue; ! } } /// <summary> *************** *** 106,119 **** /// <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; --- 113,129 ---- /// <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; *************** *** 123,129 **** /// Name of actual method called. /// </summary> ! public string ActualMethodName { ! get { return ActualMethodCall.MethodName; } } /// <summary> --- 133,142 ---- /// Name of actual method called. /// </summary> ! public string ActualMethodName { ! get ! { ! return ActualMethodCall.MethodName; ! } } /// <summary> *************** *** 132,149 **** 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( --- 145,162 ---- 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( *************** *** 151,159 **** ); } ! if ( ActualMethodCallWasSet ) { throw new InvalidOperationException( ! "Cannot set property ActualMethodCall "+ ! "more than once." ); } --- 164,172 ---- ); } ! if ( ActualMethodCallWasSet ) { throw new InvalidOperationException( ! "Cannot set property ActualMethodCall " + ! "more than once." ); } *************** *** 162,187 **** } //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; --- 175,200 ---- } //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; *************** *** 190,212 **** /// Verifies this expectation /// </summary> ! public void Verify() { ! if ( ! ActualMethodCallWasSet ) { ! Assertion.Fail(String.Format( "{0}({1}) expected but never called.", ExpectedMethodName, ! argsToString(_argumentExpectations) ! )); } ! if( ExpectedMethodName != ActualMethodName ) { ! Assertion.Fail(String.Format( "{0}({1}) expected, but {2} called.", ! ExpectedMethodName, argsToString(_argumentExpectations), ActualMethodCall ! )); } ! if ( _argumentExpectations==null ) { return; --- 203,225 ---- /// Verifies this expectation /// </summary> ! public void Verify( ) { ! if ( ! ActualMethodCallWasSet ) { ! Assertion.Fail( String.Format( "{0}({1}) expected but never called.", ExpectedMethodName, ! argsToString( _argumentExpectations ) ! ) ); } ! if ( ExpectedMethodName != ActualMethodName ) { ! Assertion.Fail( String.Format( "{0}({1}) expected, but {2} called.", ! ExpectedMethodName, argsToString( _argumentExpectations ), ActualMethodCall ! ) ); } ! if ( _argumentExpectations == null ) { return; *************** *** 214,275 **** 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 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; } } ! } --- 227,284 ---- 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 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 |