using System; using Amphora; using UnitTestSharp; namespace Konis.UnitTests { public class PixelSheetDefTests : TestFixture { public static PixelSheetDef Sheet( int width = 32, int height = 16, int spriteWidth = 8, int spriteHeight = 8, int borderSize = 0, int byteCount = -1) { if (byteCount < 0) { byteCount = width * height * PixelSheetDef.BytesPerPixel; } return new PixelSheetDef { Width = width, Height = height, SpriteWidth = spriteWidth, SpriteHeight = spriteHeight, BorderSize = borderSize, Rgba = new ByteView(new byte[byteCount]), }; } public class DefaultsTests : TestFixture { public void HasNoSizeAndNoPixels() { var def = new PixelSheetDef(); CheckEqual(0, def.Width); CheckEqual(0, def.Height); CheckEqual(0, def.Rgba.Length); } } public class ColumnCountTests : TestFixture { public void IsHowManySpritesFitAcrossTheImage() { CheckEqual(4, Sheet(width: 32, spriteWidth: 8).ColumnCount); } public void LeavesOutAPartialSpriteAtTheEdge() { CheckEqual(3, Sheet(width: 30, spriteWidth: 8).ColumnCount); } public void CountsABorderBetweenSpritesButNotAfterTheLast() { CheckEqual(2, Sheet(width: 18, spriteWidth: 8, borderSize: 2).ColumnCount); } public void WithoutASpriteSize_IsZero() { CheckEqual(0, Sheet(spriteWidth: 0).ColumnCount); } public void WithANegativeBorder_IsZero() { CheckEqual(0, Sheet(borderSize: -1).ColumnCount); } } public class RowCountTests : TestFixture { public void IsHowManySpritesFitDownTheImage() { CheckEqual(2, Sheet(height: 16, spriteHeight: 8).RowCount); } public void CountsABorderBetweenSpritesButNotAfterTheLast() { CheckEqual(3, Sheet(height: 28, spriteHeight: 8, borderSize: 2).RowCount); CheckEqual(2, Sheet(height: 27, spriteHeight: 8, borderSize: 2).RowCount); } public void WithoutASpriteSize_IsZero() { CheckEqual(0, Sheet(spriteHeight: 0).RowCount); } } public class ValidateTests : TestFixture { public void WithFourBytesForEachPixelAndSpritesThatFit_Passes() { Sheet().Validate(); } public void ASpriteWidthOfZero_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(spriteWidth: 0).Validate(); } public void ANegativeBorder_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(borderSize: -1).Validate(); } public void AWidthOfZero_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(width: 0).Validate(); } public void AHeightOfZero_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(height: 0).Validate(); } public void TooFewBytes_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(byteCount: 32 * 16 * 4 - 1).Validate(); } public void TooManyBytes_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(byteCount: 32 * 16 * 4 + 1).Validate(); } public void WithNoPixelsGiven_Throws() { CheckThrow(typeof(InvalidOperationException)); new PixelSheetDef { Width = 8, Height = 8, SpriteWidth = 8, SpriteHeight = 8 }.Validate(); } public void ViewingPartOfAnArray_CountsOnlyTheViewedBytes() { new PixelSheetDef { Width = 1, Height = 1, SpriteWidth = 1, SpriteHeight = 1, Rgba = new ByteView(new byte[10], 3, 7), }.Validate(); } public void ASpriteWiderThanTheImage_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(width: 7, height: 8, spriteWidth: 8).Validate(); } public void ASpriteTallerThanTheImage_Throws() { CheckThrow(typeof(InvalidOperationException)); Sheet(width: 8, height: 7, spriteHeight: 8).Validate(); } } } }