From fe67b70332cea49f00c172791700cd98d5e887cd Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 31 Jul 2020 17:14:25 +0200 Subject: [PATCH] (finally) added some tests --- MLEM.sln | 6 ++++ MLEM/Extensions/NumberExtensions.cs | 12 +------- Tests/CameraTests.cs | 20 ++++++++++++ Tests/FormattingTests.cs | 36 ++++++++++++++++++++++ Tests/NumberTests.cs | 32 ++++++++++++++++++++ Tests/Stub/StubContent.cs | 18 +++++++++++ Tests/Stub/StubFont.cs | 36 ++++++++++++++++++++++ Tests/Stub/StubGraphics.cs | 10 ++++++ Tests/Stub/StubServices.cs | 11 +++++++ Tests/TestDataTextureAtlas.cs | 47 +++++++++++++++++++++++++++++ Tests/Tests.csproj | 27 +++++++++++++++++ build.cake | 10 +++++- 12 files changed, 253 insertions(+), 12 deletions(-) create mode 100644 Tests/CameraTests.cs create mode 100644 Tests/FormattingTests.cs create mode 100644 Tests/NumberTests.cs create mode 100644 Tests/Stub/StubContent.cs create mode 100644 Tests/Stub/StubFont.cs create mode 100644 Tests/Stub/StubGraphics.cs create mode 100644 Tests/Stub/StubServices.cs create mode 100644 Tests/TestDataTextureAtlas.cs create mode 100644 Tests/Tests.csproj diff --git a/MLEM.sln b/MLEM.sln index 49e9478..a28f679 100644 --- a/MLEM.sln +++ b/MLEM.sln @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Templates", "MLEM.Temp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demos.Android", "Demos.Android\Demos.Android.csproj", "{410C0262-131C-4D0E-910D-D01B4F7143E0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{B89824FA-CD6D-430D-8952-CDDDB71EC942}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -66,5 +68,9 @@ Global {410C0262-131C-4D0E-910D-D01B4F7143E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {410C0262-131C-4D0E-910D-D01B4F7143E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {410C0262-131C-4D0E-910D-D01B4F7143E0}.Release|Any CPU.Build.0 = Release|Any CPU + {B89824FA-CD6D-430D-8952-CDDDB71EC942}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B89824FA-CD6D-430D-8952-CDDDB71EC942}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B89824FA-CD6D-430D-8952-CDDDB71EC942}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B89824FA-CD6D-430D-8952-CDDDB71EC942}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MLEM/Extensions/NumberExtensions.cs b/MLEM/Extensions/NumberExtensions.cs index 7ee8459..d25365f 100644 --- a/MLEM/Extensions/NumberExtensions.cs +++ b/MLEM/Extensions/NumberExtensions.cs @@ -152,15 +152,6 @@ namespace MLEM.Extensions { return new Vector2(vector.X, vector.Y); } - /// - /// Returns the 3-dimensional translation of the given matrix. - /// - /// The matrix - /// The translation of the matrix - public static Vector3 Translation(this Matrix matrix) { - return new Vector3(matrix.M41, matrix.M42, matrix.M43); - } - /// /// Returns the 3-dimensional scale of the given matrix. /// @@ -185,7 +176,7 @@ namespace MLEM.Extensions { /// The rotation of the matrix public static Quaternion Rotation(this Matrix matrix) { var (scX, scY, scZ) = matrix.Scale(); - if (scX == 0.0 || scY == 0.0 || scZ == 0.0) + if (scX == 0 || scY == 0 || scZ == 0) return Quaternion.Identity; return Quaternion.CreateFromRotationMatrix(new Matrix( matrix.M11 / scX, matrix.M12 / scX, matrix.M13 / scX, 0, @@ -193,6 +184,5 @@ namespace MLEM.Extensions { matrix.M31 / scZ, matrix.M32 / scZ, matrix.M33 / scZ, 0, 0, 0, 0, 1)); } - } } \ No newline at end of file diff --git a/Tests/CameraTests.cs b/Tests/CameraTests.cs new file mode 100644 index 0000000..455e528 --- /dev/null +++ b/Tests/CameraTests.cs @@ -0,0 +1,20 @@ +using System.Numerics; +using MLEM.Cameras; +using NUnit.Framework; +using Tests.Stub; +using Vector2 = Microsoft.Xna.Framework.Vector2; + +namespace Tests { + public class CameraTests { + + [Test] + public void TestConversions([Range(-4, 4, 4F)] float x, [Range(-4, 4, 4F)] float y) { + var camera = new Camera(new StubGraphics()); + var pos = new Vector2(x, y); + var cam = camera.ToCameraPos(pos); + var ret = camera.ToWorldPos(cam); + Assert.AreEqual(pos, ret); + } + + } +} \ No newline at end of file diff --git a/Tests/FormattingTests.cs b/Tests/FormattingTests.cs new file mode 100644 index 0000000..8983b2d --- /dev/null +++ b/Tests/FormattingTests.cs @@ -0,0 +1,36 @@ +using System.Text.RegularExpressions; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Font; +using MLEM.Formatting; +using MLEM.Formatting.Codes; +using MLEM.Textures; +using NUnit.Framework; +using Tests.Stub; + +namespace Tests { + public class FormattingTests { + + [Test] + public void TestMacros() { + var formatter = new TextFormatter(); + formatter.Macros.Add(new Regex(""), (f, m, r) => ""); + formatter.Macros.Add(new Regex(""), (f, m, r) => "blue"); + formatter.Macros.Add(new Regex(""), (f, m, r) => ""); + const string strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into text."; + const string goal = "This text uses a bunch of non-breaking\u00A0spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into blue text."; + Assert.AreEqual(formatter.ResolveMacros(strg), goal); + } + + [Test] + public void TestFormatting() { + var formatter = new TextFormatter(); + formatter.AddImage("Test", new TextureRegion(new Texture2D(new StubGraphics(), 1, 1), 0, 8, 24, 24)); + const string strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."; + var ret = formatter.Tokenize(new StubFont(), strg); + Assert.AreEqual(ret.Tokens.Length, 13); + Assert.AreEqual(ret.DisplayString, "Lorem Ipsum \uF8FF is simply dummy text of the \uF8FF printing and typesetting \uF8FF industry. Lorem Ipsum has been the industry's standard dummy text \uF8FF ever since the \uF8FF 1500s, when \uF8FF\uF8FF\uF8FF\uF8FF\uF8FF\uF8FF\uF8FF an unknown printer took a galley of type and scrambled it to make a type specimen book."); + Assert.AreEqual(ret.AllCodes.Length, 12); + } + + } +} \ No newline at end of file diff --git a/Tests/NumberTests.cs b/Tests/NumberTests.cs new file mode 100644 index 0000000..280877a --- /dev/null +++ b/Tests/NumberTests.cs @@ -0,0 +1,32 @@ +using Microsoft.Xna.Framework; +using MLEM.Extensions; +using MonoGame.Extended; +using NUnit.Framework; + +namespace Tests { + public class NumberTests { + + [Test] + public void TestRounding() { + Assert.AreEqual(1.25F.Floor(), 1); + Assert.AreEqual(-1.25F.Floor(), -1); + + Assert.AreEqual(1.25F.Ceil(), 2); + Assert.AreEqual(-1.25F.Ceil(), -2); + } + + [Test] + public void TestEquals() { + Assert.IsTrue(0.25F.Equals(0.26F, 0.01F)); + Assert.IsFalse(0.25F.Equals(0.26F, 0.009F)); + } + + [Test] + public void TestMatrixOps() { + var matrix = Matrix.CreateRotationX(2) * Matrix.CreateScale(2.5F); + Assert.AreEqual(matrix.Scale(), new Vector3(2.5F)); + Assert.AreEqual(matrix.Rotation(), Quaternion.CreateFromAxisAngle(Vector3.UnitX, 2)); + } + + } +} \ No newline at end of file diff --git a/Tests/Stub/StubContent.cs b/Tests/Stub/StubContent.cs new file mode 100644 index 0000000..601e0b6 --- /dev/null +++ b/Tests/Stub/StubContent.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework.Content; + +namespace Tests.Stub { + public class StubContent : ContentManager { + + private readonly Dictionary assets; + + public StubContent(Dictionary assets) : base(new StubServices()) { + this.assets = assets; + } + + public override T Load(string assetName) { + return (T) this.assets[assetName]; + } + + } +} \ No newline at end of file diff --git a/Tests/Stub/StubFont.cs b/Tests/Stub/StubFont.cs new file mode 100644 index 0000000..a1ce2b5 --- /dev/null +++ b/Tests/Stub/StubFont.cs @@ -0,0 +1,36 @@ +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Font; + +namespace Tests.Stub { + public class StubFont : GenericFont { + + public override GenericFont Bold { get; } = null; + public override GenericFont Italic { get; } = null; + public override float LineHeight { get; } = 1; + + public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) { + } + + public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) { + } + + public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { + } + + public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) { + } + + public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) { + } + + public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) { + } + + protected override Vector2 MeasureChar(char c) { + return Vector2.One; + } + + } +} \ No newline at end of file diff --git a/Tests/Stub/StubGraphics.cs b/Tests/Stub/StubGraphics.cs new file mode 100644 index 0000000..f7ea579 --- /dev/null +++ b/Tests/Stub/StubGraphics.cs @@ -0,0 +1,10 @@ +using Microsoft.Xna.Framework.Graphics; + +namespace Tests.Stub { + public class StubGraphics : GraphicsDevice { + + public StubGraphics() : + base(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, new PresentationParameters()) { + } + } +} \ No newline at end of file diff --git a/Tests/Stub/StubServices.cs b/Tests/Stub/StubServices.cs new file mode 100644 index 0000000..9f5f0ee --- /dev/null +++ b/Tests/Stub/StubServices.cs @@ -0,0 +1,11 @@ +using System; + +namespace Tests.Stub { + public class StubServices : IServiceProvider { + + public object GetService(Type serviceType) { + return null; + } + + } +} \ No newline at end of file diff --git a/Tests/TestDataTextureAtlas.cs b/Tests/TestDataTextureAtlas.cs new file mode 100644 index 0000000..8df5f4c --- /dev/null +++ b/Tests/TestDataTextureAtlas.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Textures; +using NUnit.Framework; +using Tests.Stub; + +namespace Tests { + public class TestDataTextureAtlas { + + [Test] + public void Test() { + const string data = @" +SimpleDeskUp +loc 0 0 48 32 +piv 16 16 +SimpleDeskRight +loc 48 0 48 32 +piv 80 16 + +Plant +loc 96 0 16 32 + +LongTableUp +loc 0 32 64 48 +piv 16 48 +LongTableRight +loc 64 32 64 48 +piv 112 48"; + using (var file = new FileInfo("texture.atlas").CreateText()) + file.Write(data); + + var content = new StubContent(new Dictionary { + {"texture", new Texture2D(new StubGraphics(), 1, 1)} + }); + var atlas = content.LoadTextureAtlas("texture"); + Assert.AreEqual(atlas.Regions.Count(), 5); + + var table = atlas["LongTableUp"]; + Assert.AreEqual(table.Area, new Rectangle(0, 32, 64, 48)); + Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); + } + + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..85a49f6 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,27 @@ + + + + net462 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build.cake b/build.cake index 93120b8..cc0da95 100644 --- a/build.cake +++ b/build.cake @@ -1,5 +1,6 @@ #addin Cake.DocFx&version=0.13.1 #tool docfx.console&version=2.51.0 +#tool NUnit.ConsoleRunner&version=3.11.1 // this is the upcoming version, for prereleases var version = Argument("version", "4.1.0"); @@ -29,7 +30,14 @@ Task("Build").IsDependentOn("Prepare").Does(() =>{ DotNetCoreBuild("Demos/Demos.csproj", settings); }); -Task("Pack").IsDependentOn("Build").Does(() => { +Task("Test").IsDependentOn("Build").Does(() => { + DotNetCoreTest("Tests/Tests.csproj", new DotNetCoreTestSettings { + NoWorkingDirectory = true, + Configuration = config, + }); +}); + +Task("Pack").IsDependentOn("Test").Does(() => { var settings = new DotNetCorePackSettings { Configuration = config, ArgumentCustomization = args => args.Append($"/p:Version={version}")