|
From: <joe...@us...> - 2002-12-12 21:39:47
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv18108/src/NMock
Modified Files:
Mock.cs IMock.cs
Added Files:
IVerifiable.cs
Log Message:
Forgotten whatever the hell it is we've done (but Steve did some too)
--- NEW FILE: IVerifiable.cs ---
namespace NMock
{
public interface IVerifiable
{
void Verify();
}
}
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Mock.cs 15 Nov 2002 22:31:09 -0000 1.4
--- Mock.cs 12 Dec 2002 21:39:43 -0000 1.5
***************
*** 60,74 ****
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(new NoCallExpectation(methodName));
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(new Expectation(methodName, result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! addExpectation(new Expectation(methodName, null, e, args));
}
--- 60,74 ----
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(new MockNoCall(methodName));
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(new MockCall(methodName, result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! addExpectation(new MockCall(methodName, null, e, args));
}
***************
*** 85,90 ****
}
! IList list = (IList) expectations[methodName];
! if (list == null)
{
if ( strict )
--- 85,90 ----
}
! IList methodExpectations = (IList) expectations[methodName];
! if (methodExpectations == null)
{
if ( strict )
***************
*** 94,108 ****
return null;
}
! if (list.Count == counter(methodName))
{
! throw new VerifyException(methodName + "() called too many times", list.Count, counter(methodName) + 1);
}
! Expectation e = (Expectation)list[counter(methodName)];
! if (e is NoCallExpectation)
{
throw new VerifyException(methodName + "() called", 0, 1);
}
increment(methodName);
! return e.Verify(methodName, args);
}
--- 94,108 ----
return null;
}
! if (methodExpectations.Count == counter(methodName))
{
! throw new VerifyException(methodName + "() called too many times", methodExpectations.Count, counter(methodName) + 1);
}
! MockCall mockCall = (MockCall)methodExpectations[counter(methodName)];
! if (mockCall is MockNoCall)
{
throw new VerifyException(methodName + "() called", 0, 1);
}
increment(methodName);
! return mockCall.Call(methodName, args);
}
***************
*** 117,186 ****
}
! private void addExpectation(Expectation e)
{
! IList list = (IList) expectations[e.MethodName];
if (list == null)
{
list = new ArrayList();
! expectations[e.MethodName] = list;
}
! list.Add(e);
}
! private class Expectation
{
public string MethodName;
! private object[] args;
private object returnValue;
private Exception e;
! public Expectation(string methodName, object returnValue, Exception e, object[] args)
{
this.MethodName = methodName;
if (args == null)
{
! this.args = new object[0];
}
! else
{
! this.args = args;
}
!
! this.returnValue = returnValue;
! this.e = e;
}
!
! public virtual object Verify(string methodName, object[] args)
{
! if ( this.args.Length > 0 )
{
// assert that the correct number of arguments were passed
! Assertion.AssertEquals("x() called with incorrect number of parameters", this.args.Length, args.Length);
// assert that each passed in arg is validated by the appropriate predicate.
! for (int i = 0; i < this.args.Length; i++)
{
! object expectedArg = this.args[i];
! object actualArg = args[i];
! IConstraint Constraint = expectedArg as IConstraint;
! // if expectedArg is not an IConstraint, use default
! // behavior of IsEqual or IsAnything
! if (Constraint == null)
! {
! if (expectedArg == null)
! {
! Constraint = new IsAnything();
! }
! else
! {
! Constraint = new IsEqual(expectedArg);
! }
! }
! String messageFormat = "{0}() called with incorrect parameter ({1})";
! String message = String.Format(messageFormat, methodName, i + 1);
! if (!Constraint.Eval(actualArg))
{
! throw new VerifyException(message, Constraint.Message, actualArg);
}
}
--- 117,188 ----
}
! private void addExpectation(MockCall call)
{
! IList list = (IList) expectations[call.MethodName];
if (list == null)
{
list = new ArrayList();
! expectations[call.MethodName] = list;
}
! list.Add(call);
}
! private class MockCall
{
public string MethodName;
! private IConstraint[] expectedArgs;
private object returnValue;
private Exception e;
! public MockCall(string methodName, object returnValue, Exception e, object[] expectedArgs)
{
this.MethodName = methodName;
+ this.returnValue = returnValue;
+ this.e = e;
+ this.expectedArgs = argsAsConstraints(expectedArgs);
+ }
+
+ private IConstraint[] argsAsConstraints(object[] args)
+ {
if (args == null)
{
! return new IConstraint[0];
}
! IConstraint[] result = new IConstraint[args.Length];
! for (int i = 0; i < args.Length; ++i)
{
! result[i] = argAsConstraint(args[i]);
}
! return result;
}
!
! private IConstraint argAsConstraint(object arg)
{
! IConstraint constraint = arg as IConstraint;
! if (constraint != null)
! {
! return constraint;
! }
! return arg == null ? (IConstraint)new IsAnything() : (IConstraint)new IsEqual(arg);
! }
!
! public virtual object Call(string methodName, object[] actualArgs)
! {
! if ( this.expectedArgs.Length > 0 )
{
// assert that the correct number of arguments were passed
! Assertion.AssertEquals("x() called with incorrect number of parameters", this.expectedArgs.Length, actualArgs.Length);
// assert that each passed in arg is validated by the appropriate predicate.
! for (int i = 0; i < this.expectedArgs.Length; i++)
{
! IConstraint expectedArg = expectedArgs[i];
! object actualArg = actualArgs[i];
! if (!expectedArg.Eval(actualArg))
{
! String messageFormat = "{0}() called with incorrect parameter ({1})";
! String message = String.Format(messageFormat, methodName, i + 1);
! throw new VerifyException(message, expectedArg.Message, actualArg);
}
}
***************
*** 197,203 ****
}
! private class NoCallExpectation : Expectation
{
! public NoCallExpectation(string methodName) : base(methodName, null, null, null)
{
}
--- 199,205 ----
}
! private class MockNoCall : MockCall
{
! public MockNoCall(string methodName) : base(methodName, null, null, null)
{
}
Index: IMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/IMock.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** IMock.cs 15 Nov 2002 22:31:09 -0000 1.2
--- IMock.cs 12 Dec 2002 21:39:43 -0000 1.3
***************
*** 8,12 ****
/// </summary>
/// <see cref="Mock"/>
! public interface IMock
{
/// <summary>
--- 8,12 ----
/// </summary>
/// <see cref="Mock"/>
! public interface IMock : IVerifiable
{
/// <summary>
***************
*** 59,67 ****
/// </summary>
object Call(string methodName, params object[] args);
!
! /// <summary>
! /// Verify that all expectations were met.
! /// </summary>
! void Verify();
}
}
--- 59,63 ----
/// </summary>
object Call(string methodName, params object[] args);
!
}
}
|