|
From: <joe...@us...> - 2003-01-21 17:36:04
|
Update of /cvsroot/nmock/nmock/src/NMock/Constraints
In directory sc8-pr-cvs1:/tmp/cvs-serv30252/src/NMock/Constraints
Modified Files:
Constraints.cs
Log Message:
Added PropertyIs decorator for constraints to allow a constraint to test the value of a property rather than the object itself.
Index: Constraints.cs
===================================================================
RCS file: /cvsroot/nmock/nmock/src/NMock/Constraints/Constraints.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Constraints.cs 31 Dec 2002 21:36:37 -0000 1.3
--- Constraints.cs 21 Jan 2003 17:36:00 -0000 1.4
***************
*** 2,5 ****
--- 2,6 ----
using System.Text;
using System.Text.RegularExpressions;
+ using System.Reflection;
namespace NMock.Constraints
***************
*** 357,360 ****
--- 358,398 ----
{
get { return "<" + expected + ">"; }
+ }
+ }
+
+ /// <summary>
+ /// This constraint decorates another constraint, allowing it to test
+ /// a property of the object, rather than the property itself.
+ /// </summary>
+ public class PropertyIs : IConstraint
+ {
+ private string property;
+ private IConstraint constraint;
+
+ public PropertyIs(string property, object expected)
+ {
+ this.property = property;
+ constraint = expected as IConstraint;
+ if (constraint == null)
+ {
+ constraint = new IsEqual(expected);
+ }
+ }
+
+ public bool Eval(object val)
+ {
+ if (val == null)
+ {
+ return false;
+ }
+ Type type = val.GetType();
+ PropertyInfo propertyInfo = type.GetProperty(property);
+ object propertyValue = propertyInfo.GetValue(val, null);
+ return constraint.Eval(propertyValue);
+ }
+
+ public string Message
+ {
+ get { return String.Format("Property {0}: {1}", property, constraint.Message); }
}
}
|