mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Start adding DatItem tests, fix corner cases
This commit is contained in:
@@ -544,7 +544,7 @@ namespace SabreTools.DatItems
|
|||||||
if (file == null)
|
if (file == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If we don't have a Dis, Media, or Rom, we skip checking for duplicates
|
// If we don't have a Disk, Media, or Rom, we skip checking for duplicates
|
||||||
if (file.ItemType != ItemType.Disk && file.ItemType != ItemType.Media && file.ItemType != ItemType.Rom)
|
if (file.ItemType != ItemType.Disk && file.ItemType != ItemType.Media && file.ItemType != ItemType.Rom)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,14 @@ namespace SabreTools.FileTypes.Aaru
|
|||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Empty constructor
|
||||||
|
/// </summary>
|
||||||
|
public AaruFormat()
|
||||||
|
{
|
||||||
|
Type = FileType.AaruFormat;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new AaruFormat from an input file
|
/// Create a new AaruFormat from an input file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -23,6 +23,14 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Empty constructor
|
||||||
|
/// </summary>
|
||||||
|
public CHDFile()
|
||||||
|
{
|
||||||
|
Type = FileType.CHD;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new CHDFile from an input file
|
/// Create a new CHDFile from an input file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD V1 File
|
/// CHD V1 File
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CHDFileV1 : CHDFile
|
public class CHDFileV1 : CHDFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD flags
|
/// CHD flags
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD V2 File
|
/// CHD V2 File
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CHDFileV2 : CHDFile
|
public class CHDFileV2 : CHDFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD flags
|
/// CHD flags
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD V3 File
|
/// CHD V3 File
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CHDFileV3 : CHDFile
|
public class CHDFileV3 : CHDFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD flags
|
/// CHD flags
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD V4 File
|
/// CHD V4 File
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CHDFileV4 : CHDFile
|
public class CHDFileV4 : CHDFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD flags
|
/// CHD flags
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// CHD V5 File
|
/// CHD V5 File
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CHDFileV5 : CHDFile
|
public class CHDFileV5 : CHDFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Uncompressed map format
|
/// Uncompressed map format
|
||||||
|
|||||||
214
SabreTools.Test/DatItems/DatItemTests.cs
Normal file
214
SabreTools.Test/DatItems/DatItemTests.cs
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
using SabreTools.Core;
|
||||||
|
using SabreTools.DatItems;
|
||||||
|
using SabreTools.FileTypes;
|
||||||
|
using SabreTools.FileTypes.Aaru;
|
||||||
|
using SabreTools.FileTypes.Archives;
|
||||||
|
using SabreTools.FileTypes.CHD;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace SabreTools.Test.DatItems
|
||||||
|
{
|
||||||
|
public class DatItemTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, ItemType.Rom)]
|
||||||
|
[InlineData(ItemType.Disk, ItemType.Disk)]
|
||||||
|
[InlineData(ItemType.Media, ItemType.Media)]
|
||||||
|
[InlineData(ItemType.Rom, ItemType.Rom)]
|
||||||
|
public void CreateItemTypeTest(ItemType? itemType, ItemType expected)
|
||||||
|
{
|
||||||
|
var actual = DatItem.Create(itemType);
|
||||||
|
Assert.Equal(expected, actual.ItemType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(FileType.None, ItemType.Rom)]
|
||||||
|
[InlineData(FileType.Folder, null)]
|
||||||
|
[InlineData(FileType.AaruFormat, ItemType.Media)]
|
||||||
|
[InlineData(FileType.CHD, ItemType.Disk)]
|
||||||
|
[InlineData(FileType.ZipArchive, ItemType.Rom)]
|
||||||
|
public void CreateBaseFileTest(FileType fileType, ItemType? expected)
|
||||||
|
{
|
||||||
|
var baseFile = CreateBaseFile(fileType);
|
||||||
|
var actual = DatItem.Create(baseFile);
|
||||||
|
Assert.Equal(expected, actual?.ItemType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DuplicateStatusUnequalTest()
|
||||||
|
{
|
||||||
|
var rom = new Rom();
|
||||||
|
var disk = new Disk();
|
||||||
|
var actual = rom.GetDuplicateStatus(disk);
|
||||||
|
Assert.Equal((DupeType)0x00, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DuplicateStatusExternalAllTest()
|
||||||
|
{
|
||||||
|
var romA = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var romB = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var actual = romA.GetDuplicateStatus(romB);
|
||||||
|
Assert.Equal(DupeType.External | DupeType.All, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DuplicateStatusExternalHashTest()
|
||||||
|
{
|
||||||
|
var romA = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var romB = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "not-name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var actual = romA.GetDuplicateStatus(romB);
|
||||||
|
Assert.Equal(DupeType.External | DupeType.Hash, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DuplicateStatusInternalAllTest()
|
||||||
|
{
|
||||||
|
var romA = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var romB = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var actual = romA.GetDuplicateStatus(romB);
|
||||||
|
Assert.Equal(DupeType.Internal | DupeType.All, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DuplicateStatusInternalHashTest()
|
||||||
|
{
|
||||||
|
var romA = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var romB = new Rom
|
||||||
|
{
|
||||||
|
Name = "same-name",
|
||||||
|
CRC = "DEADBEEF",
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = "not-name-same",
|
||||||
|
},
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var actual = romA.GetDuplicateStatus(romB);
|
||||||
|
Assert.Equal(DupeType.Internal | DupeType.Hash, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, null, true)]
|
||||||
|
[InlineData(null, new byte[0], true)]
|
||||||
|
[InlineData(new byte[0], null, true)]
|
||||||
|
[InlineData(new byte[] { 0x00 }, new byte[] { 0x00, 0x01 }, false)]
|
||||||
|
[InlineData(new byte[] { 0x00 }, new byte[] { 0x01 }, false)]
|
||||||
|
[InlineData(new byte[] { 0x00 }, new byte[] { 0x00 }, true)]
|
||||||
|
public void ConditionalHashEqualsTest(byte[] first, byte[] second, bool expected)
|
||||||
|
{
|
||||||
|
bool actual = DatItem.ConditionalHashEquals(first, second);
|
||||||
|
Assert.Equal(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Add tests for DatItem.Merge
|
||||||
|
// TODO: Add tests for ResolveNames
|
||||||
|
// TODO: Add tests for Sort
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a BaseFile for testing
|
||||||
|
/// </summary>
|
||||||
|
private BaseFile CreateBaseFile(FileType fileType)
|
||||||
|
{
|
||||||
|
return fileType switch
|
||||||
|
{
|
||||||
|
FileType.Folder => new Folder(),
|
||||||
|
FileType.AaruFormat => new AaruFormat(),
|
||||||
|
FileType.CHD => new CHDFileV5(),
|
||||||
|
FileType.ZipArchive => new ZipArchive(),
|
||||||
|
_ => new BaseFile(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
|
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
|
||||||
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
|
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
|
||||||
|
<ProjectReference Include="..\SabreTools.FileTypes\SabreTools.FileTypes.csproj" />
|
||||||
<ProjectReference Include="..\SabreTools.Filtering\SabreTools.Filtering.csproj" />
|
<ProjectReference Include="..\SabreTools.Filtering\SabreTools.Filtering.csproj" />
|
||||||
<ProjectReference Include="..\SabreTools.IO\SabreTools.IO.csproj" />
|
<ProjectReference Include="..\SabreTools.IO\SabreTools.IO.csproj" />
|
||||||
<ProjectReference Include="..\SabreTools.Skippers\SabreTools.Skippers.csproj" />
|
<ProjectReference Include="..\SabreTools.Skippers\SabreTools.Skippers.csproj" />
|
||||||
|
|||||||
Reference in New Issue
Block a user