mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added a few more tests
This commit is contained in:
parent
2055c3a6ef
commit
d8d29bf10d
3 changed files with 122 additions and 3 deletions
30
Tests/CameraTests.cs
Normal file
30
Tests/CameraTests.cs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using MLEM.Cameras;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Tests {
|
||||||
|
public class CameraTests {
|
||||||
|
|
||||||
|
private TestGame game;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp() {
|
||||||
|
this.game = TestGame.Create();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown() {
|
||||||
|
this.game?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConversions([Range(-4, 4, 4F)] float x, [Range(-4, 4, 4F)] float y) {
|
||||||
|
var camera = new Camera(this.game.GraphicsDevice);
|
||||||
|
var pos = new Vector2(x, y);
|
||||||
|
var cam = camera.ToCameraPos(pos);
|
||||||
|
var ret = camera.ToWorldPos(cam);
|
||||||
|
Assert.AreEqual(pos, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
76
Tests/TexturePackerTests.cs
Normal file
76
Tests/TexturePackerTests.cs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Data;
|
||||||
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Textures;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Tests {
|
||||||
|
public class TexturePackerTests {
|
||||||
|
|
||||||
|
private Texture2D testTexture;
|
||||||
|
private TestGame game;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp() {
|
||||||
|
this.game = TestGame.Create();
|
||||||
|
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown() {
|
||||||
|
this.game?.Dispose();
|
||||||
|
this.testTexture?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestPacking() {
|
||||||
|
using var packer = new RuntimeTexturePacker();
|
||||||
|
for (var i = 0; i < 5; i++) {
|
||||||
|
var width = 16 * (i + 1);
|
||||||
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, width, 64), r => {
|
||||||
|
Assert.AreEqual(r.Width, width);
|
||||||
|
Assert.AreEqual(r.Height, 64);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
packer.Pack(this.game.GraphicsDevice);
|
||||||
|
Assert.AreEqual(packer.PackedTexture.Width, 16 + 32 + 48 + 64 + 80);
|
||||||
|
Assert.AreEqual(packer.PackedTexture.Height, 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestBounds() {
|
||||||
|
// test forced max width
|
||||||
|
using var packer = new RuntimeTexturePacker(128);
|
||||||
|
Assert.Throws<InvalidOperationException>(() => {
|
||||||
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), StubResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
// test auto-expanding width
|
||||||
|
using var packer2 = new RuntimeTexturePacker(128, true);
|
||||||
|
Assert.DoesNotThrow(() => {
|
||||||
|
packer2.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), StubResult);
|
||||||
|
});
|
||||||
|
packer2.Pack(this.game.GraphicsDevice);
|
||||||
|
|
||||||
|
// test power of two forcing
|
||||||
|
using var packer3 = new RuntimeTexturePacker(128, forcePowerOfTwo: true);
|
||||||
|
packer3.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), StubResult);
|
||||||
|
packer3.Pack(this.game.GraphicsDevice);
|
||||||
|
Assert.AreEqual(64, packer3.PackedTexture.Width);
|
||||||
|
Assert.AreEqual(256, packer3.PackedTexture.Height);
|
||||||
|
|
||||||
|
// test square forcing
|
||||||
|
using var packer4 = new RuntimeTexturePacker(128, forceSquare: true);
|
||||||
|
packer4.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), StubResult);
|
||||||
|
packer4.Pack(this.game.GraphicsDevice);
|
||||||
|
Assert.AreEqual(170, packer4.PackedTexture.Width);
|
||||||
|
Assert.AreEqual(170, packer4.PackedTexture.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void StubResult(TextureRegion region) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,7 +30,7 @@ namespace Tests {
|
||||||
};
|
};
|
||||||
invalidPanel.AddChild(new Paragraph(Anchor.AutoRight, 1, "This is some test text!", true));
|
invalidPanel.AddChild(new Paragraph(Anchor.AutoRight, 1, "This is some test text!", true));
|
||||||
invalidPanel.AddChild(new VerticalSpace(1));
|
invalidPanel.AddChild(new VerticalSpace(1));
|
||||||
Assert.Throws<ArithmeticException>(() => AddAndUpdate(invalidPanel));
|
Assert.Throws<ArithmeticException>(() => this.AddAndUpdate(invalidPanel));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -49,7 +49,7 @@ namespace Tests {
|
||||||
CanBeMoused = false
|
CanBeMoused = false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
AddAndUpdate(group);
|
this.AddAndUpdate(group);
|
||||||
|
|
||||||
// group has 1 panel with 1 scroll bar, and the panel's 10 children
|
// group has 1 panel with 1 scroll bar, and the panel's 10 children
|
||||||
Assert.AreEqual(1, group.GetChildren().Count());
|
Assert.AreEqual(1, group.GetChildren().Count());
|
||||||
|
@ -66,12 +66,25 @@ namespace Tests {
|
||||||
Assert.AreEqual(testBtn.GetChildren<Group>().Single().DisplayArea.Width, (150 - 5 - 10 - 3 - 3) * 0.5F);
|
Assert.AreEqual(testBtn.GetChildren<Group>().Single().DisplayArea.Width, (150 - 5 - 10 - 3 - 3) * 0.5F);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestStyle() {
|
||||||
|
var style = new StyleProp<string>();
|
||||||
|
Assert.AreEqual(null, style.Value);
|
||||||
|
style.SetFromStyle("from style");
|
||||||
|
Assert.AreEqual("from style", style.Value);
|
||||||
|
style.Set("custom");
|
||||||
|
Assert.AreEqual("custom", style.Value);
|
||||||
|
style.SetFromStyle("from style again");
|
||||||
|
Assert.AreEqual("custom", style.Value);
|
||||||
|
}
|
||||||
|
|
||||||
private void AddAndUpdate(Element element) {
|
private void AddAndUpdate(Element element) {
|
||||||
foreach (var root in this.game.UiSystem.GetRootElements())
|
foreach (var root in this.game.UiSystem.GetRootElements())
|
||||||
this.game.UiSystem.Remove(root.Name);
|
this.game.UiSystem.Remove(root.Name);
|
||||||
|
|
||||||
this.game.UiSystem.Add("Test", element);
|
this.game.UiSystem.Add("Test", element);
|
||||||
element.ForceUpdateArea();
|
element.ForceUpdateArea();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue