using Annulus; using System; using System.Reflection; using UnitTestSharp; namespace Glass.UnitTests { public class MaterialLibraryTests : TestFixture { [SpriteSheet(3, 4, 5, "foo")] public sealed class TestLibrary { private TestLibrary() { } public static readonly NineSliceMaterial Panel = MaterialLibrary.CreateNineSlice( new Point(7, 11)); public static readonly ThreeWideMaterial Button = MaterialLibrary.CreateThreeWide( new Point(13, 17)); public static readonly ThreeTallMaterial Slider = MaterialLibrary.CreateThreeTall( new Point(19, 23)); } public sealed class LibraryWithoutASpriteSheet { private LibraryWithoutASpriteSheet() { } } [SpriteSheet(3, 4, 5, "foo")] public class LibraryWithAnInheritedSpriteSheet { } public sealed class DerivedLibrary : LibraryWithAnInheritedSpriteSheet { } public static SpriteSheetAttribute SheetOn() => typeof(TLibrary).GetCustomAttribute(); public class CreateNineSliceTests : TestFixture { public void BindsTheLibrarysSheetToEveryCell() { foreach (Material cell in TestLibrary.Panel.ChildMaterials) { CheckEqual(SheetOn(), ((SpriteMaterial)cell).SpriteSheet); } } public void CentersTheCellsOnTheRequestedCell() { CheckEqual(new Point(7, 11), ((SpriteMaterial)TestLibrary.Panel.CenterMaterial).CellLocation); CheckEqual(new Point(6, 10), ((SpriteMaterial)TestLibrary.Panel.UpperLeftMaterial).CellLocation); } public void DebugNameDefaultsToTheDeclaringFieldName() { CheckEqual("Panel", TestLibrary.Panel.ToString()); } public void AppliesTheBorderSize() { var border = new Rect.BorderSize { Top = 1, Bottom = 2, Left = 3, Right = 4 }; NineSliceMaterial panel = MaterialLibrary.CreateNineSlice(new Point(7, 11), border); CheckEqual(border, panel.BorderSize); } public void LibraryWithoutASpriteSheetThrows() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateNineSlice(new Point(7, 11)); } public void SpriteSheetAttributeIsNotInherited() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateNineSlice(new Point(7, 11)); } } public class CreateThreeWideTests : TestFixture { public void BindsTheLibrarysSheetToEveryCell() { foreach (Material cell in TestLibrary.Button.ChildMaterials) { CheckEqual(SheetOn(), ((SpriteMaterial)cell).SpriteSheet); } } public void CentersTheCellsOnTheRequestedCell() { CheckEqual(new Point(13, 17), ((SpriteMaterial)TestLibrary.Button.CenterMaterial).CellLocation); CheckEqual(new Point(12, 17), ((SpriteMaterial)TestLibrary.Button.LeftMaterial).CellLocation); } public void DebugNameDefaultsToTheDeclaringFieldName() { CheckEqual("Button", TestLibrary.Button.ToString()); } public void AppliesTheBorderSize() { var border = new Rect.BorderSize { Top = 1, Bottom = 2, Left = 3, Right = 4 }; ThreeWideMaterial button = MaterialLibrary.CreateThreeWide(new Point(13, 17), border); CheckEqual(border, button.BorderSize); } public void LibraryWithoutASpriteSheetThrows() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateThreeWide(new Point(13, 17)); } public void SpriteSheetAttributeIsNotInherited() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateThreeWide(new Point(13, 17)); } } public class CreateThreeTallTests : TestFixture { public void BindsTheLibrarysSheetToEveryCell() { foreach (Material cell in TestLibrary.Slider.ChildMaterials) { CheckEqual(SheetOn(), ((SpriteMaterial)cell).SpriteSheet); } } public void CentersTheCellsOnTheRequestedCell() { CheckEqual(new Point(19, 23), ((SpriteMaterial)TestLibrary.Slider.CenterMaterial).CellLocation); CheckEqual(new Point(19, 22), ((SpriteMaterial)TestLibrary.Slider.TopMaterial).CellLocation); } public void DebugNameDefaultsToTheDeclaringFieldName() { CheckEqual("Slider", TestLibrary.Slider.ToString()); } public void AppliesTheBorderSize() { var border = new Rect.BorderSize { Top = 1, Bottom = 2, Left = 3, Right = 4 }; ThreeTallMaterial slider = MaterialLibrary.CreateThreeTall(new Point(19, 23), border); CheckEqual(border, slider.BorderSize); } public void LibraryWithoutASpriteSheetThrows() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateThreeTall(new Point(19, 23)); } public void SpriteSheetAttributeIsNotInherited() { CheckThrow(typeof(InvalidOperationException)); MaterialLibrary.CreateThreeTall(new Point(19, 23)); } } } }