Finish filling out first round of Extension tests

This commit is contained in:
Matt Nadareski
2021-11-24 23:36:12 -08:00
parent 88a0ce38f9
commit 3b2e14d0de
5 changed files with 158 additions and 8 deletions

View File

@@ -32,6 +32,7 @@
- Update to DIC 20211101
- Add protection sanitization for StarForce
- Trim filenames for DVD protection from DIC
- Fill out internal tests around Redump library
### 2.1 (2021-07-22)
- Enum, no more

View File

@@ -360,7 +360,7 @@ namespace MPF.Test.RedumpLib
/// <summary>
/// Check that every Region has a long name provided
/// </summary>
/// <param name="mediaType">Region value to check</param>
/// <param name="region">Region value to check</param>
/// <param name="expectNull">True to expect a null value, false otherwise</param>
[Theory]
[MemberData(nameof(GenerateRegionTestData))]
@@ -407,5 +407,154 @@ namespace MPF.Test.RedumpLib
}
#endregion
#region System
/// <summary>
/// RedumpSystem values that are considered markers and not real systems
/// </summary>
private static readonly RedumpSystem?[] _markerSystemTypes = new RedumpSystem?[]
{
RedumpSystem.MarkerArcadeEnd,
RedumpSystem.MarkerComputerEnd,
RedumpSystem.MarkerDiscBasedConsoleEnd,
RedumpSystem.MarkerOtherEnd,
};
/// <summary>
/// Check that every RedumpSystem has a long name provided
/// </summary>
/// <param name="redumpSystem">RedumpSystem value to check</param>
/// <param name="expectNull">True to expect a null value, false otherwise</param>
[Theory]
[MemberData(nameof(GenerateRedumpSystemTestData))]
public void RedumpSystemLongNameTest(RedumpSystem? redumpSystem, bool expectNull)
{
string actual = redumpSystem.LongName();
if (expectNull)
Assert.Null(actual);
else
Assert.NotNull(actual);
}
// TODO: Re-enable the following test once non-Redump systems are accounted for
/// <summary>
/// Check that every RedumpSystem has a short name provided
/// </summary>
/// <param name="redumpSystem">RedumpSystem value to check</param>
/// <param name="expectNull">True to expect a null value, false otherwise</param>
//[Theory]
//[MemberData(nameof(GenerateRedumpSystemTestData))]
//public void RedumpSystemShortNameTest(RedumpSystem? redumpSystem, bool expectNull)
//{
// string actual = redumpSystem.ShortName();
// if (expectNull)
// Assert.Null(actual);
// else
// Assert.NotNull(actual);
//}
// TODO: Test the other attributes as well
// Most are bool checks so they're not as interesting to have unit tests around
// SystemCategory always returns something as well, so is it worth testing?
/// <summary>
/// Generate a test set of RedumpSystem values
/// </summary>
/// <returns>MemberData-compatible list of RedumpSystem values</returns>
public static List<object[]> GenerateRedumpSystemTestData()
{
var testData = new List<object[]>() { new object[] { null, true } };
foreach (RedumpSystem? redumpSystem in Enum.GetValues(typeof(RedumpSystem)))
{
// We want to skip all markers for this
if (_markerSystemTypes.Contains(redumpSystem))
continue;
testData.Add(new object[] { redumpSystem, false });
}
return testData;
}
#endregion
#region System Category
/// <summary>
/// Check that every SystemCategory has a long name provided
/// </summary>
/// <param name="systemCategory">SystemCategory value to check</param>
/// <param name="expectNull">True to expect a null value, false otherwise</param>
[Theory]
[MemberData(nameof(GenerateSystemCategoryTestData))]
public void SystemCategoryLongNameTest(SystemCategory? systemCategory, bool expectNull)
{
string actual = systemCategory.LongName();
if (expectNull)
Assert.Null(actual);
else
Assert.NotNull(actual);
}
/// <summary>
/// Generate a test set of SystemCategory values
/// </summary>
/// <returns>MemberData-compatible list of SystemCategory values</returns>
public static List<object[]> GenerateSystemCategoryTestData()
{
var testData = new List<object[]>() { new object[] { null, true } };
foreach (SystemCategory? systemCategory in Enum.GetValues(typeof(SystemCategory)))
{
if (systemCategory == SystemCategory.NONE)
testData.Add(new object[] { systemCategory, true });
else
testData.Add(new object[] { systemCategory, false });
}
return testData;
}
#endregion
#region Yes/No
/// <summary>
/// Check that every YesNo has a long name provided
/// </summary>
/// <param name="yesNo">YesNo value to check</param>
/// <param name="expectNull">True to expect a null value, false otherwise</param>
[Theory]
[MemberData(nameof(GenerateYesNoTestData))]
public void YesNoLongNameTest(YesNo? yesNo, bool expectNull)
{
string actual = yesNo.LongName();
if (expectNull)
Assert.Null(actual);
else
Assert.NotNull(actual);
}
/// <summary>
/// Generate a test set of YesNo values
/// </summary>
/// <returns>MemberData-compatible list of YesNo values</returns>
public static List<object[]> GenerateYesNoTestData()
{
var testData = new List<object[]>() { new object[] { null, false } };
foreach (YesNo? yesNo in Enum.GetValues(typeof(YesNo)))
{
testData.Add(new object[] { yesNo, false });
}
return testData;
}
#endregion
}
}

View File

@@ -8,16 +8,16 @@ namespace RedumpLib.Converters
/// <summary>
/// Serialize YesNo enum values
/// </summary>
public class YesNoConverter : JsonConverter<YesNo>
public class YesNoConverter : JsonConverter<YesNo?>
{
public override bool CanRead { get { return false; } }
public override YesNo ReadJson(JsonReader reader, Type objectType, YesNo existingValue, bool hasExistingValue, JsonSerializer serializer)
public override YesNo? ReadJson(JsonReader reader, Type objectType, YesNo? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, YesNo value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, YesNo? value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value.LongName() ?? string.Empty);
t.WriteTo(writer);

View File

@@ -2138,7 +2138,7 @@ namespace RedumpLib.Data
/// </summary>
/// <param name="yesno"></param>
/// <returns></returns>
public static string LongName(this YesNo yesno) => AttributeHelper<YesNo>.GetAttribute(yesno)?.LongName ?? "Yes/No";
public static string LongName(this YesNo? yesno) => AttributeHelper<YesNo?>.GetAttribute(yesno)?.LongName ?? "Yes/No";
/// <summary>
/// Get the YesNo enum value for a given string

View File

@@ -296,7 +296,7 @@ namespace RedumpLib.Data
{
[JsonProperty(PropertyName = "d_edc", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(YesNoConverter))]
public YesNo EDC { get; set; }
public YesNo? EDC { get; set; }
public object Clone()
{
@@ -376,11 +376,11 @@ namespace RedumpLib.Data
{
[JsonProperty(PropertyName = "d_protection_a", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(YesNoConverter))]
public YesNo AntiModchip { get; set; }
public YesNo? AntiModchip { get; set; }
[JsonProperty(PropertyName = "d_protection_1", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(YesNoConverter))]
public YesNo LibCrypt { get; set; }
public YesNo? LibCrypt { get; set; }
[JsonProperty(PropertyName = "d_libcrypt", NullValueHandling = NullValueHandling.Ignore)]
public string LibCryptData { get; set; }