From b2c4f29264931b9785195bdb9f65ef903313dda7 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 11 May 2025 23:36:48 -0400 Subject: [PATCH] Replace AsStringValue for performance --- SabreTools.Core.Test/Tools/ConvertersTests.cs | 68 -- SabreTools.Core/Tools/Converters.cs | 24 +- SabreTools.DatFiles.Test/ConvertersTests.cs | 23 + SabreTools.DatFiles.Test/ExtensionsTests.cs | 55 ++ SabreTools.DatFiles/Extensions.cs | 102 ++- SabreTools.DatItems.Test/ConvertersTests.cs | 604 ------------------ SabreTools.DatItems.Test/ExtensionsTests.cs | 591 +++++++++++++++++ SabreTools.DatItems/Extensions.cs | 466 ++++++++++++-- 8 files changed, 1159 insertions(+), 774 deletions(-) create mode 100644 SabreTools.DatFiles.Test/ConvertersTests.cs create mode 100644 SabreTools.DatItems.Test/ExtensionsTests.cs diff --git a/SabreTools.Core.Test/Tools/ConvertersTests.cs b/SabreTools.Core.Test/Tools/ConvertersTests.cs index 67b6b11a..5f5c7167 100644 --- a/SabreTools.Core.Test/Tools/ConvertersTests.cs +++ b/SabreTools.Core.Test/Tools/ConvertersTests.cs @@ -1,10 +1,8 @@ using SabreTools.Core.Tools; -using SabreTools.DatFiles; using Xunit; namespace SabreTools.Core.Test.Tools { - // TODO: Remove reliance on anything but SabreTools.Core public class ConvertersTests { #region String to Enum @@ -26,57 +24,6 @@ namespace SabreTools.Core.Test.Tools #region Enum to String - [Theory] - [InlineData(MergingFlag.None, true, "none")] - [InlineData(MergingFlag.None, false, "none")] - [InlineData(MergingFlag.Split, true, "split")] - [InlineData(MergingFlag.Split, false, "split")] - [InlineData(MergingFlag.Merged, true, "merged")] - [InlineData(MergingFlag.Merged, false, "merged")] - [InlineData(MergingFlag.NonMerged, true, "unmerged")] - [InlineData(MergingFlag.NonMerged, false, "nonmerged")] - [InlineData(MergingFlag.FullMerged, true, "fullmerged")] - [InlineData(MergingFlag.FullMerged, false, "fullmerged")] - [InlineData(MergingFlag.DeviceNonMerged, true, "deviceunmerged")] - [InlineData(MergingFlag.DeviceNonMerged, false, "device")] - [InlineData(MergingFlag.FullNonMerged, true, "fullunmerged")] - [InlineData(MergingFlag.FullNonMerged, false, "full")] - public void FromMergingFlagTest(MergingFlag field, bool useSecond, string? expected) - { - string? actual = field.AsStringValue(useSecond); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(NodumpFlag.None, "none")] - [InlineData(NodumpFlag.Obsolete, "obsolete")] - [InlineData(NodumpFlag.Required, "required")] - [InlineData(NodumpFlag.Ignore, "ignore")] - public void FromNodumpFlagTest(NodumpFlag field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(PackingFlag.None, true, "none")] - [InlineData(PackingFlag.None, false, "none")] - [InlineData(PackingFlag.Zip, true, "yes")] - [InlineData(PackingFlag.Zip, false, "zip")] - [InlineData(PackingFlag.Unzip, true, "no")] - [InlineData(PackingFlag.Unzip, false, "unzip")] - [InlineData(PackingFlag.Partial, true, "partial")] - [InlineData(PackingFlag.Partial, false, "partial")] - [InlineData(PackingFlag.Flat, true, "flat")] - [InlineData(PackingFlag.Flat, false, "flat")] - [InlineData(PackingFlag.FileOnly, true, "fileonly")] - [InlineData(PackingFlag.FileOnly, false, "fileonly")] - public void FromPackingFlagTest(PackingFlag field, bool useSecond, string? expected) - { - string? actual = field.AsStringValue(useSecond); - Assert.Equal(expected, actual); - } - [Theory] [InlineData(null, null)] [InlineData(true, "yes")] @@ -88,20 +35,5 @@ namespace SabreTools.Core.Test.Tools } #endregion - - #region Generators - - [Theory] - [InlineData(MergingFlag.None, 12)] - [InlineData(NodumpFlag.None, 4)] - [InlineData(PackingFlag.None, 8)] - public void GenerateToEnumTest(T value, int expected) - { - var actual = Converters.GenerateToEnum(); - Assert.Equal(default, value); - Assert.Equal(expected, actual.Keys.Count); - } - - #endregion } } \ No newline at end of file diff --git a/SabreTools.Core/Tools/Converters.cs b/SabreTools.Core/Tools/Converters.cs index 55cd33ec..432983e8 100644 --- a/SabreTools.Core/Tools/Converters.cs +++ b/SabreTools.Core/Tools/Converters.cs @@ -27,7 +27,7 @@ namespace SabreTools.Core.Tools /// /// Enum type that is expected /// Dictionary of string to enum values - public static Dictionary GenerateToEnum() where T : notnull + public static Dictionary GenerateToEnum() { try { @@ -84,33 +84,13 @@ namespace SabreTools.Core.Tools }; } - /// - /// Get the string value for an input enum, if possible - /// - /// Enum value to parse/param> - /// True to use the second mapping option, if it exists - /// Enum type that is expected - /// String value representing the input, default on error - public static string? AsStringValue(this T value, bool useSecond = false) where T : notnull - { - // Get the mapping dictionary - var mappings = GenerateToString(useSecond); - - // Try to get the value from the mappings - if (mappings.ContainsKey(value)) - return mappings[value]; - - // Otherwise, return null - return null; - } - /// /// Get a set of mappings from enum values to string /// /// True to use the second mapping option, if it exists /// Enum type that is expected /// Dictionary of enum to string values - internal static Dictionary GenerateToString(bool useSecond) where T : notnull + public static Dictionary GenerateToString(bool useSecond) where T : notnull { try { diff --git a/SabreTools.DatFiles.Test/ConvertersTests.cs b/SabreTools.DatFiles.Test/ConvertersTests.cs new file mode 100644 index 00000000..cc2ef75c --- /dev/null +++ b/SabreTools.DatFiles.Test/ConvertersTests.cs @@ -0,0 +1,23 @@ +using SabreTools.Core.Tools; +using Xunit; + +namespace SabreTools.DatFiles.Test +{ + public class ConvertersTests + { + #region Generators + + [Theory] + [InlineData(MergingFlag.None, 12)] + [InlineData(NodumpFlag.None, 4)] + [InlineData(PackingFlag.None, 8)] + public void GenerateToEnumTest(T value, int expected) + { + var actual = Converters.GenerateToEnum(); + Assert.Equal(default, value); + Assert.Equal(expected, actual.Keys.Count); + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.DatFiles.Test/ExtensionsTests.cs b/SabreTools.DatFiles.Test/ExtensionsTests.cs index a063eb05..5edc2e61 100644 --- a/SabreTools.DatFiles.Test/ExtensionsTests.cs +++ b/SabreTools.DatFiles.Test/ExtensionsTests.cs @@ -55,5 +55,60 @@ namespace SabreTools.DatFiles.Test } #endregion + + #region Enum to String + + [Theory] + [InlineData(MergingFlag.None, true, "none")] + [InlineData(MergingFlag.None, false, "none")] + [InlineData(MergingFlag.Split, true, "split")] + [InlineData(MergingFlag.Split, false, "split")] + [InlineData(MergingFlag.Merged, true, "merged")] + [InlineData(MergingFlag.Merged, false, "merged")] + [InlineData(MergingFlag.NonMerged, true, "unmerged")] + [InlineData(MergingFlag.NonMerged, false, "nonmerged")] + [InlineData(MergingFlag.FullMerged, true, "fullmerged")] + [InlineData(MergingFlag.FullMerged, false, "fullmerged")] + [InlineData(MergingFlag.DeviceNonMerged, true, "deviceunmerged")] + [InlineData(MergingFlag.DeviceNonMerged, false, "device")] + [InlineData(MergingFlag.FullNonMerged, true, "fullunmerged")] + [InlineData(MergingFlag.FullNonMerged, false, "full")] + public void FromMergingFlagTest(MergingFlag field, bool useSecond, string? expected) + { + string? actual = field.AsStringValue(useSecond); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(NodumpFlag.None, "none")] + [InlineData(NodumpFlag.Obsolete, "obsolete")] + [InlineData(NodumpFlag.Required, "required")] + [InlineData(NodumpFlag.Ignore, "ignore")] + public void FromNodumpFlagTest(NodumpFlag field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(PackingFlag.None, true, "none")] + [InlineData(PackingFlag.None, false, "none")] + [InlineData(PackingFlag.Zip, true, "yes")] + [InlineData(PackingFlag.Zip, false, "zip")] + [InlineData(PackingFlag.Unzip, true, "no")] + [InlineData(PackingFlag.Unzip, false, "unzip")] + [InlineData(PackingFlag.Partial, true, "partial")] + [InlineData(PackingFlag.Partial, false, "partial")] + [InlineData(PackingFlag.Flat, true, "flat")] + [InlineData(PackingFlag.Flat, false, "flat")] + [InlineData(PackingFlag.FileOnly, true, "fileonly")] + [InlineData(PackingFlag.FileOnly, false, "fileonly")] + public void FromPackingFlagTest(PackingFlag field, bool useSecond, string? expected) + { + string? actual = field.AsStringValue(useSecond); + Assert.Equal(expected, actual); + } + + #endregion } } \ No newline at end of file diff --git a/SabreTools.DatFiles/Extensions.cs b/SabreTools.DatFiles/Extensions.cs index e86f47d0..cebee08e 100644 --- a/SabreTools.DatFiles/Extensions.cs +++ b/SabreTools.DatFiles/Extensions.cs @@ -10,17 +10,42 @@ namespace SabreTools.DatFiles /// /// Set of enum to string mappings for MergingFlag /// - private static readonly Dictionary _mergingFlagMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toMergingFlagMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for MergingFlag + /// + private static readonly Dictionary _fromMergingFlagMap = Converters.GenerateToString(useSecond: false); + + /// + /// Set of string to enum mappings for MergingFlag (secondary) + /// + private static readonly Dictionary _fromMergingFlagSecondaryMap = Converters.GenerateToString(useSecond: true); /// /// Set of enum to string mappings for NodumpFlag /// - private static readonly Dictionary _nodumpFlagMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toNodumpFlagMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for NodumpFlag + /// + private static readonly Dictionary _fromNodumpFlagMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for PackingFlag /// - private static readonly Dictionary _packingFlagMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toPackingFlagMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for PackingFlag + /// + private static readonly Dictionary _fromPackingFlagMap = Converters.GenerateToString(useSecond: false); + + /// + /// Set of string to enum mappings for PackingFlag (secondary) + /// + private static readonly Dictionary _fromPackingFlagSecondaryMap = Converters.GenerateToString(useSecond: true); #endregion @@ -30,7 +55,6 @@ namespace SabreTools.DatFiles /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static MergingFlag AsMergingFlag(this string? value) { @@ -40,8 +64,8 @@ namespace SabreTools.DatFiles return default; // Try to get the value from the mappings - if (_mergingFlagMap.ContainsKey(value)) - return _mergingFlagMap[value]; + if (_toMergingFlagMap.ContainsKey(value)) + return _toMergingFlagMap[value]; // Otherwise, return the default value for the enum return default; @@ -51,7 +75,6 @@ namespace SabreTools.DatFiles /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static NodumpFlag AsNodumpFlag(this string? value) { @@ -61,8 +84,8 @@ namespace SabreTools.DatFiles return default; // Try to get the value from the mappings - if (_nodumpFlagMap.ContainsKey(value)) - return _nodumpFlagMap[value]; + if (_toNodumpFlagMap.ContainsKey(value)) + return _toNodumpFlagMap[value]; // Otherwise, return the default value for the enum return default; @@ -72,7 +95,6 @@ namespace SabreTools.DatFiles /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static PackingFlag AsPackingFlag(this string? value) { @@ -82,13 +104,69 @@ namespace SabreTools.DatFiles return default; // Try to get the value from the mappings - if (_packingFlagMap.ContainsKey(value)) - return _packingFlagMap[value]; + if (_toPackingFlagMap.ContainsKey(value)) + return _toPackingFlagMap[value]; // Otherwise, return the default value for the enum return default; } #endregion + + #region Enum to String + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this MergingFlag value, bool useSecond = false) + { + // Try to get the value from the mappings + if (!useSecond && _fromMergingFlagMap.ContainsKey(value)) + return _fromMergingFlagMap[value]; + else if (useSecond && _fromMergingFlagSecondaryMap.ContainsKey(value)) + return _fromMergingFlagSecondaryMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this NodumpFlag value) + { + // Try to get the value from the mappings + if (_fromNodumpFlagMap.ContainsKey(value)) + return _fromNodumpFlagMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this PackingFlag value, bool useSecond = false) + { + // Try to get the value from the mappings + if (!useSecond && _fromPackingFlagMap.ContainsKey(value)) + return _fromPackingFlagMap[value]; + else if (useSecond && _fromPackingFlagSecondaryMap.ContainsKey(value)) + return _fromPackingFlagSecondaryMap[value]; + + // Otherwise, return null + return null; + } + + #endregion } } \ No newline at end of file diff --git a/SabreTools.DatItems.Test/ConvertersTests.cs b/SabreTools.DatItems.Test/ConvertersTests.cs index 4ed80d6e..1aefa2e9 100644 --- a/SabreTools.DatItems.Test/ConvertersTests.cs +++ b/SabreTools.DatItems.Test/ConvertersTests.cs @@ -5,610 +5,6 @@ namespace SabreTools.DatItems.Test { public class ConvertersTests { - #region String to Enum - - [Theory] - [InlineData(null, ChipType.NULL)] - [InlineData("cpu", ChipType.CPU)] - [InlineData("audio", ChipType.Audio)] - public void AsChipTypeTest(string? field, ChipType expected) - { - ChipType actual = field.AsChipType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, ControlType.NULL)] - [InlineData("joy", ControlType.Joy)] - [InlineData("stick", ControlType.Stick)] - [InlineData("paddle", ControlType.Paddle)] - [InlineData("pedal", ControlType.Pedal)] - [InlineData("lightgun", ControlType.Lightgun)] - [InlineData("positional", ControlType.Positional)] - [InlineData("dial", ControlType.Dial)] - [InlineData("trackball", ControlType.Trackball)] - [InlineData("mouse", ControlType.Mouse)] - [InlineData("only_buttons", ControlType.OnlyButtons)] - [InlineData("keypad", ControlType.Keypad)] - [InlineData("keyboard", ControlType.Keyboard)] - [InlineData("mahjong", ControlType.Mahjong)] - [InlineData("hanafuda", ControlType.Hanafuda)] - [InlineData("gambling", ControlType.Gambling)] - public void AsControlTypeTest(string? field, ControlType expected) - { - ControlType actual = field.AsControlType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, DeviceType.NULL)] - [InlineData("unknown", DeviceType.Unknown)] - [InlineData("cartridge", DeviceType.Cartridge)] - [InlineData("floppydisk", DeviceType.FloppyDisk)] - [InlineData("harddisk", DeviceType.HardDisk)] - [InlineData("cylinder", DeviceType.Cylinder)] - [InlineData("cassette", DeviceType.Cassette)] - [InlineData("punchcard", DeviceType.PunchCard)] - [InlineData("punchtape", DeviceType.PunchTape)] - [InlineData("printout", DeviceType.Printout)] - [InlineData("serial", DeviceType.Serial)] - [InlineData("parallel", DeviceType.Parallel)] - [InlineData("snapshot", DeviceType.Snapshot)] - [InlineData("quickload", DeviceType.QuickLoad)] - [InlineData("memcard", DeviceType.MemCard)] - [InlineData("cdrom", DeviceType.CDROM)] - [InlineData("magtape", DeviceType.MagTape)] - [InlineData("romimage", DeviceType.ROMImage)] - [InlineData("midiin", DeviceType.MIDIIn)] - [InlineData("midiout", DeviceType.MIDIOut)] - [InlineData("picture", DeviceType.Picture)] - [InlineData("vidfile", DeviceType.VidFile)] - public void AsDeviceTypeTest(string? field, DeviceType expected) - { - DeviceType actual = field.AsDeviceType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, DisplayType.NULL)] - [InlineData("raster", DisplayType.Raster)] - [InlineData("vector", DisplayType.Vector)] - [InlineData("lcd", DisplayType.LCD)] - [InlineData("svg", DisplayType.SVG)] - [InlineData("unknown", DisplayType.Unknown)] - public void AsDisplayTypeTest(string? field, DisplayType expected) - { - DisplayType actual = field.AsDisplayType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, Endianness.NULL)] - [InlineData("big", Endianness.Big)] - [InlineData("little", Endianness.Little)] - public void AsEndiannessTest(string? field, Endianness expected) - { - Endianness actual = field.AsEndianness(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, FeatureStatus.NULL)] - [InlineData("unemulated", FeatureStatus.Unemulated)] - [InlineData("imperfect", FeatureStatus.Imperfect)] - public void AsFeatureStatusTest(string? field, FeatureStatus expected) - { - FeatureStatus actual = field.AsFeatureStatus(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, FeatureType.NULL)] - [InlineData("protection", FeatureType.Protection)] - [InlineData("palette", FeatureType.Palette)] - [InlineData("graphics", FeatureType.Graphics)] - [InlineData("sound", FeatureType.Sound)] - [InlineData("controls", FeatureType.Controls)] - [InlineData("keyboard", FeatureType.Keyboard)] - [InlineData("mouse", FeatureType.Mouse)] - [InlineData("microphone", FeatureType.Microphone)] - [InlineData("camera", FeatureType.Camera)] - [InlineData("disk", FeatureType.Disk)] - [InlineData("printer", FeatureType.Printer)] - [InlineData("lan", FeatureType.Lan)] - [InlineData("wan", FeatureType.Wan)] - [InlineData("timing", FeatureType.Timing)] - public void AsFeatureTypeTest(string? field, FeatureType expected) - { - FeatureType actual = field.AsFeatureType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, ItemStatus.NULL)] - [InlineData("none", ItemStatus.None)] - [InlineData("no", ItemStatus.None)] - [InlineData("good", ItemStatus.Good)] - [InlineData("baddump", ItemStatus.BadDump)] - [InlineData("nodump", ItemStatus.Nodump)] - [InlineData("yes", ItemStatus.Nodump)] - [InlineData("verified", ItemStatus.Verified)] - public void AsItemStatusTest(string? field, ItemStatus expected) - { - ItemStatus actual = field.AsItemStatus(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, ItemType.NULL)] - [InlineData("adjuster", ItemType.Adjuster)] - [InlineData("analog", ItemType.Analog)] - [InlineData("archive", ItemType.Archive)] - [InlineData("biosset", ItemType.BiosSet)] - [InlineData("blank", ItemType.Blank)] - [InlineData("chip", ItemType.Chip)] - [InlineData("condition", ItemType.Condition)] - [InlineData("configuration", ItemType.Configuration)] - [InlineData("conflocation", ItemType.ConfLocation)] - [InlineData("confsetting", ItemType.ConfSetting)] - [InlineData("control", ItemType.Control)] - [InlineData("dataarea", ItemType.DataArea)] - [InlineData("device", ItemType.Device)] - [InlineData("deviceref", ItemType.DeviceRef)] - [InlineData("device_ref", ItemType.DeviceRef)] - [InlineData("diplocation", ItemType.DipLocation)] - [InlineData("dipswitch", ItemType.DipSwitch)] - [InlineData("dipvalue", ItemType.DipValue)] - [InlineData("disk", ItemType.Disk)] - [InlineData("diskarea", ItemType.DiskArea)] - [InlineData("display", ItemType.Display)] - [InlineData("driver", ItemType.Driver)] - [InlineData("extension", ItemType.Extension)] - [InlineData("feature", ItemType.Feature)] - [InlineData("file", ItemType.File)] - [InlineData("info", ItemType.Info)] - [InlineData("input", ItemType.Input)] - [InlineData("instance", ItemType.Instance)] - [InlineData("media", ItemType.Media)] - [InlineData("part", ItemType.Part)] - [InlineData("partfeature", ItemType.PartFeature)] - [InlineData("part_feature", ItemType.PartFeature)] - [InlineData("port", ItemType.Port)] - [InlineData("ramoption", ItemType.RamOption)] - [InlineData("ram_option", ItemType.RamOption)] - [InlineData("release", ItemType.Release)] - [InlineData("releasedetails", ItemType.ReleaseDetails)] - [InlineData("release_details", ItemType.ReleaseDetails)] - [InlineData("rom", ItemType.Rom)] - [InlineData("sample", ItemType.Sample)] - [InlineData("serials", ItemType.Serials)] - [InlineData("sharedfeat", ItemType.SharedFeat)] - [InlineData("shared_feat", ItemType.SharedFeat)] - [InlineData("sharedfeature", ItemType.SharedFeat)] - [InlineData("shared_feature", ItemType.SharedFeat)] - [InlineData("slot", ItemType.Slot)] - [InlineData("slotoption", ItemType.SlotOption)] - [InlineData("slot_option", ItemType.SlotOption)] - [InlineData("softwarelist", ItemType.SoftwareList)] - [InlineData("software_list", ItemType.SoftwareList)] - [InlineData("sound", ItemType.Sound)] - [InlineData("sourcedetails", ItemType.SourceDetails)] - [InlineData("source_details", ItemType.SourceDetails)] - public void AsItemTypeTest(string? field, ItemType expected) - { - ItemType actual = field.AsItemType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, LoadFlag.NULL)] - [InlineData("load16_byte", LoadFlag.Load16Byte)] - [InlineData("load16_word", LoadFlag.Load16Word)] - [InlineData("load16_word_swap", LoadFlag.Load16WordSwap)] - [InlineData("load32_byte", LoadFlag.Load32Byte)] - [InlineData("load32_word", LoadFlag.Load32Word)] - [InlineData("load32_word_swap", LoadFlag.Load32WordSwap)] - [InlineData("load32_dword", LoadFlag.Load32DWord)] - [InlineData("load64_word", LoadFlag.Load64Word)] - [InlineData("load64_word_swap", LoadFlag.Load64WordSwap)] - [InlineData("reload", LoadFlag.Reload)] - [InlineData("fill", LoadFlag.Fill)] - [InlineData("continue", LoadFlag.Continue)] - [InlineData("reload_plain", LoadFlag.ReloadPlain)] - [InlineData("ignore", LoadFlag.Ignore)] - public void AsLoadFlagTest(string? field, LoadFlag expected) - { - LoadFlag actual = field.AsLoadFlag(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, MachineType.None)] - [InlineData("none", MachineType.None)] - [InlineData("bios", MachineType.Bios)] - [InlineData("dev", MachineType.Device)] - [InlineData("device", MachineType.Device)] - [InlineData("mech", MachineType.Mechanical)] - [InlineData("mechanical", MachineType.Mechanical)] - public void AsMachineTypeTest(string? field, MachineType expected) - { - MachineType actual = field.AsMachineType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, OpenMSXSubType.NULL)] - [InlineData("rom", OpenMSXSubType.Rom)] - [InlineData("megarom", OpenMSXSubType.MegaRom)] - [InlineData("sccpluscart", OpenMSXSubType.SCCPlusCart)] - public void AsOpenMSXSubTypeTest(string? field, OpenMSXSubType expected) - { - OpenMSXSubType actual = field.AsOpenMSXSubType(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, Relation.NULL)] - [InlineData("eq", Relation.Equal)] - [InlineData("ne", Relation.NotEqual)] - [InlineData("gt", Relation.GreaterThan)] - [InlineData("le", Relation.LessThanOrEqual)] - [InlineData("lt", Relation.LessThan)] - [InlineData("ge", Relation.GreaterThanOrEqual)] - public void AsRelationTest(string? field, Relation expected) - { - Relation actual = field.AsRelation(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, Runnable.NULL)] - [InlineData("no", Runnable.No)] - [InlineData("partial", Runnable.Partial)] - [InlineData("yes", Runnable.Yes)] - public void AsRunnableTest(string? field, Runnable expected) - { - Runnable actual = field.AsRunnable(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, SoftwareListStatus.None)] - [InlineData("none", SoftwareListStatus.None)] - [InlineData("original", SoftwareListStatus.Original)] - [InlineData("compatible", SoftwareListStatus.Compatible)] - public void AsSoftwareListStatusTest(string? field, SoftwareListStatus expected) - { - SoftwareListStatus actual = field.AsSoftwareListStatus(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, Supported.NULL)] - [InlineData("no", Supported.No)] - [InlineData("unsupported", Supported.No)] - [InlineData("partial", Supported.Partial)] - [InlineData("yes", Supported.Yes)] - [InlineData("supported", Supported.Yes)] - public void AsSupportedTest(string? field, Supported expected) - { - Supported actual = field.AsSupported(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, SupportStatus.NULL)] - [InlineData("good", SupportStatus.Good)] - [InlineData("imperfect", SupportStatus.Imperfect)] - [InlineData("preliminary", SupportStatus.Preliminary)] - public void AsSupportStatusTest(string? field, SupportStatus expected) - { - SupportStatus actual = field.AsSupportStatus(); - Assert.Equal(expected, actual); - } - - #endregion - - #region Enum to String - - [Theory] - [InlineData(ChipType.NULL, null)] - [InlineData(ChipType.CPU, "cpu")] - [InlineData(ChipType.Audio, "audio")] - public void FromChipTypeTest(ChipType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(ControlType.NULL, null)] - [InlineData(ControlType.Joy, "joy")] - [InlineData(ControlType.Stick, "stick")] - [InlineData(ControlType.Paddle, "paddle")] - [InlineData(ControlType.Pedal, "pedal")] - [InlineData(ControlType.Lightgun, "lightgun")] - [InlineData(ControlType.Positional, "positional")] - [InlineData(ControlType.Dial, "dial")] - [InlineData(ControlType.Trackball, "trackball")] - [InlineData(ControlType.Mouse, "mouse")] - [InlineData(ControlType.OnlyButtons, "only_buttons")] - [InlineData(ControlType.Keypad, "keypad")] - [InlineData(ControlType.Keyboard, "keyboard")] - [InlineData(ControlType.Mahjong, "mahjong")] - [InlineData(ControlType.Hanafuda, "hanafuda")] - [InlineData(ControlType.Gambling, "gambling")] - public void FromControlTypeTest(ControlType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(DeviceType.NULL, null)] - [InlineData(DeviceType.Unknown, "unknown")] - [InlineData(DeviceType.Cartridge, "cartridge")] - [InlineData(DeviceType.FloppyDisk, "floppydisk")] - [InlineData(DeviceType.HardDisk, "harddisk")] - [InlineData(DeviceType.Cylinder, "cylinder")] - [InlineData(DeviceType.Cassette, "cassette")] - [InlineData(DeviceType.PunchCard, "punchcard")] - [InlineData(DeviceType.PunchTape, "punchtape")] - [InlineData(DeviceType.Printout, "printout")] - [InlineData(DeviceType.Serial, "serial")] - [InlineData(DeviceType.Parallel, "parallel")] - [InlineData(DeviceType.Snapshot, "snapshot")] - [InlineData(DeviceType.QuickLoad, "quickload")] - [InlineData(DeviceType.MemCard, "memcard")] - [InlineData(DeviceType.CDROM, "cdrom")] - [InlineData(DeviceType.MagTape, "magtape")] - [InlineData(DeviceType.ROMImage, "romimage")] - [InlineData(DeviceType.MIDIIn, "midiin")] - [InlineData(DeviceType.MIDIOut, "midiout")] - [InlineData(DeviceType.Picture, "picture")] - [InlineData(DeviceType.VidFile, "vidfile")] - public void FromDeviceTypeTest(DeviceType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(DisplayType.NULL, null)] - [InlineData(DisplayType.Raster, "raster")] - [InlineData(DisplayType.Vector, "vector")] - [InlineData(DisplayType.LCD, "lcd")] - [InlineData(DisplayType.SVG, "svg")] - [InlineData(DisplayType.Unknown, "unknown")] - public void FromDisplayTypeTest(DisplayType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(Endianness.NULL, null)] - [InlineData(Endianness.Big, "big")] - [InlineData(Endianness.Little, "little")] - public void FromEndiannessTest(Endianness field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(FeatureStatus.NULL, null)] - [InlineData(FeatureStatus.Unemulated, "unemulated")] - [InlineData(FeatureStatus.Imperfect, "imperfect")] - public void FromFeatureStatusTest(FeatureStatus field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(FeatureType.NULL, null)] - [InlineData(FeatureType.Protection, "protection")] - [InlineData(FeatureType.Palette, "palette")] - [InlineData(FeatureType.Graphics, "graphics")] - [InlineData(FeatureType.Sound, "sound")] - [InlineData(FeatureType.Controls, "controls")] - [InlineData(FeatureType.Keyboard, "keyboard")] - [InlineData(FeatureType.Mouse, "mouse")] - [InlineData(FeatureType.Microphone, "microphone")] - [InlineData(FeatureType.Camera, "camera")] - [InlineData(FeatureType.Disk, "disk")] - [InlineData(FeatureType.Printer, "printer")] - [InlineData(FeatureType.Lan, "lan")] - [InlineData(FeatureType.Wan, "wan")] - [InlineData(FeatureType.Timing, "timing")] - public void FromFeatureTypeTest(FeatureType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(ItemStatus.NULL, true, null)] - [InlineData(ItemStatus.NULL, false, null)] - [InlineData(ItemStatus.None, true, "no")] - [InlineData(ItemStatus.None, false, "none")] - [InlineData(ItemStatus.Good, true, "good")] - [InlineData(ItemStatus.Good, false, "good")] - [InlineData(ItemStatus.BadDump, true, "baddump")] - [InlineData(ItemStatus.BadDump, false, "baddump")] - [InlineData(ItemStatus.Nodump, true, "yes")] - [InlineData(ItemStatus.Nodump, false, "nodump")] - [InlineData(ItemStatus.Verified, true, "verified")] - [InlineData(ItemStatus.Verified, false, "verified")] - public void FromItemStatusTest(ItemStatus field, bool useSecond, string? expected) - { - string? actual = field.AsStringValue(useSecond); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(ItemType.NULL, null)] - [InlineData(ItemType.Adjuster, "adjuster")] - [InlineData(ItemType.Analog, "analog")] - [InlineData(ItemType.Archive, "archive")] - [InlineData(ItemType.BiosSet, "biosset")] - [InlineData(ItemType.Blank, "blank")] - [InlineData(ItemType.Chip, "chip")] - [InlineData(ItemType.Condition, "condition")] - [InlineData(ItemType.Configuration, "configuration")] - [InlineData(ItemType.ConfLocation, "conflocation")] - [InlineData(ItemType.ConfSetting, "confsetting")] - [InlineData(ItemType.Control, "control")] - [InlineData(ItemType.DataArea, "dataarea")] - [InlineData(ItemType.Device, "device")] - [InlineData(ItemType.DeviceRef, "device_ref")] - [InlineData(ItemType.DipLocation, "diplocation")] - [InlineData(ItemType.DipSwitch, "dipswitch")] - [InlineData(ItemType.DipValue, "dipvalue")] - [InlineData(ItemType.Disk, "disk")] - [InlineData(ItemType.DiskArea, "diskarea")] - [InlineData(ItemType.Display, "display")] - [InlineData(ItemType.Driver, "driver")] - [InlineData(ItemType.Extension, "extension")] - [InlineData(ItemType.Feature, "feature")] - [InlineData(ItemType.File, "file")] - [InlineData(ItemType.Info, "info")] - [InlineData(ItemType.Input, "input")] - [InlineData(ItemType.Instance, "instance")] - [InlineData(ItemType.Media, "media")] - [InlineData(ItemType.Part, "part")] - [InlineData(ItemType.PartFeature, "part_feature")] - [InlineData(ItemType.Port, "port")] - [InlineData(ItemType.RamOption, "ramoption")] - [InlineData(ItemType.Release, "release")] - [InlineData(ItemType.ReleaseDetails, "release_details")] - [InlineData(ItemType.Rom, "rom")] - [InlineData(ItemType.Sample, "sample")] - [InlineData(ItemType.Serials, "serials")] - [InlineData(ItemType.SharedFeat, "sharedfeat")] - [InlineData(ItemType.Slot, "slot")] - [InlineData(ItemType.SlotOption, "slotoption")] - [InlineData(ItemType.SoftwareList, "softwarelist")] - [InlineData(ItemType.Sound, "sound")] - [InlineData(ItemType.SourceDetails, "source_details")] - public void FromItemTypeTest(ItemType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(LoadFlag.NULL, null)] - [InlineData(LoadFlag.Load16Byte, "load16_byte")] - [InlineData(LoadFlag.Load16Word, "load16_word")] - [InlineData(LoadFlag.Load16WordSwap, "load16_word_swap")] - [InlineData(LoadFlag.Load32Byte, "load32_byte")] - [InlineData(LoadFlag.Load32Word, "load32_word")] - [InlineData(LoadFlag.Load32WordSwap, "load32_word_swap")] - [InlineData(LoadFlag.Load32DWord, "load32_dword")] - [InlineData(LoadFlag.Load64Word, "load64_word")] - [InlineData(LoadFlag.Load64WordSwap, "load64_word_swap")] - [InlineData(LoadFlag.Reload, "reload")] - [InlineData(LoadFlag.Fill, "fill")] - [InlineData(LoadFlag.Continue, "continue")] - [InlineData(LoadFlag.ReloadPlain, "reload_plain")] - [InlineData(LoadFlag.Ignore, "ignore")] - public void FromLoadFlagTest(LoadFlag field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(MachineType.None, true, "none")] - [InlineData(MachineType.None, false, "none")] - [InlineData(MachineType.Bios, true, "bios")] - [InlineData(MachineType.Bios, false, "bios")] - [InlineData(MachineType.Device, true, "dev")] - [InlineData(MachineType.Device, false, "device")] - [InlineData(MachineType.Mechanical, true, "mech")] - [InlineData(MachineType.Mechanical, false, "mechanical")] - public void FromMachineTypeTest(MachineType field, bool old, string? expected) - { - string? actual = field.AsStringValue(old); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(OpenMSXSubType.NULL, null)] - [InlineData(OpenMSXSubType.Rom, "rom")] - [InlineData(OpenMSXSubType.MegaRom, "megarom")] - [InlineData(OpenMSXSubType.SCCPlusCart, "sccpluscart")] - public void FromOpenMSXSubTypeTest(OpenMSXSubType field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(Relation.NULL, null)] - [InlineData(Relation.Equal, "eq")] - [InlineData(Relation.NotEqual, "ne")] - [InlineData(Relation.GreaterThan, "gt")] - [InlineData(Relation.LessThanOrEqual, "le")] - [InlineData(Relation.LessThan, "lt")] - [InlineData(Relation.GreaterThanOrEqual, "ge")] - public void FromRelationTest(Relation field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(Runnable.NULL, null)] - [InlineData(Runnable.No, "no")] - [InlineData(Runnable.Partial, "partial")] - [InlineData(Runnable.Yes, "yes")] - public void FromRunnableTest(Runnable field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(SoftwareListStatus.None, "none")] - [InlineData(SoftwareListStatus.Original, "original")] - [InlineData(SoftwareListStatus.Compatible, "compatible")] - public void FromSoftwareListStatusTest(SoftwareListStatus field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(Supported.NULL, true, null)] - [InlineData(Supported.NULL, false, null)] - [InlineData(Supported.No, true, "unsupported")] - [InlineData(Supported.No, false, "no")] - [InlineData(Supported.Partial, true, "partial")] - [InlineData(Supported.Partial, false, "partial")] - [InlineData(Supported.Yes, true, "supported")] - [InlineData(Supported.Yes, false, "yes")] - public void FromSupportedTest(Supported field, bool useSecond, string? expected) - { - string? actual = field.AsStringValue(useSecond); - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(SupportStatus.NULL, null)] - [InlineData(SupportStatus.Good, "good")] - [InlineData(SupportStatus.Imperfect, "imperfect")] - [InlineData(SupportStatus.Preliminary, "preliminary")] - public void FromSupportStatusTest(SupportStatus field, string? expected) - { - string? actual = field.AsStringValue(); - Assert.Equal(expected, actual); - } - - #endregion - #region Generators [Theory] diff --git a/SabreTools.DatItems.Test/ExtensionsTests.cs b/SabreTools.DatItems.Test/ExtensionsTests.cs new file mode 100644 index 00000000..8807a532 --- /dev/null +++ b/SabreTools.DatItems.Test/ExtensionsTests.cs @@ -0,0 +1,591 @@ +using SabreTools.Core.Tools; +using Xunit; + +namespace SabreTools.DatItems.Test +{ + public class ExtensionsTests + { + #region String to Enum + + [Theory] + [InlineData(null, ChipType.NULL)] + [InlineData("cpu", ChipType.CPU)] + [InlineData("audio", ChipType.Audio)] + public void AsChipTypeTest(string? field, ChipType expected) + { + ChipType actual = field.AsChipType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, ControlType.NULL)] + [InlineData("joy", ControlType.Joy)] + [InlineData("stick", ControlType.Stick)] + [InlineData("paddle", ControlType.Paddle)] + [InlineData("pedal", ControlType.Pedal)] + [InlineData("lightgun", ControlType.Lightgun)] + [InlineData("positional", ControlType.Positional)] + [InlineData("dial", ControlType.Dial)] + [InlineData("trackball", ControlType.Trackball)] + [InlineData("mouse", ControlType.Mouse)] + [InlineData("only_buttons", ControlType.OnlyButtons)] + [InlineData("keypad", ControlType.Keypad)] + [InlineData("keyboard", ControlType.Keyboard)] + [InlineData("mahjong", ControlType.Mahjong)] + [InlineData("hanafuda", ControlType.Hanafuda)] + [InlineData("gambling", ControlType.Gambling)] + public void AsControlTypeTest(string? field, ControlType expected) + { + ControlType actual = field.AsControlType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, DeviceType.NULL)] + [InlineData("unknown", DeviceType.Unknown)] + [InlineData("cartridge", DeviceType.Cartridge)] + [InlineData("floppydisk", DeviceType.FloppyDisk)] + [InlineData("harddisk", DeviceType.HardDisk)] + [InlineData("cylinder", DeviceType.Cylinder)] + [InlineData("cassette", DeviceType.Cassette)] + [InlineData("punchcard", DeviceType.PunchCard)] + [InlineData("punchtape", DeviceType.PunchTape)] + [InlineData("printout", DeviceType.Printout)] + [InlineData("serial", DeviceType.Serial)] + [InlineData("parallel", DeviceType.Parallel)] + [InlineData("snapshot", DeviceType.Snapshot)] + [InlineData("quickload", DeviceType.QuickLoad)] + [InlineData("memcard", DeviceType.MemCard)] + [InlineData("cdrom", DeviceType.CDROM)] + [InlineData("magtape", DeviceType.MagTape)] + [InlineData("romimage", DeviceType.ROMImage)] + [InlineData("midiin", DeviceType.MIDIIn)] + [InlineData("midiout", DeviceType.MIDIOut)] + [InlineData("picture", DeviceType.Picture)] + [InlineData("vidfile", DeviceType.VidFile)] + public void AsDeviceTypeTest(string? field, DeviceType expected) + { + DeviceType actual = field.AsDeviceType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, DisplayType.NULL)] + [InlineData("raster", DisplayType.Raster)] + [InlineData("vector", DisplayType.Vector)] + [InlineData("lcd", DisplayType.LCD)] + [InlineData("svg", DisplayType.SVG)] + [InlineData("unknown", DisplayType.Unknown)] + public void AsDisplayTypeTest(string? field, DisplayType expected) + { + DisplayType actual = field.AsDisplayType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, Endianness.NULL)] + [InlineData("big", Endianness.Big)] + [InlineData("little", Endianness.Little)] + public void AsEndiannessTest(string? field, Endianness expected) + { + Endianness actual = field.AsEndianness(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, FeatureStatus.NULL)] + [InlineData("unemulated", FeatureStatus.Unemulated)] + [InlineData("imperfect", FeatureStatus.Imperfect)] + public void AsFeatureStatusTest(string? field, FeatureStatus expected) + { + FeatureStatus actual = field.AsFeatureStatus(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, FeatureType.NULL)] + [InlineData("protection", FeatureType.Protection)] + [InlineData("palette", FeatureType.Palette)] + [InlineData("graphics", FeatureType.Graphics)] + [InlineData("sound", FeatureType.Sound)] + [InlineData("controls", FeatureType.Controls)] + [InlineData("keyboard", FeatureType.Keyboard)] + [InlineData("mouse", FeatureType.Mouse)] + [InlineData("microphone", FeatureType.Microphone)] + [InlineData("camera", FeatureType.Camera)] + [InlineData("disk", FeatureType.Disk)] + [InlineData("printer", FeatureType.Printer)] + [InlineData("lan", FeatureType.Lan)] + [InlineData("wan", FeatureType.Wan)] + [InlineData("timing", FeatureType.Timing)] + public void AsFeatureTypeTest(string? field, FeatureType expected) + { + FeatureType actual = field.AsFeatureType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, ItemStatus.NULL)] + [InlineData("none", ItemStatus.None)] + [InlineData("no", ItemStatus.None)] + [InlineData("good", ItemStatus.Good)] + [InlineData("baddump", ItemStatus.BadDump)] + [InlineData("nodump", ItemStatus.Nodump)] + [InlineData("yes", ItemStatus.Nodump)] + [InlineData("verified", ItemStatus.Verified)] + public void AsItemStatusTest(string? field, ItemStatus expected) + { + ItemStatus actual = field.AsItemStatus(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, ItemType.NULL)] + [InlineData("adjuster", ItemType.Adjuster)] + [InlineData("analog", ItemType.Analog)] + [InlineData("archive", ItemType.Archive)] + [InlineData("biosset", ItemType.BiosSet)] + [InlineData("blank", ItemType.Blank)] + [InlineData("chip", ItemType.Chip)] + [InlineData("condition", ItemType.Condition)] + [InlineData("configuration", ItemType.Configuration)] + [InlineData("conflocation", ItemType.ConfLocation)] + [InlineData("confsetting", ItemType.ConfSetting)] + [InlineData("control", ItemType.Control)] + [InlineData("dataarea", ItemType.DataArea)] + [InlineData("device", ItemType.Device)] + [InlineData("deviceref", ItemType.DeviceRef)] + [InlineData("device_ref", ItemType.DeviceRef)] + [InlineData("diplocation", ItemType.DipLocation)] + [InlineData("dipswitch", ItemType.DipSwitch)] + [InlineData("dipvalue", ItemType.DipValue)] + [InlineData("disk", ItemType.Disk)] + [InlineData("diskarea", ItemType.DiskArea)] + [InlineData("display", ItemType.Display)] + [InlineData("driver", ItemType.Driver)] + [InlineData("extension", ItemType.Extension)] + [InlineData("feature", ItemType.Feature)] + [InlineData("file", ItemType.File)] + [InlineData("info", ItemType.Info)] + [InlineData("input", ItemType.Input)] + [InlineData("instance", ItemType.Instance)] + [InlineData("media", ItemType.Media)] + [InlineData("part", ItemType.Part)] + [InlineData("partfeature", ItemType.PartFeature)] + [InlineData("part_feature", ItemType.PartFeature)] + [InlineData("port", ItemType.Port)] + [InlineData("ramoption", ItemType.RamOption)] + [InlineData("ram_option", ItemType.RamOption)] + [InlineData("release", ItemType.Release)] + [InlineData("releasedetails", ItemType.ReleaseDetails)] + [InlineData("release_details", ItemType.ReleaseDetails)] + [InlineData("rom", ItemType.Rom)] + [InlineData("sample", ItemType.Sample)] + [InlineData("serials", ItemType.Serials)] + [InlineData("sharedfeat", ItemType.SharedFeat)] + [InlineData("shared_feat", ItemType.SharedFeat)] + [InlineData("sharedfeature", ItemType.SharedFeat)] + [InlineData("shared_feature", ItemType.SharedFeat)] + [InlineData("slot", ItemType.Slot)] + [InlineData("slotoption", ItemType.SlotOption)] + [InlineData("slot_option", ItemType.SlotOption)] + [InlineData("softwarelist", ItemType.SoftwareList)] + [InlineData("software_list", ItemType.SoftwareList)] + [InlineData("sound", ItemType.Sound)] + [InlineData("sourcedetails", ItemType.SourceDetails)] + [InlineData("source_details", ItemType.SourceDetails)] + public void AsItemTypeTest(string? field, ItemType expected) + { + ItemType actual = field.AsItemType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, LoadFlag.NULL)] + [InlineData("load16_byte", LoadFlag.Load16Byte)] + [InlineData("load16_word", LoadFlag.Load16Word)] + [InlineData("load16_word_swap", LoadFlag.Load16WordSwap)] + [InlineData("load32_byte", LoadFlag.Load32Byte)] + [InlineData("load32_word", LoadFlag.Load32Word)] + [InlineData("load32_word_swap", LoadFlag.Load32WordSwap)] + [InlineData("load32_dword", LoadFlag.Load32DWord)] + [InlineData("load64_word", LoadFlag.Load64Word)] + [InlineData("load64_word_swap", LoadFlag.Load64WordSwap)] + [InlineData("reload", LoadFlag.Reload)] + [InlineData("fill", LoadFlag.Fill)] + [InlineData("continue", LoadFlag.Continue)] + [InlineData("reload_plain", LoadFlag.ReloadPlain)] + [InlineData("ignore", LoadFlag.Ignore)] + public void AsLoadFlagTest(string? field, LoadFlag expected) + { + LoadFlag actual = field.AsLoadFlag(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, MachineType.None)] + [InlineData("none", MachineType.None)] + [InlineData("bios", MachineType.Bios)] + [InlineData("dev", MachineType.Device)] + [InlineData("device", MachineType.Device)] + [InlineData("mech", MachineType.Mechanical)] + [InlineData("mechanical", MachineType.Mechanical)] + public void AsMachineTypeTest(string? field, MachineType expected) + { + MachineType actual = field.AsMachineType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, OpenMSXSubType.NULL)] + [InlineData("rom", OpenMSXSubType.Rom)] + [InlineData("megarom", OpenMSXSubType.MegaRom)] + [InlineData("sccpluscart", OpenMSXSubType.SCCPlusCart)] + public void AsOpenMSXSubTypeTest(string? field, OpenMSXSubType expected) + { + OpenMSXSubType actual = field.AsOpenMSXSubType(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, Relation.NULL)] + [InlineData("eq", Relation.Equal)] + [InlineData("ne", Relation.NotEqual)] + [InlineData("gt", Relation.GreaterThan)] + [InlineData("le", Relation.LessThanOrEqual)] + [InlineData("lt", Relation.LessThan)] + [InlineData("ge", Relation.GreaterThanOrEqual)] + public void AsRelationTest(string? field, Relation expected) + { + Relation actual = field.AsRelation(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, Runnable.NULL)] + [InlineData("no", Runnable.No)] + [InlineData("partial", Runnable.Partial)] + [InlineData("yes", Runnable.Yes)] + public void AsRunnableTest(string? field, Runnable expected) + { + Runnable actual = field.AsRunnable(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, SoftwareListStatus.None)] + [InlineData("none", SoftwareListStatus.None)] + [InlineData("original", SoftwareListStatus.Original)] + [InlineData("compatible", SoftwareListStatus.Compatible)] + public void AsSoftwareListStatusTest(string? field, SoftwareListStatus expected) + { + SoftwareListStatus actual = field.AsSoftwareListStatus(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, Supported.NULL)] + [InlineData("no", Supported.No)] + [InlineData("unsupported", Supported.No)] + [InlineData("partial", Supported.Partial)] + [InlineData("yes", Supported.Yes)] + [InlineData("supported", Supported.Yes)] + public void AsSupportedTest(string? field, Supported expected) + { + Supported actual = field.AsSupported(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, SupportStatus.NULL)] + [InlineData("good", SupportStatus.Good)] + [InlineData("imperfect", SupportStatus.Imperfect)] + [InlineData("preliminary", SupportStatus.Preliminary)] + public void AsSupportStatusTest(string? field, SupportStatus expected) + { + SupportStatus actual = field.AsSupportStatus(); + Assert.Equal(expected, actual); + } + + #endregion + + #region Enum to String + + [Theory] + [InlineData(ChipType.NULL, null)] + [InlineData(ChipType.CPU, "cpu")] + [InlineData(ChipType.Audio, "audio")] + public void FromChipTypeTest(ChipType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(ControlType.NULL, null)] + [InlineData(ControlType.Joy, "joy")] + [InlineData(ControlType.Stick, "stick")] + [InlineData(ControlType.Paddle, "paddle")] + [InlineData(ControlType.Pedal, "pedal")] + [InlineData(ControlType.Lightgun, "lightgun")] + [InlineData(ControlType.Positional, "positional")] + [InlineData(ControlType.Dial, "dial")] + [InlineData(ControlType.Trackball, "trackball")] + [InlineData(ControlType.Mouse, "mouse")] + [InlineData(ControlType.OnlyButtons, "only_buttons")] + [InlineData(ControlType.Keypad, "keypad")] + [InlineData(ControlType.Keyboard, "keyboard")] + [InlineData(ControlType.Mahjong, "mahjong")] + [InlineData(ControlType.Hanafuda, "hanafuda")] + [InlineData(ControlType.Gambling, "gambling")] + public void FromControlTypeTest(ControlType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(DeviceType.NULL, null)] + [InlineData(DeviceType.Unknown, "unknown")] + [InlineData(DeviceType.Cartridge, "cartridge")] + [InlineData(DeviceType.FloppyDisk, "floppydisk")] + [InlineData(DeviceType.HardDisk, "harddisk")] + [InlineData(DeviceType.Cylinder, "cylinder")] + [InlineData(DeviceType.Cassette, "cassette")] + [InlineData(DeviceType.PunchCard, "punchcard")] + [InlineData(DeviceType.PunchTape, "punchtape")] + [InlineData(DeviceType.Printout, "printout")] + [InlineData(DeviceType.Serial, "serial")] + [InlineData(DeviceType.Parallel, "parallel")] + [InlineData(DeviceType.Snapshot, "snapshot")] + [InlineData(DeviceType.QuickLoad, "quickload")] + [InlineData(DeviceType.MemCard, "memcard")] + [InlineData(DeviceType.CDROM, "cdrom")] + [InlineData(DeviceType.MagTape, "magtape")] + [InlineData(DeviceType.ROMImage, "romimage")] + [InlineData(DeviceType.MIDIIn, "midiin")] + [InlineData(DeviceType.MIDIOut, "midiout")] + [InlineData(DeviceType.Picture, "picture")] + [InlineData(DeviceType.VidFile, "vidfile")] + public void FromDeviceTypeTest(DeviceType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(DisplayType.NULL, null)] + [InlineData(DisplayType.Raster, "raster")] + [InlineData(DisplayType.Vector, "vector")] + [InlineData(DisplayType.LCD, "lcd")] + [InlineData(DisplayType.SVG, "svg")] + [InlineData(DisplayType.Unknown, "unknown")] + public void FromDisplayTypeTest(DisplayType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(Endianness.NULL, null)] + [InlineData(Endianness.Big, "big")] + [InlineData(Endianness.Little, "little")] + public void FromEndiannessTest(Endianness field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(FeatureStatus.NULL, null)] + [InlineData(FeatureStatus.Unemulated, "unemulated")] + [InlineData(FeatureStatus.Imperfect, "imperfect")] + public void FromFeatureStatusTest(FeatureStatus field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(FeatureType.NULL, null)] + [InlineData(FeatureType.Protection, "protection")] + [InlineData(FeatureType.Palette, "palette")] + [InlineData(FeatureType.Graphics, "graphics")] + [InlineData(FeatureType.Sound, "sound")] + [InlineData(FeatureType.Controls, "controls")] + [InlineData(FeatureType.Keyboard, "keyboard")] + [InlineData(FeatureType.Mouse, "mouse")] + [InlineData(FeatureType.Microphone, "microphone")] + [InlineData(FeatureType.Camera, "camera")] + [InlineData(FeatureType.Disk, "disk")] + [InlineData(FeatureType.Printer, "printer")] + [InlineData(FeatureType.Lan, "lan")] + [InlineData(FeatureType.Wan, "wan")] + [InlineData(FeatureType.Timing, "timing")] + public void FromFeatureTypeTest(FeatureType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(ItemStatus.NULL, null)] + [InlineData(ItemStatus.None, "none")] + [InlineData(ItemStatus.Good, "good")] + [InlineData(ItemStatus.BadDump, "baddump")] + [InlineData(ItemStatus.Nodump, "nodump")] + [InlineData(ItemStatus.Verified, "verified")] + public void FromItemStatusTest(ItemStatus field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(ItemType.NULL, null)] + [InlineData(ItemType.Adjuster, "adjuster")] + [InlineData(ItemType.Analog, "analog")] + [InlineData(ItemType.Archive, "archive")] + [InlineData(ItemType.BiosSet, "biosset")] + [InlineData(ItemType.Blank, "blank")] + [InlineData(ItemType.Chip, "chip")] + [InlineData(ItemType.Condition, "condition")] + [InlineData(ItemType.Configuration, "configuration")] + [InlineData(ItemType.ConfLocation, "conflocation")] + [InlineData(ItemType.ConfSetting, "confsetting")] + [InlineData(ItemType.Control, "control")] + [InlineData(ItemType.DataArea, "dataarea")] + [InlineData(ItemType.Device, "device")] + [InlineData(ItemType.DeviceRef, "device_ref")] + [InlineData(ItemType.DipLocation, "diplocation")] + [InlineData(ItemType.DipSwitch, "dipswitch")] + [InlineData(ItemType.DipValue, "dipvalue")] + [InlineData(ItemType.Disk, "disk")] + [InlineData(ItemType.DiskArea, "diskarea")] + [InlineData(ItemType.Display, "display")] + [InlineData(ItemType.Driver, "driver")] + [InlineData(ItemType.Extension, "extension")] + [InlineData(ItemType.Feature, "feature")] + [InlineData(ItemType.File, "file")] + [InlineData(ItemType.Info, "info")] + [InlineData(ItemType.Input, "input")] + [InlineData(ItemType.Instance, "instance")] + [InlineData(ItemType.Media, "media")] + [InlineData(ItemType.Part, "part")] + [InlineData(ItemType.PartFeature, "part_feature")] + [InlineData(ItemType.Port, "port")] + [InlineData(ItemType.RamOption, "ramoption")] + [InlineData(ItemType.Release, "release")] + [InlineData(ItemType.ReleaseDetails, "release_details")] + [InlineData(ItemType.Rom, "rom")] + [InlineData(ItemType.Sample, "sample")] + [InlineData(ItemType.Serials, "serials")] + [InlineData(ItemType.SharedFeat, "sharedfeat")] + [InlineData(ItemType.Slot, "slot")] + [InlineData(ItemType.SlotOption, "slotoption")] + [InlineData(ItemType.SoftwareList, "softwarelist")] + [InlineData(ItemType.Sound, "sound")] + [InlineData(ItemType.SourceDetails, "source_details")] + public void FromItemTypeTest(ItemType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(LoadFlag.NULL, null)] + [InlineData(LoadFlag.Load16Byte, "load16_byte")] + [InlineData(LoadFlag.Load16Word, "load16_word")] + [InlineData(LoadFlag.Load16WordSwap, "load16_word_swap")] + [InlineData(LoadFlag.Load32Byte, "load32_byte")] + [InlineData(LoadFlag.Load32Word, "load32_word")] + [InlineData(LoadFlag.Load32WordSwap, "load32_word_swap")] + [InlineData(LoadFlag.Load32DWord, "load32_dword")] + [InlineData(LoadFlag.Load64Word, "load64_word")] + [InlineData(LoadFlag.Load64WordSwap, "load64_word_swap")] + [InlineData(LoadFlag.Reload, "reload")] + [InlineData(LoadFlag.Fill, "fill")] + [InlineData(LoadFlag.Continue, "continue")] + [InlineData(LoadFlag.ReloadPlain, "reload_plain")] + [InlineData(LoadFlag.Ignore, "ignore")] + public void FromLoadFlagTest(LoadFlag field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(OpenMSXSubType.NULL, null)] + [InlineData(OpenMSXSubType.Rom, "rom")] + [InlineData(OpenMSXSubType.MegaRom, "megarom")] + [InlineData(OpenMSXSubType.SCCPlusCart, "sccpluscart")] + public void FromOpenMSXSubTypeTest(OpenMSXSubType field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(Relation.NULL, null)] + [InlineData(Relation.Equal, "eq")] + [InlineData(Relation.NotEqual, "ne")] + [InlineData(Relation.GreaterThan, "gt")] + [InlineData(Relation.LessThanOrEqual, "le")] + [InlineData(Relation.LessThan, "lt")] + [InlineData(Relation.GreaterThanOrEqual, "ge")] + public void FromRelationTest(Relation field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(Runnable.NULL, null)] + [InlineData(Runnable.No, "no")] + [InlineData(Runnable.Partial, "partial")] + [InlineData(Runnable.Yes, "yes")] + public void FromRunnableTest(Runnable field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(SoftwareListStatus.None, "none")] + [InlineData(SoftwareListStatus.Original, "original")] + [InlineData(SoftwareListStatus.Compatible, "compatible")] + public void FromSoftwareListStatusTest(SoftwareListStatus field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(Supported.NULL, true, null)] + [InlineData(Supported.NULL, false, null)] + [InlineData(Supported.No, true, "unsupported")] + [InlineData(Supported.No, false, "no")] + [InlineData(Supported.Partial, true, "partial")] + [InlineData(Supported.Partial, false, "partial")] + [InlineData(Supported.Yes, true, "supported")] + [InlineData(Supported.Yes, false, "yes")] + public void FromSupportedTest(Supported field, bool useSecond, string? expected) + { + string? actual = field.AsStringValue(useSecond); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(SupportStatus.NULL, null)] + [InlineData(SupportStatus.Good, "good")] + [InlineData(SupportStatus.Imperfect, "imperfect")] + [InlineData(SupportStatus.Preliminary, "preliminary")] + public void FromSupportStatusTest(SupportStatus field, string? expected) + { + string? actual = field.AsStringValue(); + Assert.Equal(expected, actual); + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.DatItems/Extensions.cs b/SabreTools.DatItems/Extensions.cs index a8bb88b2..78b1a4c0 100644 --- a/SabreTools.DatItems/Extensions.cs +++ b/SabreTools.DatItems/Extensions.cs @@ -10,87 +10,172 @@ namespace SabreTools.DatItems /// /// Set of enum to string mappings for ChipType /// - private static readonly Dictionary _chipTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toChipTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for ChipType + /// + private static readonly Dictionary _fromChipTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ControlType /// - private static readonly Dictionary _controlTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toControlTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for ControlType + /// + private static readonly Dictionary _fromControlTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for DeviceType /// - private static readonly Dictionary _deviceTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toDeviceTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for DeviceType + /// + private static readonly Dictionary _fromDeviceTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for DisplayType /// - private static readonly Dictionary _displayTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toDisplayTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for DisplayType + /// + private static readonly Dictionary _fromDisplayTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Endianness /// - private static readonly Dictionary _endiannessMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toEndiannessMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for Endianness + /// + private static readonly Dictionary _fromEndiannessMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for FeatureStatus /// - private static readonly Dictionary _featureStatusMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toFeatureStatusMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for FeatureStatus + /// + private static readonly Dictionary _fromFeatureStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for FeatureType /// - private static readonly Dictionary _featureTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toFeatureTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for FeatureType + /// + private static readonly Dictionary _fromFeatureTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ItemStatus /// - private static readonly Dictionary _itemStatusMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toItemStatusMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for ItemStatus + /// + private static readonly Dictionary _fromItemStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ItemType /// - private static readonly Dictionary _itemTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toItemTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for ItemType + /// + private static readonly Dictionary _fromItemTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for LoadFlag /// - private static readonly Dictionary _loadFlagMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toLoadFlagMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for LoadFlag + /// + private static readonly Dictionary _fromLoadFlagMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for MachineType /// - private static readonly Dictionary _machineTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toMachineTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for OpenMSXSubType /// - private static readonly Dictionary _openMSXSubTypeMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toOpenMSXSubTypeMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for OpenMSXSubType + /// + private static readonly Dictionary _fromOpenMSXSubTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Relation /// - private static readonly Dictionary _relationMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toRelationMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for Relation + /// + private static readonly Dictionary _fromRelationMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Runnable /// - private static readonly Dictionary _runnableMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toRunnableMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for Runnable + /// + private static readonly Dictionary _fromRunnableMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for SoftwareListStatus /// - private static readonly Dictionary _softwareListStatusMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toSoftwareListStatusMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for SoftwareListStatus + /// + private static readonly Dictionary _fromSoftwareListStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Supported /// - private static readonly Dictionary _supportedMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toSupportedMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for Supported + /// + private static readonly Dictionary _fromSupportedMap = Converters.GenerateToString(useSecond: false); + + /// + /// Set of string to enum mappings for Supported (secondary) + /// + private static readonly Dictionary _fromSupportedSecondaryMap = Converters.GenerateToString(useSecond: true); /// /// Set of enum to string mappings for SupportStatus /// - private static readonly Dictionary _supportStatusMap = Converters.GenerateToEnum(); + private static readonly Dictionary _toSupportStatusMap = Converters.GenerateToEnum(); + + /// + /// Set of string to enum mappings for SupportStatus + /// + private static readonly Dictionary _fromSupportStatusMap = Converters.GenerateToString(useSecond: false); #endregion @@ -100,7 +185,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static ChipType AsChipType(this string? value) { @@ -110,8 +194,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_chipTypeMap.ContainsKey(value)) - return _chipTypeMap[value]; + if (_toChipTypeMap.ContainsKey(value)) + return _toChipTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -121,7 +205,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static ControlType AsControlType(this string? value) { @@ -131,8 +214,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_controlTypeMap.ContainsKey(value)) - return _controlTypeMap[value]; + if (_toControlTypeMap.ContainsKey(value)) + return _toControlTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -142,7 +225,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static DeviceType AsDeviceType(this string? value) { @@ -152,8 +234,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_deviceTypeMap.ContainsKey(value)) - return _deviceTypeMap[value]; + if (_toDeviceTypeMap.ContainsKey(value)) + return _toDeviceTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -163,7 +245,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static DisplayType AsDisplayType(this string? value) { @@ -173,8 +254,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_displayTypeMap.ContainsKey(value)) - return _displayTypeMap[value]; + if (_toDisplayTypeMap.ContainsKey(value)) + return _toDisplayTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -184,7 +265,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static Endianness AsEndianness(this string? value) { @@ -194,8 +274,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_endiannessMap.ContainsKey(value)) - return _endiannessMap[value]; + if (_toEndiannessMap.ContainsKey(value)) + return _toEndiannessMap[value]; // Otherwise, return the default value for the enum return default; @@ -205,7 +285,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static FeatureStatus AsFeatureStatus(this string? value) { @@ -215,8 +294,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_featureStatusMap.ContainsKey(value)) - return _featureStatusMap[value]; + if (_toFeatureStatusMap.ContainsKey(value)) + return _toFeatureStatusMap[value]; // Otherwise, return the default value for the enum return default; @@ -226,7 +305,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static FeatureType AsFeatureType(this string? value) { @@ -236,8 +314,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_featureTypeMap.ContainsKey(value)) - return _featureTypeMap[value]; + if (_toFeatureTypeMap.ContainsKey(value)) + return _toFeatureTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -247,7 +325,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static ItemStatus AsItemStatus(this string? value) { @@ -257,8 +334,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_itemStatusMap.ContainsKey(value)) - return _itemStatusMap[value]; + if (_toItemStatusMap.ContainsKey(value)) + return _toItemStatusMap[value]; // Otherwise, return the default value for the enum return default; @@ -268,7 +345,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static ItemType AsItemType(this string? value) { @@ -278,8 +354,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_itemTypeMap.ContainsKey(value)) - return _itemTypeMap[value]; + if (_toItemTypeMap.ContainsKey(value)) + return _toItemTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -289,7 +365,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static LoadFlag AsLoadFlag(this string? value) { @@ -299,8 +374,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_loadFlagMap.ContainsKey(value)) - return _loadFlagMap[value]; + if (_toLoadFlagMap.ContainsKey(value)) + return _toLoadFlagMap[value]; // Otherwise, return the default value for the enum return default; @@ -310,7 +385,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static MachineType AsMachineType(this string? value) { @@ -320,8 +394,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_machineTypeMap.ContainsKey(value)) - return _machineTypeMap[value]; + if (_toMachineTypeMap.ContainsKey(value)) + return _toMachineTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -331,7 +405,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static OpenMSXSubType AsOpenMSXSubType(this string? value) { @@ -341,8 +414,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_openMSXSubTypeMap.ContainsKey(value)) - return _openMSXSubTypeMap[value]; + if (_toOpenMSXSubTypeMap.ContainsKey(value)) + return _toOpenMSXSubTypeMap[value]; // Otherwise, return the default value for the enum return default; @@ -352,7 +425,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static Relation AsRelation(this string? value) { @@ -362,8 +434,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_relationMap.ContainsKey(value)) - return _relationMap[value]; + if (_toRelationMap.ContainsKey(value)) + return _toRelationMap[value]; // Otherwise, return the default value for the enum return default; @@ -373,7 +445,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static Runnable AsRunnable(this string? value) { @@ -383,8 +454,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_runnableMap.ContainsKey(value)) - return _runnableMap[value]; + if (_toRunnableMap.ContainsKey(value)) + return _toRunnableMap[value]; // Otherwise, return the default value for the enum return default; @@ -394,7 +465,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static SoftwareListStatus AsSoftwareListStatus(this string? value) { @@ -404,8 +474,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_softwareListStatusMap.ContainsKey(value)) - return _softwareListStatusMap[value]; + if (_toSoftwareListStatusMap.ContainsKey(value)) + return _toSoftwareListStatusMap[value]; // Otherwise, return the default value for the enum return default; @@ -415,7 +485,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static Supported AsSupported(this string? value) { @@ -425,8 +494,8 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_supportedMap.ContainsKey(value)) - return _supportedMap[value]; + if (_toSupportedMap.ContainsKey(value)) + return _toSupportedMap[value]; // Otherwise, return the default value for the enum return default; @@ -436,7 +505,6 @@ namespace SabreTools.DatItems /// Get the enum value for an input string, if possible /// /// String value to parse/param> - /// Enum type that is expected /// Enum value representing the input, default on error public static SupportStatus AsSupportStatus(this string? value) { @@ -446,13 +514,275 @@ namespace SabreTools.DatItems return default; // Try to get the value from the mappings - if (_supportStatusMap.ContainsKey(value)) - return _supportStatusMap[value]; + if (_toSupportStatusMap.ContainsKey(value)) + return _toSupportStatusMap[value]; // Otherwise, return the default value for the enum return default; } #endregion + + #region Enum to String + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this ChipType value) + { + // Try to get the value from the mappings + if (_fromChipTypeMap.ContainsKey(value)) + return _fromChipTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this ControlType value) + { + // Try to get the value from the mappings + if (_fromControlTypeMap.ContainsKey(value)) + return _fromControlTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this DeviceType value) + { + // Try to get the value from the mappings + if (_fromDeviceTypeMap.ContainsKey(value)) + return _fromDeviceTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this DisplayType value) + { + // Try to get the value from the mappings + if (_fromDisplayTypeMap.ContainsKey(value)) + return _fromDisplayTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this Endianness value) + { + // Try to get the value from the mappings + if (_fromEndiannessMap.ContainsKey(value)) + return _fromEndiannessMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this FeatureStatus value) + { + // Try to get the value from the mappings + if (_fromFeatureStatusMap.ContainsKey(value)) + return _fromFeatureStatusMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this FeatureType value) + { + // Try to get the value from the mappings + if (_fromFeatureTypeMap.ContainsKey(value)) + return _fromFeatureTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this ItemStatus value) + { + // Try to get the value from the mappings + if (_fromItemStatusMap.ContainsKey(value)) + return _fromItemStatusMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this ItemType value) + { + // Try to get the value from the mappings + if (_fromItemTypeMap.ContainsKey(value)) + return _fromItemTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this LoadFlag value) + { + // Try to get the value from the mappings + if (_fromLoadFlagMap.ContainsKey(value)) + return _fromLoadFlagMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this OpenMSXSubType value) + { + // Try to get the value from the mappings + if (_fromOpenMSXSubTypeMap.ContainsKey(value)) + return _fromOpenMSXSubTypeMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this Relation value) + { + // Try to get the value from the mappings + if (_fromRelationMap.ContainsKey(value)) + return _fromRelationMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this Runnable value) + { + // Try to get the value from the mappings + if (_fromRunnableMap.ContainsKey(value)) + return _fromRunnableMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this SoftwareListStatus value) + { + // Try to get the value from the mappings + if (_fromSoftwareListStatusMap.ContainsKey(value)) + return _fromSoftwareListStatusMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this Supported value, bool useSecond = false) + { + // Try to get the value from the mappings + if (!useSecond && _fromSupportedMap.ContainsKey(value)) + return _fromSupportedMap[value]; + else if (useSecond && _fromSupportedSecondaryMap.ContainsKey(value)) + return _fromSupportedSecondaryMap[value]; + + // Otherwise, return null + return null; + } + + /// + /// Get the string value for an input enum, if possible + /// + /// Enum value to parse/param> + /// True to use the second mapping option, if it exists + /// String value representing the input, default on error + public static string? AsStringValue(this SupportStatus value) + { + // Try to get the value from the mappings + if (_fromSupportStatusMap.ContainsKey(value)) + return _fromSupportStatusMap[value]; + + // Otherwise, return null + return null; + } + + #endregion } } \ No newline at end of file