Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv24022/src/NMock
Modified Files:
Mock.cs IMock.cs DynamicMock.cs MockCall.cs SingleMethod.cs
Added Files:
CallMethodWithoutExpectation.cs
Log Message:
--- NEW FILE: CallMethodWithoutExpectation.cs ---
using System;
namespace NMock
{
public class CallMethodWithoutExpectation : IMethod
{
private string name;
private MockCall call;
public CallMethodWithoutExpectation(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
public virtual void SetExpectation(MockCall call)
{
this.call = call;
}
public virtual object Call(params object[] parameters)
{
return call.Call(name, parameters);
}
public virtual void Verify()
{
// noop
}
}
}
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** Mock.cs 13 Mar 2003 22:22:11 -0000 1.8
--- Mock.cs 24 Jul 2003 23:08:42 -0000 1.9
***************
*** 1,4 ****
--- 1,5 ----
using System;
using System.Collections;
+ using System.Reflection;
using NMock.Constraints;
***************
*** 11,15 ****
private bool strict;
private IDictionary methods;
!
public Mock(string name)
{
--- 12,16 ----
private bool strict;
private IDictionary methods;
!
public Mock(string name)
{
***************
*** 41,47 ****
}
! public virtual void ExpectNoCall(string methodName)
{
! addExpectation(methodName, new MockNoCall());
}
--- 42,48 ----
}
! public virtual void ExpectNoCall(string methodName, params Type[] argTypes)
{
! addExpectation(methodName, new MockNoCall(argTypes));
}
***************
*** 58,62 ****
private void addExpectation(string methodName, MockCall call)
{
! IMethod method = getMethod(methodName);
if (method == null)
{
--- 59,63 ----
private void addExpectation(string methodName, MockCall call)
{
! IMethod method = getMethod(methodName, call.ArgTypes);
if (method == null)
{
***************
*** 67,84 ****
}
! public virtual void SetupResult(string methodName, object returnVal)
{
! IMethod method = getMethod(methodName);
if (method == null)
{
! method = new SingleMethod(methodName);
methods[methodName] = method;
}
! method.SetExpectation(new MockCall(returnVal, null, null));
}
public virtual object Call(string methodName, params object[] args)
{
! IMethod method = getMethod(methodName);
if (method == null)
{
--- 68,91 ----
}
! // public virtual void SetupResult(string methodName, object returnVal)
! // {
! // SetupResult(methodName, new Type[0], returnVal);
! // }
!
! public virtual void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! IMethod method = getMethod(methodName, argTypes);
if (method == null)
{
! method = new CallMethodWithoutExpectation(methodName);
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)
{
***************
*** 100,104 ****
}
! protected virtual IMethod getMethod(string methodName)
{
return (IMethod)methods[methodName];
--- 107,111 ----
}
! protected virtual IMethod getMethod(string methodName, Type[] argTypes)
{
return (IMethod)methods[methodName];
Index: IMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/IMock.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** IMock.cs 12 Dec 2002 21:39:43 -0000 1.3
--- IMock.cs 24 Jul 2003 23:08:42 -0000 1.4
***************
*** 34,38 ****
/// Expect no call to this method.
/// </summary>
! void ExpectNoCall(string methodName);
/// <summary>
--- 34,38 ----
/// Expect no call to this method.
/// </summary>
! void ExpectNoCall(string methodName, params Type[] argTypes);
/// <summary>
***************
*** 53,57 ****
/// each time. Useful for getter style methods.
/// </summary>
! void SetupResult(string methodName, object returnVal);
/// <summary>
--- 53,58 ----
/// each time. Useful for getter style methods.
/// </summary>
! void SetupResult(string methodName, object returnVal, params Type[] argTypes);
!
/// <summary>
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** DynamicMock.cs 13 Jul 2003 13:27:06 -0000 1.10
--- DynamicMock.cs 24 Jul 2003 23:08:42 -0000 1.11
***************
*** 3,6 ****
--- 3,7 ----
using System.Reflection;
using NMock.Dynamic;
+ using NMock.Constraints;
namespace NMock
***************
*** 30,34 ****
if (obj == null)
{
! obj = generate();
}
return obj;
--- 31,35 ----
if (obj == null)
{
! generate();
}
return obj;
***************
*** 44,70 ****
}
! protected virtual object generate()
{
! ClassGenerator cg = createClassGenerator();
! return cg.Generate(type, this, ignore);
}
! protected virtual ClassGenerator createClassGenerator()
! {
! return new ClassGenerator();
! }
!
! protected override IMethod getMethod(string methodName)
{
! checkMethodIsValid(methodName);
! return base.getMethod(methodName);
}
! public override void SetupResult(string methodName, object returnVal)
{
! checkMethodIsValid(methodName);
checkReturnTypeIsValid(methodName, returnVal);
! base.SetupResult(methodName, returnVal);
}
--- 45,66 ----
}
! private void generate()
{
! ClassGenerator cg = new ClassGenerator();
! obj = cg.Generate(type, this, ignore);
}
! 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);
}
***************
*** 98,108 ****
}
! void checkMethodIsValid(string methodName)
{
!
Type[] allTypes = new InterfaceLister().List(type);
foreach (Type t in allTypes)
{
! MethodInfo method = t.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
PropertyInfo property = t.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
if (property != null)
--- 94,109 ----
}
! void checkMethodIsValidIfNoConstraints(string methodName, Type[] argTypes)
{
! foreach(Type argType in argTypes)
! {
! if(typeof(IConstraint).IsAssignableFrom(argType)) return;
! }
!
Type[] allTypes = new InterfaceLister().List(type);
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)
Index: MockCall.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/MockCall.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MockCall.cs 31 Dec 2002 21:36:39 -0000 1.2
--- MockCall.cs 24 Jul 2003 23:08:42 -0000 1.3
***************
*** 6,9 ****
--- 6,10 ----
public class MockCall
{
+ protected Type[] argTypes;
private IConstraint[] expectedArgs;
private object returnValue;
***************
*** 12,15 ****
--- 13,17 ----
public MockCall(object returnValue, Exception e, object[] expectedArgs)
{
+ this.argTypes = argsAsTypes(expectedArgs);
this.expectedArgs = argsAsConstraints(expectedArgs);
this.returnValue = returnValue;
***************
*** 22,25 ****
--- 24,40 ----
}
+ public Type[] ArgTypes
+ {
+ get
+ {
+ return argTypes;
+ }
+ }
+
+ private Type[] argsAsTypes(object[] args)
+ {
+ return MockCall.GetArgTypes(args);
+ }
+
private IConstraint[] argsAsConstraints(object[] args)
{
***************
*** 82,91 ****
}
}
}
public class MockNoCall : MockCall
{
! public MockNoCall() : base(null, null, null)
{
}
--- 97,130 ----
}
}
+
+ public static Type[] GetArgTypes(object[] args)
+ {
+ if (null == args)
+ {
+ return new Type[0];
+ }
+
+ Type[] result = new Type[args.Length];
+ for (int i = 0; i < result.Length; ++i)
+ {
+ if (args[i] == null)
+ {
+ result[i] = typeof(object);
+ }
+ else
+ {
+ result[i] = args[i].GetType();
+ }
+ }
+
+ return result;
+ }
}
public class MockNoCall : MockCall
{
! public MockNoCall(Type[] argTypes) : base(null, null, null)
{
+ this.argTypes = argTypes;
}
Index: SingleMethod.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/SingleMethod.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** SingleMethod.cs 13 Mar 2003 22:22:12 -0000 1.1
--- SingleMethod.cs 24 Jul 2003 23:08:42 -0000 1.2
***************
*** 3,6 ****
--- 3,7 ----
namespace NMock
{
+ //TODO: should be renamed to CallMethodAtLeastOnce
public class SingleMethod : IMethod
{
|