|
From: <sm...@us...> - 2002-12-16 14:50:17
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory sc8-pr-cvs1:/tmp/cvs-serv14529/src/NMock
Modified Files:
Mock.cs
Added Files:
MockCall.cs Method.cs
Log Message:
Refactoring.
Pulled out Method object
--- NEW FILE: MockCall.cs ---
using System;
using NMock.Constraints;
namespace NMock
{
public class MockCall
{
private IConstraint[] expectedArgs;
private object returnValue;
private Exception e;
public MockCall(object returnValue, Exception e, object[] expectedArgs)
{
this.expectedArgs = argsAsConstraints(expectedArgs);
this.returnValue = returnValue;
this.e = e;
}
public virtual bool HasExpectations
{
get { return expectedArgs.Length > 0; }
}
private IConstraint[] argsAsConstraints(object[] args)
{
if (null == args)
{
return new IConstraint[0];
}
IConstraint[] result = new IConstraint[args.Length];
for (int i = 0; i < result.Length; ++i)
{
result[i] = argAsConstraint(args[i]);
}
return result;
}
private IConstraint argAsConstraint(object arg)
{
IConstraint constraint = arg as IConstraint;
if (constraint != null)
{
return constraint;
}
return arg == null ? (IConstraint)new IsAnything() : (IConstraint)new IsEqual(arg);
}
public virtual object Call(string methodName, object[] actualArgs)
{
checkArguments(methodName, actualArgs);
if (e != null)
{
throw e;
}
return returnValue;
}
private void checkArguments(string methodName, object[] actualArgs)
{
if ( HasExpectations )
{
Mock.Assertion.AssertEquals("x() called with incorrect number of parameters",
expectedArgs.Length, actualArgs.Length);
for (int i = 0; i < expectedArgs.Length; i++)
{
checkConstraint(methodName, expectedArgs[i], actualArgs[i], i);
}
}
}
private void checkConstraint(string methodName, IConstraint expected, object actual, int index)
{
if (!expected.Eval(actual))
{
String messageFormat = "{0}() called with incorrect parameter ({1})";
String message = String.Format(messageFormat, methodName, index + 1);
throw new VerifyException(message, expected.Message, actual);
}
}
}
public class MockNoCall : MockCall
{
public MockNoCall() : base(null, null, null)
{
}
public override object Call(string methodName, object[] actualArgs)
{
throw new VerifyException(methodName + "() called", 0, 1);
}
}
}
--- NEW FILE: Method.cs ---
using System;
using System.Collections;
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;
public Method(string aName)
{
name = aName;
expectations = new CallSequence(aName);
}
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
{
get { return expectations.Count == 0; }
}
public virtual string Name
{
get { return name; }
}
public virtual void AddExpected(MockCall aCall)
{
expectations.Add(aCall);
}
public virtual object Call(params object[] parameters)
{
MockCall mockCall = expectations[timesCalled];
timesCalled++;
return mockCall.Call(name, parameters);
}
public virtual void Verify()
{
Mock.Assertion.AssertEquals(name + "() " + CallCountErrorMessage(),
expectations.Count, timesCalled);
}
private string CallCountErrorMessage()
{
return (timesCalled == 0 ? "never called" : "not called enough times");
}
/// <summary>
/// Specialised collection class for Method Calls
/// </summary>
public class CallSequence
{
private string name;
private ArrayList sequence = new ArrayList();
public CallSequence(string aName)
{
name = aName;
}
public MockCall this[int timesCalled]
{
get
{
if (sequence.Count == timesCalled)
{
throw new VerifyException(name + "() called too many times", sequence.Count, timesCalled + 1);
}
return (MockCall)sequence[timesCalled];
}
}
public int Count
{
get { return sequence.Count; }
}
public void Add(MockCall aCall)
{
sequence.Add(aCall);
}
}
}
}
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Mock.cs 12 Dec 2002 21:39:43 -0000 1.5
--- Mock.cs 16 Dec 2002 14:49:46 -0000 1.6
***************
*** 5,25 ****
namespace NMock
{
-
public class Mock : IMock
{
-
private string name;
! private object obj;
private bool strict;
! private IDictionary expectations;
! private IDictionary values;
! private IDictionary counters;
public Mock(string name)
{
this.name = name;
! expectations = new Hashtable();
! values = new Hashtable();
! counters = new Hashtable();
}
--- 5,19 ----
namespace NMock
{
public class Mock : IMock
{
private string name;
! private object instance;
private bool strict;
! private IDictionary methods;
public Mock(string name)
{
this.name = name;
! methods = new Hashtable();
}
***************
*** 32,37 ****
public virtual object MockInstance
{
! get { return obj; }
! set { obj = value; }
}
--- 26,31 ----
public virtual object MockInstance
{
! get { return instance; }
! set { instance = value; }
}
***************
*** 42,56 ****
}
- public virtual void Verify()
- {
- foreach (String methodName in expectations.Keys)
- {
- IList list = (IList) expectations[methodName];
- int count = counter(methodName);
- string msg = count == 0 ? "never called" : "not called enough times";
- Assertion.AssertEquals(methodName + "() " + msg, list.Count, count);
- }
- }
-
public virtual void Expect(string methodName, params object[] args)
{
--- 36,39 ----
***************
*** 60,210 ****
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(new MockNoCall(methodName));
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(new MockCall(methodName, result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! addExpectation(new MockCall(methodName, null, e, args));
}
public virtual void SetupResult(string methodName, object returnVal)
{
! values[methodName] = returnVal;
}
public virtual object Call(string methodName, params object[] args)
{
! if (values.Contains(methodName))
{
! return values[methodName];
}
! IList methodExpectations = (IList) expectations[methodName];
! if (methodExpectations == null)
! {
if ( strict )
{
! throw new VerifyException(methodName + "() called too many times", 0, counter(methodName) + 1);
}
return null;
}
! if (methodExpectations.Count == counter(methodName))
! {
! throw new VerifyException(methodName + "() called too many times", methodExpectations.Count, counter(methodName) + 1);
! }
! MockCall mockCall = (MockCall)methodExpectations[counter(methodName)];
! if (mockCall is MockNoCall)
! {
! throw new VerifyException(methodName + "() called", 0, 1);
! }
! increment(methodName);
! return mockCall.Call(methodName, args);
}
! private void increment(string methodName)
! {
! counters[methodName] = counter(methodName) + 1;
! }
!
! private int counter(string methodName)
! {
! return counters.Contains(methodName) ? (int)counters[methodName] : 0;
! }
!
! private void addExpectation(MockCall call)
{
! IList list = (IList) expectations[call.MethodName];
! if (list == null)
{
! list = new ArrayList();
! expectations[call.MethodName] = list;
}
- list.Add(call);
}
! private class MockCall
{
! public string MethodName;
! private IConstraint[] expectedArgs;
! private object returnValue;
! private Exception e;
!
! public MockCall(string methodName, object returnValue, Exception e, object[] expectedArgs)
! {
! this.MethodName = methodName;
! this.returnValue = returnValue;
! this.e = e;
! this.expectedArgs = argsAsConstraints(expectedArgs);
! }
!
! private IConstraint[] argsAsConstraints(object[] args)
! {
! if (args == null)
! {
! return new IConstraint[0];
! }
! IConstraint[] result = new IConstraint[args.Length];
! for (int i = 0; i < args.Length; ++i)
! {
! result[i] = argAsConstraint(args[i]);
! }
! return result;
! }
!
! private IConstraint argAsConstraint(object arg)
! {
! IConstraint constraint = arg as IConstraint;
! if (constraint != null)
! {
! return constraint;
! }
! return arg == null ? (IConstraint)new IsAnything() : (IConstraint)new IsEqual(arg);
! }
!
! public virtual object Call(string methodName, object[] actualArgs)
! {
! if ( this.expectedArgs.Length > 0 )
! {
! // assert that the correct number of arguments were passed
! Assertion.AssertEquals("x() called with incorrect number of parameters", this.expectedArgs.Length, actualArgs.Length);
!
! // assert that each passed in arg is validated by the appropriate predicate.
! for (int i = 0; i < this.expectedArgs.Length; i++)
! {
! IConstraint expectedArg = expectedArgs[i];
! object actualArg = actualArgs[i];
!
! if (!expectedArg.Eval(actualArg))
! {
! String messageFormat = "{0}() called with incorrect parameter ({1})";
! String message = String.Format(messageFormat, methodName, i + 1);
! throw new VerifyException(message, expectedArg.Message, actualArg);
! }
! }
! }
!
! // if exception setup to be thrown, throw it
! if (e != null)
! {
! throw e;
! }
! // otherwise return desired value
! return returnValue;
! }
}
!
! private class MockNoCall : MockCall
{
! public MockNoCall(string methodName) : base(methodName, null, null, null)
! {
}
}
! private class Assertion
{
public static void Assert(string message, bool expression)
--- 43,108 ----
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(methodName, new MockNoCall());
}
public virtual void ExpectAndReturn(string methodName, object result, params object[] args)
{
! addExpectation(methodName, new MockCall(result, null, args));
}
public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args)
{
! 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;
}
! return method.Call(args);
}
! public virtual void Verify()
{
! foreach (Method method in methods.Values)
{
! method.Verify();
}
}
! private void addExpectation(string methodName, MockCall call)
{
! getMethod(methodName).AddExpected(call);
}
!
! private Method getMethod(string methodName)
{
! Method found = (Method)methods[methodName];
! if (found == null)
! {
! found = new Method(methodName);
! methods[methodName] = found;
}
+ return found;
}
!
! public class Assertion
{
public static void Assert(string message, bool expression)
***************
*** 223,232 ****
}
}
-
}
-
-
-
}
-
}
--- 121,125 ----
|