|
From: <sm...@us...> - 2003-11-28 17:31:26
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv24047/src/NMock
Modified Files:
MethodSignature.cs DynamicMock.cs
Log Message:
Refactoring, moved some method lookup behaviour into MethodSignature
Index: MethodSignature.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/MethodSignature.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** MethodSignature.cs 8 Aug 2003 00:05:03 -0000 1.1
--- MethodSignature.cs 28 Nov 2003 17:31:22 -0000 1.2
***************
*** 1,3 ****
--- 1,7 ----
using System;
+ using System.Reflection;
+
+ using NMock.Constraints;
+ using NMock.Dynamic;
namespace NMock
***************
*** 20,23 ****
--- 24,84 ----
return typeName + "." + methodName + "()";
}
+
+ public bool HasAConstraintArgument
+ {
+ get
+ {
+ foreach(Type argType in argumentTypes)
+ {
+ if(typeof(IConstraint).IsAssignableFrom(argType))
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public bool IsPropertyOn(Type type)
+ {
+ return type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS) != null;
+ }
+ public bool HasMatchingMethodIn(Type type)
+ {
+ foreach (MethodInfo methodInfo in type.GetMethods(ClassGenerator.ALL_INSTANCE_METHODS))
+ {
+ if (MatchesNameAndAllNonNullArguments(methodInfo))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private bool MatchesNameAndAllNonNullArguments(MethodInfo methodInfo)
+ {
+ if (!methodName.Equals(methodInfo.Name))
+ return false;
+
+ if (! MatchNonNullArguments(methodInfo.GetParameters()))
+ return false;
+
+ if(!methodInfo.IsVirtual)
+ throw new ArgumentException(String.Format("method <{0}> is not virtual", methodName));
+
+ return true;
+ }
+
+ private bool MatchNonNullArguments(ParameterInfo[] parameters)
+ {
+ if ( argumentTypes.Length != parameters.Length )
+ return false;
+
+ for ( int pi=0; pi<parameters.Length; pi++ )
+ {
+ Type argumentType = argumentTypes[pi];
+ if ( argumentType != null && parameters[pi].GetType().IsAssignableFrom(argumentType) )
+ return false;
+ }
+ return true;
+ }
}
}
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** DynamicMock.cs 16 Oct 2003 13:49:13 -0000 1.17
--- DynamicMock.cs 28 Nov 2003 17:31:22 -0000 1.18
***************
*** 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;
! }
! }
}
--- 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));
! }
! }
}
|