[Adapdev-commits] Adapdev/src/Adapdev.Tests CompositeValidatorTest.cs,NONE,1.1 IValidatorTest.cs,NON
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-06-03 04:33:08
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.Tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26938/src/Adapdev.Tests Added Files: CompositeValidatorTest.cs IValidatorTest.cs ObjectComparerTest.cs Log Message: --- NEW FILE: CompositeValidatorTest.cs --- using System; using System.Collections; using NUnit.Framework; using Adapdev; namespace Adapdev.Tests { /// <summary> /// Summary description for IValidatable. /// </summary> /// [TestFixture] public class CompositeValidatorTest { public static string BadHeightMessage = "Height must be greater than zero."; public static string BadWidthMessage = "Width must be greater than zero."; public static string NoSquareMessage = "Must be a rectangle, not a square. Please use the Square object for squares."; [Test] public void Invalid() { Rectangle rectangle = new Rectangle(); rectangle.Height = 2; rectangle.Width = 2; ValidationResult vr = rectangle.Validate(); Console.WriteLine(vr.Message); Assert.IsFalse(vr.IsValid, "Object should not be valid."); Assert.IsTrue(vr.Message.Length > 0); Assert.IsTrue(vr.Message.IndexOf(CompositeValidatorTest.NoSquareMessage) >= 0); Assert.AreEqual(CompositeValidatorTest.NoSquareMessage + Environment.NewLine + Environment.NewLine, vr.Message); } [Test] public void Valid() { Rectangle rectangle = new Rectangle(); rectangle.Height = 3; rectangle.Width = 2; ValidationResult vr = rectangle.Validate(); Assert.IsTrue(vr.IsValid); Assert.IsTrue(vr.Message.Length == 0, "Object should not have a validation message."); } } public class Rectangle : CompositeValidator { public Rectangle() { this.AddRule(new RectangleNoSquareRule(this)); this.AddRule(new RectangleHeightRule(this)); this.AddRule(new RectangleWidthRule(this)); } public int Height = 0; public int Width = 0; } public class RectangleNoSquareRule : IValidationRule { private Rectangle _rectangle; public RectangleNoSquareRule(Rectangle r) { this._rectangle = r; } #region IValidationRule Members public ValidationResult Validate() { ValidationResult vr = new ValidationResult(); if(this._rectangle.Height == this._rectangle.Width) { vr.IsValid = false; vr.AddMessage(CompositeValidatorTest.NoSquareMessage); } return vr; } #endregion } public class RectangleHeightRule : IValidationRule { #region IValidationRule Members private Rectangle _rectangle; public RectangleHeightRule(Rectangle r) { this._rectangle = r; } public ValidationResult Validate() { ValidationResult vr = new ValidationResult(); if(this._rectangle.Height <= 0) { vr.IsValid = false; vr.AddMessage(CompositeValidatorTest.BadHeightMessage); } return vr; } #endregion } public class RectangleWidthRule : IValidationRule { #region IValidationRule Members private Rectangle _rectangle; public RectangleWidthRule(Rectangle r) { this._rectangle = r; } public ValidationResult Validate() { ValidationResult vr = new ValidationResult(); if(this._rectangle.Width <= 0) { vr.IsValid = false; vr.AddMessage(CompositeValidatorTest.BadWidthMessage); } return vr; } #endregion } } --- NEW FILE: ObjectComparerTest.cs --- using System; using Adapdev.Mock; using NUnit.Framework; namespace Adapdev.Tests { /// <summary> /// Summary description for ObjectComparerTest. /// </summary> /// [TestFixture] public class ObjectComparerTest { [Test] public void AreEqual() { DateTime now = DateTime.Now; SuppliersEntity e1 = new SuppliersEntity(); e1.Address = "Test"; e1.SupplierID = 12; e1.Created = now; SuppliersEntity e2 = new SuppliersEntity(); e2.Address = "Test"; e2.SupplierID = 12; e2.Created = now; Assert.IsTrue(ObjectComparer.AreEqual(e1, e2), "Objects should be equal."); e2.Created = DateTime.Now.AddHours(1); Assert.IsFalse(ObjectComparer.AreEqual(e1, e2), "Objects should not be equal."); } } } --- NEW FILE: IValidatorTest.cs --- using System; using NUnit.Framework; using Adapdev; namespace Adapdev.Tests { /// <summary> /// Summary description for IValidatable. /// </summary> /// [TestFixture] public class IValidatorTest { public static string BadHeightMessage = "Height must be greater than zero."; public static string BadWidthMessage = "Width must be greater than zero."; [Test] public void Invalid() { Square square = new Square(); square.Height = -1; square.Width = 0; ValidationResult vr = square.Validate(); Console.WriteLine(vr.Message); Assert.IsFalse(vr.IsValid, "Object should not be valid."); Assert.IsTrue(vr.Message.Length > 0); Assert.IsTrue(vr.Message.IndexOf(IValidatorTest.BadHeightMessage) >= 0); Assert.IsTrue(vr.Message.IndexOf(IValidatorTest.BadWidthMessage) >= 0); } [Test] public void Valid() { Square square = new Square(); square.Height = 2; square.Width = 2; ValidationResult vr = square.Validate(); Assert.IsTrue(vr.IsValid); Assert.IsTrue(vr.Message.Length == 0, "Object should not have a validation message."); } } public class Square : IValidator { #region IValidator Members public int Height = 0; public int Width = 0; public ValidationResult Validate() { ValidationResult vr = new ValidationResult(); if(this.Height <= 0) { vr.IsValid = false; vr.AddMessage(IValidatorTest.BadHeightMessage); } if(this.Width <= 0) { vr.IsValid = false; vr.AddMessage(IValidatorTest.BadWidthMessage); } return vr; } #endregion } } |