|
From: <sk...@us...> - 2003-10-16 13:49:21
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv16965/src/NMock
Modified Files:
DynamicMock.cs MockCall.cs
Log Message:
Fixed bug with null parameters at execution to do with method resolution
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** DynamicMock.cs 14 Oct 2003 14:08:52 -0000 1.16
--- DynamicMock.cs 16 Oct 2003 13:49:13 -0000 1.17
***************
*** 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));
! }
!
! }
}
--- 7,166 ----
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);
!
! //find method with argumentTypes
! foreach (Type t in allTypes)
! {
! PropertyInfo property = t.GetProperty(signature.methodName, ClassGenerator.ALL_INSTANCE_METHODS);
! if (property != null)
! {
! return;
! }
!
! MethodInfo method = FindBestMatchingMethod(t, signature);
! 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));
! }
!
!
! public MethodInfo FindBestMatchingMethod(Type t, MethodSignature signature)
! {
! //find any methods that match all non-null argument types
! MethodInfo[] methods = t.GetMethods(ClassGenerator.ALL_INSTANCE_METHODS);
! foreach (MethodInfo m in methods)
! {
! if (!m.Name.Equals(signature.methodName))
! continue;
!
! ParameterInfo[] parameters = m.GetParameters();
! if ( parameters.Length!=signature.argumentTypes.Length )
! continue;
!
! bool match = true;
! for ( int pi=0; pi<parameters.Length; pi++ )
! {
! if ( signature.argumentTypes[pi]==null )
! continue;
!
! if ( parameters[pi].GetType().IsAssignableFrom(signature.argumentTypes[pi]) )
! {
! match = false;
! break;
! }
!
! }
!
! if (match)
! {
! return m;
! }
! }
! return null;
! }
!
! }
}
Index: MockCall.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/MockCall.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** MockCall.cs 8 Aug 2003 00:05:03 -0000 1.4
--- MockCall.cs 16 Oct 2003 13:49:13 -0000 1.5
***************
*** 99,103 ****
if (args[i] == null)
{
! result[i] = typeof(object);
}
else
--- 99,103 ----
if (args[i] == null)
{
! result[i] = null; //typeof(object);
}
else
|