|
From: <joe...@us...> - 2003-02-27 22:45:54
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv30577/src/NMock
Modified Files:
DynamicMock.cs
Log Message:
Added support for faster error handling (thanks to Jeremy Stell Smith for the failing test, Jim Arnold for implementing it and to Paul Hammant and Martin Fowler for keeping us company)
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** DynamicMock.cs 21 Jan 2003 16:44:08 -0000 1.5
--- DynamicMock.cs 27 Feb 2003 22:45:51 -0000 1.6
***************
*** 1,4 ****
--- 1,5 ----
using System;
using System.Collections;
+ using System.Reflection;
using NMock.Dynamic;
***************
*** 54,57 ****
--- 55,119 ----
obj = cg.Generate(type, this, ignore);
}
+
+ public override void Expect(string methodName, params object[] args)
+ {
+ checkMethodIsValid(methodName);
+
+ base.Expect(methodName, args);
+ }
+
+ public override void SetupResult(string methodName, object returnVal)
+ {
+ checkMethodIsValid(methodName);
+ checkReturnTypeIsValid(methodName, returnVal);
+ base.SetupResult(methodName, returnVal);
+ }
+
+ 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 checkMethodIsValid(string methodName)
+ {
+ MethodInfo method = type.GetMethod(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
+ PropertyInfo property = type.GetProperty(methodName, ClassGenerator.ALL_INSTANCE_METHODS);
+
+ if (method == null && property == null)
+ {
+ throw new MissingMethodException(String.Format("method <{0}> not defined", methodName));
+ }
+
+
+ if(method != null)
+ {
+ if(!method.IsVirtual) throw new ArgumentException(String.Format("method <{0}> is not virtual", methodName));
+ }
+ }
+
}
}
|