using UnitTestSharp; using Azimuth; namespace Glass.UnitTests { public class ConstraintsTests : TestFixture { public class CtorTests : TestFixture { public void AssignsAllFourBounds() { var constraints = new Constraints(1, 2, 3, 4); CheckEqual((Scalar)1, constraints.MinWidth); CheckEqual((Scalar)2, constraints.MaxWidth); CheckEqual((Scalar)3, constraints.MinHeight); CheckEqual((Scalar)4, constraints.MaxHeight); } } public class IsWidthTightTests : TestFixture { public void TrueWhenTheBoundsAgree() { CheckTrue(new Constraints(5, 5, 0, 10).IsWidthTight); } public void FalseWhenTheBoundsDiffer() { CheckFalse(new Constraints(0, 5, 0, 10).IsWidthTight); } } public class IsHeightTightTests : TestFixture { public void TrueWhenTheBoundsAgree() { CheckTrue(new Constraints(0, 10, 5, 5).IsHeightTight); } public void FalseWhenTheBoundsDiffer() { CheckFalse(new Constraints(0, 10, 0, 5).IsHeightTight); } } public class MinSizeTests : TestFixture { public void PairsTheTwoLowerBounds() { CheckEqual(new Vector(1, 3), new Constraints(1, 2, 3, 4).MinSize); } } public class MaxSizeTests : TestFixture { public void PairsTheTwoUpperBounds() { CheckEqual(new Vector(2, 4), new Constraints(1, 2, 3, 4).MaxSize); } } public class ConstrainTests : TestFixture { public void SizeAlreadyInRangeIsUnchanged() { CheckEqual(new Vector(5, 6), new Constraints(0, 10, 0, 10).Constrain(new Vector(5, 6))); } public void SizeAboveTheMaximumIsClampedDown() { CheckEqual(new Vector(10, 10), new Constraints(0, 10, 0, 10).Constrain(new Vector(50, 60))); } public void SizeBelowTheMinimumIsClampedUp() { CheckEqual(new Vector(4, 5), new Constraints(4, 10, 5, 10).Constrain(Vector.Zero)); } public void AxesAreClampedIndependently() { CheckEqual(new Vector(10, 2), new Constraints(0, 10, 0, 10).Constrain(new Vector(50, 2))); } public void TightConstraintsIgnoreTheRequestedSize() { CheckEqual(new Vector(7, 8), Constraints.Tight(new Vector(7, 8)).Constrain(new Vector(50, 1))); } } public class LooseTests : TestFixture { public void HasNoLowerBound() { var constraints = Constraints.Loose(new Vector(10, 20)); CheckEqual(Vector.Zero, constraints.MinSize); CheckEqual(new Vector(10, 20), constraints.MaxSize); } public void IsTightOnNeitherAxis() { var constraints = Constraints.Loose(new Vector(10, 20)); CheckFalse(constraints.IsWidthTight); CheckFalse(constraints.IsHeightTight); } } public class TightTests : TestFixture { public void BoundsAgreeOnBothAxes() { var constraints = Constraints.Tight(new Vector(10, 20)); CheckEqual(new Vector(10, 20), constraints.MinSize); CheckEqual(new Vector(10, 20), constraints.MaxSize); } public void IsTightOnBothAxes() { var constraints = Constraints.Tight(new Vector(10, 20)); CheckTrue(constraints.IsWidthTight); CheckTrue(constraints.IsHeightTight); } } public class TightWidthTests : TestFixture { public void PinsWidthAndLeavesHeightLoose() { var constraints = Constraints.TightWidth(width: 10, maxHeight: 20); CheckTrue(constraints.IsWidthTight); CheckFalse(constraints.IsHeightTight); CheckEqual(new Vector(10, 0), constraints.MinSize); CheckEqual(new Vector(10, 20), constraints.MaxSize); } } public class TightHeightTests : TestFixture { public void PinsHeightAndLeavesWidthLoose() { var constraints = Constraints.TightHeight(height: 20, maxWidth: 10); CheckFalse(constraints.IsWidthTight); CheckTrue(constraints.IsHeightTight); CheckEqual(new Vector(0, 20), constraints.MinSize); CheckEqual(new Vector(10, 20), constraints.MaxSize); } } public class UnboundedTests : TestFixture { public void HasNoUpperBoundOnEitherAxis() { CheckEqual(Vector.PositiveInfinity, Constraints.Unbounded.MaxSize); CheckEqual(Vector.Zero, Constraints.Unbounded.MinSize); } public void LeavesAnIntrinsicSizeUntouched() { CheckEqual(new Vector(3, 4), Constraints.Unbounded.Constrain(new Vector(3, 4))); } } public class EqualsOperatorTests : TestFixture { public void MatchingBoundsAreEqual() { CheckTrue(new Constraints(1, 2, 3, 4) == new Constraints(1, 2, 3, 4)); } public void DifferingMinWidthIsNotEqual() { CheckFalse(new Constraints(0, 2, 3, 4) == new Constraints(1, 2, 3, 4)); } public void DifferingMaxWidthIsNotEqual() { CheckFalse(new Constraints(1, 0, 3, 4) == new Constraints(1, 2, 3, 4)); } public void DifferingMinHeightIsNotEqual() { CheckFalse(new Constraints(1, 2, 0, 4) == new Constraints(1, 2, 3, 4)); } public void DifferingMaxHeightIsNotEqual() { CheckFalse(new Constraints(1, 2, 3, 0) == new Constraints(1, 2, 3, 4)); } } public class NotEqualsOperatorTests : TestFixture { public void MatchingBoundsAreNotUnequal() { CheckFalse(new Constraints(1, 2, 3, 4) != new Constraints(1, 2, 3, 4)); } public void DifferingBoundsAreUnequal() { CheckTrue(new Constraints(1, 2, 3, 4) != new Constraints(4, 3, 2, 1)); } } public class EqualsTests : TestFixture { public void MatchingBoundsAreEqual() { CheckTrue(new Constraints(1, 2, 3, 4).Equals(new Constraints(1, 2, 3, 4))); } public void DifferingBoundsAreNotEqual() { CheckFalse(new Constraints(1, 2, 3, 4).Equals(new Constraints(4, 3, 2, 1))); } public void OtherTypesAreNotEqual() { CheckFalse(new Constraints(1, 2, 3, 4).Equals("not a Constraints")); } public void NullIsNotEqual() { CheckFalse(new Constraints(1, 2, 3, 4).Equals(null)); } } public class GetHashCodeTests : TestFixture { public void MatchingBoundsShareAHashCode() { CheckEqual(new Constraints(1, 2, 3, 4).GetHashCode(), new Constraints(1, 2, 3, 4).GetHashCode()); } public void ReorderedBoundsDoNotShareAHashCode() { CheckFalse(new Constraints(1, 2, 3, 4).GetHashCode() == new Constraints(4, 3, 2, 1).GetHashCode()); } public void EveryBoundContributes() { int reference = new Constraints(1, 2, 3, 4).GetHashCode(); CheckFalse(reference == new Constraints(9, 2, 3, 4).GetHashCode()); CheckFalse(reference == new Constraints(1, 9, 3, 4).GetHashCode()); CheckFalse(reference == new Constraints(1, 2, 9, 4).GetHashCode()); CheckFalse(reference == new Constraints(1, 2, 3, 9).GetHashCode()); } } public class ToStringTests : TestFixture { public void ReportsBothRanges() { CheckEqual("width [1, 2], height [3, 4]", new Constraints(1, 2, 3, 4).ToString()); } } public class EqualityTests : EqualityTestFixture { public EqualityTests() : base(new Constraints(1, 2, 3, 4)) { } } } }