From f5e64eb7195a0ef2ac75d42825ee5a7dbb400a58 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 26 Oct 2024 14:41:36 +0200 Subject: [PATCH] clean up structure of tests that use TestGame --- Tests/CameraTests.cs | 16 +------- Tests/DataTextureAtlasTests.cs | 7 ++-- Tests/FontTests.cs | 67 ++++++++++++++-------------------- Tests/TestGame.cs | 18 +++++++++ Tests/TexturePackerTests.cs | 31 +++++++--------- Tests/UiTests.cs | 28 ++++---------- 6 files changed, 72 insertions(+), 95 deletions(-) diff --git a/Tests/CameraTests.cs b/Tests/CameraTests.cs index 1e81ad9..c8ca433 100644 --- a/Tests/CameraTests.cs +++ b/Tests/CameraTests.cs @@ -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); diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 9a6437c..fd9b6e9 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -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 diff --git a/Tests/FontTests.cs b/Tests/FontTests.cs index 72b171f..bb4ebb2 100644 --- a/Tests/FontTests.cs +++ b/Tests/FontTests.cs @@ -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' source code for more in-depth explanations of their functionality or the website 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_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"); - 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 content that will be split.\nNow this is a new line with additional regular content 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(""), (_, _, _) => ""); - this.formatter.Macros.Add(new Regex(""), (_, _, _) => "blue"); - this.formatter.Macros.Add(new Regex(""), (_, _, _) => ""); + this.Formatter.Macros.Add(new Regex(""), (_, _, _) => ""); + this.Formatter.Macros.Add(new Regex(""), (_, _, _) => "blue"); + this.Formatter.Macros.Add(new Regex(""), (_, _, _) => ""); 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(this.formatter.ResolveMacros(strg), goal); + Assert.AreEqual(this.Formatter.ResolveMacros(strg), goal); // test recursive macros - this.formatter.Macros.Add(new Regex(""), (_, _, _) => ""); - this.formatter.Macros.Add(new Regex(""), (_, _, _) => ""); - Assert.Throws(() => this.formatter.ResolveMacros("Test string")); + this.Formatter.Macros.Add(new Regex(""), (_, _, _) => ""); + this.Formatter.Macros.Add(new Regex(""), (_, _, _) => ""); + Assert.Throws(() => this.Formatter.ResolveMacros("Test 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 = "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 = 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 test string with a lot ofcontent and an invalid code as well<ü>."); + var stripped = this.Formatter.StripAllFormatting("This is a test string with a lot ofcontent and an invalid code as well<ü>."); Assert.AreEqual("This is a test string with a lot ofcontent and an 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())); } } diff --git a/Tests/TestGame.cs b/Tests/TestGame.cs index 7470135..cd83c09 100644 --- a/Tests/TestGame.cs +++ b/Tests/TestGame.cs @@ -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; + } + +} diff --git a/Tests/TexturePackerTests.cs b/Tests/TexturePackerTests.cs index e7204bf..e95cea7 100644 --- a/Tests/TexturePackerTests.cs +++ b/Tests/TexturePackerTests.cs @@ -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, diff --git a/Tests/UiTests.cs b/Tests/UiTests.cs index 11b65f6..2bc1373 100644 --- a/Tests/UiTests.cs +++ b/Tests/UiTests.cs @@ -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;