|
From: <exo...@us...> - 2003-03-13 22:22:16
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv25196/src/NMock
Modified Files:
DynamicMock.cs Method.cs Mock.cs
Added Files:
IMethod.cs SingleMethod.cs
Log Message:
split Method class into Method and SingleMethod and extracted IMethod interface
--- NEW FILE: IMethod.cs ---
using System;
namespace NMock
{
public interface IMethod : IVerifiable
{
string Name { get; }
object Call(params object[] parameters);
void SetExpectation(MockCall call);
}
}
--- NEW FILE: SingleMethod.cs ---
using System;
namespace NMock
{
public class SingleMethod : IMethod
{
private string name;
private MockCall expectation;
private int timesCalled = 0;
public SingleMethod(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
public virtual void SetExpectation(MockCall expectation)
{
this.expectation = expectation;
}
public virtual object Call(params object[] parameters)
{
object obj = expectation.Call(name, parameters);
timesCalled++;
return obj;
}
public virtual void Verify()
{
Mock.Assertion.Assert(name + "() never called.", timesCalled > 1);
}
}
}
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** DynamicMock.cs 13 Mar 2003 21:54:05 -0000 1.7
--- DynamicMock.cs 13 Mar 2003 22:22:11 -0000 1.8
***************
*** 50,54 ****
}
! protected override Method getMethod(string methodName)
{
checkMethodIsValid(methodName);
--- 50,54 ----
}
! protected override IMethod getMethod(string methodName)
{
checkMethodIsValid(methodName);
Index: Method.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Method.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Method.cs 13 Mar 2003 20:11:51 -0000 1.3
--- Method.cs 13 Mar 2003 22:22:11 -0000 1.4
***************
*** 4,16 ****
namespace NMock
{
! /// <summary>
! /// nb. Shouldn't return value belong to the Call?
! /// </summary>
! public class Method : IVerifiable
{
private static object NO_RETURN_VALUE = new object();
private string name;
- private object returnValue = NO_RETURN_VALUE;
private int timesCalled = 0;
private CallSequence expectations;
--- 4,12 ----
namespace NMock
{
! public class Method : IMethod
{
private static object NO_RETURN_VALUE = new object();
private string name;
private int timesCalled = 0;
private CallSequence expectations;
***************
*** 22,36 ****
}
! public virtual object ReturnValue
{
! set { returnValue = value; }
! get { return returnValue; }
}
- public virtual bool HasReturnValue
- {
- get { return ! Object.ReferenceEquals(NO_RETURN_VALUE, returnValue); }
- }
-
public virtual bool HasNoExpectations
{
--- 18,26 ----
}
! public virtual string Name
{
! get { return name; }
}
public virtual bool HasNoExpectations
{
***************
*** 38,47 ****
}
! public virtual string Name
! {
! get { return name; }
! }
!
! public virtual void AddExpected(MockCall aCall)
{
expectations.Add(aCall);
--- 28,32 ----
}
! public virtual void SetExpectation(MockCall aCall)
{
expectations.Add(aCall);
***************
*** 57,62 ****
public virtual void Verify()
{
- // HACK!
- // TODO: iterate through and verify every call. delegate verification to call instance
Mock.Assertion.AssertEquals(name + "() " + CallCountErrorMessage(),
expectations.CountExpectedCalls, timesCalled);
--- 42,45 ----
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** Mock.cs 13 Mar 2003 21:54:04 -0000 1.7
--- Mock.cs 13 Mar 2003 22:22:11 -0000 1.8
***************
*** 55,76 ****
addExpectation(methodName, new MockCall(null, e, args));
}
public virtual void SetupResult(string methodName, object returnVal)
{
! getMethod(methodName).ReturnValue = returnVal;
}
public virtual object Call(string methodName, params object[] args)
{
! Method method = getMethod(methodName);
! if (method.HasReturnValue)
{
! return method.ReturnValue;
! }
!
! if (method.HasNoExpectations) {
! if ( strict )
{
! throw new VerifyException(method.Name + "() called too many times", 0, 1);
}
return null;
--- 55,89 ----
addExpectation(methodName, new MockCall(null, e, args));
}
+
+ private void addExpectation(string methodName, MockCall call)
+ {
+ IMethod method = getMethod(methodName);
+ if (method == null)
+ {
+ method = new Method(methodName);
+ methods[methodName] = method;
+ }
+ method.SetExpectation(call);
+ }
public virtual void SetupResult(string methodName, object returnVal)
{
! IMethod method = getMethod(methodName);
! if (method == null)
! {
! method = new SingleMethod(methodName);
! methods[methodName] = method;
! }
! method.SetExpectation(new MockCall(returnVal, null, null));
}
public virtual object Call(string methodName, params object[] args)
{
! IMethod method = getMethod(methodName);
! if (method == null)
{
! if (strict)
{
! throw new VerifyException(methodName + "() called too many times", 0, 1);
}
return null;
***************
*** 81,85 ****
public virtual void Verify()
{
! foreach (Method method in methods.Values)
{
method.Verify();
--- 94,98 ----
public virtual void Verify()
{
! foreach (IMethod method in methods.Values)
{
method.Verify();
***************
*** 87,106 ****
}
! private void addExpectation(string methodName, MockCall call)
! {
! getMethod(methodName).AddExpected(call);
! }
!
! protected virtual Method getMethod(string methodName)
{
! Method found = (Method)methods[methodName];
! if (found == null)
! {
! found = new Method(methodName);
! methods[methodName] = found;
! }
! return found;
}
-
public class Assertion
--- 100,107 ----
}
! protected virtual IMethod getMethod(string methodName)
{
! return (IMethod)methods[methodName];
}
public class Assertion
|