Skip to content

Commit

Permalink
Start of test fixing
Browse files Browse the repository at this point in the history
Converted tests to xunit, joined both projects since they both only test the lib anyway. They still fail, but that's to be fixed later. I just wanted start organizing it for now.
  • Loading branch information
luizzeroxis committed Aug 28, 2024
1 parent dc1e209 commit 4ab51a3
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 620 deletions.
54 changes: 54 additions & 0 deletions UndertaleModLib.Tests/EmbeddedAudioTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UndertaleModLib.Models;

namespace UndertaleModLib.Tests
{
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..]);
}

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

audio.Serialize(writer);

Assert.True(stream.Length == data.Length);
Assert.Equal(stream.ToArray(), data);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UndertaleModLib;
//using UndertaleModLib;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Models;

namespace UndertaleModTests
namespace UndertaleModLib.Tests
{
public abstract class GameLoadingTestBase : GameTestBase
{
public GameLoadingTestBase(string path, string md5) : base(path, md5)
{
}

[TestMethod]
[Fact]
public void SaveDataAndCompare()
{
using (MemoryStream ms = new MemoryStream())
Expand All @@ -26,11 +24,11 @@ public void SaveDataAndCompare()

ms.Position = 0;
string writtenMD5 = GenerateMD5(ms);
Assert.AreEqual(expectedMD5, writtenMD5, "Written file doesn't match read file");
Assert.True(expectedMD5 == writtenMD5, "Written file doesn't match read file");
}
}

[TestMethod]
[Fact]
public void DecompileAllScripts()
{
GlobalDecompileContext context = new GlobalDecompileContext(data, true);
Expand All @@ -39,7 +37,7 @@ public void DecompileAllScripts()
//Console.WriteLine(code.Name.Content);
try
{
Decompiler.Decompile(code, context);
Decompiler.Decompiler.Decompile(code, context);
}
catch (Exception e)
{
Expand All @@ -48,15 +46,15 @@ public void DecompileAllScripts()
});
}

[TestMethod]
[Fact]
public void DisassembleAndReassembleAllScripts()
{
Parallel.ForEach(data.Code, (code) =>
{
//Console.WriteLine(code.Name.Content);

bool knownBug = false;
foreach(var instr in code.Instructions)
foreach (var instr in code.Instructions)
{
if (instr.Value?.GetType() == typeof(UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>))
{
Expand All @@ -82,72 +80,74 @@ public void DisassembleAndReassembleAllScripts()
}

IList<UndertaleInstruction> reasm = Assembler.Assemble(disasm, data.Functions, data.Variables, data.Strings);
Assert.AreEqual(code.Instructions.Count, reasm.Count, "Reassembled instruction count didn't match the disassembly for script " + code.Name.Content);
for(int i = 0; i < code.Instructions.Count; i++)

Assert.True(code.Instructions.Count == reasm.Count, "Reassembled instruction count didn't match the disassembly for script " + code.Name.Content);

for (int i = 0; i < code.Instructions.Count; i++)
{
string errMsg = "Instruction at " + code.Instructions[i].Address.ToString("D5") + " didn't match for script: " + code.Name.Content;
Assert.AreEqual(code.Instructions[i].Kind, reasm[i].Kind, errMsg);
Assert.AreEqual(code.Instructions[i].ComparisonKind, reasm[i].ComparisonKind, errMsg);
Assert.AreEqual(code.Instructions[i].Type1, reasm[i].Type1, errMsg);
Assert.AreEqual(code.Instructions[i].Type2, reasm[i].Type2, errMsg);
Assert.AreEqual(code.Instructions[i].TypeInst, reasm[i].TypeInst, errMsg);
Assert.AreEqual(code.Instructions[i].Extra, reasm[i].Extra, errMsg);
Assert.AreEqual(code.Instructions[i].SwapExtra, reasm[i].SwapExtra, errMsg);
Assert.AreEqual(code.Instructions[i].ArgumentsCount, reasm[i].ArgumentsCount, errMsg);
Assert.AreEqual(code.Instructions[i].JumpOffsetPopenvExitMagic, reasm[i].JumpOffsetPopenvExitMagic, errMsg);

Assert.True(code.Instructions[i].Kind == reasm[i].Kind, errMsg);
Assert.True(code.Instructions[i].ComparisonKind == reasm[i].ComparisonKind, errMsg);
Assert.True(code.Instructions[i].Type1 == reasm[i].Type1, errMsg);
Assert.True(code.Instructions[i].Type2 == reasm[i].Type2, errMsg);
Assert.True(code.Instructions[i].TypeInst == reasm[i].TypeInst, errMsg);
Assert.True(code.Instructions[i].Extra == reasm[i].Extra, errMsg);
Assert.True(code.Instructions[i].SwapExtra == reasm[i].SwapExtra, errMsg);
Assert.True(code.Instructions[i].ArgumentsCount == reasm[i].ArgumentsCount, errMsg);
Assert.True(code.Instructions[i].JumpOffsetPopenvExitMagic == reasm[i].JumpOffsetPopenvExitMagic, errMsg);

if (!code.Instructions[i].JumpOffsetPopenvExitMagic)
Assert.AreEqual(code.Instructions[i].JumpOffset, reasm[i].JumpOffset, errMsg); // note: also handles IntArgument implicitly
Assert.AreSame(code.Instructions[i].Destination?.Target, reasm[i].Destination?.Target, errMsg);
Assert.AreEqual(code.Instructions[i].Destination?.Type, reasm[i].Destination?.Type, errMsg);
Assert.AreSame(code.Instructions[i].Function?.Target, reasm[i].Function?.Target, errMsg);
Assert.AreEqual(code.Instructions[i].Function?.Type, reasm[i].Function?.Type, errMsg);
Assert.True(code.Instructions[i].JumpOffset == reasm[i].JumpOffset, errMsg); // note: also handles IntArgument implicitly

Assert.True(Object.ReferenceEquals(code.Instructions[i].Destination?.Target, reasm[i].Destination?.Target), errMsg);
Assert.True(code.Instructions[i].Destination?.Type == reasm[i].Destination?.Type, errMsg);
Assert.True(Object.ReferenceEquals(code.Instructions[i].Function?.Target, reasm[i].Function?.Target), errMsg);
Assert.True(code.Instructions[i].Function?.Type == reasm[i].Function?.Type, errMsg);

Assert.True(Object.ReferenceEquals(code.Instructions[i].Value?.GetType(), reasm[i].Value?.GetType()), errMsg);

Assert.AreEqual(code.Instructions[i].Value?.GetType(), reasm[i].Value?.GetType(), errMsg);
if (code.Instructions[i].Value?.GetType() == typeof(double))
Assert.AreEqual((double)code.Instructions[i].Value, (double)reasm[i].Value, Math.Abs((double)code.Instructions[i].Value) * (1e-5), errMsg); // see issue #53
Assert.True(Math.Abs((double)code.Instructions[i].Value - (double)reasm[i].Value)
<= (Math.Abs((double)code.Instructions[i].Value) * (1e-5)), errMsg); // see issue #53
else if (code.Instructions[i].Value?.GetType() == typeof(float))
Assert.AreEqual((float)code.Instructions[i].Value, (float)reasm[i].Value, Math.Abs((float)code.Instructions[i].Value) * (1e-5), errMsg); // see issue #53
Assert.True(Math.Abs((float)code.Instructions[i].Value - (float)reasm[i].Value)
<= (Math.Abs((float)code.Instructions[i].Value) * (1e-5)), errMsg); // see issue #53
else if (code.Instructions[i].Value?.GetType() == typeof(UndertaleInstruction.Reference<UndertaleVariable>))
Assert.AreSame(((UndertaleInstruction.Reference<UndertaleVariable>)code.Instructions[i].Value).Target, ((UndertaleInstruction.Reference<UndertaleVariable>)reasm[i].Value).Target, errMsg);
Assert.True(Object.ReferenceEquals(((UndertaleInstruction.Reference<UndertaleVariable>)code.Instructions[i].Value).Target, ((UndertaleInstruction.Reference<UndertaleVariable>)reasm[i].Value).Target), errMsg);
else if (code.Instructions[i].Value?.GetType() == typeof(UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>))
Assert.AreSame(((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)code.Instructions[i].Value).Resource, ((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)reasm[i].Value).Resource, errMsg);
Assert.True(Object.ReferenceEquals(((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)code.Instructions[i].Value).Resource, ((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)reasm[i].Value).Resource), errMsg);
else
Assert.AreEqual(code.Instructions[i].Value, reasm[i].Value, errMsg);
Assert.True(code.Instructions[i].Value == reasm[i].Value, errMsg);
}
});
}
}

[TestClass]
public class UndertaleLoadingTest : GameLoadingTestBase
{
public UndertaleLoadingTest() : base(GamePaths.UNDERTALE_PATH, GamePaths.UNDERTALE_MD5)
{
}
}

[TestClass]
public class UndertaleSwitchLoadingTest : GameLoadingTestBase
{
public UndertaleSwitchLoadingTest() : base(GamePaths.UNDERTALE_SWITCH_PATH, GamePaths.UNDERTALE_SWITCH_MD5)
{
}
}

[TestClass]
public class DeltaruneLoadingTest : GameLoadingTestBase
{
public DeltaruneLoadingTest() : base(GamePaths.DELTARUNE_PATH, GamePaths.DELTARUNE_MD5)
{
}
}



[TestClass]
public class EmptyGameTest
{
[TestMethod]
[Fact]
public void CreateAndSaveEmptyGame()
{
UndertaleData data = UndertaleData.CreateNew();
Expand All @@ -157,5 +157,4 @@ public void CreateAndSaveEmptyGame()
}
}
}

}
20 changes: 20 additions & 0 deletions UndertaleModLib.Tests/GamePaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UndertaleModLib.Tests
{
static class GamePaths
{
// TODO: Maybe these should be configuration variables?
//public static string UNDERTALE_PATH = @"C:\Program Files (x86)\Steam\steamapps\common\Undertale\data.win";
public static string UNDERTALE_PATH = @"D:\SteamLibrary\steamapps\common\Undertale\data.win";
public static string UNDERTALE_MD5 = "5903fc5cb042a728d4ad8ee9e949c6eb";
public static string UNDERTALE_SWITCH_PATH = @"..\..\..\Test\bin\Debug\switch\game.win";
public static string UNDERTALE_SWITCH_MD5 = "427520a97db28c87da4220abb3a334c1";
public static string DELTARUNE_PATH = @"C:\Program Files (x86)\SURVEY_PROGRAM\data.win";
public static string DELTARUNE_MD5 = "a88a2db3a68c714ca2b1ff57ac08a032";
}
}
Loading

0 comments on commit 4ab51a3

Please sign in to comment.