|
From: <joe...@us...> - 2002-12-12 21:42:44
|
Update of /cvsroot/nmock/nmock/src/NMock/Constraints
In directory sc8-pr-cvs1:/tmp/cvs-serv19262/src/NMock/Constraints
Modified Files:
Constraints.cs
Log Message:
*** keyword substitution change ***
Index: Constraints.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Constraints/Constraints.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Constraints.cs 15 Nov 2002 22:48:20 -0000 1.1
--- Constraints.cs 12 Dec 2002 21:42:35 -0000 1.2
***************
*** 1,383 ****
! using System;
! using System.Text;
! using System.Text.RegularExpressions;
!
! namespace NMock.Constraints
! {
!
! public class IsNull : IConstraint
! {
! public bool Eval(object val)
! {
! return val == null;
! }
!
! public string Message
! {
! get { return "null"; }
! }
! }
!
! public class IsAnything : IConstraint
! {
! public bool Eval(object val)
! {
! return true;
! }
!
! public string Message
! {
! get { return ""; }
! }
! }
!
! public class IsIn : IConstraint
! {
! 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 string Message
! {
! get
! {
! StringBuilder builder = new StringBuilder("IN ");
! foreach (object o in inList)
! {
! builder.Append('<');
! builder.Append(o);
! builder.Append(">, ");
! }
! string result = builder.ToString();
! if (result.EndsWith(", "))
! {
! result = result.Substring(0, result.Length - 2);
! }
! return result;
! }
! }
! }
!
! public class IsEqual : IConstraint {
! private object compare;
!
! public IsEqual(object compare)
! {
! this.compare = compare;
! }
!
! public bool Eval(object val)
! {
! return compare.Equals(val);
! }
!
! public string Message
! {
! get { return "<" + compare + ">"; }
! }
! }
!
! public class IsTypeOf : IConstraint
! {
! private Type type;
!
! public IsTypeOf(Type type)
! {
! this.type = type;
! }
!
! public bool Eval(object val)
! {
! return val == null ? false : type.IsAssignableFrom(val.GetType());
! }
!
! public string Message
! {
! get { return "typeof <" + type.FullName + ">"; }
! }
! }
!
! public class Not : IConstraint
! {
! private IConstraint p;
!
! public Not(IConstraint p)
! {
! this.p = p;
! }
!
! public bool Eval(object val)
! {
! return !p.Eval(val);
! }
!
! public string Message
! {
! get { return "NOT " + p.Message; }
! }
! }
!
! public class And : IConstraint
! {
! private IConstraint p1, p2;
!
! public And(IConstraint p1, IConstraint p2)
! {
! this.p1 = p1;
! this.p2 = p2;
! }
!
! public bool Eval(object val)
! {
! return p1.Eval(val) && p2.Eval(val);
! }
!
! public string Message
! {
! get { return p1.Message + " AND " + p2.Message; }
! }
! }
!
! public class Or : IConstraint
! {
! private IConstraint p1, p2;
!
! public Or(IConstraint p1, IConstraint p2)
! {
! this.p1 = p1;
! this.p2 = p2;
! }
!
! public bool Eval(object val)
! {
! return p1.Eval(val) || p2.Eval(val);
! }
!
! public string Message
! {
! get { return p1.Message + " OR " + p2.Message; }
! }
! }
!
! public class NotNull : IConstraint
! {
! public bool Eval(object val)
! {
! return val != null;
! }
!
! public string Message
! {
! get { return "NOT null"; }
! }
! }
!
! public class NotEqual : IConstraint
! {
! private IConstraint p;
!
! public NotEqual(object compare)
! {
! p = new Not(new IsEqual(compare));
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val);
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class NotIn : IConstraint
! {
! private IConstraint p;
!
! public NotIn(params object[] inList)
! {
! p = new Not(new IsIn(inList));
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val);
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsEqualIgnoreCase : IConstraint
! {
! private IConstraint p;
!
! public IsEqualIgnoreCase(object compare)
! {
! p = new IsEqual(compare.ToString().ToLower());
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val.ToString().ToLower());
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsEqualIgnoreWhiteSpace : IConstraint
! {
! private IConstraint 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 string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsMatch : IConstraint
! {
! 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 string Message
! {
! get { return "<" + regex.ToString() + ">"; }
! }
! }
!
! public class IsCloseTo : IConstraint
! {
!
! 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 string Message
! {
! get { return "<" + expected + ">"; }
! }
! }
!
! public class Constraint : IConstraint
! {
! public delegate bool Method(object val);
! private Method m;
!
! public Constraint(Method m)
! {
! this.m = m;
! }
!
! public bool Eval(object val)
! {
! return m(val);
! }
!
! public string Message
! {
! get { return "Custom Constraint"; }
! }
! }
!
! }
--- 1,383 ----
! using System;
! using System.Text;
! using System.Text.RegularExpressions;
!
! namespace NMock.Constraints
! {
!
! public class IsNull : IConstraint
! {
! public bool Eval(object val)
! {
! return val == null;
! }
!
! public string Message
! {
! get { return "null"; }
! }
! }
!
! public class IsAnything : IConstraint
! {
! public bool Eval(object val)
! {
! return true;
! }
!
! public string Message
! {
! get { return ""; }
! }
! }
!
! public class IsIn : IConstraint
! {
! 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 string Message
! {
! get
! {
! StringBuilder builder = new StringBuilder("IN ");
! foreach (object o in inList)
! {
! builder.Append('<');
! builder.Append(o);
! builder.Append(">, ");
! }
! string result = builder.ToString();
! if (result.EndsWith(", "))
! {
! result = result.Substring(0, result.Length - 2);
! }
! return result;
! }
! }
! }
!
! public class IsEqual : IConstraint {
! private object compare;
!
! public IsEqual(object compare)
! {
! this.compare = compare;
! }
!
! public bool Eval(object val)
! {
! return compare.Equals(val);
! }
!
! public string Message
! {
! get { return "<" + compare + ">"; }
! }
! }
!
! public class IsTypeOf : IConstraint
! {
! private Type type;
!
! public IsTypeOf(Type type)
! {
! this.type = type;
! }
!
! public bool Eval(object val)
! {
! return val == null ? false : type.IsAssignableFrom(val.GetType());
! }
!
! public string Message
! {
! get { return "typeof <" + type.FullName + ">"; }
! }
! }
!
! public class Not : IConstraint
! {
! private IConstraint p;
!
! public Not(IConstraint p)
! {
! this.p = p;
! }
!
! public bool Eval(object val)
! {
! return !p.Eval(val);
! }
!
! public string Message
! {
! get { return "NOT " + p.Message; }
! }
! }
!
! public class And : IConstraint
! {
! private IConstraint p1, p2;
!
! public And(IConstraint p1, IConstraint p2)
! {
! this.p1 = p1;
! this.p2 = p2;
! }
!
! public bool Eval(object val)
! {
! return p1.Eval(val) && p2.Eval(val);
! }
!
! public string Message
! {
! get { return p1.Message + " AND " + p2.Message; }
! }
! }
!
! public class Or : IConstraint
! {
! private IConstraint p1, p2;
!
! public Or(IConstraint p1, IConstraint p2)
! {
! this.p1 = p1;
! this.p2 = p2;
! }
!
! public bool Eval(object val)
! {
! return p1.Eval(val) || p2.Eval(val);
! }
!
! public string Message
! {
! get { return p1.Message + " OR " + p2.Message; }
! }
! }
!
! public class NotNull : IConstraint
! {
! public bool Eval(object val)
! {
! return val != null;
! }
!
! public string Message
! {
! get { return "NOT null"; }
! }
! }
!
! public class NotEqual : IConstraint
! {
! private IConstraint p;
!
! public NotEqual(object compare)
! {
! p = new Not(new IsEqual(compare));
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val);
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class NotIn : IConstraint
! {
! private IConstraint p;
!
! public NotIn(params object[] inList)
! {
! p = new Not(new IsIn(inList));
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val);
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsEqualIgnoreCase : IConstraint
! {
! private IConstraint p;
!
! public IsEqualIgnoreCase(object compare)
! {
! p = new IsEqual(compare.ToString().ToLower());
! }
!
! public bool Eval(object val)
! {
! return p.Eval(val.ToString().ToLower());
! }
!
! public string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsEqualIgnoreWhiteSpace : IConstraint
! {
! private IConstraint 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 string Message
! {
! get { return p.Message; }
! }
! }
!
! public class IsMatch : IConstraint
! {
! 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 string Message
! {
! get { return "<" + regex.ToString() + ">"; }
! }
! }
!
! public class IsCloseTo : IConstraint
! {
!
! 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 string Message
! {
! get { return "<" + expected + ">"; }
! }
! }
!
! public class Constraint : IConstraint
! {
! public delegate bool Method(object val);
! private Method m;
!
! public Constraint(Method m)
! {
! this.m = m;
! }
!
! public bool Eval(object val)
! {
! return m(val);
! }
!
! public string Message
! {
! get { return "Custom Constraint"; }
! }
! }
!
! }
|