Update enum handling, part 3

This commit is contained in:
Matt Nadareski
2023-04-20 13:54:59 -04:00
parent 0594a6296c
commit a2958a703c
3 changed files with 46 additions and 6 deletions

View File

@@ -4,6 +4,10 @@
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> <TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="SabreTools.Test" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup> </ItemGroup>

View File

@@ -317,7 +317,7 @@ namespace SabreTools.Core.Tools
/// <param name="value">String value to parse/param> /// <param name="value">String value to parse/param>
/// <typeparam name="T">Enum type that is expected</typeparam> /// <typeparam name="T">Enum type that is expected</typeparam>
/// <returns>Enum value representing the input, default on error</returns> /// <returns>Enum value representing the input, default on error</returns>
private static T AsEnumValue<T>(string value) internal static T AsEnumValue<T>(string value)
{ {
// Get the mapping dictionary // Get the mapping dictionary
var mappings = GenerateToEnum<T>(); var mappings = GenerateToEnum<T>();
@@ -334,13 +334,13 @@ namespace SabreTools.Core.Tools
// Otherwise, return the default value for the enum // Otherwise, return the default value for the enum
return default; return default;
} }
/// <summary> /// <summary>
/// Get a set of mappings from strings to enum values /// Get a set of mappings from strings to enum values
/// </summary> /// </summary>
/// <typeparam name="T">Enum type that is expected</typeparam> /// <typeparam name="T">Enum type that is expected</typeparam>
/// <returns>Dictionary of string to enum values</returns> /// <returns>Dictionary of string to enum values</returns>
private static Dictionary<string, T> GenerateToEnum<T>() internal static Dictionary<string, T> GenerateToEnum<T>()
{ {
try try
{ {
@@ -565,7 +565,7 @@ namespace SabreTools.Core.Tools
/// <param name="useSecond">True to use the second mapping option, if it exists</param> /// <param name="useSecond">True to use the second mapping option, if it exists</param>
/// <typeparam name="T">Enum type that is expected</typeparam> /// <typeparam name="T">Enum type that is expected</typeparam>
/// <returns>String value representing the input, default on error</returns> /// <returns>String value representing the input, default on error</returns>
private static string? AsStringValue<T>(T value, bool useSecond = false) internal static string? AsStringValue<T>(T value, bool useSecond = false)
{ {
// Get the mapping dictionary // Get the mapping dictionary
var mappings = GenerateToString<T>(useSecond); var mappings = GenerateToString<T>(useSecond);
@@ -577,14 +577,14 @@ namespace SabreTools.Core.Tools
// Otherwise, return null // Otherwise, return null
return null; return null;
} }
/// <summary> /// <summary>
/// Get a set of mappings from enum values to string /// Get a set of mappings from enum values to string
/// </summary> /// </summary>
/// <param name="useSecond">True to use the second mapping option, if it exists</param> /// <param name="useSecond">True to use the second mapping option, if it exists</param>
/// <typeparam name="T">Enum type that is expected</typeparam> /// <typeparam name="T">Enum type that is expected</typeparam>
/// <returns>Dictionary of enum to string values</returns> /// <returns>Dictionary of enum to string values</returns>
private static Dictionary<T, string> GenerateToString<T>(bool useSecond) internal static Dictionary<T, string> GenerateToString<T>(bool useSecond)
{ {
try try
{ {

View File

@@ -857,5 +857,41 @@ namespace SabreTools.Test.Core
} }
#endregion #endregion
#region Generators
[Theory]
[InlineData(ChipType.NULL, 2)]
[InlineData(ControlType.NULL, 15)]
[InlineData(DatHeaderField.NULL, 94)]
[InlineData(DatItemField.NULL, 197)]
[InlineData(DeviceType.NULL, 21)]
[InlineData(DisplayType.NULL, 5)]
[InlineData(Endianness.NULL, 2)]
[InlineData(FeatureStatus.NULL, 2)]
[InlineData(FeatureType.NULL, 14)]
[InlineData(ItemStatus.NULL, 7)]
[InlineData(ItemType.NULL, 51)]
[InlineData(LoadFlag.NULL, 14)]
[InlineData(LogLevel.VERBOSE, 4)]
[InlineData(MachineField.NULL, 68)]
[InlineData(MachineType.None, 6)]
[InlineData(MergingFlag.None, 12)]
[InlineData(NodumpFlag.None, 4)]
[InlineData(OpenMSXSubType.NULL, 3)]
[InlineData(PackingFlag.None, 7)]
[InlineData(Relation.NULL, 6)]
[InlineData(Runnable.NULL, 3)]
[InlineData(SoftwareListStatus.None, 3)]
[InlineData(Supported.NULL, 5)]
[InlineData(SupportStatus.NULL, 3)]
public void GenerateToEnumTest<T>(T value, int expected)
{
var actual = Converters.GenerateToEnum<T>();
Assert.Equal(default, value);
Assert.Equal(expected, actual.Keys.Count);
}
#endregion
} }
} }