Rename FileTester to Tools and allow testing of single Streams.

This commit is contained in:
Ray Koopa 2020-06-28 19:00:57 +02:00
parent 66d4d15305
commit 10008eaf6e
15 changed files with 55 additions and 40 deletions

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Armageddon.ProjectX
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadLibraries() => FileTester.Run<Library>(Game.Armageddon, "*.pxl"); public void LoadLibraries() => Tools.TestFiles<Library>(Game.Armageddon, "*.pxl");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Armageddon.ProjectX
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadSchemes() => FileTester.Run<Scheme>(Game.Armageddon, "*.pxs"); public void LoadSchemes() => Tools.TestFiles<Scheme>(Game.Armageddon, "*.pxs");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Armageddon
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadGeneratedMaps() => FileTester.Run<GeneratedMap>(Game.Armageddon | Game.WorldParty, "*.lev"); public void LoadGeneratedMaps() => Tools.TestFiles<GeneratedMap>(Game.Armageddon | Game.WorldParty, "*.lev");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Armageddon
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadLandData() => FileTester.Run<LandData>(Game.Armageddon, "land.dat"); public void LoadLandData() => Tools.TestFiles<LandData>(Game.Armageddon, "land.dat");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Armageddon
/// Tests all files found in the test directory. /// Tests all files found in the test directory.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadTeamContainers() => FileTester.Run<TeamContainer>(Game.Test, "*.wgt"); public void LoadTeamContainers() => Tools.TestFiles<TeamContainer>(Game.Test, "*.wgt");
} }
} }

View File

@ -15,6 +15,6 @@ namespace Syroot.Worms.Test
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadArchives() => FileTester.Run<Archive>(Game.Team17, "*.dir"); public void LoadArchives() => Tools.TestFiles<Archive>(Game.Team17, "*.dir");
} }
} }

View File

@ -11,7 +11,7 @@ namespace Syroot.Worms.Test.Core
/// <summary> /// <summary>
/// Represents methods helping in executing file-based tests. /// Represents methods helping in executing file-based tests.
/// </summary> /// </summary>
public static class FileTester public static class Tools
{ {
// ---- CONSTANTS ---------------------------------------------------------------------------------------------- // ---- CONSTANTS ----------------------------------------------------------------------------------------------
@ -27,6 +27,40 @@ namespace Syroot.Worms.Test.Core
// ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- // ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
/// <summary>
/// Loads, saves, reloads, and compares the data in the given <paramref name="stream"/>, as long as each
/// operation is supported.
/// </summary>
/// <typeparam name="T">The type of the data to load.</typeparam>
/// <param name="name">Name logged to output.</param>
/// <param name="stream"><see cref="Stream"/> storing the raw data to load.</param>
public static void TestStream<T>(string name, Stream stream) where T : ILoadable, new()
{
Debug.Write($"\"{name}\" load");
T instance = new T();
instance.Load(stream);
if (instance is ISaveable saveable)
{
Debug.Write($" save");
using MemoryStream newStream = new MemoryStream();
saveable.Save(newStream);
Debug.Write($" reload");
newStream.Position = 0;
T newInstance = new T();
newInstance.Load(newStream);
if (instance is IEquatable<T> equatable)
{
Debug.Write($" compare");
Assert.IsTrue(((IEquatable<T>)newInstance).Equals(equatable));
}
}
Debug.WriteLine($" OK");
}
/// <summary> /// <summary>
/// Loads, saves, reloads, and compares the files found with the given <paramref name="wildcard"/>, as long as /// Loads, saves, reloads, and compares the files found with the given <paramref name="wildcard"/>, as long as
/// each operation is supported. Excludes file names specified in the optional array /// each operation is supported. Excludes file names specified in the optional array
@ -36,39 +70,20 @@ namespace Syroot.Worms.Test.Core
/// <param name="games">The games to test.</param> /// <param name="games">The games to test.</param>
/// <param name="wildcard">The wildcard to match.</param> /// <param name="wildcard">The wildcard to match.</param>
/// <param name="excludedFiles">Optionally, the files to exclude.</param> /// <param name="excludedFiles">Optionally, the files to exclude.</param>
public static void Run<T>(Game games, string wildcard, string[] excludedFiles = null) public static void TestFiles<T>(Game games, string wildcard, string[] excludedFiles = null)
where T : ILoadableFile, new() where T : ILoadable, new()
{ {
foreach (string fileName in FindFiles(games, wildcard)) foreach (string fileName in FindFiles(games, wildcard))
{ {
if (excludedFiles?.Contains(Path.GetFileName(fileName), StringComparer.OrdinalIgnoreCase) == true) if (excludedFiles?.Contains(Path.GetFileName(fileName), StringComparer.OrdinalIgnoreCase) == true)
{ {
Debug.WriteLine($"Skipping {fileName}"); Debug.WriteLine($"\"{fileName}\" skipped");
continue;
} }
else
Debug.Write($"\"{fileName}\" load");
T instance = new T();
instance.Load(fileName);
if (instance is ISaveableFile saveable)
{ {
Debug.Write($" save"); using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
using MemoryStream stream = new MemoryStream(); TestStream<T>(fileName, stream);
saveable.Save(stream);
Debug.Write($" reload");
stream.Position = 0;
T newInstance = new T();
newInstance.Load(stream);
if (instance is IEquatable<T> equatable)
{
Debug.Write($" compare");
Assert.IsTrue(((IEquatable<T>)newInstance).Equals(equatable));
}
} }
Debug.WriteLine($" OK");
} }
} }

View File

@ -15,6 +15,6 @@ namespace Syroot.Worms.Test
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadImages() => FileTester.Run<Img>(Game.All, "*.img"); public void LoadImages() => Tools.TestFiles<Img>(Game.All, "*.img");
} }
} }

View File

@ -17,7 +17,7 @@ namespace Syroot.Worms.Test
[TestMethod] [TestMethod]
public void LoadPalettes() public void LoadPalettes()
{ {
FileTester.Run<RiffPalette>(Game.Team17, "*.pal", new string[] Tools.TestFiles<RiffPalette>(Game.Team17, "*.pal", new string[]
{ {
"wwp.pal", // Contains 4 bytes of trash after the data chunk. "wwp.pal", // Contains 4 bytes of trash after the data chunk.
"wwpmaped.pal" // Contains 4 bytes of trash after the data chunk. "wwpmaped.pal" // Contains 4 bytes of trash after the data chunk.

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.WorldParty
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadLandData() => FileTester.Run<LandData>(Game.WorldParty, "land.dat"); public void LoadLandData() => Tools.TestFiles<LandData>(Game.WorldParty, "land.dat");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.WorldParty
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadTeamContainers() => FileTester.Run<TeamContainer>(Game.WorldParty, "*.wwp"); public void LoadTeamContainers() => Tools.TestFiles<TeamContainer>(Game.WorldParty, "*.wwp");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Worms2
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadLandData() => FileTester.Run<LandData>(Game.Worms2, "land.dat"); public void LoadLandData() => Tools.TestFiles<LandData>(Game.Worms2, "land.dat");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Worms2
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadSchemeOptions() => FileTester.Run<SchemeOptions>(Game.Worms2, "*.opt"); public void LoadSchemeOptions() => Tools.TestFiles<SchemeOptions>(Game.Worms2, "*.opt");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Worms2
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadSchemeWeapons() => FileTester.Run<SchemeWeapons>(Game.Worms2, "*.wep"); public void LoadSchemeWeapons() => Tools.TestFiles<SchemeWeapons>(Game.Worms2, "*.wep");
} }
} }

View File

@ -16,6 +16,6 @@ namespace Syroot.Worms.Test.Worms2
/// Loads all files found in any game directories. /// Loads all files found in any game directories.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void LoadTeamContainers() => FileTester.Run<TeamContainer>(Game.Worms2, "*.st1"); public void LoadTeamContainers() => Tools.TestFiles<TeamContainer>(Game.Worms2, "*.st1");
} }
} }