|
From: Joe W. <joe...@us...> - 2002-10-14 19:14:20
|
Update of /cvsroot/mockobjects/nmock/src/NMock
In directory usw-pr-cvs1:/tmp/cvs-serv19252a/src/NMock
Modified Files:
Mock.cs
Log Message:
Expectations now setup on per method basis rather than per mock
Index: Mock.cs
===================================================================
RCS file: /cvsroot/mockobjects/nmock/src/NMock/Mock.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Mock.cs 14 Oct 2002 18:43:29 -0000 1.3
+++ Mock.cs 14 Oct 2002 19:14:13 -0000 1.4
@@ -7,18 +7,16 @@
public class Mock : IMock
{
- private int currentExpectation;
private string name;
private object obj;
- private IList expectations;
+ private IDictionary expectations;
private IDictionary values;
public Mock(string name)
{
this.name = name;
- expectations = new ArrayList();
+ expectations = new Hashtable();
values = new Hashtable();
- currentExpectation = 0;
}
public virtual string Name
@@ -35,7 +33,10 @@
public virtual void Verify()
{
- Assertion.AssertEquals(currentExpectation, expectations.Count);
+ foreach (IList list in expectations.Values)
+ {
+ Assertion.AssertEquals(0, list.Count);
+ }
}
public virtual void Expect(string methodName, params object[] args)
@@ -45,12 +46,12 @@
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
- expectations.Add(new Expectation(methodName, result, null, args));
+ addExpectation(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));
+ addExpectation(new Expectation(methodName, null, e, args));
}
public virtual void SetValue(string methodName, object returnVal)
@@ -64,25 +65,43 @@
{
return values[methodName];
}
- if (currentExpectation == expectations.Count)
+
+ IList list = (IList) expectations[methodName];
+ if (list == null)
+ {
+ return null;
+ }
+ if (list.Count == 0)
{
Assertion.Fail(methodName + "() called too many times");
}
- Expectation exp = (Expectation)expectations[currentExpectation];
- currentExpectation++;
- return exp.Verify(methodName, args);
+ 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
{
- private string methodName;
+ public string MethodName;
private object[] args;
private object returnValue;
private Exception e;
public Expectation(string methodName, object returnValue, Exception e, object[] args)
{
- this.methodName = methodName;
+ this.MethodName = methodName;
if (args == null)
{
this.args = new object[0];
@@ -98,7 +117,7 @@
public object Verify(string methodName, object[] args)
{
- Assertion.AssertEquals(this.methodName, methodName);
+ Assertion.AssertEquals(this.MethodName, methodName);
// assert that each passed in arg is validated by the appropriate predicate.
for (int i = 0; i < this.args.Length; i++)
|