Add core tests, fix found issues

This commit is contained in:
Matt Nadareski
2020-12-18 14:22:56 -08:00
parent 80d6517f3c
commit e6f2e70263
4 changed files with 137 additions and 3 deletions

View File

@@ -124,6 +124,10 @@ namespace SabreTools.Core.Tools
/// <returns>Subfolder path for the given hash</returns>
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
/// <returns>True if the extension is valid, false otherwise</returns>
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)

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -1,11 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU;x64</Platforms>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />