diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 727dfd7..6cf141e 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,7 +3,7 @@ "isRoot": true, "tools": { "cake.tool": { - "version": "0.38.5", + "version": "1.1.0", "commands": [ "dotnet-cake" ] diff --git a/.gitignore b/.gitignore index 41fbca8..78ff2f1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ bin obj packages *.user -tools \ No newline at end of file +tools +TestResults \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 24c4614..0937d0c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,7 +4,13 @@ pipeline { stage('Cake Build') { steps { sh 'dotnet tool restore' - sh 'dotnet dotnet-cake -Target=Publish -Branch=' + env.BRANCH_NAME + sh 'dotnet dotnet-cake --Target=Publish -Branch=' + env.BRANCH_NAME + } + } + stage('Publish Test Results') { + steps { + nunit testResultsPattern: '**/TestResults.xml' + cobertura coberturaReportFile: '**/coverage.cobertura.xml' } } stage('Document') { @@ -12,7 +18,7 @@ pipeline { branch 'release' } steps { - sh 'dotnet dotnet-cake -Target=Document' + sh 'dotnet dotnet-cake --Target=Document' sh 'cp Docs/_site/** /var/www/MLEM/ -r' } } diff --git a/MLEM.sln b/MLEM.sln index 49e9478..40fe144 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", "{53D52C3F-67FB-4F32-A794-EAB140BBFC11}" +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 + {53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MLEM/Textures/TextureRegion.cs b/MLEM/Textures/TextureRegion.cs index 2f69183..534086a 100644 --- a/MLEM/Textures/TextureRegion.cs +++ b/MLEM/Textures/TextureRegion.cs @@ -126,7 +126,7 @@ namespace MLEM.Textures { /// The top left corner of this area /// The size of this area public TextureRegion(TextureRegion region, Point uv, Point size) : - this(region.Texture, region.Position + uv, size) { + this(region?.Texture, (region?.Position ?? Point.Zero) + uv, size) { } } diff --git a/Tests/Content/Texture.atlas b/Tests/Content/Texture.atlas new file mode 100644 index 0000000..b7b5658 --- /dev/null +++ b/Tests/Content/Texture.atlas @@ -0,0 +1,14 @@ +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 \ No newline at end of file diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs new file mode 100644 index 0000000..810ebe6 --- /dev/null +++ b/Tests/DataTextureAtlasTests.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Data; +using MLEM.Data.Content; +using NUnit.Framework; +using Tests.Stub; + +namespace Tests { + public class TestDataTextureAtlas { + + [Test] + public void Test() { + var atlas = DataTextureAtlas.LoadAtlasData(null, new RawContentManager(new StubServices()), "Texture.atlas"); + 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/FormattingTests.cs b/Tests/FormattingTests.cs new file mode 100644 index 0000000..3cc2613 --- /dev/null +++ b/Tests/FormattingTests.cs @@ -0,0 +1,35 @@ +using System.Text.RegularExpressions; +using Microsoft.Xna.Framework.Graphics; +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((Texture2D) null, 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 \u2003 is simply dummy text of the \u2003 printing and typesetting \u2003 industry. Lorem Ipsum has been the industry's standard dummy text \u2003 ever since the \u2003 1500s, when \u2003\u2003\u2003\u2003\u2003\u2003\u2003 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..ce51496 --- /dev/null +++ b/Tests/NumberTests.cs @@ -0,0 +1,36 @@ +using Microsoft.Xna.Framework; +using MLEM.Extensions; +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); + + Assert.AreEqual(new Vector2(5, 2.5F).FloorCopy(), new Vector2(5, 2)); + Assert.AreEqual(new Vector2(5, 2.5F).CeilCopy(), new Vector2(5, 3)); + Assert.AreEqual(new Vector2(5.25F, 2).FloorCopy(), new Vector2(5, 2)); + Assert.AreEqual(new Vector2(5.25F, 2).CeilCopy(), new Vector2(6, 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/StubFont.cs b/Tests/Stub/StubFont.cs new file mode 100644 index 0000000..83b2e0e --- /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 => this; + public override GenericFont Italic => this; + public override float LineHeight => 1; + + protected override Vector2 MeasureChar(char c) { + return Vector2.Zero; + } + + 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) { + } + + } +} \ 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/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..f41e98c --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,27 @@ + + + netcoreapp3.1 + nunit + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + diff --git a/build.cake b/build.cake index d35ea9c..8607111 100644 --- a/build.cake +++ b/build.cake @@ -29,7 +29,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 { + Configuration = config, + Collectors = {"XPlat Code Coverage"} + }); +}); + +Task("Pack").IsDependentOn("Test").Does(() => { var settings = new DotNetCorePackSettings { Configuration = config, ArgumentCustomization = args => args.Append($"/p:Version={version}")