From 205c29bc8831ae655551c9c02ff8a8bc8c26d82e Mon Sep 17 00:00:00 2001 From: James Puleo Date: Fri, 2 Dec 2022 17:33:07 -0500 Subject: [PATCH] Add tests for parsing achievement tags in chat These tests ensure that `ChatManager.ParseMessage` will not throw on the server with TShock when given an achievement tag. --- TShockLauncher.Tests/ChatTests.cs | 30 ++++++++++++++++++++++++++++++ TShockLauncher.Tests/TestSetup.cs | 3 +++ 2 files changed, 33 insertions(+) create mode 100644 TShockLauncher.Tests/ChatTests.cs diff --git a/TShockLauncher.Tests/ChatTests.cs b/TShockLauncher.Tests/ChatTests.cs new file mode 100644 index 000000000..718698188 --- /dev/null +++ b/TShockLauncher.Tests/ChatTests.cs @@ -0,0 +1,30 @@ +using Microsoft.Xna.Framework; +using NUnit.Framework; +using Terraria.UI.Chat; + +namespace TShockLauncher.Tests; + +public class ChatTests +{ + /// + /// Ensures that the does not cause exceptions when used on the server. + /// + /// + /// The behaviour of TShock regarding the achievement tag handler changes depending on if TShock has + /// a instance or not. Therefore, we do not check the correctness of the parsed message, but only if it + /// throws an exception. + /// + [TestCase] + public void TestChatAchievementTagHandler() + { + Assert.That(() => + { + ChatManager.ParseMessage("No achievement tags", Color.White); + ChatManager.ParseMessage("One achievement tag: [a:KILL_THE_SUN]", Color.White); + ChatManager.ParseMessage("One achievement tag, using the longer variant: [achievement:KILL_THE_SUN]", Color.White); + ChatManager.ParseMessage("Multiple achievement tags: [a:KILL_THE_SUN] and [a:TOPPED_OFF]", Color.White); + ChatManager.ParseMessage("One achievement tag, referring to a non-existent achievement: [a:_THIS_WILL_NEVER_EXIST_]", Color.White); + ChatManager.ParseMessage("Both valid and invalid achievement tags: [a:KILL_THE_SUN] and [a:_THIS_WILL_NEVER_EXIST_]", Color.White); + }, Throws.Nothing); + } +} diff --git a/TShockLauncher.Tests/TestSetup.cs b/TShockLauncher.Tests/TestSetup.cs index 0e4abb429..016f26790 100644 --- a/TShockLauncher.Tests/TestSetup.cs +++ b/TShockLauncher.Tests/TestSetup.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using Terraria; +using Terraria.Initializers; using Terraria.Localization; using TShockAPI; @@ -15,6 +16,8 @@ public class TestSetup [OneTimeSetUp] public static void SetupTShock() { + ChatInitializer.Load(); + Program.SavePath = ""; // 1.4.4.2 staticness introduced this where by default it is null, and any touch to Terraria.Main will use it and cause a crash. LanguageManager.Instance.SetLanguage(GameCulture.DefaultCulture); // TShockAPI.Localization will fail without ActiveCulture set Lang.InitializeLegacyLocalization(); // TShockAPI.Localization will fail without preparing NPC names etc