|
From: <sm...@us...> - 2003-08-08 00:05:08
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv5316/src/NMock
Modified Files:
Mock.cs DynamicMock.cs MockCall.cs Method.cs
Added Files:
Invocation.cs MethodSignature.cs
Log Message:
Added Invocation and MethodSignature to collect data values together.
Include type name in error reports
Tidied up MockTest.
--- NEW FILE: Invocation.cs ---
using System;
namespace NMock
{
/// <summary>
/// Name and argument details of an attempted call of a method
/// </summary>
public class Invocation
{
public readonly string methodName;
public readonly object[] arguments;
public Invocation(string methodName, object[] arguments)
{
this.methodName = methodName;
this.arguments = arguments;
}
}
}
--- NEW FILE: MethodSignature.cs ---
using System;
namespace NMock
{
public class MethodSignature
{
public readonly string typeName;
public readonly string methodName;
public readonly Type[] argumentTypes;
public MethodSignature(string typeName, string methodName, Type[] argumentTypes)
{
this.typeName = typeName;
this.methodName = methodName;
this.argumentTypes = argumentTypes;
}
public override string ToString()
{
return typeName + "." + methodName + "()";
}
}
}
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Mock.cs 25 Jul 2003 15:52:11 -0000 1.10
--- Mock.cs 8 Aug 2003 00:05:02 -0000 1.11
***************
*** 44,66 ****
public virtual void ExpectNoCall(string methodName, params Type[] argTypes)
{
! addExpectation(methodName, new MockNoCall(argTypes));
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(methodName, new MockCall(result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! addExpectation(methodName, new MockCall(null, e, args));
}
private void addExpectation(string methodName, MockCall call)
{
! IMethod method = getMethod(methodName, call.ArgTypes);
if (method == null)
{
! method = new Method(methodName);
methods[methodName] = method;
}
--- 44,69 ----
public virtual void ExpectNoCall(string methodName, params Type[] argTypes)
{
! addExpectation(methodName, new MockNoCall(new MethodSignature(Name, methodName, argTypes)));
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(methodName,
! new MockCall(new MethodSignature(Name, methodName, MockCall.GetArgTypes(args)), result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! addExpectation(methodName,
! new MockCall(new MethodSignature(Name, methodName, MockCall.GetArgTypes(args)), null, e, args));
}
private void addExpectation(string methodName, MockCall call)
{
! MethodSignature signature = new MethodSignature(Name, methodName, call.ArgTypes);
! IMethod method = getMethod(signature);
if (method == null)
{
! method = new Method(signature);
methods[methodName] = method;
}
***************
*** 70,74 ****
public virtual void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! IMethod method = getMethod(methodName, argTypes);
if (method == null)
{
--- 73,77 ----
public virtual void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! IMethod method = getMethod(new MethodSignature(Name, methodName, argTypes));
if (method == null)
{
***************
*** 76,86 ****
methods[methodName] = method;
}
! method.SetExpectation(new MockCall( returnVal, null, null));
}
public virtual object Call(string methodName, params object[] args)
{
! Type[] argTypes = MockCall.GetArgTypes(args);
! IMethod method = getMethod(methodName, argTypes);
if (method == null)
{
--- 79,89 ----
methods[methodName] = method;
}
! method.SetExpectation(new MockCall(new MethodSignature(Name, methodName, argTypes), returnVal, null, null));
}
public virtual object Call(string methodName, params object[] args)
{
! MethodSignature signature = new MethodSignature(Name, methodName, MockCall.GetArgTypes(args));
! IMethod method = getMethod(signature);
if (method == null)
{
***************
*** 102,108 ****
}
! protected virtual IMethod getMethod(string methodName, Type[] argTypes)
{
! return (IMethod)methods[methodName];
}
--- 105,111 ----
}
! protected virtual IMethod getMethod(MethodSignature signature)
{
! return (IMethod)methods[signature.methodName];
}
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** DynamicMock.cs 3 Aug 2003 22:42:47 -0000 1.13
--- DynamicMock.cs 8 Aug 2003 00:05:03 -0000 1.14
***************
*** 53,66 ****
}
! protected override IMethod getMethod(string methodName, Type[] argTypes)
{
! checkMethodIsValidIfNoConstraints(methodName, argTypes);
! return base.getMethod(methodName, argTypes);
}
public override void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! checkMethodIsValidIfNoConstraints(methodName, argTypes);
checkReturnTypeIsValid(methodName, returnVal);
base.SetupResult(methodName, returnVal, argTypes);
--- 53,67 ----
}
! protected override IMethod getMethod(MethodSignature signature)
{
! checkMethodIsValidIfNoConstraints(signature);
! return base.getMethod(signature);
}
public override void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! MethodSignature signature = new MethodSignature(Name, methodName, argTypes);
! checkMethodIsValidIfNoConstraints(signature);
checkReturnTypeIsValid(methodName, returnVal);
base.SetupResult(methodName, returnVal, argTypes);
***************
*** 96,102 ****
}
! void checkMethodIsValidIfNoConstraints(string methodName, Type[] argTypes)
{
! foreach(Type argType in argTypes)
{
if(typeof(IConstraint).IsAssignableFrom(argType)) return;
--- 97,103 ----
}
! void checkMethodIsValidIfNoConstraints(MethodSignature signature)
{
! foreach(Type argType in signature.argumentTypes)
{
if(typeof(IConstraint).IsAssignableFrom(argType)) return;
***************
*** 106,112 ****
foreach (Type t in allTypes)
{
! MethodInfo method = t.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS, null,
! argTypes, null);
! PropertyInfo property = t.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
if (property != null)
{
--- 107,113 ----
foreach (Type t in allTypes)
{
! MethodInfo method = t.GetMethod(signature.methodName, ClassGenerator.ALL_INSTANCE_METHODS, null,
! signature.argumentTypes, null);
! PropertyInfo property = t.GetProperty(signature.methodName, ClassGenerator.ALL_INSTANCE_METHODS);
if (property != null)
{
***************
*** 117,126 ****
if(!method.IsVirtual)
{
! throw new ArgumentException(String.Format("method <{0}> is not virtual", methodName));
}
return;
}
}
! throw new MissingMethodException(String.Format("method <{0}> not defined", methodName));
}
--- 118,127 ----
if(!method.IsVirtual)
{
! throw new ArgumentException(String.Format("method <{0}> is not virtual", signature.methodName));
}
return;
}
}
! throw new MissingMethodException(String.Format("method <{0}> not defined", signature.methodName));
}
Index: MockCall.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/MockCall.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MockCall.cs 24 Jul 2003 23:08:42 -0000 1.3
--- MockCall.cs 8 Aug 2003 00:05:03 -0000 1.4
***************
*** 6,20 ****
public class MockCall
{
! protected Type[] argTypes;
private IConstraint[] expectedArgs;
private object returnValue;
private Exception e;
! public MockCall(object returnValue, Exception e, object[] expectedArgs)
{
! this.argTypes = argsAsTypes(expectedArgs);
! this.expectedArgs = argsAsConstraints(expectedArgs);
this.returnValue = returnValue;
this.e = e;
}
--- 6,20 ----
public class MockCall
{
! protected MethodSignature signature;
private IConstraint[] expectedArgs;
private object returnValue;
private Exception e;
! public MockCall(MethodSignature signature, object returnValue, Exception e, object[] expectedArgs)
{
! this.signature = signature;
this.returnValue = returnValue;
this.e = e;
+ this.expectedArgs = argsAsConstraints(expectedArgs);
}
***************
*** 24,39 ****
}
! public Type[] ArgTypes
! {
! get
! {
! return argTypes;
! }
! }
!
! private Type[] argsAsTypes(object[] args)
! {
! return MockCall.GetArgTypes(args);
! }
private IConstraint[] argsAsConstraints(object[] args)
--- 24,28 ----
}
! public Type[] ArgTypes { get {return signature.argumentTypes; } }
private IConstraint[] argsAsConstraints(object[] args)
***************
*** 124,135 ****
public class MockNoCall : MockCall
{
! public MockNoCall(Type[] argTypes) : base(null, null, null)
{
- this.argTypes = argTypes;
}
public override object Call(string methodName, object[] actualArgs)
{
! throw new VerifyException(methodName + "() called", 0, 1);
}
}
--- 113,123 ----
public class MockNoCall : MockCall
{
! public MockNoCall(MethodSignature signature) : base(signature, null, null, null)
{
}
public override object Call(string methodName, object[] actualArgs)
{
! throw new VerifyException(signature.ToString() + " called", 0, 1);
}
}
Index: Method.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Method.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Method.cs 13 Mar 2003 22:22:11 -0000 1.4
--- Method.cs 8 Aug 2003 00:05:03 -0000 1.5
***************
*** 8,24 ****
private static object NO_RETURN_VALUE = new object();
! private string name;
private int timesCalled = 0;
private CallSequence expectations;
! public Method(string aName)
{
! name = aName;
! expectations = new CallSequence(aName);
}
public virtual string Name
{
! get { return name; }
}
--- 8,24 ----
private static object NO_RETURN_VALUE = new object();
! private MethodSignature signature;
private int timesCalled = 0;
private CallSequence expectations;
! public Method(MethodSignature signature)
{
! this.signature = signature;
! expectations = new CallSequence(signature.methodName);
}
public virtual string Name
{
! get { return signature.methodName; }
}
***************
*** 37,46 ****
MockCall mockCall = expectations[timesCalled];
timesCalled++;
! return mockCall.Call(name, parameters);
}
public virtual void Verify()
{
! Mock.Assertion.AssertEquals(name + "() " + CallCountErrorMessage(),
expectations.CountExpectedCalls, timesCalled);
}
--- 37,46 ----
MockCall mockCall = expectations[timesCalled];
timesCalled++;
! return mockCall.Call(signature.methodName, parameters);
}
public virtual void Verify()
{
! Mock.Assertion.AssertEquals(signature + " " + CallCountErrorMessage(),
expectations.CountExpectedCalls, timesCalled);
}
***************
*** 67,71 ****
get
{
! if (sequence.Count == timesCalled)
{
throw new VerifyException(name + "() called too many times", sequence.Count, timesCalled + 1);
--- 67,71 ----
get
{
! if (sequence.Count <= timesCalled)
{
throw new VerifyException(name + "() called too many times", sequence.Count, timesCalled + 1);
***************
*** 84,90 ****
{
int count = 0;
! for (int i = 0; i < sequence.Count; i++)
{
! if (! (sequence[i] is MockNoCall))
{
count++;
--- 84,90 ----
{
int count = 0;
! foreach (Object mockCall in sequence)
{
! if (! (mockCall is MockNoCall))
{
count++;
|