From e6f2e702639e894ce964a00f0a425b933165f649 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 18 Dec 2020 14:22:56 -0800 Subject: [PATCH] Add core tests, fix found issues --- SabreTools.Core/Tools/Utilities.cs | 10 +++- SabreTools.Test/Core/ConvertersTests.cs | 70 +++++++++++++++++++++++++ SabreTools.Test/Core/UtilitiesTests.cs | 51 ++++++++++++++++++ SabreTools.Test/SabreTools.Test.csproj | 9 +++- 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 SabreTools.Test/Core/ConvertersTests.cs create mode 100644 SabreTools.Test/Core/UtilitiesTests.cs diff --git a/SabreTools.Core/Tools/Utilities.cs b/SabreTools.Core/Tools/Utilities.cs index 01809743..1d8d51e9 100644 --- a/SabreTools.Core/Tools/Utilities.cs +++ b/SabreTools.Core/Tools/Utilities.cs @@ -124,6 +124,10 @@ namespace SabreTools.Core.Tools /// Subfolder path for the given hash public static string GetDepotPath(string hash, int depth) { + // If the hash is null or empty, then we return null + if (string.IsNullOrEmpty(hash)) + return null; + // If the hash isn't the right size, then we return null if (hash.Length != Constants.SHA1Length) return null; @@ -153,8 +157,12 @@ namespace SabreTools.Core.Tools /// True if the extension is valid, false otherwise public static bool HasValidDatExtension(string path) { + // If the path is null or empty, then we return false + if (string.IsNullOrEmpty(path)) + return false; + // Get the extension from the path, if possible - string ext = Path.GetExtension(path).TrimStart('.'); + string ext = Path.GetExtension(path).TrimStart('.').ToLowerInvariant(); // Check against the list of known DAT extensions switch (ext) diff --git a/SabreTools.Test/Core/ConvertersTests.cs b/SabreTools.Test/Core/ConvertersTests.cs new file mode 100644 index 00000000..d9168d49 --- /dev/null +++ b/SabreTools.Test/Core/ConvertersTests.cs @@ -0,0 +1,70 @@ +using SabreTools.Core; +using SabreTools.Core.Tools; +using Xunit; + +namespace SabreTools.Test.Core +{ + public class ConvertersTests + { + [Theory] + [InlineData(null, DatHeaderField.NULL)] + [InlineData("datname", DatHeaderField.NULL)] + [InlineData("dat-datname", DatHeaderField.Name)] + [InlineData("dat.datname", DatHeaderField.Name)] + [InlineData("dat_datname", DatHeaderField.Name)] + [InlineData("dat datname", DatHeaderField.Name)] + [InlineData("datheader-datname", DatHeaderField.Name)] + [InlineData("datheader.datname", DatHeaderField.Name)] + [InlineData("datheader_datname", DatHeaderField.Name)] + [InlineData("datheader datname", DatHeaderField.Name)] + [InlineData("header-datname", DatHeaderField.Name)] + [InlineData("header.datname", DatHeaderField.Name)] + [InlineData("header_datname", DatHeaderField.Name)] + [InlineData("header datname", DatHeaderField.Name)] + [InlineData("DAT.DATNAME", DatHeaderField.Name)] + [InlineData("dAt.DAtnamE", DatHeaderField.Name)] + public void AsDatHeaderFieldTest(string field, DatHeaderField expected) + { + DatHeaderField actual = field.AsDatHeaderField(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, DatItemField.NULL)] + [InlineData("name", DatItemField.NULL)] + [InlineData("item-name", DatItemField.Name)] + [InlineData("item.name", DatItemField.Name)] + [InlineData("item_name", DatItemField.Name)] + [InlineData("item name", DatItemField.Name)] + [InlineData("datitem-name", DatItemField.Name)] + [InlineData("datitem.name", DatItemField.Name)] + [InlineData("datitem_name", DatItemField.Name)] + [InlineData("datitem name", DatItemField.Name)] + [InlineData("ITEM.NAME", DatItemField.Name)] + [InlineData("iTeM.namE", DatItemField.Name)] + public void AsDatItemFieldTest(string field, DatItemField expected) + { + DatItemField actual = field.AsDatItemField(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, MachineField.NULL)] + [InlineData("name", MachineField.NULL)] + [InlineData("game-name", MachineField.Name)] + [InlineData("game.name", MachineField.Name)] + [InlineData("game_name", MachineField.Name)] + [InlineData("game name", MachineField.Name)] + [InlineData("machine-name", MachineField.Name)] + [InlineData("machine.name", MachineField.Name)] + [InlineData("machine_name", MachineField.Name)] + [InlineData("machine name", MachineField.Name)] + [InlineData("GAME.NAME", MachineField.Name)] + [InlineData("gAmE.namE", MachineField.Name)] + public void AsMachineFieldTest(string field, MachineField expected) + { + MachineField actual = field.AsMachineField(); + Assert.Equal(expected, actual); + } + } +} \ No newline at end of file diff --git a/SabreTools.Test/Core/UtilitiesTests.cs b/SabreTools.Test/Core/UtilitiesTests.cs new file mode 100644 index 00000000..1956125f --- /dev/null +++ b/SabreTools.Test/Core/UtilitiesTests.cs @@ -0,0 +1,51 @@ +using SabreTools.Core.Tools; +using Xunit; + +namespace SabreTools.Test.Core +{ + public class UtiltiesTests + { + [Theory] + [InlineData(null, null)] + [InlineData("null", null)] + [InlineData("0b00001", null)] + [InlineData("12345", 12345)] + [InlineData("00000000012345", 12345)] + [InlineData("10h", null)] + [InlineData("0x10", 16)] + [InlineData(" 12345 ", 12345)] + public void CleanLongTest(string input, long? expected) + { + long? actual = Utilities.CleanLong(input); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, 0, null)] + [InlineData(null, 4, null)] + [InlineData("123456", 0, null)] + [InlineData("123456", 4, null)] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + public void GetDepotPathTest(string hash, int depth, string expected) + { + string actual = Utilities.GetDepotPath(hash, depth); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, false)] + [InlineData("", false)] + [InlineData("no-extension", false)] + [InlineData("invalid.ext", false)] + [InlineData("INVALID.EXT", false)] + [InlineData("valid_extension.dat", true)] + [InlineData("valid_extension.DAT", true)] + public void HasValidDatExtensionTest(string path, bool expected) + { + bool actual = Utilities.HasValidDatExtension(path); + Assert.Equal(expected, actual); + } + } +} \ No newline at end of file diff --git a/SabreTools.Test/SabreTools.Test.csproj b/SabreTools.Test/SabreTools.Test.csproj index c0dc1ace..e37fefe1 100644 --- a/SabreTools.Test/SabreTools.Test.csproj +++ b/SabreTools.Test/SabreTools.Test.csproj @@ -1,11 +1,16 @@ - net5.0 - + netcoreapp3.1;net5.0 + Debug;Release + AnyCPU;x64 false + + + +