1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

clean up structure of tests that use TestGame

This commit is contained in:
Ell 2024-10-26 14:41:36 +02:00
parent 1cfdedfc80
commit f5e64eb719
6 changed files with 72 additions and 95 deletions

View file

@ -4,23 +4,11 @@ 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();
}
public class CameraTests : GameTestFixture {
[Test]
public void TestConversions([Range(-4, 4, 4F)] float x, [Range(-4, 4, 4F)] float y) {
var camera = new Camera(this.game.GraphicsDevice);
var camera = new Camera(this.Game.GraphicsDevice);
var pos = new Vector2(x, y);
var cam = camera.ToCameraPos(pos);
var ret = camera.ToWorldPos(cam);

View file

@ -7,14 +7,13 @@ using NUnit.Framework;
namespace Tests;
public class TestDataTextureAtlas {
public class TestDataTextureAtlas : GameTestFixture {
[Test]
public void Test([Values(0, 4)] int regionX, [Values(0, 4)] int regionY) {
using var game = TestGame.Create();
using var texture = new Texture2D(game.GraphicsDevice, 1, 1);
using var texture = new Texture2D(this.Game.GraphicsDevice, 1, 1);
var region = new TextureRegion(texture, regionX, regionY, 1, 1);
var atlas = DataTextureAtlas.LoadAtlasData(region, game.RawContent, "Texture.atlas");
var atlas = DataTextureAtlas.LoadAtlasData(region, this.Game.RawContent, "Texture.atlas");
Assert.AreEqual(12, atlas.Regions.Count());
// no pivot

View file

@ -10,33 +10,20 @@ using NUnit.Framework;
namespace Tests;
public class FontTests {
public class FontTests : GameTestFixture {
private TestGame game;
private GenericFont font;
private TextFormatter formatter;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
this.font = this.game.UiSystem.Style.Font;
this.formatter = this.game.UiSystem.TextFormatter;
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
}
private GenericFont Font => this.Game.UiSystem.Style.Font;
private TextFormatter Formatter => this.Game.UiSystem.TextFormatter;
[Test]
public void TestRegularSplit() {
Assert.AreEqual(this.font.SplitStringSeparate(
Assert.AreEqual(this.Font.SplitStringSeparate(
"Note that the default style does not contain any textures or font files and, as such, is quite bland. However, the default style is quite easy to override, as can be seen in the code for this demo.",
65, 0.1F), new[] {"Note that the default style does ", "not contain any textures or font ", "files and, as such, is quite ", "bland. However, the default ", "style is quite easy to override, ", "as can be seen in the code for ", "this demo."});
var formatted = this.formatter.Tokenize(this.font,
var formatted = this.Formatter.Tokenize(this.Font,
"Select the demo you want to see below using your mouse, touch input, your keyboard or a controller. Check the demos' <c CornflowerBlue><l https://github.com/Ellpeck/MLEM/tree/main/Demos>source code</l></c> for more in-depth explanations of their functionality or the <c CornflowerBlue><l https://mlem.ellpeck.de/>website</l></c> for tutorials and API documentation.");
formatted.Split(this.font, 90, 0.1F);
formatted.Split(this.Font, 90, 0.1F);
Assert.AreEqual(formatted.DisplayString, "Select the demo you want to see below using \nyour mouse, touch input, your keyboard or a \ncontroller. Check the demos' source code for \nmore in-depth explanations of their \nfunctionality or the website for tutorials and \nAPI documentation.");
var tokens = new[] {
@ -58,13 +45,13 @@ public class FontTests {
public void TestLongLineSplit() {
var expectedDisplay = new[] {"This_is_a_really_long_line_to_s", "ee_if_splitting_without_spaces_", "works_properly._I_also_want_to_", "see_if_it_works_across_multiple", "_lines_or_just_on_the_first_one. ", "But after this, I want the text to ", "continue normally before ", "changing_back_to_being_really_", "long_oh_yes"};
Assert.AreEqual(this.font.SplitStringSeparate(
Assert.AreEqual(this.Font.SplitStringSeparate(
"This_is_a_really_long_line_to_see_if_splitting_without_spaces_works_properly._I_also_want_to_see_if_it_works_across_multiple_lines_or_just_on_the_first_one. But after this, I want the text to continue normally before changing_back_to_being_really_long_oh_yes",
65, 0.1F), expectedDisplay);
var formatted = this.formatter.Tokenize(this.font,
var formatted = this.Formatter.Tokenize(this.Font,
"This_is_a_really_long_line_to_see_if_<c Blue>splitting</c>_without_spaces_works_properly._I_also_want_to_see_if_it_works_across_multiple_<c Yellow>lines</c>_or_just_on_the_first_one. But after this, I want the <b>text</b> to continue normally before changing_back_<i>to</i>_being_really_long_oh_yes");
formatted.Split(this.font, 65, 0.1F);
formatted.Split(this.Font, 65, 0.1F);
Assert.AreEqual(formatted.DisplayString, string.Join('\n', expectedDisplay));
var tokens = new[] {
@ -84,9 +71,9 @@ public class FontTests {
[Test]
public void TestNewlineSplit() {
var formatted = this.formatter.Tokenize(this.font,
var formatted = this.Formatter.Tokenize(this.Font,
"This is a pretty long line with regular <c Blue>content</c> that will be split.\nNow this is a new line with additional regular <c Blue>content</c> that is forced into a new line.");
formatted.Split(this.font, 65, 0.1F);
formatted.Split(this.Font, 65, 0.1F);
Assert.AreEqual(formatted.DisplayString, "This is a pretty long line with \nregular content that will be \nsplit.\nNow this is a new line with \nadditional regular content that \nis forced into a new line.");
var tokens = new[] {
@ -102,24 +89,24 @@ public class FontTests {
[Test]
public void TestMacros() {
this.formatter.Macros.Add(new Regex("<testmacro>"), (_, _, _) => "<test1>");
this.formatter.Macros.Add(new Regex("<test1>"), (_, _, _) => "<test2>blue");
this.formatter.Macros.Add(new Regex("<test2>"), (_, _, _) => "<c Blue>");
this.Formatter.Macros.Add(new Regex("<testmacro>"), (_, _, _) => "<test1>");
this.Formatter.Macros.Add(new Regex("<test1>"), (_, _, _) => "<test2>blue");
this.Formatter.Macros.Add(new Regex("<test2>"), (_, _, _) => "<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(this.formatter.ResolveMacros(strg), goal);
Assert.AreEqual(this.Formatter.ResolveMacros(strg), goal);
// test recursive macros
this.formatter.Macros.Add(new Regex("<rec1>"), (_, _, _) => "<rec2>");
this.formatter.Macros.Add(new Regex("<rec2>"), (_, _, _) => "<rec1>");
Assert.Throws<ArithmeticException>(() => this.formatter.ResolveMacros("Test <rec1> string"));
this.Formatter.Macros.Add(new Regex("<rec1>"), (_, _, _) => "<rec2>");
this.Formatter.Macros.Add(new Regex("<rec2>"), (_, _, _) => "<rec1>");
Assert.Throws<ArithmeticException>(() => this.Formatter.ResolveMacros("Test <rec1> string"));
}
[Test]
public void TestFormatting() {
this.formatter.AddImage("Test", new TextureRegion((Texture2D) null, 0, 8, 24, 24));
this.Formatter.AddImage("Test", new TextureRegion((Texture2D) null, 0, 8, 24, 24));
const string strg = "<b>Lorem</b> 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 <b></b>book.";
var ret = this.formatter.Tokenize(this.font, strg);
var ret = this.Formatter.Tokenize(this.Font, strg);
Assert.AreEqual(ret.Tokens.Length, 16);
Assert.AreEqual(ret.DisplayString, "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.");
Assert.AreEqual(ret.AllCodes.Length, 16);
@ -127,15 +114,15 @@ public class FontTests {
[Test]
public void TestStripping() {
var stripped = this.formatter.StripAllFormatting("This is a <b>test string</b></b><b></b> <i>with a lot of</i>content</b><i></b></i> and an <k> invalid code</b> as well<ü>.");
var stripped = this.Formatter.StripAllFormatting("This is a <b>test string</b></b><b></b> <i>with a lot of</i>content</b><i></b></i> and an <k> invalid code</b> as well<ü>.");
Assert.AreEqual("This is a test string with a lot ofcontent and an <k> invalid code as well<ü>.", stripped);
}
[Test]
public void TestConsistency() {
void CompareSizes(string s) {
var spriteFont = ((GenericSpriteFont) this.font).Font.MeasureString(s);
var genericFont = this.font.MeasureString(s);
var spriteFont = ((GenericSpriteFont) this.Font).Font.MeasureString(s);
var genericFont = this.Font.MeasureString(s);
Assert.AreEqual(spriteFont.X, genericFont.X);
// we leave a bit of room for the Y value since sprite fonts sometimes increase line height for specific characters, which generic fonts don't
Assert.AreEqual(spriteFont.Y, genericFont.Y, 10);
@ -153,16 +140,16 @@ public class FontTests {
[Test]
public void TestSpecialCharacters() {
void CompareSizes(string s) {
var spriteFont = ((GenericSpriteFont) this.font).Font;
Assert.AreNotEqual(spriteFont.MeasureString(s), this.font.MeasureString(s));
var spriteFont = ((GenericSpriteFont) this.Font).Font;
Assert.AreNotEqual(spriteFont.MeasureString(s), this.Font.MeasureString(s));
}
CompareSizes($"This is a very simple{GenericFont.Nbsp}test string");
CompareSizes($"This is a very simple{GenericFont.Emsp}test string");
CompareSizes($"This is a very simple{GenericFont.Zwsp}test string");
Assert.AreEqual(new Vector2(this.font.LineHeight, this.font.LineHeight), this.font.MeasureString(GenericFont.Emsp.ToString()));
Assert.AreEqual(new Vector2(0, this.font.LineHeight), this.font.MeasureString(GenericFont.Zwsp.ToString()));
Assert.AreEqual(new Vector2(this.Font.LineHeight, this.Font.LineHeight), this.Font.MeasureString(GenericFont.Emsp.ToString()));
Assert.AreEqual(new Vector2(0, this.Font.LineHeight), this.Font.MeasureString(GenericFont.Zwsp.ToString()));
}
}

View file

@ -2,6 +2,7 @@
using MLEM.Data.Content;
using MLEM.Font;
using MLEM.Startup;
using NUnit.Framework;
namespace Tests;
@ -36,3 +37,20 @@ public class TestGame : MlemGame {
}
}
public class GameTestFixture {
protected TestGame Game { get; private set; }
[SetUp]
public void SetUpGame() {
this.Game = TestGame.Create();
}
[TearDown]
public void TearDownGame() {
this.Game?.Dispose();
this.Game = null;
}
}

View file

@ -7,22 +7,19 @@ using NUnit.Framework;
namespace Tests;
public class TexturePackerTests {
public class TexturePackerTests : GameTestFixture {
private Texture2D testTexture;
private Texture2D disposedTestTexture;
private TestGame game;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
this.testTexture = new Texture2D(this.game.GraphicsDevice, 2048, 2048);
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
this.testTexture = new Texture2D(this.Game.GraphicsDevice, 2048, 2048);
this.disposedTestTexture = new Texture2D(this.Game.GraphicsDevice, 16, 16);
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
this.testTexture?.Dispose();
this.disposedTestTexture?.Dispose();
}
@ -37,7 +34,7 @@ public class TexturePackerTests {
Assert.AreEqual(r.Height, 64);
});
}
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
Assert.AreEqual(packer.PackedTexture.Width, 16 + 32 + 48 + 64 + 80);
Assert.AreEqual(packer.PackedTexture.Height, 64);
}
@ -48,7 +45,7 @@ public class TexturePackerTests {
using (var packer = new RuntimeTexturePacker(8192)) {
for (var i = 1; i <= 1000; i++)
packer.Add(new TextureRegion(this.testTexture, 0, 0, i % 239, i % 673), packed.Add);
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
}
foreach (var r1 in packed) {
@ -65,7 +62,7 @@ public class TexturePackerTests {
using var packer = new RuntimeTexturePacker(128, disposeTextures: true);
packer.Add(new TextureRegion(this.disposedTestTexture), TexturePackerTests.StubResult);
packer.Add(new TextureRegion(this.disposedTestTexture, 0, 0, 8, 8), TexturePackerTests.StubResult);
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
Assert.True(this.disposedTestTexture.IsDisposed);
Assert.False(packer.PackedTexture.IsDisposed);
}
@ -83,19 +80,19 @@ public class TexturePackerTests {
Assert.DoesNotThrow(() => {
packer2.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), TexturePackerTests.StubResult);
});
packer2.Pack(this.game.GraphicsDevice);
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), TexturePackerTests.StubResult);
packer3.Pack(this.game.GraphicsDevice);
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), TexturePackerTests.StubResult);
packer4.Pack(this.game.GraphicsDevice);
packer4.Pack(this.Game.GraphicsDevice);
Assert.AreEqual(170, packer4.PackedTexture.Width);
Assert.AreEqual(170, packer4.PackedTexture.Height);
}
@ -108,17 +105,17 @@ public class TexturePackerTests {
var results = 0;
for (var i = 0; i < 10; i++)
packer.Add(new TextureRegion(this.testTexture, 0, 0, 64, 64), _ => results++);
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
Assert.AreEqual(10, results);
// pack without resizing
packer.Add(new TextureRegion(this.testTexture, 0, 0, 0, 0), _ => results++);
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
Assert.AreEqual(11, results);
// pack and force a resize
packer.Add(new TextureRegion(this.testTexture, 0, 0, 64, 64), _ => results++);
packer.Pack(this.game.GraphicsDevice);
packer.Pack(this.Game.GraphicsDevice);
// all callbacks are called again, so we add 11 again, as well as the callback we just added
Assert.AreEqual(2 * 11 + 1, results);
}
@ -132,8 +129,8 @@ public class TexturePackerTests {
sameSizePacker.Add(new TextureRegion(this.testTexture, 0, 0, 10, 10), TexturePackerTests.StubResult);
diffSizePacker.Add(new TextureRegion(this.testTexture, 0, 0, 10 + i % 129, 10 * (i % 5 + 1)), TexturePackerTests.StubResult);
}
sameSizePacker.Pack(this.game.GraphicsDevice);
diffSizePacker.Pack(this.game.GraphicsDevice);
sameSizePacker.Pack(this.Game.GraphicsDevice);
diffSizePacker.Pack(this.Game.GraphicsDevice);
TestContext.WriteLine($"""
{total} regions,

View file

@ -10,19 +10,7 @@ using NUnit.Framework;
namespace Tests;
public class UiTests {
private TestGame game;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
}
public class UiTests : GameTestFixture {
[Test]
public void TestInvalidPanel() {
@ -126,7 +114,7 @@ public class UiTests {
group = group.AddChild(new Group(Anchor.TopLeft, Vector2.One));
this.AddAndUpdate(main, out var addTime, out var updateTime);
var allChildren = main.GetChildren(regardGrandchildren: true);
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.game.UiSystem.Metrics}");
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.Game.UiSystem.Metrics}");
}
}
@ -138,7 +126,7 @@ public class UiTests {
main.AddChild(new Group(Anchor.AutoInlineIgnoreOverflow, new Vector2(1F / i, 1)));
this.AddAndUpdate(main, out var addTime, out var updateTime);
var allChildren = main.GetChildren(regardGrandchildren: true);
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.game.UiSystem.Metrics}");
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.Game.UiSystem.Metrics}");
}
}
@ -155,17 +143,17 @@ public class UiTests {
}
this.AddAndUpdate(main, out var addTime, out var updateTime);
var allChildren = main.GetChildren(regardGrandchildren: true);
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.game.UiSystem.Metrics}");
TestContext.WriteLine($"{allChildren.Count()} children, took {addTime.TotalMilliseconds * 1000000}ns to add, {updateTime.TotalMilliseconds * 1000000}ns to update, metrics {this.Game.UiSystem.Metrics}");
}
}
private void AddAndUpdate(Element element, out TimeSpan addTime, out TimeSpan updateTime) {
foreach (var root in this.game.UiSystem.GetRootElements())
this.game.UiSystem.Remove(root.Name);
this.game.UiSystem.Metrics.ResetUpdates();
foreach (var root in this.Game.UiSystem.GetRootElements())
this.Game.UiSystem.Remove(root.Name);
this.Game.UiSystem.Metrics.ResetUpdates();
var stopwatch = Stopwatch.StartNew();
this.game.UiSystem.Add("Test", element);
this.Game.UiSystem.Add("Test", element);
stopwatch.Stop();
addTime = stopwatch.Elapsed;