From: <sm...@us...> - 2003-02-10 13:26:21
|
Update of /cvsroot/nmock/nmock/src/NMock/Constraints In directory sc8-pr-cvs1:/tmp/cvs-serv32401/src/NMock/Constraints Modified Files: Constraints.cs Log Message: Added public object ExtractActualValue(object actual) to IConstraint interface Index: Constraints.cs =================================================================== RCS file: /cvsroot/nmock/nmock/src/NMock/Constraints/Constraints.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Constraints.cs 21 Jan 2003 18:16:13 -0000 1.5 --- Constraints.cs 10 Feb 2003 13:26:17 -0000 1.6 *************** *** 7,18 **** { ! public class IsNull : IConstraint { ! public bool Eval(object val) { ! return val == null; } ! public string Message { get { return "null"; } --- 7,29 ---- { ! public abstract class BaseConstraint : IConstraint ! { ! public virtual object ExtractActualValue(object actual) ! { ! return actual; ! } ! ! public abstract bool Eval(object val); ! public abstract string Message { get; } ! } ! ! public class IsNull : BaseConstraint { ! public override bool Eval(object val) { ! return ExtractActualValue(val) == null; } ! public override string Message { get { return "null"; } *************** *** 20,31 **** } ! public class IsAnything : IConstraint { ! public bool Eval(object val) { return true; } ! public string Message { get { return ""; } --- 31,42 ---- } ! public class IsAnything : BaseConstraint { ! public override bool Eval(object val) { return true; } ! public override string Message { get { return ""; } *************** *** 33,37 **** } ! public class IsIn : IConstraint { private object[] inList; --- 44,48 ---- } ! public class IsIn : BaseConstraint { private object[] inList; *************** *** 51,59 **** } ! public bool Eval(object val) { foreach (object o in inList) { ! if (o.Equals(val)) { return true; --- 62,70 ---- } ! public override bool Eval(object val) { foreach (object o in inList) { ! if (o.Equals(ExtractActualValue(val))) { return true; *************** *** 63,67 **** } ! public string Message { get --- 74,78 ---- } ! public override string Message { get *************** *** 84,88 **** } ! public class IsEqual : IConstraint { private object compare; --- 95,99 ---- } ! public class IsEqual : BaseConstraint { private object compare; *************** *** 92,101 **** } ! public bool Eval(object val) { ! return compare.Equals(val); } ! public string Message { get { return "<" + compare + ">"; } --- 103,112 ---- } ! public override bool Eval(object val) { ! return compare.Equals(ExtractActualValue(val)); } ! public override string Message { get { return "<" + compare + ">"; } *************** *** 103,107 **** } ! public class IsTypeOf : IConstraint { private Type type; --- 114,118 ---- } ! public class IsTypeOf : BaseConstraint { private Type type; *************** *** 112,121 **** } ! public bool Eval(object val) { ! return val == null ? false : type.IsAssignableFrom(val.GetType()); } ! public string Message { get { return "typeof <" + type.FullName + ">"; } --- 123,133 ---- } ! public override bool Eval(object val) { ! object actualValue = ExtractActualValue(val); ! return actualValue == null ? false : type.IsAssignableFrom(actualValue.GetType()); } ! public override string Message { get { return "typeof <" + type.FullName + ">"; } *************** *** 123,127 **** } ! public class Not : IConstraint { private IConstraint p; --- 135,139 ---- } ! public class Not : BaseConstraint { private IConstraint p; *************** *** 132,141 **** } ! public bool Eval(object val) { ! return !p.Eval(val); } ! public string Message { get { return "NOT " + p.Message; } --- 144,153 ---- } ! public override bool Eval(object val) { ! return !p.Eval(ExtractActualValue(val)); } ! public override string Message { get { return "NOT " + p.Message; } *************** *** 143,147 **** } ! public class And : IConstraint { private IConstraint p1, p2; --- 155,159 ---- } ! public class And : BaseConstraint { private IConstraint p1, p2; *************** *** 153,162 **** } ! public bool Eval(object val) { ! return p1.Eval(val) && p2.Eval(val); } ! public string Message { get { return p1.Message + " AND " + p2.Message; } --- 165,175 ---- } ! public override bool Eval(object val) { ! object actualValue = ExtractActualValue(val); ! return p1.Eval(actualValue) && p2.Eval(actualValue); } ! public override string Message { get { return p1.Message + " AND " + p2.Message; } *************** *** 164,168 **** } ! public class Or : IConstraint { private IConstraint p1, p2; --- 177,181 ---- } ! public class Or : BaseConstraint { private IConstraint p1, p2; *************** *** 174,183 **** } ! public bool Eval(object val) { ! return p1.Eval(val) || p2.Eval(val); } ! public string Message { get { return p1.Message + " OR " + p2.Message; } --- 187,197 ---- } ! public override bool Eval(object val) { ! object actualValue = ExtractActualValue(val); ! return p1.Eval(actualValue) || p2.Eval(actualValue); } ! public override string Message { get { return p1.Message + " OR " + p2.Message; } *************** *** 185,196 **** } ! public class NotNull : IConstraint { ! public bool Eval(object val) { ! return val != null; } ! public string Message { get { return "NOT null"; } --- 199,210 ---- } ! public class NotNull : BaseConstraint { ! public override bool Eval(object val) { ! return ExtractActualValue(val) != null; } ! public override string Message { get { return "NOT null"; } *************** *** 198,202 **** } ! public class NotEqual : IConstraint { private IConstraint p; --- 212,216 ---- } ! public class NotEqual : BaseConstraint { private IConstraint p; *************** *** 207,216 **** } ! public bool Eval(object val) { ! return p.Eval(val); } ! public string Message { get { return p.Message; } --- 221,230 ---- } ! public override bool Eval(object val) { ! return p.Eval(ExtractActualValue(val)); } ! public override string Message { get { return p.Message; } *************** *** 218,222 **** } ! public class NotIn : IConstraint { private IConstraint p; --- 232,236 ---- } ! public class NotIn : BaseConstraint { private IConstraint p; *************** *** 227,236 **** } ! public bool Eval(object val) { ! return p.Eval(val); } ! public string Message { get { return p.Message; } --- 241,250 ---- } ! public override bool Eval(object val) { ! return p.Eval(ExtractActualValue(val)); } ! public override string Message { get { return p.Message; } *************** *** 238,242 **** } ! public class IsEqualIgnoreCase : IConstraint { private IConstraint p; --- 252,256 ---- } ! public class IsEqualIgnoreCase : BaseConstraint { private IConstraint p; *************** *** 247,256 **** } ! public bool Eval(object val) { ! return p.Eval(val.ToString().ToLower()); } ! public string Message { get { return p.Message; } --- 261,270 ---- } ! public override bool Eval(object val) { ! return p.Eval(ExtractActualValue(val).ToString().ToLower()); } ! public override string Message { get { return p.Message; } *************** *** 258,262 **** } ! public class IsEqualIgnoreWhiteSpace : IConstraint { private IConstraint p; --- 272,276 ---- } ! public class IsEqualIgnoreWhiteSpace : BaseConstraint { private IConstraint p; *************** *** 267,273 **** } ! public bool Eval(object val) { ! return p.Eval(StripSpace(val.ToString())); } --- 281,287 ---- } ! public override bool Eval(object val) { ! return p.Eval(StripSpace(ExtractActualValue(val).ToString())); } *************** *** 295,299 **** } ! public string Message { get { return p.Message; } --- 309,313 ---- } ! public override string Message { get { return p.Message; } *************** *** 301,305 **** } ! public class IsMatch : IConstraint { private Regex regex; --- 315,319 ---- } ! public class IsMatch : BaseConstraint { private Regex regex; *************** *** 319,328 **** } ! public bool Eval(object val) { ! return val == null ? false : regex.IsMatch(val.ToString()); } ! public string Message { get { return "<" + regex.ToString() + ">"; } --- 333,343 ---- } ! public override bool Eval(object val) { ! object actualValue = ExtractActualValue(val); ! return actualValue == null ? false : regex.IsMatch(actualValue.ToString()); } ! public override string Message { get { return "<" + regex.ToString() + ">"; } *************** *** 330,334 **** } ! public class IsCloseTo : IConstraint { --- 345,349 ---- } ! public class IsCloseTo : BaseConstraint { *************** *** 342,350 **** } ! public bool Eval(object val) { try { ! double actual = Convert.ToDouble(val); return Math.Abs(actual - expected) <= error; } --- 357,365 ---- } ! public override bool Eval(object val) { try { ! double actual = Convert.ToDouble(ExtractActualValue(val)); return Math.Abs(actual - expected) <= error; } *************** *** 355,359 **** } ! public string Message { get { return "<" + expected + ">"; } --- 370,374 ---- } ! public override string Message { get { return "<" + expected + ">"; } *************** *** 368,372 **** /// "Property.SubProperty" notation. /// </summary> ! public class PropertyIs : IConstraint { private string property; --- 383,387 ---- /// "Property.SubProperty" notation. /// </summary> ! public class PropertyIs : BaseConstraint { private string property; *************** *** 383,394 **** } ! public bool Eval(object val) { ! if (val == null) { return false; } // split "a.b.c" into "a", "b", "c" ! object propertyValue = val; foreach(string propertyBit in property.Split(new char[] {'.'})) { --- 398,410 ---- } ! public override bool Eval(object val) { ! object actualValue = ExtractActualValue(val); ! if (actualValue == null) { return false; } // split "a.b.c" into "a", "b", "c" ! object propertyValue = actualValue; foreach(string propertyBit in property.Split(new char[] {'.'})) { *************** *** 405,409 **** } ! public string Message { get { return String.Format("Property {0}: {1}", property, constraint.Message); } --- 421,425 ---- } ! public override string Message { get { return String.Format("Property {0}: {1}", property, constraint.Message); } *************** *** 411,415 **** } ! public class Constraint : IConstraint { public delegate bool Method(object val); --- 427,431 ---- } ! public class Constraint : BaseConstraint { public delegate bool Method(object val); *************** *** 421,430 **** } ! public bool Eval(object val) { ! return m(val); } ! public string Message { get { return "Custom Constraint"; } --- 437,446 ---- } ! public override bool Eval(object val) { ! return m(ExtractActualValue(val)); } ! public override string Message { get { return "Custom Constraint"; } *************** *** 433,437 **** ! public class CollectingConstraint : IConstraint { private object parameter; --- 449,453 ---- ! public class CollectingConstraint : BaseConstraint { private object parameter; *************** *** 442,452 **** } ! public bool Eval(object val) { ! parameter = val; return true; } ! public string Message { get { return "Collecting Constraint"; } --- 458,468 ---- } ! public override bool Eval(object val) { ! parameter = ExtractActualValue(val); return true; } ! public override string Message { get { return "Collecting Constraint"; } |