|
From: Owen R. <exo...@us...> - 2004-04-08 04:00:46
|
Update of /cvsroot/nmock/nmock/src/NMock In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8638/src/NMock Modified Files: DynamicMock.cs MethodSignature.cs Log Message: - applying Levi's fixes - fixed bug where NMock overrides the class' destructor Index: MethodSignature.cs =================================================================== RCS file: /cvsroot/nmock/nmock/src/NMock/MethodSignature.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MethodSignature.cs 28 Nov 2003 17:31:22 -0000 1.2 --- MethodSignature.cs 8 Apr 2004 03:47:39 -0000 1.3 *************** *** 1,4 **** --- 1,5 ---- using System; using System.Reflection; + using System.Text; using NMock.Constraints; *************** *** 22,26 **** public override string ToString() { ! return typeName + "." + methodName + "()"; } --- 23,34 ---- public override string ToString() { ! StringBuilder argumentList = new StringBuilder(); ! foreach (Type type in argumentTypes) ! { ! if (argumentList.Length > 0) argumentList.Append(", "); ! ! argumentList.Append(type.FullName); ! } ! return string.Format("{0}.{1}({2})", typeName, methodName, argumentList); } *************** *** 38,45 **** --- 46,72 ---- } + public PropertyInfo FindMatchingPropertyOn(Type type) + { + return type.GetProperty( + methodName, ClassGenerator.ALL_INSTANCE_METHODS); + } + + public MethodInfo FindMatchingMethodIn(Type type) + { + foreach (MethodInfo methodInfo in type.GetMethods(ClassGenerator.ALL_INSTANCE_METHODS)) + { + if (MatchesNameAndAllNonNullArguments(methodInfo)) + { + return methodInfo; + } + } + return null; + } + public bool IsPropertyOn(Type type) { return type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS) != null; } + public bool HasMatchingMethodIn(Type type) { *************** *** 68,71 **** --- 95,99 ---- } + private bool MatchNonNullArguments(ParameterInfo[] parameters) { *************** *** 76,84 **** { Type argumentType = argumentTypes[pi]; ! if ( argumentType != null && parameters[pi].GetType().IsAssignableFrom(argumentType) ) ! return false; } return true; - } } } --- 104,116 ---- { Type argumentType = argumentTypes[pi]; ! if (argumentType == null) ! continue; ! ! Type parameterType = parameters[pi].ParameterType; ! if (!parameterType.IsAssignableFrom(argumentType)) ! return false; } return true; } } + } Index: DynamicMock.cs =================================================================== RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** DynamicMock.cs 23 Mar 2004 06:20:17 -0000 1.20 --- DynamicMock.cs 8 Apr 2004 03:47:38 -0000 1.21 *************** *** 59,76 **** { 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)); } } --- 59,80 ---- { MethodSignature signature = new MethodSignature(Name, methodName, argTypes); ! CheckReturnTypeIsValid(signature, returnVal); SetupResult(signature, returnVal); } ! private void CheckReturnTypeIsValid(MethodSignature signature, object returnVal) { if (returnVal == null) return; ! if (signature.HasAConstraintArgument) ! return; ! ! Type realReturnType = GetReturnTypeForSignature(signature); if(! realReturnType.IsAssignableFrom(returnVal.GetType())) { ! throw new ArgumentException( ! string.Format("method <{0}> should return a {1}", ! signature, realReturnType)); } } *************** *** 81,107 **** 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) { foreach (Type t in new InterfaceLister().List(type)) { ! MethodInfo method = t.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS); if (method != null) return method.ReturnType; ! PropertyInfo property = t.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS); if (property != null) return property.PropertyType; } ! throw new MissingMethodException(String.Format("method <{0}> not defined", methodName)); } } --- 85,106 ---- return; ! GetReturnTypeForSignature(signature); } ! private Type GetReturnTypeForSignature(MethodSignature signature) { foreach (Type t in new InterfaceLister().List(type)) { ! MethodInfo method = signature.FindMatchingMethodIn(t); if (method != null) return method.ReturnType; ! PropertyInfo property = signature.FindMatchingPropertyOn(t); if (property != null) return property.PropertyType; } ! throw new MissingMethodException( ! string.Format("method <{0}> not defined", signature)); } } |