|
From: <sm...@us...> - 2003-10-14 14:08:57
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv12109/src/NMock
Modified Files:
DynamicMock.cs
Log Message:
Fixed SetupResult to check for conformance of return type, not just strict match
Tidied up some conditional logic.
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** DynamicMock.cs 13 Aug 2003 20:44:12 -0000 1.15
--- DynamicMock.cs 14 Oct 2003 14:08:52 -0000 1.16
***************
*** 7,127 ****
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)
! {
! foreach(Type argType in signature.argumentTypes)
! {
! if(typeof(IConstraint).IsAssignableFrom(argType)) return;
! }
! Type[] allTypes = new InterfaceLister().List(type);
! 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)
! {
! return;
! }
! if(method != null)
! {
! 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));
! }
! }
}
--- 7,116 ----
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 realReturnType = (method == null
! ? type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS).PropertyType
! : method.ReturnType);
! if(! realReturnType.IsAssignableFrom(returnVal.GetType()))
! {
! throw new ArgumentException(String.Format("method <{0}> returns a {1}", methodName, realReturnType));
! }
! }
! void checkMethodIsValidIfNoConstraints(MethodSignature signature)
! {
! foreach(Type argType in signature.argumentTypes)
! {
! if(typeof(IConstraint).IsAssignableFrom(argType)) return;
! }
! Type[] allTypes = new InterfaceLister().List(type);
! 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)
! {
! return;
! }
! if(method != null)
! {
! 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));
! }
! }
}
|