From: <gc...@us...> - 2002-10-30 04:16:35
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic In directory usw-pr-cvs1:/tmp/cvs-serv28292/DotNetMock/Dynamic Modified Files: DynamicMock.cs IMock.cs IPredicate.cs Mock.cs Removed Files: Predicate.cs Log Message: Index: DynamicMock.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/DynamicMock.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DynamicMock.cs 12 Oct 2002 03:29:53 -0000 1.1 --- DynamicMock.cs 30 Oct 2002 04:16:32 -0000 1.2 *************** *** 6,12 **** public class DynamicMock : Mock { private object obj; private Type type; ! public DynamicMock(Type type) : this(type, null) { --- 6,13 ---- public class DynamicMock : Mock { + private object obj; private Type type; ! public DynamicMock(Type type) : this(type, null) { *************** *** 18,22 **** Name = "Mock" + name; } ! public DynamicMock(Type type, string name) : base(name) { --- 19,23 ---- Name = "Mock" + name; } ! public DynamicMock(Type type, string name) : base(name) { Index: IMock.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/IMock.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** IMock.cs 12 Oct 2002 03:29:53 -0000 1.1 --- IMock.cs 30 Oct 2002 04:16:32 -0000 1.2 *************** *** 21,27 **** --- 21,38 ---- /// <summary> + /// If strict, any method called that doesn't have an expectation set + /// will fail. (Defaults false) + /// </summary> + bool Strict { get; set; } + + /// <summary> /// Expect a method to be called with the supplied parameters. /// </summary> void Expect(string methodName, params object[] args); + + /// <summary> + /// Expect no call to this method. + /// </summary> + void ExpectNoCall(string methodName); /// <summary> Index: IPredicate.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/IPredicate.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** IPredicate.cs 12 Oct 2002 03:29:53 -0000 1.1 --- IPredicate.cs 30 Oct 2002 04:16:32 -0000 1.2 *************** *** 1,12 **** - using System; - namespace DotNetMock.Dynamic { - /// <summary> - /// Interface for all Predicates to implement. - /// </summary> public interface IPredicate ! { ! bool eval(object val); } } --- 1,7 ---- namespace DotNetMock.Dynamic { public interface IPredicate ! { ! bool Eval(object currentValue); } } Index: Mock.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic/Mock.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Mock.cs 12 Oct 2002 03:29:53 -0000 1.1 --- Mock.cs 30 Oct 2002 04:16:32 -0000 1.2 *************** *** 1,17 **** using System; using System.Collections; ! using NUnit.Framework; ! namespace DotNetMock.Dynamic { ! /// <summary> ! /// Summary description for Mock. ! /// </summary> public class Mock : IMock { ! private int currentExpectation; private string name; private object obj; ! private IList expectations; private IDictionary values; --- 1,16 ---- using System; using System.Collections; ! using DotNetMock.Dynamic.Predicates; ! namespace DotNetMock.Dynamic { ! public class Mock : IMock { ! private string name; private object obj; ! private bool strict; ! private IDictionary expectations; private IDictionary values; *************** *** 19,25 **** { this.name = name; ! expectations = new ArrayList(); values = new Hashtable(); - currentExpectation = 0; } --- 18,23 ---- { this.name = name; ! expectations = new Hashtable(); values = new Hashtable(); } *************** *** 36,42 **** } public virtual void Verify() { ! Assertion.AssertEquals(currentExpectation, expectations.Count); } --- 34,49 ---- } + public virtual bool Strict + { + get { return strict; } + set { strict = value; } + } + public virtual void Verify() { ! foreach (IList list in expectations.Values) ! { ! Assertion.AssertEquals(0, list.Count); ! } } *************** *** 46,57 **** } public virtual void ExpectAndReturn(string methodName, object result, params object[] args) { ! expectations.Add(new Expectation(methodName, result, null, args)); } public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args) { ! expectations.Add(new Expectation(methodName, null, e, args)); } --- 53,69 ---- } + public virtual void ExpectNoCall(string methodName) + { + addExpectation(new Expectation(methodName, null, new VerifyException(methodName + "() should never be called."), null)); + } + public virtual void ExpectAndReturn(string methodName, object result, params object[] args) { ! addExpectation(new Expectation(methodName, result, null, args)); } public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args) { ! addExpectation(new Expectation(methodName, null, e, args)); } *************** *** 67,82 **** return values[methodName]; } ! if (currentExpectation == expectations.Count) { Assertion.Fail(methodName + "() called too many times"); } ! Expectation exp = (Expectation)expectations[currentExpectation]; ! currentExpectation++; ! return exp.Verify(methodName, args); } private class Expectation { ! private string methodName; private object[] args; private object returnValue; --- 79,115 ---- return values[methodName]; } ! ! IList list = (IList) expectations[methodName]; ! if (list == null) ! { ! if ( strict ) ! { ! throw new VerifyException(methodName + "() called too many times"); ! } ! return null; ! } ! if (list.Count == 0) { Assertion.Fail(methodName + "() called too many times"); } ! Expectation e = (Expectation)list[0]; ! list.RemoveAt(0); ! return e.Verify(methodName, args); ! } ! ! private void addExpectation(Expectation e) ! { ! IList list = (IList) expectations[e.MethodName]; ! if (list == null) ! { ! list = new ArrayList(); ! expectations[e.MethodName] = list; ! } ! list.Add(e); } private class Expectation { ! public string MethodName; private object[] args; private object returnValue; *************** *** 85,89 **** public Expectation(string methodName, object returnValue, Exception e, object[] args) { ! this.methodName = methodName; if (args == null) { --- 118,122 ---- public Expectation(string methodName, object returnValue, Exception e, object[] args) { ! this.MethodName = methodName; if (args == null) { *************** *** 99,105 **** } ! public object Verify(string methodName, object[] args) { ! Assertion.AssertEquals(this.methodName, methodName); // assert that each passed in arg is validated by the appropriate predicate. --- 132,138 ---- } ! public virtual object Verify(string methodName, object[] args) { ! Assertion.AssertEquals(this.MethodName, methodName); // assert that each passed in arg is validated by the appropriate predicate. *************** *** 124,128 **** } ! Assertion.Assert(predicate.eval(actualArg)); } --- 157,161 ---- } ! Assertion.Assert(predicate.Eval(actualArg)); } *************** *** 136,139 **** --- 169,200 ---- } } + + private class Assertion + { + public static void Assert(bool expression) + { + if (!expression) + { + Fail("Verification failed."); + } + } + + public static void AssertEquals(object a, object b) + { + if (! a.Equals(b)) + { + Fail("Verification failed."); + } + } + + public static void Fail(string msg) + { + throw new VerifyException(msg); + } + } + + + } + } --- Predicate.cs DELETED --- |