Write SetOneGamePerRegion tests, make fixes

This commit is contained in:
Matt Nadareski
2025-01-14 09:47:40 -05:00
parent a1002fa284
commit e948ce7197
2 changed files with 57 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using SabreTools.DatFiles.Formats; using SabreTools.DatFiles.Formats;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
@@ -142,7 +143,61 @@ namespace SabreTools.DatFiles.Test
#region SetOneGamePerRegion #region SetOneGamePerRegion
// TODO: Implement SetOneGamePerRegion tests [Fact]
public void SetOneGamePerRegion_Items()
{
Machine nowhereMachine = new Machine();
nowhereMachine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine (Nowhere)");
Machine worldMachine = new Machine();
worldMachine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine (World)");
worldMachine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, "machine (Nowhere)");
DatItem nowhereRom = new Rom();
nowhereRom.SetName("rom.bin");
nowhereRom.SetFieldValue(DatItem.MachineKey, nowhereMachine);
DatItem worldRom = new Rom();
worldRom.SetName("rom.nib");
worldRom.SetFieldValue(DatItem.MachineKey, worldMachine);
DatFile datFile = new Logiqx(datFile: null, deprecated: false);
datFile.AddItem(nowhereRom, statsOnly: false);
datFile.AddItem(worldRom, statsOnly: false);
List<string> regions = ["World", "Nowhere"];
datFile.SetOneGamePerRegion(regions);
Assert.Empty(datFile.GetItemsForBucket("machine (nowhere)"));
var actualDatItems = datFile.GetItemsForBucket("machine (world)");
DatItem actualWorldRom = Assert.Single(actualDatItems);
Machine? actualWorldMachine = actualWorldRom.GetFieldValue<Machine>(DatItem.MachineKey);
Assert.NotNull(actualWorldMachine);
Assert.Equal("machine (World)", actualWorldMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
[Fact]
public void SetOneGamePerRegion_ItemsDB()
{
Machine nowhereMachine = new Machine();
nowhereMachine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine (Nowhere)");
Machine worldMachine = new Machine();
worldMachine.SetFieldValue(Models.Metadata.Machine.NameKey, "machine (World)");
worldMachine.SetFieldValue(Models.Metadata.Machine.CloneOfKey, "machine (Nowhere)");
DatFile datFile = new Logiqx(datFile: null, deprecated: false);
_ = datFile.AddMachineDB(nowhereMachine);
_ = datFile.AddMachineDB(worldMachine);
List<string> regions = ["World", "Nowhere"];
datFile.SetOneGamePerRegion(regions);
var actualWorldMachine = Assert.Single(datFile.GetMachinesDB());
Assert.NotNull(actualWorldMachine.Value);
Assert.Equal("machine (World)", actualWorldMachine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
#endregion #endregion

View File

@@ -303,9 +303,6 @@ namespace SabreTools.DatFiles
/// <remarks>Applies to <see cref="ItemsDB"/></remarks> /// <remarks>Applies to <see cref="ItemsDB"/></remarks>
private void SetOneGamePerRegionImplDB(List<string> regionList) private void SetOneGamePerRegionImplDB(List<string> regionList)
{ {
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, DedupeType.None, norename: true);
// Then we want to get a mapping of all machines to parents // Then we want to get a mapping of all machines to parents
Dictionary<string, List<string>> parents = []; Dictionary<string, List<string>> parents = [];
foreach (var machine in GetMachinesDB()) foreach (var machine in GetMachinesDB())
@@ -314,7 +311,7 @@ namespace SabreTools.DatFiles
continue; continue;
// Get machine information // Get machine information
Machine? machineObj = machine.Value.GetFieldValue<Machine>(DatItem.MachineKey); Machine? machineObj = machine.Value;
string? machineName = machineObj?.GetStringFieldValue(Models.Metadata.Machine.NameKey)?.ToLowerInvariant(); string? machineName = machineObj?.GetStringFieldValue(Models.Metadata.Machine.NameKey)?.ToLowerInvariant();
if (machineObj == null || machineName == null) if (machineObj == null || machineName == null)
continue; continue;