Split ApplyRemovals for testability

This commit is contained in:
Matt Nadareski
2025-01-10 21:15:50 -05:00
parent a16a5db197
commit b37787be64
3 changed files with 51 additions and 29 deletions

View File

@@ -2,6 +2,35 @@ namespace SabreTools.DatFiles.Test
{ {
public partial class DatFileTests public partial class DatFileTests
{ {
#region RemoveHeaderFields
// TODO: Write RemoveHeaderFields tests
// - Null header
// - Empty list
// - Full list
#endregion
#region RemoveItemFields
// TODO: Write RemoveItemFields tests
// - Null item dict
// - Both lists empty
// - Machine only
// - Item only
// - Nested
#endregion
#region RemoveItemFieldsDB
// TODO: Write RemoveItemFieldsDB tests
// - Null item dict
// - Both lists empty
// - Machine only
// - Item only
// - Nested
#endregion
} }
} }

View File

@@ -13,27 +13,29 @@ namespace SabreTools.DatFiles
#region Removal #region Removal
/// <summary> /// <summary>
/// Remove fields indicated by the three input lists /// Remove header fields with given values
/// </summary> /// </summary>
public void ApplyRemovals(List<string> headerFieldNames, List<string> machineFieldNames, Dictionary<string, List<string>> itemFieldNames) public void RemoveHeaderFields(List<string> headerFieldNames)
{ {
// Remove DatHeader fields // If we have an invalid input, return
if (headerFieldNames.Count > 0) if (Header == null || headerFieldNames.Count == 0)
RemoveHeaderFields(headerFieldNames); return;
// Remove DatItem and Machine fields foreach (var fieldName in headerFieldNames)
if (machineFieldNames.Count > 0 || itemFieldNames.Count > 0)
{ {
ApplyRemovalsItemDictionary(machineFieldNames, itemFieldNames); Header.RemoveField(fieldName);
ApplyRemovalsItemDictionaryDB(machineFieldNames, itemFieldNames);
} }
} }
/// <summary> /// <summary>
/// Apply removals to the item dictionary /// Apply removals to the item dictionary
/// </summary> /// </summary>
private void ApplyRemovalsItemDictionary(List<string> machineFieldNames, Dictionary<string, List<string>> itemFieldNames) public void RemoveItemFields(List<string> machineFieldNames, Dictionary<string, List<string>> itemFieldNames)
{ {
// If we have an invalid input, return
if (Items == null || (machineFieldNames.Count == 0 && itemFieldNames.Count == 0))
return;
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(Items.Keys, Core.Globals.ParallelOptions, key => Parallel.ForEach(Items.Keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -64,8 +66,12 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Apply removals to the item dictionary /// Apply removals to the item dictionary
/// </summary> /// </summary>
private void ApplyRemovalsItemDictionaryDB(List<string> machineFieldNames, Dictionary<string, List<string>> itemFieldNames) public void RemoveItemFieldsDB(List<string> machineFieldNames, Dictionary<string, List<string>> itemFieldNames)
{ {
// If we have an invalid input, return
if (ItemsDB == null || (machineFieldNames.Count == 0 && itemFieldNames.Count == 0))
return;
#if NET452_OR_GREATER || NETCOREAPP #if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key => Parallel.ForEach(ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER #elif NET40_OR_GREATER
@@ -94,22 +100,7 @@ namespace SabreTools.DatFiles
} }
/// <summary> /// <summary>
/// Remove fields with given values /// Remove machine fields with given values
/// </summary>
private void RemoveHeaderFields(List<string> headerFieldNames)
{
// If we have an invalid input, return
if (Header == null || headerFieldNames.Count == 0)
return;
foreach (var fieldName in headerFieldNames)
{
Header.RemoveField(fieldName);
}
}
/// <summary>
/// Remove fields with given values
/// </summary> /// </summary>
private static void RemoveFields(Machine? machine, List<string> machineFieldNames) private static void RemoveFields(Machine? machine, List<string> machineFieldNames)
{ {

View File

@@ -130,7 +130,9 @@ namespace SabreTools.DatTools
public void ApplyRemovals(DatFile datFile) public void ApplyRemovals(DatFile datFile)
{ {
InternalStopwatch watch = new("Applying removals to DAT"); InternalStopwatch watch = new("Applying removals to DAT");
datFile.ApplyRemovals(HeaderFieldNames, MachineFieldNames, ItemFieldNames); datFile.RemoveHeaderFields(HeaderFieldNames);
datFile.RemoveItemFields(MachineFieldNames, ItemFieldNames);
datFile.RemoveItemFieldsDB(MachineFieldNames, ItemFieldNames);
watch.Stop(); watch.Stop();
} }