Skip to content

Commit

Permalink
Add tests for parsing achievement tags in chat
Browse files Browse the repository at this point in the history
These tests ensure that `ChatManager.ParseMessage` will not throw on the
server with TShock when given an achievement tag.
  • Loading branch information
drunderscore authored and hakusaro committed Dec 6, 2022
1 parent d0554ab commit 205c29b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
30 changes: 30 additions & 0 deletions TShockLauncher.Tests/ChatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Xna.Framework;
using NUnit.Framework;
using Terraria.UI.Chat;

namespace TShockLauncher.Tests;

public class ChatTests
{
/// <summary>
/// Ensures that the <see cref="Terraria.GameContent.UI.Chat.AchievementTagHandler"/> does not cause exceptions when used on the server.
/// </summary>
///
/// <remarks>The behaviour of TShock regarding the achievement tag handler changes depending on if TShock has
/// a <see cref="Terraria.Main"/> instance or not. Therefore, we do not check the correctness of the parsed message, but only if it
/// throws an exception.
/// </remarks>
[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);
}
}
3 changes: 3 additions & 0 deletions TShockLauncher.Tests/TestSetup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using Terraria;
using Terraria.Initializers;
using Terraria.Localization;
using TShockAPI;

Expand All @@ -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
Expand Down

0 comments on commit 205c29b

Please sign in to comment.