Add tests for Core; fix found issues

This commit is contained in:
Matt Nadareski
2025-01-04 19:47:39 -05:00
parent acfd6b1d11
commit f5e2d8a11c
24 changed files with 3531 additions and 293 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Core.Filter;
using Xunit;
namespace SabreTools.Core.Test.Filter
{
public class ExtraIniItemTests
{
[Fact]
public void Constructor_EmptyPath_NoMappings()
{
string iniPath = string.Empty;
ExtraIniItem extraIniItem = new ExtraIniItem("Sample.Name", iniPath);
Assert.Empty(extraIniItem.Mappings);
}
[Fact]
public void Constructor_InvalidPath_NoMappings()
{
string iniPath = "INVALID";
ExtraIniItem extraIniItem = new ExtraIniItem("Sample.Name", iniPath);
Assert.Empty(extraIniItem.Mappings);
}
[Fact]
public void Constructor_ValidPath_Mappings()
{
string iniPath = Path.Combine(Environment.CurrentDirectory, "TestData", "extra.ini");
ExtraIniItem extraIniItem = new ExtraIniItem("Sample.Name", iniPath);
Dictionary<string, string> mappings = extraIniItem.Mappings;
Assert.NotEmpty(mappings);
Assert.Equal("true", mappings["useBool"]);
Assert.Equal("Value", mappings["useValue"]);
Assert.Equal("Other", mappings["useOther"]);
}
}
}

View File

@@ -0,0 +1,192 @@
using SabreTools.Core.Filter;
using SabreTools.Models.Metadata;
using Xunit;
namespace SabreTools.Core.Test.Filter
{
public class FieldManipulatorTests
{
#region RemoveField
[Fact]
public void RemoveField_NullItem_False()
{
DictionaryBase? dictionaryBase = null;
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.RemoveField(dictionaryBase, fieldName);
Assert.False(actual);
}
[Fact]
public void RemoveField_NullFieldName_False()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = null;
bool actual = FieldManipulator.RemoveField(dictionaryBase, fieldName);
Assert.False(actual);
}
[Fact]
public void RemoveField_EmptyFieldName_False()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = string.Empty;
bool actual = FieldManipulator.RemoveField(dictionaryBase, fieldName);
Assert.False(actual);
}
[Fact]
public void RemoveField_MissingKey_True()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.RemoveField(dictionaryBase, fieldName);
Assert.True(actual);
Assert.DoesNotContain(fieldName, dictionaryBase.Keys);
}
[Fact]
public void RemoveField_ValidKey_True()
{
DictionaryBase? dictionaryBase = new Sample { [Sample.NameKey] = "value" };
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.RemoveField(dictionaryBase, fieldName);
Assert.True(actual);
Assert.DoesNotContain(fieldName, dictionaryBase.Keys);
}
#endregion
#region ReplaceField
[Fact]
public void ReplaceField_NullFrom_False()
{
DictionaryBase? from = null;
DictionaryBase? to = new Sample();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_NullTo_False()
{
DictionaryBase? from = null;
DictionaryBase? to = new Sample();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_NullFieldName_False()
{
DictionaryBase? from = new Sample();
DictionaryBase? to = new Sample();
string? fieldName = null;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_EmptyFieldName_False()
{
DictionaryBase? from = new Sample();
DictionaryBase? to = new Sample();
string? fieldName = string.Empty;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_MismatchedTypes_False()
{
DictionaryBase? from = new Sample();
DictionaryBase? to = new Rom();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_MissingKey_False()
{
DictionaryBase? from = new Sample();
DictionaryBase? to = new Sample();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.False(actual);
}
[Fact]
public void ReplaceField_ValidKey_True()
{
DictionaryBase? from = new Sample { [Sample.NameKey] = "value" };
DictionaryBase? to = new Sample();
string? fieldName = Sample.NameKey;
bool actual = FieldManipulator.ReplaceField(from, to, fieldName);
Assert.True(actual);
Assert.Contains(fieldName, to.Keys);
Assert.Equal("value", to[Sample.NameKey]);
}
#endregion
#region SetField
[Fact]
public void SetField_NullItem_False()
{
DictionaryBase? dictionaryBase = null;
string? fieldName = Sample.NameKey;
object value = "value";
bool actual = FieldManipulator.SetField(dictionaryBase, fieldName, value);
Assert.False(actual);
}
[Fact]
public void SetField_NullFieldName_False()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = null;
object value = "value";
bool actual = FieldManipulator.SetField(dictionaryBase, fieldName, value);
Assert.False(actual);
}
[Fact]
public void SetField_EmptyFieldName_False()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = string.Empty;
object value = "value";
bool actual = FieldManipulator.SetField(dictionaryBase, fieldName, value);
Assert.False(actual);
}
[Fact]
public void SetField_MissingKey_False()
{
DictionaryBase? dictionaryBase = new Sample();
string? fieldName = Rom.SHA1Key;
object value = "value";
bool actual = FieldManipulator.SetField(dictionaryBase, fieldName, value);
Assert.False(actual);
}
[Fact]
public void SetField_ValidKey_True()
{
DictionaryBase? dictionaryBase = new Sample { [Sample.NameKey] = "old" };
string? fieldName = Sample.NameKey;
object value = "value";
bool actual = FieldManipulator.SetField(dictionaryBase, fieldName, value);
Assert.True(actual);
Assert.Contains(fieldName, dictionaryBase.Keys);
Assert.Equal(value, dictionaryBase[fieldName]);
}
#endregion
}
}

View File

@@ -0,0 +1,82 @@
using System;
using SabreTools.Core.Filter;
using Xunit;
namespace SabreTools.Core.Test.Filter
{
public class FilterKeyTests
{
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("ItemName")]
[InlineData("ItemName.FieldName.Extra")]
[InlineData("InvalidItemName.FieldName")]
[InlineData("DatItem.InvalidFieldName")]
[InlineData("Item.InvalidFieldName")]
[InlineData("Sample.InvalidFieldName")]
public void Constructor_InvalidKey_Throws(string? key)
{
Assert.Throws<ArgumentException>(() => new FilterKey(key));
}
[Theory]
[InlineData("header.name", "header", "name")]
[InlineData("HEADER.NAME", "header", "name")]
[InlineData("game.name", "machine", "name")]
[InlineData("GAME.NAME", "machine", "name")]
[InlineData("machine.name", "machine", "name")]
[InlineData("MACHINE.NAME", "machine", "name")]
[InlineData("resource.name", "machine", "name")]
[InlineData("RESOURCE.NAME", "machine", "name")]
[InlineData("set.name", "machine", "name")]
[InlineData("SET.NAME", "machine", "name")]
[InlineData("datitem.name", "item", "name")]
[InlineData("DATITEM.NAME", "item", "name")]
[InlineData("item.name", "item", "name")]
[InlineData("ITEM.NAME", "item", "name")]
[InlineData("sample.name", "sample", "name")]
[InlineData("SAMPLE.NAME", "sample", "name")]
public void Constructor_ValidKey_Sets(string? key, string expectedItemName, string expectedFieldName)
{
FilterKey filterKey = new FilterKey(key);
Assert.Equal(expectedItemName, filterKey.ItemName);
Assert.Equal(expectedFieldName, filterKey.FieldName);
}
[Theory]
[InlineData("", "FieldName")]
[InlineData("ItemName", "")]
[InlineData("DatItem", "InvalidFieldName")]
[InlineData("Item", "InvalidFieldName")]
[InlineData("Sample", "InvalidFieldName")]
public void Constructor_InvalidNames_Throws(string itemName, string fieldName)
{
Assert.Throws<ArgumentException>(() => new FilterKey(itemName, fieldName));
}
[Theory]
[InlineData("header", "name", "header", "name")]
[InlineData("HEADER", "NAME", "header", "name")]
[InlineData("game", "name", "machine", "name")]
[InlineData("GAME", "NAME", "machine", "name")]
[InlineData("machine", "name", "machine", "name")]
[InlineData("MACHINE", "NAME", "machine", "name")]
[InlineData("resource", "name", "machine", "name")]
[InlineData("RESOURCE", "NAME", "machine", "name")]
[InlineData("set", "name", "machine", "name")]
[InlineData("SET", "NAME", "machine", "name")]
[InlineData("datitem", "name", "item", "name")]
[InlineData("DATITEM", "NAME", "item", "name")]
[InlineData("item", "name", "item", "name")]
[InlineData("ITEM", "NAME", "item", "name")]
[InlineData("sample", "name", "sample", "name")]
[InlineData("SAMPLE", "NAME", "sample", "name")]
public void Constructor_ValidNames_Sets(string itemName, string fieldName, string expectedItemName, string expectedFieldName)
{
FilterKey filterKey = new FilterKey(itemName, fieldName);
Assert.Equal(expectedItemName, filterKey.ItemName);
Assert.Equal(expectedFieldName, filterKey.FieldName);
}
}
}

View File

@@ -0,0 +1,471 @@
using System;
using SabreTools.Core.Filter;
using SabreTools.Models.Metadata;
using Xunit;
namespace SabreTools.Core.Test.Filter
{
public class FilterObjectTests
{
#region Constructor
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("Sample.Name")]
[InlineData("Sample.Name++")]
public void Constructor_InvalidKey_Throws(string? filterString)
{
Assert.Throws<ArgumentException>(() => new FilterObject(filterString));
}
[Theory]
[InlineData("Sample.Name=XXXXXX", Operation.Equals)]
[InlineData("Sample.Name==XXXXXX", Operation.Equals)]
[InlineData("Sample.Name:XXXXXX", Operation.Equals)]
[InlineData("Sample.Name::XXXXXX", Operation.Equals)]
[InlineData("Sample.Name!XXXXXX", Operation.NotEquals)]
[InlineData("Sample.Name!=XXXXXX", Operation.NotEquals)]
[InlineData("Sample.Name!:XXXXXX", Operation.NotEquals)]
[InlineData("Sample.Name>XXXXXX", Operation.GreaterThan)]
[InlineData("Sample.Name>=XXXXXX", Operation.GreaterThanOrEqual)]
[InlineData("Sample.Name<XXXXXX", Operation.LessThan)]
[InlineData("Sample.Name<=XXXXXX", Operation.LessThanOrEqual)]
[InlineData("Sample.Name:!XXXXXX", Operation.NONE)]
[InlineData("Sample.Name=>XXXXXX", Operation.NONE)]
[InlineData("Sample.Name=<XXXXXX", Operation.NONE)]
[InlineData("Sample.Name<>XXXXXX", Operation.NONE)]
[InlineData("Sample.Name><XXXXXX", Operation.NONE)]
public void Constructor_FilterString(string filterString, Operation expected)
{
FilterObject filterObject = new FilterObject(filterString);
Assert.Equal(expected, filterObject.Operation);
}
[Theory]
[InlineData("Sample.Name", "XXXXXX", "=", Operation.Equals)]
[InlineData("Sample.Name", "XXXXXX", "==", Operation.Equals)]
[InlineData("Sample.Name", "XXXXXX", ":", Operation.Equals)]
[InlineData("Sample.Name", "XXXXXX", "::", Operation.Equals)]
[InlineData("Sample.Name", "XXXXXX", "!", Operation.NotEquals)]
[InlineData("Sample.Name", "XXXXXX", "!=", Operation.NotEquals)]
[InlineData("Sample.Name", "XXXXXX", "!:", Operation.NotEquals)]
[InlineData("Sample.Name", "XXXXXX", ">", Operation.GreaterThan)]
[InlineData("Sample.Name", "XXXXXX", ">=", Operation.GreaterThanOrEqual)]
[InlineData("Sample.Name", "XXXXXX", "<", Operation.LessThan)]
[InlineData("Sample.Name", "XXXXXX", "<=", Operation.LessThanOrEqual)]
[InlineData("Sample.Name", "XXXXXX", "@@", Operation.NONE)]
[InlineData("Sample.Name", "XXXXXX", ":!", Operation.NONE)]
[InlineData("Sample.Name", "XXXXXX", "=>", Operation.NONE)]
[InlineData("Sample.Name", "XXXXXX", "=<", Operation.NONE)]
public void Constructor_TripleString(string itemField, string? value, string? operation, Operation expected)
{
FilterObject filterObject = new FilterObject(itemField, value, operation);
Assert.Equal(expected, filterObject.Operation);
}
#endregion
#region MatchesEqual
[Fact]
public void MatchesEqual_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12345" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.345" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesEqual_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.Equals);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
#endregion
#region MatchesNotEqual
[Fact]
public void MatchesNotEqual_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12345" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.345" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesNotEqual_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.NotEquals);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
#endregion
#region MatchesGreaterThan
[Fact]
public void MatchesGreaterThan_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThan_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThan_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThan_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12346" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesGreaterThan_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.346" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesGreaterThan_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThan_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.GreaterThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
#endregion
#region MatchesGreaterThanOrEqual
[Fact]
public void MatchesGreaterThanOrEqual_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12346" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.346" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesGreaterThanOrEqual_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.GreaterThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
#endregion
#region MatchesLessThan
[Fact]
public void MatchesLessThan_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThan_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThan_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThan_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12344" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesLessThan_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.344" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesLessThan_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThan_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.LessThan);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
#endregion
#region MatchesLessThanOrEqual
[Fact]
public void MatchesLessThanOrEqual_NoKey()
{
Sample sample = new Sample();
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThanOrEqual_NoValue()
{
Sample sample = new Sample { [Sample.NameKey] = null };
FilterObject filterObject = new FilterObject("Sample.Name", null, Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThanOrEqual_BoolValue()
{
Sample sample = new Sample { [Sample.NameKey] = "true" };
FilterObject filterObject = new FilterObject("Sample.Name", "yes", Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThanOrEqual_Int64Value()
{
Sample sample = new Sample { [Sample.NameKey] = "12344" };
FilterObject filterObject = new FilterObject("Sample.Name", "12345", Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesLessThanOrEqual_DoubleValue()
{
Sample sample = new Sample { [Sample.NameKey] = "12.344" };
FilterObject filterObject = new FilterObject("Sample.Name", "12.345", Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.True(actual);
}
[Fact]
public void MatchesLessThanOrEqual_RegexValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "^XXXXXX$", Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
[Fact]
public void MatchesLessThanOrEqual_StringValue()
{
Sample sample = new Sample { [Sample.NameKey] = "XXXXXX" };
FilterObject filterObject = new FilterObject("Sample.Name", "XXXXXX", Operation.LessThanOrEqual);
bool actual = filterObject.Matches(sample);
Assert.False(actual);
}
#endregion
}
}

View File

@@ -0,0 +1,220 @@
using SabreTools.Core.Filter;
using SabreTools.Models.Metadata;
using Xunit;
namespace SabreTools.Core.Test.Filter
{
public class FilterRunnerTests
{
private static readonly FilterRunner _filterRunner;
static FilterRunnerTests()
{
FilterObject[] filters =
[
new FilterObject("header.author", "auth", Operation.Equals),
new FilterObject("machine.description", "desc", Operation.Equals),
new FilterObject("item.name", "name", Operation.Equals),
new FilterObject("rom.crc", "crc", Operation.Equals),
];
_filterRunner = new FilterRunner(filters);
}
#region Header
[Fact]
public void Header_Missing_False()
{
Header header = new Header();
bool actual = _filterRunner.Run(header);
Assert.False(actual);
}
[Fact]
public void Header_Null_False()
{
Header header = new Header { [Header.AuthorKey] = null };
bool actual = _filterRunner.Run(header);
Assert.False(actual);
}
[Fact]
public void Header_Empty_False()
{
Header header = new Header { [Header.AuthorKey] = "" };
bool actual = _filterRunner.Run(header);
Assert.False(actual);
}
[Fact]
public void Header_Incorrect_False()
{
Header header = new Header { [Header.AuthorKey] = "NO_MATCH" };
bool actual = _filterRunner.Run(header);
Assert.False(actual);
}
[Fact]
public void Header_Correct_True()
{
Header header = new Header { [Header.AuthorKey] = "auth" };
bool actual = _filterRunner.Run(header);
Assert.True(actual);
}
#endregion
#region Machine
[Fact]
public void Machine_Missing_False()
{
Machine machine = new Machine();
bool actual = _filterRunner.Run(machine);
Assert.False(actual);
}
[Fact]
public void Machine_Null_False()
{
Machine machine = new Machine { [Machine.DescriptionKey] = null };
bool actual = _filterRunner.Run(machine);
Assert.False(actual);
}
[Fact]
public void Machine_Empty_False()
{
Machine machine = new Machine { [Machine.DescriptionKey] = "" };
bool actual = _filterRunner.Run(machine);
Assert.False(actual);
}
[Fact]
public void Machine_Incorrect_False()
{
Machine machine = new Machine { [Machine.DescriptionKey] = "NO_MATCH" };
bool actual = _filterRunner.Run(machine);
Assert.False(actual);
}
[Fact]
public void Machine_Correct_True()
{
Machine machine = new Machine { [Machine.DescriptionKey] = "desc" };
bool actual = _filterRunner.Run(machine);
Assert.True(actual);
}
#endregion
#region DatItem (General)
[Fact]
public void DatItem_Missing_False()
{
DatItem datItem = new Sample();
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void DatItem_Null_False()
{
DatItem datItem = new Sample { [Sample.NameKey] = null };
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void DatItem_Empty_False()
{
DatItem datItem = new Sample { [Sample.NameKey] = "" };
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void DatItem_Incorrect_False()
{
DatItem datItem = new Sample { [Sample.NameKey] = "NO_MATCH" };
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void DatItem_Correct_True()
{
DatItem datItem = new Sample { [Sample.NameKey] = "name" };
bool actual = _filterRunner.Run(datItem);
Assert.True(actual);
}
#endregion
#region DatItem (Specific)
[Fact]
public void Rom_Missing_False()
{
DatItem datItem = new Rom();
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void Rom_Null_False()
{
DatItem datItem = new Rom
{
[Rom.NameKey] = "name",
[Rom.CRCKey] = null,
};
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void Rom_Empty_False()
{
DatItem datItem = new Rom
{
[Rom.NameKey] = "name",
[Rom.CRCKey] = "",
};
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void Rom_Incorrect_False()
{
DatItem datItem = new Rom
{
[Rom.NameKey] = "name",
[Rom.CRCKey] = "NO_MATCH",
};
bool actual = _filterRunner.Run(datItem);
Assert.False(actual);
}
[Fact]
public void Rom_Correct_True()
{
DatItem datItem = new Rom
{
[Rom.NameKey] = "name",
[Rom.CRCKey] = "crc",
};
bool actual = _filterRunner.Run(datItem);
Assert.True(actual);
}
#endregion
}
}

View File

@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.DatFiles\SabreTools.DatFiles.csproj" />
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="TestData\*" />
</ItemGroup>
<ItemGroup>
<Content Include="TestData\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="SabreTools.Hashing" Version="1.4.1" />
<PackageReference Include="SabreTools.Models" Version="1.5.8" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
[FOLDER_SETTINGS]
RootFolderIcon mame
SubFolderIcon folder
[ROOT_FOLDER]
useBool
[Value]
useValue
[Other]
useOther

View File

@@ -0,0 +1,769 @@
using SabreTools.Core.Tools;
using SabreTools.DatFiles;
using SabreTools.DatItems;
using Xunit;
namespace SabreTools.Core.Test.Tools
{
// TODO: Remove reliance on anything but SabreTools.Core
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.AsEnumValue<ChipType>();
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.AsEnumValue<ControlType>();
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.AsEnumValue<DeviceType>();
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.AsEnumValue<DisplayType>();
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.AsEnumValue<Endianness>();
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.AsEnumValue<FeatureStatus>();
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.AsEnumValue<FeatureType>();
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.AsEnumValue<ItemStatus>();
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.AsEnumValue<ItemType>();
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.AsEnumValue<LoadFlag>();
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.AsEnumValue<MachineType>();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, MergingFlag.None)]
[InlineData("none", MergingFlag.None)]
[InlineData("split", MergingFlag.Split)]
[InlineData("merged", MergingFlag.Merged)]
[InlineData("nonmerged", MergingFlag.NonMerged)]
[InlineData("unmerged", MergingFlag.NonMerged)]
[InlineData("fullmerged", MergingFlag.FullMerged)]
[InlineData("device", MergingFlag.DeviceNonMerged)]
[InlineData("devicenonmerged", MergingFlag.DeviceNonMerged)]
[InlineData("deviceunmerged", MergingFlag.DeviceNonMerged)]
[InlineData("full", MergingFlag.FullNonMerged)]
[InlineData("fullnonmerged", MergingFlag.FullNonMerged)]
[InlineData("fullunmerged", MergingFlag.FullNonMerged)]
public void AsMergingFlagTest(string? field, MergingFlag expected)
{
MergingFlag actual = field.AsEnumValue<MergingFlag>();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, NodumpFlag.None)]
[InlineData("none", NodumpFlag.None)]
[InlineData("obsolete", NodumpFlag.Obsolete)]
[InlineData("required", NodumpFlag.Required)]
[InlineData("ignore", NodumpFlag.Ignore)]
public void AsNodumpFlagTest(string? field, NodumpFlag expected)
{
NodumpFlag actual = field.AsEnumValue<NodumpFlag>();
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.AsEnumValue<OpenMSXSubType>();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, PackingFlag.None)]
[InlineData("none", PackingFlag.None)]
[InlineData("yes", PackingFlag.Zip)]
[InlineData("zip", PackingFlag.Zip)]
[InlineData("no", PackingFlag.Unzip)]
[InlineData("unzip", PackingFlag.Unzip)]
[InlineData("partial", PackingFlag.Partial)]
[InlineData("flat", PackingFlag.Flat)]
[InlineData("fileonly", PackingFlag.FileOnly)]
public void AsPackingFlagTest(string? field, PackingFlag expected)
{
PackingFlag actual = field.AsEnumValue<PackingFlag>();
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.AsEnumValue<Relation>();
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.AsEnumValue<Runnable>();
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.AsEnumValue<SoftwareListStatus>();
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.AsEnumValue<Supported>();
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.AsEnumValue<SupportStatus>();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, null)]
[InlineData("INVALID", null)]
[InlineData("yes", true)]
[InlineData("True", true)]
[InlineData("no", false)]
[InlineData("False", false)]
public void AsYesNoTest(string? field, bool? expected)
{
bool? actual = field.AsYesNo();
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<ChipType>();
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<ControlType>();
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<DeviceType>();
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<DisplayType>();
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<Endianness>();
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<FeatureStatus>();
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<FeatureType>();
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<ItemStatus>(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<ItemType>();
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<LoadFlag>();
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<MachineType>(old);
Assert.Equal(expected, actual);
}
[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<MergingFlag>(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<NodumpFlag>();
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<OpenMSXSubType>();
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<PackingFlag>(useSecond);
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<Relation>();
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<Runnable>();
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<SoftwareListStatus>();
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<Supported>(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<SupportStatus>();
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, null)]
[InlineData(true, "yes")]
[InlineData(false, "no")]
public void FromYesNo(bool? field, string? expected)
{
string? actual = field.FromYesNo();
Assert.Equal(expected, actual);
}
#endregion
#region Generators
[Theory]
[InlineData(ChipType.NULL, 2)]
[InlineData(ControlType.NULL, 15)]
[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, 54)]
[InlineData(LoadFlag.NULL, 14)]
[InlineData(MachineType.None, 6)]
[InlineData(MergingFlag.None, 12)]
[InlineData(NodumpFlag.None, 4)]
[InlineData(OpenMSXSubType.NULL, 3)]
[InlineData(PackingFlag.None, 8)]
[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
}
}

View File

@@ -0,0 +1,65 @@
using System;
using SabreTools.Core.Tools;
using Xunit;
namespace SabreTools.Core.Test.Tools
{
public class DateTimeHelperTests
{
#region ConvertToMsDosTimeFormat
[Fact]
public void ConvertToMsDosTimeFormat_MinSupported()
{
long expected = 2162688;
DateTime dateTime = new DateTime(1980, 01, 01, 00, 00, 00, 00);
long actual = DateTimeHelper.ConvertToMsDosTimeFormat(dateTime);
Assert.Equal(expected, actual);
}
[Fact]
public void ConvertToMsDosTimeFormat_Y2K()
{
long expected = 673251328;
DateTime dateTime = new DateTime(2000, 01, 01, 00, 00, 00, 00);
long actual = DateTimeHelper.ConvertToMsDosTimeFormat(dateTime);
Assert.Equal(expected, actual);
}
#endregion
#region ConvertFromMsDosTimeFormat
[Fact]
public void ConvertFromMsDosTimeFormat_MinSupported()
{
uint msDosDateTime = 2162688;
DateTime actual = DateTimeHelper.ConvertFromMsDosTimeFormat(msDosDateTime);
Assert.Equal(1980, actual.Year);
Assert.Equal(01, actual.Month);
Assert.Equal(01, actual.Day);
Assert.Equal(00, actual.Hour);
Assert.Equal(00, actual.Minute);
Assert.Equal(00, actual.Second);
Assert.Equal(000, actual.Millisecond);
}
[Fact]
public void ConvertFromMsDosTimeFormat_Y2K()
{
uint msDosDateTime = 673251328;
DateTime actual = DateTimeHelper.ConvertFromMsDosTimeFormat(msDosDateTime);
Assert.Equal(2000, actual.Year);
Assert.Equal(01, actual.Month);
Assert.Equal(01, actual.Day);
Assert.Equal(00, actual.Hour);
Assert.Equal(00, actual.Minute);
Assert.Equal(00, actual.Second);
Assert.Equal(000, actual.Millisecond);
}
#endregion
}
}

View File

@@ -0,0 +1,106 @@
using System;
using SabreTools.Core.Tools;
using Xunit;
namespace SabreTools.Core.Test.Tools
{
public class NumberHelperTests
{
#region ConvertToDouble
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("INVALID")]
public void ConvertToDoubleTest_NullExpected(string? numeric)
{
double? actual = NumberHelper.ConvertToDouble(numeric);
Assert.Null(actual);
}
[Theory]
[InlineData("0", 0f)]
[InlineData("100", 100f)]
[InlineData("-100", -100f)]
[InlineData("3.14", 3.14f)]
[InlineData("-3.14", -3.14f)]
public void ConvertToDoubleTest_NumericExpected(string? numeric, double expected)
{
double? actual = NumberHelper.ConvertToDouble(numeric);
Assert.NotNull(actual);
double variance = Math.Abs(expected - actual.Value);
Assert.True(variance < 0.1f);
}
#endregion
#region ConvertToInt64
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("INVALID")]
[InlineData("0b0001")]
[InlineData("0o765")]
[InlineData("01h")]
public void ConvertToInt64_NullExpected(string? numeric)
{
long? actual = NumberHelper.ConvertToInt64(numeric);
Assert.Null(actual);
}
[Theory]
[InlineData("0", 0)]
[InlineData(" 0 ", 0)]
[InlineData("100", 100)]
[InlineData("-100", -100)]
[InlineData("0x01", 1)]
[InlineData("1k", 1_000)]
[InlineData("1ki", 1_024)]
[InlineData("1m", 1_000_000)]
[InlineData("1mi", 1_048_576)]
[InlineData("1g", 1_000_000_000)]
[InlineData("1gi", 1_073_741_824)]
[InlineData("1t", 1_000_000_000_000)]
[InlineData("1ti", 1_099_511_627_776)]
[InlineData("1p", 1_000_000_000_000_000)]
[InlineData("1pi", 1_125_899_906_842_624)]
// [InlineData("1e", 1_000_000_000_000_000_000)]
// [InlineData("1ei", 1_152_921_504_606_846_976)]
// [InlineData("1z", 1_000_000_000_000_000_000_000)]
// [InlineData("1zi", 1_180_591_620_717_411_303_424)]
// [InlineData("1y", 1_000_000_000_000_000_000_000_000)]
// [InlineData("1yi", 1_208_925_819_614_629_174_706_176)]
public void ConvertToInt64_NumericExpected(string? numeric, long expected)
{
long? actual = NumberHelper.ConvertToInt64(numeric);
Assert.NotNull(actual);
Assert.Equal(expected, actual);
}
#endregion
#region IsNumeric
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("0x", false)]
[InlineData("0", true)]
[InlineData("100", true)]
[InlineData("-100", true)]
[InlineData("3.14", true)]
[InlineData("-3.14", true)]
[InlineData("1,000", true)]
[InlineData("-1,000", true)]
[InlineData("1k", true)]
[InlineData("1ki", true)]
public void IsNumericTest(string? value, bool expected)
{
bool actual = NumberHelper.IsNumeric(value);
Assert.Equal(expected, actual);
}
#endregion
}
}

View File

@@ -0,0 +1,186 @@
using SabreTools.Core.Tools;
using Xunit;
namespace SabreTools.Core.Test.Tools
{
public class TextHelperTests
{
#region NormalizeCharacters
// TODO: Write tests for NormalizeCharacters
#endregion
#region NormalizeCRC32
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "00001234")]
[InlineData("0x1234", "00001234")]
[InlineData("1234ABCDE", "")]
[InlineData("0x1234ABCDE", "")]
[InlineData("1234ABCD", "1234abcd")]
[InlineData("0x1234ABCD", "1234abcd")]
[InlineData("abcdefgh", "")]
[InlineData("0xabcdefgh", "")]
public void NormalizeCRC32Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeCRC32(hash);
Assert.Equal(expected, actual);
}
#endregion
#region NormalizeMD5
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "00000000000000000000000000001234")]
[InlineData("0x1234", "00000000000000000000000000001234")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd")]
[InlineData("abcdefghabcdefghabcdefghabcdefgh", "")]
[InlineData("0xabcdefghabcdefghabcdefghabcdefgh", "")]
public void NormalizeMD5Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeMD5(hash);
Assert.Equal(expected, actual);
}
#endregion
#region NormalizeSHA1
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "0000000000000000000000000000000000001234")]
[InlineData("0x1234", "0000000000000000000000000000000000001234")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("abcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
[InlineData("0xabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
public void NormalizeSHA1Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeSHA1(hash);
Assert.Equal(expected, actual);
}
#endregion
#region NormalizeSHA256
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "0000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("0x1234", "0000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
[InlineData("0xabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
public void NormalizeSHA256Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeSHA256(hash);
Assert.Equal(expected, actual);
}
#endregion
#region NormalizeSHA384
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("0x1234", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
[InlineData("0xabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
public void NormalizeSHA384Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeSHA384(hash);
Assert.Equal(expected, actual);
}
#endregion
#region NormalizeSHA512
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("-", "")]
[InlineData("_", "")]
[InlineData("0x", "")]
[InlineData("1234", "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("0x1234", "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCDE", "")]
[InlineData("1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("0x1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD1234ABCD", "1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd")]
[InlineData("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
[InlineData("0xabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh", "")]
public void NormalizeSHA512Test(string? hash, string? expected)
{
string? actual = TextHelper.NormalizeSHA512(hash);
Assert.Equal(expected, actual);
}
#endregion
#region RemovePathUnsafeCharacters
[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("\0", "")]
public void RemovePathUnsafeCharactersTest(string? input, string expected)
{
string? actual = TextHelper.RemovePathUnsafeCharacters(input);
Assert.Equal(expected, actual);
}
#endregion
#region RemoveUnicodeCharacters
[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("Ā", "")]
public void RemoveUnicodeCharactersTest(string? input, string expected)
{
string? actual = TextHelper.RemoveUnicodeCharacters(input);
Assert.Equal(expected, actual);
}
#endregion
}
}

View File

@@ -0,0 +1,105 @@
using SabreTools.Core.Tools;
using Xunit;
namespace SabreTools.Core.Test.Tools
{
public class UtilitiesTests
{
#region ConditionalHashEquals
[Theory]
[InlineData(null, null, true)]
[InlineData(new byte[0], new byte[0], true)]
[InlineData(new byte[] { 0x01 }, new byte[0], true)]
[InlineData(new byte[0], new byte[] { 0x01 }, true)]
[InlineData(new byte[] { 0x01 }, new byte[] { 0x01 }, true)]
[InlineData(new byte[] { 0x01, 0x02 }, new byte[] { 0x01 }, false)]
[InlineData(new byte[] { 0x01 }, new byte[] { 0x01, 0x02 }, false)]
[InlineData(new byte[] { 0x01, 0x02 }, new byte[] { 0x02, 0x01 }, false)]
public void ConditionalHashEquals_Array(byte[]? firstHash, byte[]? secondHash, bool expected)
{
bool actual = Utilities.ConditionalHashEquals(firstHash, secondHash);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, null, true)]
[InlineData("", "", true)]
[InlineData("01", "", true)]
[InlineData("", "01", true)]
[InlineData("01", "01", true)]
[InlineData("0102", "01", false)]
[InlineData("01", "0102", false)]
[InlineData("0102", "0201", false)]
public void ConditionalHashEquals_String(string? firstHash, string? secondHash, bool expected)
{
bool actual = Utilities.ConditionalHashEquals(firstHash, secondHash);
Assert.Equal(expected, actual);
}
#endregion
#region GetDepotPath
[Theory]
[InlineData(null, 0, null)]
[InlineData(null, 4, null)]
[InlineData(new byte[] { 0x12, 0x34, 0x56 }, 0, null)]
[InlineData(new byte[] { 0x12, 0x34, 0x56 }, 4, null)]
[InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
[InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
[InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
public void GetDepotPath_Array(byte[]? hash, int depth, string? expected)
{
string? actual = Utilities.GetDepotPath(hash, depth);
if (System.IO.Path.DirectorySeparatorChar == '/')
expected = expected?.Replace('\\', '/');
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null, 0, null)]
[InlineData(null, 4, null)]
[InlineData("123456", 0, null)]
[InlineData("123456", 4, null)]
[InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
[InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
[InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")]
public void GetDepotPath_String(string? hash, int depth, string? expected)
{
string? actual = Utilities.GetDepotPath(hash, depth);
if (System.IO.Path.DirectorySeparatorChar == '/')
expected = expected?.Replace('\\', '/');
Assert.Equal(expected, actual);
}
#endregion
#region HasValidDatExtension
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("no-extension", false)]
[InlineData("no-extension.", false)]
[InlineData("invalid.ext", false)]
[InlineData("invalid..ext", false)]
[InlineData("INVALID.EXT", false)]
[InlineData("INVALID..EXT", false)]
[InlineData(".dat", true)]
[InlineData(".DAT", true)]
[InlineData("valid_extension.dat", true)]
[InlineData("valid_extension..dat", true)]
[InlineData("valid_extension.DAT", true)]
[InlineData("valid_extension..DAT", true)]
public void HasValidDatExtensionTest(string? path, bool expected)
{
bool actual = Utilities.HasValidDatExtension(path);
Assert.Equal(expected, actual);
}
#endregion
}
}