Skip to content

Commit

Permalink
Merge pull request UnderminersTeam#1515 from Miepee/unit-test
Browse files Browse the repository at this point in the history
Add proper unit testing project
  • Loading branch information
colinator27 authored Dec 21, 2023
2 parents 2fd7fd0 + b9adf71 commit 076d857
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 1 deletion.
2 changes: 2 additions & 0 deletions UndertaleModLib/Models/UndertaleEmbeddedAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class UndertaleEmbeddedAudio : UndertaleNamedResource, PaddedObject, IDis
/// <summary>
/// The name of the embedded audio entry.
/// </summary>
/// <remarks>This is an UTMT only attribute. GameMaker does not store names for them.</remarks>
public UndertaleString Name { get; set; }

/// <summary>
Expand Down Expand Up @@ -54,6 +55,7 @@ public override string ToString()
{
try
{
// TODO: Does only the GUI set this?
return Name.Content + " (" + GetType().Name + ")";
}
catch
Expand Down
6 changes: 5 additions & 1 deletion UndertaleModLib/Models/UndertaleString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void Dispose()
/// <returns>A string that represents the current object.</returns>
public string ToString(bool isGMS2)
{
// TODO: someone clean this up please. this seems insane for a tostring method.
if (Content == null)
return "\"null\""; // NPE Fix.

Expand Down Expand Up @@ -102,16 +103,19 @@ public string ToString(bool isGMS2)
/// <inheritdoc />
public bool SearchMatches(string filter)
{
// TODO: should this throw instead?
if (filter is null) return false;
return Content?.ToLower().Contains(filter.ToLower()) ?? false;
}

/// <summary>
/// Unescapes text for the assembler.
/// </summary>
/// <param name="text">The text to unescape.</param>
/// <returns>A string with <c>\n</c>, <c>\r</c>, <c>"</c> and <c>\</c> being properly escaped.</returns>
/// <returns>A string which features the <b>text</b> <c>\n</c>, <c>\r</c>, <c>"</c> and <c>\</c> being properly unescaped.</returns>
public static string UnescapeText(string text)
{
// TODO: optimize this? seems like a very whacky thing to do... why do they have escaped text in the first place?
return text.Replace("\\r", "\r").Replace("\\n", "\n").Replace("\\\"", "\"").Replace("\\\\", "\\");
}
}
46 changes: 46 additions & 0 deletions UndertaleModLibTests/Models/EmbeddedAudioTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using UndertaleModLib;
using UndertaleModLib.Models;
using Xunit;
using Xunit.Extensions;

namespace UndertaleModLibTests.Models;

public class EmbeddedAudioTest
{
[Theory]
[InlineData(new byte[]
{
4, 0, 0, 0,
252, 253, 254, 255,
})]

public void TestUnserialize(byte[] data)
{
using var stream = new MemoryStream(data);
var reader = new UndertaleReader(stream);
var embeddedAudio = new UndertaleEmbeddedAudio();

embeddedAudio.Unserialize(reader);

Assert.True(embeddedAudio.Data.Length == BitConverter.ToInt32(data[..4]));
Assert.Equal(embeddedAudio.Data, data[4..]);
}

[Fact]
public void TestSerialize()
{
using var stream = new MemoryStream();
var fullData = new byte[] { 4, 0, 0, 0, 252, 253, 254, 255 };
UndertaleEmbeddedAudio audio = new UndertaleEmbeddedAudio()
{
Name = new UndertaleString("foobar"),
Data = fullData[4..]
};
var writer = new UndertaleWriter(stream);

audio.Serialize(writer);

Assert.True(stream.Length == fullData.Length);
Assert.Equal(stream.ToArray(), fullData);
}
}
76 changes: 76 additions & 0 deletions UndertaleModLibTests/Models/StringTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using UndertaleModLib;
using UndertaleModLib.Models;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace UndertaleModLibTests.Models;

public class StringTest
{
[Theory]
[InlineData("", "")]
[InlineData("\n", "\n")]
[InlineData("\n\r\n\r\n", "\n\r\n\r\n")]
[InlineData("T\nh\ri\"s\\", "T\nh\ri\"s\\")]
[InlineData(@"\n", "\n")]
[InlineData(@"\n\r\n\r\n", "\n\r\n\r\n")]
[InlineData(@"T\nh\ri\""s\\", "T\nh\ri\"s\\")]
public void TestUnescapeText(string text, string expected)
{
var result = UndertaleString.UnescapeText(text);

Assert.Equal(result, expected);
}

[Theory]
[InlineData("", "", true)]
[InlineData(" ", "", true)]
[InlineData("HELLO", "hello", true)]
[InlineData("hello", "HELLO", true)]
[InlineData("HEllO", "heLLo", true)]
[InlineData("hello", "world", false)]
[InlineData("world", "hello", false)]
[InlineData(null, null, false)]
[InlineData("hi", null, false)]
[InlineData(null, "null", false)]
public void TestSearchMatches(string text, string substring, bool expected)
{
UndertaleString utString = new UndertaleString(text);

var result = utString.SearchMatches(substring);

Assert.Equal(result, expected);
}

[Theory]
[InlineData("I rule!", new byte[] {7, 0, 0, 0, 73, 32, 114, 117, 108, 101, 33, 0})]
[InlineData("{]öÄü¢€ΩФЙw", new byte[] {20, 0, 0, 0, 123, 93, 195, 182, 195, 132, 195, 188, 194, 162, 226, 130, 172, 206, 169, 208, 164, 208, 153, 119, 0})]
[InlineData("\ud83d\udc38\ud83d\ude03\ud83e\uddd1\ud83c\udffc\u200d\ud83d\ude80\ud83c\udff4\u200d\u2620\ufe0f", new byte[] {36, 0, 0, 0, 240, 159, 144, 184, 240, 159, 152, 131, 240, 159, 167, 145, 240, 159, 143, 188, 226, 128, 141, 240, 159, 154, 128, 240, 159, 143, 180, 226, 128, 141, 226, 152, 160, 239, 184, 143, 0})]
public void TestSerialize(string input, byte[] expected)
{
using var stream = new MemoryStream();
UndertaleString utString = new UndertaleString(input);
var writer = new UndertaleWriter(stream);

utString.Serialize(writer);

Assert.Equal(stream.ToArray(), expected);
}

[Theory]
[InlineData(new byte[] {7, 0, 0, 0, 73, 32, 114, 117, 108, 101, 33, 0}, "I rule!")]
[InlineData(new byte[] {20, 0, 0, 0, 123, 93, 195, 182, 195, 132, 195, 188, 194, 162, 226, 130, 172, 206, 169, 208, 164, 208, 153, 119, 0}, "{]öÄü¢€ΩФЙw")]
[InlineData(new byte[] {36, 0, 0, 0, 240, 159, 144, 184, 240, 159, 152, 131, 240, 159, 167, 145, 240, 159, 143, 188, 226, 128, 141, 240, 159, 154, 128, 240, 159, 143, 180, 226, 128, 141, 226, 152, 160, 239, 184, 143, 0}, "\ud83d\udc38\ud83d\ude03\ud83e\uddd1\ud83c\udffc\u200d\ud83d\ude80\ud83c\udff4\u200d\u2620\ufe0f")]

public void TestUnserialize(byte[] input, string expected)
{
using var stream = new MemoryStream(input);
var reader = new UndertaleReader(stream);
var utString = new UndertaleString();

utString.Unserialize(reader);

Assert.Equal(utString.Content, expected);
}
}
28 changes: 28 additions & 0 deletions UndertaleModLibTests/UndertaleModLibTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UndertaleModLib\UndertaleModLib.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions UndertaleModTool.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UndertaleModCli", "Undertal
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UndertaleModToolUpdater", "UndertaleModToolUpdater\UndertaleModToolUpdater.csproj", "{1E004B65-F3C8-4A62-B649-1465EA6A3B01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UndertaleModLibTests", "UndertaleModLibTests\UndertaleModLibTests.csproj", "{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -61,6 +63,14 @@ Global
{1E004B65-F3C8-4A62-B649-1465EA6A3B01}.Release|Any CPU.Build.0 = Release|Any CPU
{1E004B65-F3C8-4A62-B649-1465EA6A3B01}.Release|x64.ActiveCfg = Release|Any CPU
{1E004B65-F3C8-4A62-B649-1465EA6A3B01}.Release|x64.Build.0 = Release|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Debug|x64.ActiveCfg = Debug|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Debug|x64.Build.0 = Debug|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Release|Any CPU.Build.0 = Release|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Release|x64.ActiveCfg = Release|Any CPU
{AAE3D4CA-0B6C-4928-AE86-066F5251F70A}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 076d857

Please sign in to comment.