1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-19 07:31:22 +02:00

(finally) added some tests

This commit is contained in:
Ellpeck 2020-07-31 17:14:25 +02:00
parent e80eb17b22
commit fe67b70332
12 changed files with 253 additions and 12 deletions

View file

@ -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

View file

@ -152,15 +152,6 @@ namespace MLEM.Extensions {
return new Vector2(vector.X, vector.Y);
}
/// <summary>
/// Returns the 3-dimensional translation of the given matrix.
/// </summary>
/// <param name="matrix">The matrix</param>
/// <returns>The translation of the matrix</returns>
public static Vector3 Translation(this Matrix matrix) {
return new Vector3(matrix.M41, matrix.M42, matrix.M43);
}
/// <summary>
/// Returns the 3-dimensional scale of the given matrix.
/// </summary>
@ -185,7 +176,7 @@ namespace MLEM.Extensions {
/// <returns>The rotation of the matrix</returns>
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));
}
}
}

20
Tests/CameraTests.cs Normal file
View file

@ -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);
}
}
}

36
Tests/FormattingTests.cs Normal file
View file

@ -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("<testmacro>"), (f, m, r) => "<test1>");
formatter.Macros.Add(new Regex("<test1>"), (f, m, r) => "<test2>blue");
formatter.Macros.Add(new Regex("<test2>"), (f, m, r) => "<c Blue>");
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 <testmacro> text</c>.";
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 <c Blue>blue text</c>.";
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 <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> 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);
}
}
}

32
Tests/NumberTests.cs Normal file
View file

@ -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));
}
}
}

18
Tests/Stub/StubContent.cs Normal file
View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
namespace Tests.Stub {
public class StubContent : ContentManager {
private readonly Dictionary<string, object> assets;
public StubContent(Dictionary<string, object> assets) : base(new StubServices()) {
this.assets = assets;
}
public override T Load<T>(string assetName) {
return (T) this.assets[assetName];
}
}
}

36
Tests/Stub/StubFont.cs Normal file
View file

@ -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;
}
}
}

View file

@ -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()) {
}
}
}

View file

@ -0,0 +1,11 @@
using System;
namespace Tests.Stub {
public class StubServices : IServiceProvider {
public object GetService(Type serviceType) {
return null;
}
}
}

View file

@ -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<string, object> {
{"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));
}
}
}

27
Tests/Tests.csproj Normal file
View file

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.WindowsDX" Version="3.7.0.1708" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" />
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
<ProjectReference Include="..\MLEM.Templates\MLEM.Templates.csproj" />
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
<ProjectReference Include="..\MLEM\MLEM.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="texture.atlas" />
</ItemGroup>
</Project>

View file

@ -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}")