|
From: <joe...@us...> - 2002-11-15 22:31:14
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory usw-pr-cvs1:/tmp/cvs-serv30242/src/NMock
Modified Files:
DynamicMock.cs Mock.cs IMock.cs VerifyException.cs
Removed Files:
Conditions.cs ICondition.cs
Log Message:
- Added sensible error messages
- Renamed IMock.Object to IMock.MockInstance
- Renamed Conditions to Constraints and moved to new namespace.
- Renamed eval() to Eval() (.NET conventions).
- Renamed IMock.SetValue() to IMock.SetupResult (as in Java MockObjects).
Index: DynamicMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/DynamicMock.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** DynamicMock.cs 11 Nov 2002 20:00:19 -0000 1.2
--- DynamicMock.cs 15 Nov 2002 22:31:09 -0000 1.3
***************
*** 28,32 ****
}
! public override object Object
{
get
--- 28,32 ----
}
! public override object MockInstance
{
get
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Mock.cs 11 Nov 2002 20:00:20 -0000 1.3
--- Mock.cs 15 Nov 2002 22:31:09 -0000 1.4
***************
*** 1,4 ****
--- 1,5 ----
using System;
using System.Collections;
+ using NMock.Constraints;
namespace NMock
***************
*** 13,16 ****
--- 14,18 ----
private IDictionary expectations;
private IDictionary values;
+ private IDictionary counters;
public Mock(string name)
***************
*** 19,22 ****
--- 21,25 ----
expectations = new Hashtable();
values = new Hashtable();
+ counters = new Hashtable();
}
***************
*** 27,31 ****
}
! public virtual object Object
{
get { return obj; }
--- 30,34 ----
}
! public virtual object MockInstance
{
get { return obj; }
***************
*** 44,48 ****
{
IList list = (IList) expectations[methodName];
! Assertion.AssertEquals(methodName + "() was not called enough times", 0, list.Count);
}
}
--- 47,53 ----
{
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);
}
}
***************
*** 55,59 ****
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(new Expectation(methodName, null, new VerifyException(methodName + "() should not be called"), null));
}
--- 60,64 ----
public virtual void ExpectNoCall(string methodName)
{
! addExpectation(new NoCallExpectation(methodName));
}
***************
*** 68,72 ****
}
! public virtual void SetValue(string methodName, object returnVal)
{
values[methodName] = returnVal;
--- 73,77 ----
}
! public virtual void SetupResult(string methodName, object returnVal)
{
values[methodName] = returnVal;
***************
*** 85,101 ****
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)
{
--- 90,120 ----
if ( strict )
{
! throw new VerifyException(methodName + "() called too many times", 0, counter(methodName) + 1);
}
return null;
}
! if (list.Count == counter(methodName))
{
! throw new VerifyException(methodName + "() called too many times", list.Count, counter(methodName) + 1);
}
! Expectation e = (Expectation)list[counter(methodName)];
! if (e is NoCallExpectation)
! {
! throw new VerifyException(methodName + "() called", 0, 1);
! }
! increment(methodName);
return e.Verify(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(Expectation e)
{
***************
*** 134,139 ****
public virtual object Verify(string methodName, object[] args)
{
- Assertion.AssertEquals(this.MethodName, methodName);
-
if ( this.args.Length > 0 )
{
--- 153,156 ----
***************
*** 146,168 ****
object expectedArg = this.args[i];
object actualArg = args[i];
! ICondition condition = expectedArg as ICondition;
! // if expectedArg is not an ICondition, use default
// behavior of IsEqual or IsAnything
! if (condition == null)
{
if (expectedArg == null)
{
! condition = new IsAnything();
}
else
{
! condition = new IsEqual(expectedArg);
}
}
! String messageFormat = "{0}() called with incorrect parameter ({1}) Expected:<{2}> Was:<{3}>";
! //String message = String.Format(messageFormat, methodName, i, condition.Description, actualArg);
! String message = String.Format(messageFormat, methodName, i, "fixme", actualArg);
! Assertion.Assert(message, condition.eval(actualArg));
}
}
--- 163,187 ----
object expectedArg = this.args[i];
object actualArg = args[i];
! IConstraint Constraint = expectedArg as IConstraint;
! // if expectedArg is not an IConstraint, use default
// behavior of IsEqual or IsAnything
! if (Constraint == null)
{
if (expectedArg == null)
{
! Constraint = new IsAnything();
}
else
{
! Constraint = new IsEqual(expectedArg);
}
}
! String messageFormat = "{0}() called with incorrect parameter ({1})";
! String message = String.Format(messageFormat, methodName, i + 1);
! if (!Constraint.Eval(actualArg))
! {
! throw new VerifyException(message, Constraint.Message, actualArg);
! }
}
}
***************
*** 177,180 ****
--- 196,206 ----
}
}
+
+ private class NoCallExpectation : Expectation
+ {
+ public NoCallExpectation(string methodName) : base(methodName, null, null, null)
+ {
+ }
+ }
private class Assertion
***************
*** 184,209 ****
if (!expression)
{
! Fail(message);
}
}
! public static void AssertEquals(object a, object b)
! {
! AssertEquals("Verification failed", a, b);
! }
!
! public static void AssertEquals(string message, object a, object b)
{
! if (! a.Equals(b))
{
! String messageFormat = "{0} Expected:<{1}> Was:<{2}>";
! Fail(String.Format(messageFormat, message, a, b));
}
}
- public static void Fail(string msg)
- {
- throw new VerifyException(msg);
- }
}
--- 210,225 ----
if (!expression)
{
! throw new VerifyException(message, null, null);
}
}
! public static void AssertEquals(string message, object expected, object actual)
{
! if (!expected.Equals(actual))
{
! throw new VerifyException(message, expected, actual);
}
}
}
Index: IMock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/IMock.cs,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** IMock.cs 10 Nov 2002 16:32:27 -0000 1.1.1.1
--- IMock.cs 15 Nov 2002 22:31:09 -0000 1.2
***************
*** 18,22 ****
/// Get mocked version of object.
/// </summary>
! object Object { get; }
/// <summary>
--- 18,22 ----
/// Get mocked version of object.
/// </summary>
! object MockInstance { get; }
/// <summary>
***************
*** 53,57 ****
/// each time. Useful for getter style methods.
/// </summary>
! void SetValue(string methodName, object returnVal);
/// <summary>
--- 53,57 ----
/// each time. Useful for getter style methods.
/// </summary>
! void SetupResult(string methodName, object returnVal);
/// <summary>
Index: VerifyException.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/VerifyException.cs,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** VerifyException.cs 10 Nov 2002 16:32:27 -0000 1.1.1.1
--- VerifyException.cs 15 Nov 2002 22:31:09 -0000 1.2
***************
*** 5,10 ****
public class VerifyException : Exception
{
! public VerifyException(string msg) : base(msg)
{
}
}
--- 5,43 ----
public class VerifyException : Exception
{
!
! private string reason;
! private object actual;
! private object expected;
!
! private const string format = "{0}\nexpected:{1}\n but was:<{2}>";
!
! public VerifyException(string reason, object expected, object actual) : base(reason)
{
+ this.reason = reason;
+ this.expected = expected;
+ this.actual = actual;
+ }
+
+ public string Reason
+ {
+ get { return reason; }
+ }
+
+ public object Expected
+ {
+ get { return expected; }
+ }
+
+ public object Actual
+ {
+ get { return actual; }
+ }
+
+ public override string Message
+ {
+ get
+ {
+ return String.Format(format, reason, expected, actual);
+ }
}
}
--- Conditions.cs DELETED ---
--- ICondition.cs DELETED ---
|