using System; using UnitTestSharp; namespace Glass.UnitTests { public class SpriteFrameTests : TestFixture { public static SpriteFrame Trimmed() => new SpriteFrame(new Point(3, 4), new Point(5, 6), trimOffset: new Point(1, 2), fullSize: new Point(8, 9)); public class CtorTests : TestFixture { public void Untrimmed_IsItsOwnWholeBox() { var frame = new SpriteFrame(new Point(3, 4), new Point(5, 6)); CheckEqual(new Point(3, 4), frame.Position); CheckEqual(new Point(5, 6), frame.Size); CheckEqual(new Point(0, 0), frame.TrimOffset); CheckEqual(new Point(5, 6), frame.FullSize); } public void Trimmed_AssignsAllFourPoints() { SpriteFrame frame = Trimmed(); CheckEqual(new Point(3, 4), frame.Position); CheckEqual(new Point(5, 6), frame.Size); CheckEqual(new Point(1, 2), frame.TrimOffset); CheckEqual(new Point(8, 9), frame.FullSize); } public void APieceThatFillsItsBoxExactly_IsAllowed() { var frame = new SpriteFrame(new Point(0, 0), new Point(5, 6), new Point(3, 3), new Point(8, 9)); CheckEqual(new Point(8, 9), frame.FullSize); } public void APositionLeftOfTheImage_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new SpriteFrame(new Point(-1, 0), new Point(5, 6)); } public void APositionAboveTheImage_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new SpriteFrame(new Point(0, -1), new Point(5, 6)); } public void NoWidth_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new SpriteFrame(new Point(0, 0), new Point(0, 6)); } public void NoHeight_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new SpriteFrame(new Point(0, 0), new Point(5, 0)); } public void ATrimOffsetLeftOfTheBox_Throws() { CheckThrow(typeof(ArgumentException)); new SpriteFrame(new Point(0, 0), new Point(5, 6), new Point(-1, 0), new Point(8, 9)); } public void ATrimOffsetAboveTheBox_Throws() { CheckThrow(typeof(ArgumentException)); new SpriteFrame(new Point(0, 0), new Point(5, 6), new Point(0, -1), new Point(8, 9)); } public void APiecePastTheBoxsRightEdge_Throws() { CheckThrow(typeof(ArgumentException)); new SpriteFrame(new Point(0, 0), new Point(5, 6), new Point(4, 0), new Point(8, 9)); } public void APiecePastTheBoxsBottomEdge_Throws() { CheckThrow(typeof(ArgumentException)); new SpriteFrame(new Point(0, 0), new Point(5, 6), new Point(0, 4), new Point(8, 9)); } } public class IsTrimmedTests : TestFixture { public void ItsOwnWholeBox_IsNot() { CheckFalse(new SpriteFrame(new Point(3, 4), new Point(5, 6)).IsTrimmed); } public void ABiggerBoxWithThePieceAtItsCorner_Is() { CheckTrue(new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(0, 0), new Point(8, 9)) .IsTrimmed); } public void ABoxBiggerOnlyAcross_Is() { CheckTrue(new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(0, 0), new Point(8, 6)) .IsTrimmed); } public void ABoxBiggerOnlyDown_Is() { CheckTrue(new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(0, 0), new Point(5, 9)) .IsTrimmed); } } public class EqualsTests : TestFixture { public void MatchingFramesAreEqual() { CheckTrue(Trimmed().Equals(Trimmed())); CheckTrue(Trimmed().Equals((object)Trimmed())); } public void EveryPointMattersToEquality() { SpriteFrame reference = Trimmed(); CheckFalse(reference.Equals(new SpriteFrame(new Point(0, 4), new Point(5, 6), new Point(1, 2), new Point(8, 9)))); CheckFalse(reference.Equals(new SpriteFrame(new Point(3, 4), new Point(4, 6), new Point(1, 2), new Point(8, 9)))); CheckFalse(reference.Equals(new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(0, 2), new Point(8, 9)))); CheckFalse(reference.Equals(new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(1, 2), new Point(9, 9)))); } public void OtherTypesAreNotEqual() { CheckFalse(Trimmed().Equals("not a frame")); } } public class GetHashCodeTests : TestFixture { public void MatchingFramesShareAHashCode() { CheckEqual(Trimmed().GetHashCode(), Trimmed().GetHashCode()); } public void EveryPointContributes() { int reference = Trimmed().GetHashCode(); CheckFalse(reference == new SpriteFrame(new Point(0, 4), new Point(5, 6), new Point(1, 2), new Point(8, 9)).GetHashCode()); CheckFalse(reference == new SpriteFrame(new Point(3, 4), new Point(4, 6), new Point(1, 2), new Point(8, 9)).GetHashCode()); CheckFalse(reference == new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(0, 2), new Point(8, 9)).GetHashCode()); CheckFalse(reference == new SpriteFrame(new Point(3, 4), new Point(5, 6), new Point(1, 2), new Point(9, 9)).GetHashCode()); } } public class ToStringTests : TestFixture { public void Untrimmed_GivesTheSizeAndPosition() { CheckEqual("5 : 6 at 3 : 4", new SpriteFrame(new Point(3, 4), new Point(5, 6)).ToString()); } public void Trimmed_AlsoGivesTheBoxAndWhereThePieceSitsInIt() { CheckEqual("5 : 6 at 3 : 4, trimmed from 8 : 9 at 1 : 2", Trimmed().ToString()); } } } }