|
From: <sm...@us...> - 2003-11-29 17:04:50
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv19354/src/NMock
Modified Files:
Mock.cs DynamicMock.cs
Log Message:
Allow SetupResult to return conformant types, rather than exact match
Bit more refactoring
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** Mock.cs 13 Aug 2003 21:19:58 -0000 1.12
--- Mock.cs 29 Nov 2003 17:04:46 -0000 1.13
***************
*** 73,83 ****
public virtual void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! IMethod method = getMethod(new MethodSignature(Name, methodName, argTypes));
if (method == null)
{
! method = new CallMethodWithoutExpectation(methodName);
! methods[methodName] = method;
}
! method.SetExpectation(new MockCall(new MethodSignature(Name, methodName, argTypes), returnVal, null, null));
}
--- 73,88 ----
public virtual void SetupResult(string methodName, object returnVal, params Type[] argTypes)
{
! SetupResult(new MethodSignature(Name, methodName, argTypes), returnVal);
! }
!
! protected void SetupResult(MethodSignature signature, object returnVal)
! {
! IMethod method = getMethod(signature);
if (method == null)
{
! method = new CallMethodWithoutExpectation(signature.methodName);
! methods[signature.methodName] = method;
}
! method.SetExpectation(new MockCall(signature, returnVal, null, null));
}
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** DynamicMock.cs 28 Nov 2003 17:31:22 -0000 1.18
--- DynamicMock.cs 29 Nov 2003 17:04:46 -0000 1.19
***************
*** 7,111 ****
namespace NMock
{
! public class DynamicMock : Mock
! {
! private object mockInstance;
! private Type type;
! private IList ignoredMethodNames;
! private readonly Type superclassIfTypeIsInterface;
! public DynamicMock(Type type) : this(type, "Mock" + type.Name) {}
! public DynamicMock(Type type, string name) : this (type, name, null) {}
! public DynamicMock(Type type, string name, Type superclassIfTypeIsInterface) : base(name)
! {
! this.ignoredMethodNames = new ArrayList();
! this.type = type;
! this.superclassIfTypeIsInterface = superclassIfTypeIsInterface;
! }
! public override object MockInstance
! {
! get
! {
! if (mockInstance == null)
! {
! mockInstance = CreateClassGenerator().Generate();
! }
! return mockInstance;
! }
! }
! /// <summary>
! /// Don't generate mock method for suppied methodName.
! /// </summary>
! public virtual void Ignore(string methodName)
! {
! ignoredMethodNames.Add(methodName);
! }
! private ClassGenerator CreateClassGenerator()
! {
! return new ClassGenerator(type, this, ignoredMethodNames, superclassIfTypeIsInterface);
! }
! 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);
! }
! void checkReturnTypeIsValid(string methodName, object returnVal)
! {
! if (returnVal == null)
! {
! return;
! }
! MethodInfo method = type.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
! Type realReturnVal;
! if(method == null)
! {
! realReturnVal = type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS).PropertyType;
! }
! else
! {
! realReturnVal = method.ReturnType;
! }
! if (realReturnVal == null)
! {
! realReturnVal = type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS).PropertyType;
! }
!
! if(realReturnVal != returnVal.GetType())
! {
! throw new ArgumentException(String.Format("method <{0}> returns a {1}", methodName, realReturnVal));
! }
! }
!
! void checkMethodIsValidIfNoConstraints(MethodSignature signature)
! {
! if (signature.HasAConstraintArgument)
! return;
! foreach (Type t in new InterfaceLister().List(type))
! {
! if (signature.IsPropertyOn(t) || signature.HasMatchingMethodIn(t))
! return;
! }
! throw new MissingMethodException(String.Format("method <{0}> not defined", signature.methodName));
! }
! }
}
--- 7,101 ----
namespace NMock
{
! public class DynamicMock : Mock
! {
! private object mockInstance;
! private Type type;
! private IList ignoredMethodNames;
! private readonly Type superclassIfTypeIsInterface;
! public DynamicMock(Type type) : this(type, "Mock" + type.Name) {}
! public DynamicMock(Type type, string name) : this (type, name, null) {}
! public DynamicMock(Type type, string name, Type superclassIfTypeIsInterface) : base(name)
! {
! this.ignoredMethodNames = new ArrayList();
! this.type = type;
! this.superclassIfTypeIsInterface = superclassIfTypeIsInterface;
! }
! public override object MockInstance
! {
! get
! {
! if (mockInstance == null)
! {
! mockInstance = CreateClassGenerator().Generate();
! }
! return mockInstance;
! }
! }
! /// <summary>
! /// Don't generate mock method for suppied methodName.
! /// </summary>
! public virtual void Ignore(string methodName)
! {
! ignoredMethodNames.Add(methodName);
! }
! private ClassGenerator CreateClassGenerator()
! {
! return new ClassGenerator(type, this, ignoredMethodNames, superclassIfTypeIsInterface);
! }
! 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);
! SetupResult(signature, returnVal);
! }
! void CheckReturnTypeIsValid(string methodName, object returnVal)
! {
! if (returnVal == null)
! return;
! Type realReturnType = ReturnTypeForMethodName(methodName);
! if(! realReturnType.IsAssignableFrom(returnVal.GetType()))
! {
! throw new ArgumentException(String.Format("method <{0}> should return a {1}", methodName, realReturnType));
! }
! }
! private void CheckMethodIsValidIfNoConstraints(MethodSignature signature)
! {
! if (signature.HasAConstraintArgument)
! return;
! foreach (Type t in new InterfaceLister().List(type))
! {
! if (signature.IsPropertyOn(t) || signature.HasMatchingMethodIn(t))
! return;
! }
! throw new MissingMethodException(String.Format("method <{0}> not defined", signature.methodName));
! }
! private Type ReturnTypeForMethodName(string methodName)
! {
! MethodInfo method = type.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
! return (method == null)
! ? type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS).PropertyType
! : method.ReturnType;
! }
! }
}
|