|
From: <joe...@us...> - 2002-11-10 18:21:54
|
Update of /cvsroot/nmock/nmock/src/NMock
In directory usw-pr-cvs1:/tmp/cvs-serv24420/src/NMock
Modified Files:
Mock.cs
Added Files:
Conditions.cs ICondition.cs
Removed Files:
IPredicate.cs Predicates.cs
Log Message:
Renamed Predicate to Condition
--- NEW FILE: Conditions.cs ---
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace NMock
{
public class IsNull : ICondition
{
public bool eval(object val)
{
return val == null;
}
}
public class IsAnything : ICondition
{
public bool eval(object val)
{
return true;
}
}
public class IsIn : ICondition
{
private object[] inList;
public IsIn(params object[] inList)
{
if (inList.Length == 1 && inList[0].GetType().IsArray)
{
Array arr = (Array)inList[0];
this.inList = new object[arr.Length];
arr.CopyTo(this.inList, 0);
}
else
{
this.inList = inList;
}
}
public bool eval(object val)
{
foreach (object o in inList)
{
if (o.Equals(val))
{
return true;
}
}
return false;
}
}
public class IsEqual : ICondition {
private object compare;
public IsEqual(object compare)
{
this.compare = compare;
}
public bool eval(object val)
{
return compare.Equals(val);
}
}
public class IsTypeOf : ICondition
{
private Type type;
public IsTypeOf(Type type)
{
this.type = type;
}
public bool eval(object val)
{
return val == null ? false : type.IsAssignableFrom(val.GetType());
}
}
public class Not : ICondition
{
private ICondition p;
public Not(ICondition p)
{
this.p = p;
}
public bool eval(object val)
{
return !p.eval(val);
}
}
public class And : ICondition
{
private ICondition p1, p2;
public And(ICondition p1, ICondition p2)
{
this.p1 = p1;
this.p2 = p2;
}
public bool eval(object val)
{
return p1.eval(val) && p2.eval(val);
}
}
public class Or : ICondition
{
private ICondition p1, p2;
public Or(ICondition p1, ICondition p2)
{
this.p1 = p1;
this.p2 = p2;
}
public bool eval(object val)
{
return p1.eval(val) || p2.eval(val);
}
}
public class NotNull : ICondition
{
public bool eval(object val)
{
return val != null;
}
}
public class NotEqual : ICondition
{
private ICondition p;
public NotEqual(object compare)
{
p = new Not(new IsEqual(compare));
}
public bool eval(object val)
{
return p.eval(val);
}
}
public class NotIn : ICondition
{
private ICondition p;
public NotIn(params object[] inList)
{
p = new Not(new IsIn(inList));
}
public bool eval(object val)
{
return p.eval(val);
}
}
public class IsEqualIgnoreCase : ICondition
{
private ICondition p;
public IsEqualIgnoreCase(object compare)
{
p = new IsEqual(compare.ToString().ToLower());
}
public bool eval(object val)
{
return p.eval(val.ToString().ToLower());
}
}
public class IsEqualIgnoreWhiteSpace : ICondition
{
private ICondition p;
public IsEqualIgnoreWhiteSpace(object compare)
{
p = new IsEqual(StripSpace(compare.ToString()));
}
public bool eval(object val)
{
return p.eval(StripSpace(val.ToString()));
}
public static string StripSpace(string s)
{
StringBuilder result = new StringBuilder();
bool lastWasSpace = true;
foreach(char c in s)
{
if (Char.IsWhiteSpace(c))
{
if (!lastWasSpace)
{
result.Append(' ');
}
lastWasSpace = true;
}
else
{
result.Append(c);
lastWasSpace = false;
}
}
return result.ToString().Trim();
}
}
public class IsMatch : ICondition
{
private Regex regex;
public IsMatch(Regex regex)
{
this.regex = regex;
}
public IsMatch(String regex) : this(new Regex(regex))
{
}
public IsMatch(String regex, bool ignoreCase) :
this(new Regex(regex, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None))
{
}
public bool eval(object val)
{
return val == null ? false : regex.IsMatch(val.ToString());
}
}
public class IsCloseTo : ICondition
{
private double expected;
private double error;
public IsCloseTo(double expected, double error)
{
this.expected = expected;
this.error = error;
}
public bool eval(object val)
{
try
{
double actual = Convert.ToDouble(val);
return Math.Abs(actual - expected) <= error;
}
catch (FormatException)
{
return false;
}
}
}
public class Condition : ICondition
{
public delegate bool Method(object val);
private Method m;
public Condition(Method m)
{
this.m = m;
}
public bool eval(object val)
{
return m(val);
}
}
}
--- NEW FILE: ICondition.cs ---
namespace NMock
{
public interface ICondition
{
bool eval(object val);
}
}
Index: Mock.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Mock.cs,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** Mock.cs 10 Nov 2002 16:32:27 -0000 1.1.1.1
--- Mock.cs 10 Nov 2002 18:21:50 -0000 1.2
***************
*** 135,160 ****
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++)
{
object expectedArg = this.args[i];
object actualArg = args[i];
! IPredicate predicate = expectedArg as IPredicate;
! // if expectedArg is not an IPredicate, use default
// behavior of IsEqual or IsAnything
! if (predicate == null)
{
if (expectedArg == null)
{
! predicate = new IsAnything();
}
else
{
! predicate = new IsEqual(expectedArg);
}
}
! Assertion.Assert(predicate.eval(actualArg));
}
--- 135,160 ----
Assertion.AssertEquals(this.MethodName, methodName);
! // assert that each passed in arg is validated by the appropriate condition.
for (int i = 0; i < this.args.Length; i++)
{
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);
}
}
! Assertion.Assert(condition.eval(actualArg));
}
--- IPredicate.cs DELETED ---
--- Predicates.cs DELETED ---
|