mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add tests for Core; fix found issues
This commit is contained in:
1871
SabreTools.Core.Test/DictionaryBaseExtensionsTests.cs
Normal file
1871
SabreTools.Core.Test/DictionaryBaseExtensionsTests.cs
Normal file
File diff suppressed because it is too large
Load Diff
40
SabreTools.Core.Test/Filter/ExtraIniItemTests.cs
Normal file
40
SabreTools.Core.Test/Filter/ExtraIniItemTests.cs
Normal 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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
192
SabreTools.Core.Test/Filter/FieldManipulatorTests.cs
Normal file
192
SabreTools.Core.Test/Filter/FieldManipulatorTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
82
SabreTools.Core.Test/Filter/FilterKeyTests.cs
Normal file
82
SabreTools.Core.Test/Filter/FilterKeyTests.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
471
SabreTools.Core.Test/Filter/FilterObjectTests.cs
Normal file
471
SabreTools.Core.Test/Filter/FilterObjectTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
220
SabreTools.Core.Test/Filter/FilterRunnerTests.cs
Normal file
220
SabreTools.Core.Test/Filter/FilterRunnerTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
41
SabreTools.Core.Test/SabreTools.Core.Test.csproj
Normal file
41
SabreTools.Core.Test/SabreTools.Core.Test.csproj
Normal 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>
|
||||
12
SabreTools.Core.Test/TestData/extra.ini
Normal file
12
SabreTools.Core.Test/TestData/extra.ini
Normal file
@@ -0,0 +1,12 @@
|
||||
[FOLDER_SETTINGS]
|
||||
RootFolderIcon mame
|
||||
SubFolderIcon folder
|
||||
|
||||
[ROOT_FOLDER]
|
||||
useBool
|
||||
|
||||
[Value]
|
||||
useValue
|
||||
|
||||
[Other]
|
||||
useOther
|
||||
@@ -3,8 +3,9 @@ using SabreTools.DatFiles;
|
||||
using SabreTools.DatItems;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Test.Core
|
||||
namespace SabreTools.Core.Test.Tools
|
||||
{
|
||||
// TODO: Remove reliance on anything but SabreTools.Core
|
||||
public class ConvertersTests
|
||||
{
|
||||
#region String to Enum
|
||||
@@ -357,6 +358,19 @@ namespace SabreTools.Test.Core
|
||||
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
|
||||
@@ -708,6 +722,16 @@ namespace SabreTools.Test.Core
|
||||
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
|
||||
65
SabreTools.Core.Test/Tools/DateTimeHelperTests.cs
Normal file
65
SabreTools.Core.Test/Tools/DateTimeHelperTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
106
SabreTools.Core.Test/Tools/NumberHelperTests.cs
Normal file
106
SabreTools.Core.Test/Tools/NumberHelperTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
186
SabreTools.Core.Test/Tools/TextHelperTests.cs
Normal file
186
SabreTools.Core.Test/Tools/TextHelperTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
105
SabreTools.Core.Test/Tools/UtilitiesTests.cs
Normal file
105
SabreTools.Core.Test/Tools/UtilitiesTests.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -74,9 +74,14 @@ namespace SabreTools.Core
|
||||
if (disk == null)
|
||||
return null;
|
||||
|
||||
// Append a suffix to the name
|
||||
string? name = disk.ReadString(Disk.NameKey);
|
||||
if (name != null)
|
||||
name += ".chd";
|
||||
|
||||
return new Rom
|
||||
{
|
||||
[Rom.NameKey] = disk.ReadString(Disk.NameKey) + ".chd",
|
||||
[Rom.NameKey] = name,
|
||||
[Rom.MergeKey] = disk.ReadString(Disk.MergeKey),
|
||||
[Rom.RegionKey] = disk.ReadString(Disk.RegionKey),
|
||||
[Rom.StatusKey] = disk.ReadString(Disk.StatusKey),
|
||||
@@ -95,9 +100,14 @@ namespace SabreTools.Core
|
||||
if (media == null)
|
||||
return null;
|
||||
|
||||
// Append a suffix to the name
|
||||
string? name = media.ReadString(Media.NameKey);
|
||||
if (name != null)
|
||||
name += ".aaruf";
|
||||
|
||||
return new Rom
|
||||
{
|
||||
[Rom.NameKey] = media.ReadString(Media.NameKey) + ".aaruf",
|
||||
[Rom.NameKey] = name,
|
||||
[Rom.MD5Key] = media.ReadString(Media.MD5Key),
|
||||
[Rom.SHA1Key] = media.ReadString(Media.SHA1Key),
|
||||
[Rom.SHA256Key] = media.ReadString(Media.SHA256Key),
|
||||
@@ -179,8 +189,18 @@ namespace SabreTools.Core
|
||||
break;
|
||||
|
||||
default:
|
||||
if (kvp.Value != other[kvp.Key])
|
||||
// Handle cases where a null is involved
|
||||
if (kvp.Value == null && other[kvp.Key] == null)
|
||||
return true;
|
||||
else if (kvp.Value == null && other[kvp.Key] != null)
|
||||
return false;
|
||||
else if (kvp.Value != null && other[kvp.Key] == null)
|
||||
return false;
|
||||
|
||||
// Try to rely on type hashes
|
||||
else if (kvp.Value!.GetHashCode() != other[kvp.Key]!.GetHashCode())
|
||||
return false;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -257,6 +277,8 @@ namespace SabreTools.Core
|
||||
// If we have a file that has no known size, rely on the hashes only
|
||||
if (selfSize == null && self.HashMatch(other))
|
||||
return true;
|
||||
else if (otherSize == null && self.HashMatch(other))
|
||||
return true;
|
||||
|
||||
// If we get a partial match
|
||||
if (selfSize == otherSize && self.HashMatch(other))
|
||||
@@ -714,11 +736,8 @@ namespace SabreTools.Core
|
||||
/// <summary>
|
||||
/// Get unique duplicate suffix on name collision
|
||||
/// </summary>
|
||||
private static string GetDuplicateSuffix(this Disk? self)
|
||||
private static string GetDuplicateSuffix(this Disk self)
|
||||
{
|
||||
if (self == null)
|
||||
return string.Empty;
|
||||
|
||||
string? md5 = self.ReadString(Disk.MD5Key);
|
||||
if (!string.IsNullOrEmpty(md5))
|
||||
return $"_{md5}";
|
||||
@@ -733,11 +752,8 @@ namespace SabreTools.Core
|
||||
/// <summary>
|
||||
/// Get unique duplicate suffix on name collision
|
||||
/// </summary>
|
||||
private static string GetDuplicateSuffix(this Media? self)
|
||||
private static string GetDuplicateSuffix(this Media self)
|
||||
{
|
||||
if (self == null)
|
||||
return string.Empty;
|
||||
|
||||
string? md5 = self.ReadString(Media.MD5Key);
|
||||
if (!string.IsNullOrEmpty(md5))
|
||||
return $"_{md5}";
|
||||
@@ -760,11 +776,8 @@ namespace SabreTools.Core
|
||||
/// <summary>
|
||||
/// Get unique duplicate suffix on name collision
|
||||
/// </summary>
|
||||
private static string GetDuplicateSuffix(this Rom? self)
|
||||
private static string GetDuplicateSuffix(this Rom self)
|
||||
{
|
||||
if (self == null)
|
||||
return string.Empty;
|
||||
|
||||
string? crc = self.ReadString(Rom.CRCKey);
|
||||
if (!string.IsNullOrEmpty(crc))
|
||||
return $"_{crc}";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SabreTools.IO.Logging;
|
||||
using SabreTools.IO.Readers;
|
||||
|
||||
@@ -12,7 +13,6 @@ namespace SabreTools.Core.Filter
|
||||
/// <summary>
|
||||
/// Item type and field to update with INI information
|
||||
/// </summary>
|
||||
/// <remarks>Formatted like "ItemName.FieldName"</remarks>
|
||||
public readonly FilterKey Key;
|
||||
|
||||
/// <summary>
|
||||
@@ -24,17 +24,17 @@ namespace SabreTools.Core.Filter
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ExtraIniItem(string key, string ini)
|
||||
public ExtraIniItem(string key, string iniPath)
|
||||
{
|
||||
Key = new FilterKey(key);
|
||||
if (!PopulateFromFile(ini))
|
||||
if (!PopulateFromFile(iniPath))
|
||||
Mappings.Clear();
|
||||
}
|
||||
|
||||
public ExtraIniItem(string itemName, string fieldName, string ini)
|
||||
public ExtraIniItem(string itemName, string fieldName, string iniPath)
|
||||
{
|
||||
Key = new FilterKey(itemName, fieldName);
|
||||
if (!PopulateFromFile(ini))
|
||||
if (!PopulateFromFile(iniPath))
|
||||
Mappings.Clear();
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace SabreTools.Core.Filter
|
||||
/// <summary>
|
||||
/// Populate the dictionary from an INI file
|
||||
/// </summary>
|
||||
/// <param name="ini">Path to INI file to populate from</param>
|
||||
/// <param name="iniPath">Path to INI file to populate from</param>
|
||||
/// <remarks>
|
||||
/// The INI file format that is supported here is not exactly the same
|
||||
/// as a traditional one. This expects a MAME extras format, which usually
|
||||
@@ -54,10 +54,16 @@ namespace SabreTools.Core.Filter
|
||||
/// the value is boolean. If there's another section name, then that is set
|
||||
/// as the value instead.
|
||||
/// </remarks>
|
||||
private bool PopulateFromFile(string ini)
|
||||
private bool PopulateFromFile(string iniPath)
|
||||
{
|
||||
// Prepare all intenral variables
|
||||
IniReader ir = new(ini) { ValidateRows = false };
|
||||
// Validate the path
|
||||
if (iniPath.Length == 0)
|
||||
return false;
|
||||
else if (!File.Exists(iniPath))
|
||||
return false;
|
||||
|
||||
// Prepare all internal variables
|
||||
var ir = new IniReader(iniPath) { ValidateRows = false };
|
||||
bool foundRootFolder = false;
|
||||
|
||||
// If we got a null reader, just return
|
||||
@@ -107,7 +113,7 @@ namespace SabreTools.Core.Filter
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerImpl.Warning(ex, $"Exception found while parsing '{ini}'");
|
||||
LoggerImpl.Warning(ex, $"Exception found while parsing '{iniPath}'");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,11 +55,11 @@ namespace SabreTools.Core.Filter
|
||||
itemName = string.Empty; fieldName = string.Empty;
|
||||
|
||||
// If we don't have a filter ID, we can't do anything
|
||||
if (itemFieldString == null)
|
||||
if (string.IsNullOrEmpty(itemFieldString))
|
||||
return false;
|
||||
|
||||
// If we only have one part, we can't do anything
|
||||
string[] splitFilter = itemFieldString.Split('.');
|
||||
string[] splitFilter = itemFieldString!.Split('.');
|
||||
if (splitFilter.Length != 2)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ namespace SabreTools.Core.Filter
|
||||
/// </summary>
|
||||
public readonly Operation Operation;
|
||||
|
||||
public FilterObject(string filterString)
|
||||
public FilterObject(string? filterString)
|
||||
{
|
||||
if (!SplitFilterString(filterString, out var keyItem, out Operation operation, out var value))
|
||||
throw new ArgumentOutOfRangeException(nameof(filterString));
|
||||
throw new ArgumentException(nameof(filterString));
|
||||
|
||||
Key = new FilterKey(keyItem);
|
||||
Value = value;
|
||||
@@ -57,7 +57,6 @@ namespace SabreTools.Core.Filter
|
||||
/// </summary>
|
||||
public bool Matches(DictionaryBase dictionaryBase)
|
||||
{
|
||||
// TODO: Add validation of dictionary base type from the key values
|
||||
return Operation switch
|
||||
{
|
||||
Operation.Equals => MatchesEqual(dictionaryBase),
|
||||
@@ -77,16 +76,16 @@ namespace SabreTools.Core.Filter
|
||||
{
|
||||
// If the key doesn't exist, we count it as null
|
||||
if (!dictionaryBase.ContainsKey(Key.FieldName))
|
||||
return Value == null;
|
||||
return string.IsNullOrEmpty(Value);
|
||||
|
||||
// If the value in the dictionary is null
|
||||
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
|
||||
if (checkValue == null)
|
||||
return Value == null;
|
||||
return string.IsNullOrEmpty(Value);
|
||||
|
||||
// If we have both a potentally boolean check and value
|
||||
bool? checkValueBool = ConvertToBoolean(checkValue);
|
||||
bool? matchValueBool = ConvertToBoolean(Value);
|
||||
bool? checkValueBool = checkValue.AsYesNo();
|
||||
bool? matchValueBool = Value.AsYesNo();
|
||||
if (checkValueBool != null && matchValueBool != null)
|
||||
return checkValueBool == matchValueBool;
|
||||
|
||||
@@ -120,16 +119,16 @@ namespace SabreTools.Core.Filter
|
||||
{
|
||||
// If the key doesn't exist, we count it as null
|
||||
if (!dictionaryBase.ContainsKey(Key.FieldName))
|
||||
return Value != null;
|
||||
return !string.IsNullOrEmpty(Value);
|
||||
|
||||
// If the value in the dictionary is null
|
||||
string? checkValue = dictionaryBase.ReadString(Key.FieldName);
|
||||
if (checkValue == null)
|
||||
return Value == null;
|
||||
return !string.IsNullOrEmpty(Value);
|
||||
|
||||
// If we have both a potentally boolean check and value
|
||||
bool? checkValueBool = ConvertToBoolean(checkValue);
|
||||
bool? matchValueBool = ConvertToBoolean(Value);
|
||||
bool? checkValueBool = checkValue.AsYesNo();
|
||||
bool? matchValueBool = Value.AsYesNo();
|
||||
if (checkValueBool != null && matchValueBool != null)
|
||||
return checkValueBool != matchValueBool;
|
||||
|
||||
@@ -309,19 +308,11 @@ namespace SabreTools.Core.Filter
|
||||
return false;
|
||||
|
||||
// If we find a special character, try parsing as regex
|
||||
#if NETFRAMEWORK
|
||||
if (value.Contains("^")
|
||||
|| value.Contains("$")
|
||||
|| value.Contains("*")
|
||||
|| value.Contains("?")
|
||||
|| value.Contains("+"))
|
||||
#else
|
||||
if (value.Contains('^')
|
||||
|| value.Contains('$')
|
||||
|| value.Contains('*')
|
||||
|| value.Contains('?')
|
||||
|| value.Contains('+'))
|
||||
#endif
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -337,23 +328,6 @@ namespace SabreTools.Core.Filter
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a string to a Boolean
|
||||
/// </summary>
|
||||
private bool? ConvertToBoolean(string? value)
|
||||
{
|
||||
// If we don't have a valid string, we can't do anything
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return null;
|
||||
|
||||
return value!.ToLowerInvariant() switch
|
||||
{
|
||||
"true" or "yes" => true,
|
||||
"false" or "no" => false,
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Derive an operation from the input string, if possible
|
||||
/// </summary>
|
||||
@@ -388,11 +362,11 @@ namespace SabreTools.Core.Filter
|
||||
// Set default values
|
||||
key = null; operation = Operation.NONE; value = null;
|
||||
|
||||
if (filterString == null)
|
||||
if (string.IsNullOrEmpty(filterString))
|
||||
return false;
|
||||
|
||||
// Trim quotations, if necessary
|
||||
if (filterString.StartsWith("\""))
|
||||
if (filterString!.StartsWith("\""))
|
||||
filterString = filterString.Substring(1, filterString.Length - 2);
|
||||
|
||||
// Split the string using regex
|
||||
@@ -402,7 +376,10 @@ namespace SabreTools.Core.Filter
|
||||
|
||||
key = match.Groups["itemField"].Value;
|
||||
operation = GetOperation(match.Groups["operation"].Value);
|
||||
value = match.Groups["value"].Value;
|
||||
|
||||
// Only non-zero length values are counted as non-null
|
||||
if (value?.Length > 0)
|
||||
value = match.Groups["value"].Value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SabreTools.Test" />
|
||||
<InternalsVisibleTo Include="SabreTools.Core.Test" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Support for old .NET versions -->
|
||||
|
||||
@@ -14,9 +14,11 @@ namespace SabreTools.Core.Tools
|
||||
private readonly static long GigaByte = (long)Math.Pow(KiloByte, 3);
|
||||
private readonly static long TeraByte = (long)Math.Pow(KiloByte, 4);
|
||||
private readonly static long PetaByte = (long)Math.Pow(KiloByte, 5);
|
||||
private readonly static long ExaByte = (long)Math.Pow(KiloByte, 6);
|
||||
private readonly static long ZettaByte = (long)Math.Pow(KiloByte, 7);
|
||||
private readonly static long YottaByte = (long)Math.Pow(KiloByte, 8);
|
||||
|
||||
// The following are too big to be represented in Int64
|
||||
// private readonly static long ExaByte = (long)Math.Pow(KiloByte, 6);
|
||||
// private readonly static long ZettaByte = (long)Math.Pow(KiloByte, 7);
|
||||
// private readonly static long YottaByte = (long)Math.Pow(KiloByte, 8);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -27,9 +29,11 @@ namespace SabreTools.Core.Tools
|
||||
private readonly static long GibiByte = (long)Math.Pow(KibiByte, 3);
|
||||
private readonly static long TibiByte = (long)Math.Pow(KibiByte, 4);
|
||||
private readonly static long PibiByte = (long)Math.Pow(KibiByte, 5);
|
||||
private readonly static long ExiByte = (long)Math.Pow(KibiByte, 6);
|
||||
private readonly static long ZittiByte = (long)Math.Pow(KibiByte, 7);
|
||||
private readonly static long YittiByte = (long)Math.Pow(KibiByte, 8);
|
||||
|
||||
// The following are too big to be represented in Int64
|
||||
// private readonly static long ExiByte = (long)Math.Pow(KibiByte, 6);
|
||||
// private readonly static long ZittiByte = (long)Math.Pow(KibiByte, 7);
|
||||
// private readonly static long YittiByte = (long)Math.Pow(KibiByte, 8);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -108,18 +112,20 @@ namespace SabreTools.Core.Tools
|
||||
multiplier = PetaByte;
|
||||
else if (numeric.EndsWith("pi") || numeric.EndsWith("pib"))
|
||||
multiplier = PibiByte;
|
||||
else if (numeric.EndsWith("e") || numeric.EndsWith("eb"))
|
||||
multiplier = ExaByte;
|
||||
else if (numeric.EndsWith("ei") || numeric.EndsWith("eib"))
|
||||
multiplier = ExiByte;
|
||||
else if (numeric.EndsWith("z") || numeric.EndsWith("zb"))
|
||||
multiplier = ZettaByte;
|
||||
else if (numeric.EndsWith("zi") || numeric.EndsWith("zib"))
|
||||
multiplier = ZittiByte;
|
||||
else if (numeric.EndsWith("y") || numeric.EndsWith("yb"))
|
||||
multiplier = YottaByte;
|
||||
else if (numeric.EndsWith("yi") || numeric.EndsWith("yib"))
|
||||
multiplier = YittiByte;
|
||||
|
||||
// The following are too big to be represented in Int64
|
||||
// else if (numeric.EndsWith("e") || numeric.EndsWith("eb"))
|
||||
// multiplier = ExaByte;
|
||||
// else if (numeric.EndsWith("ei") || numeric.EndsWith("eib"))
|
||||
// multiplier = ExiByte;
|
||||
// else if (numeric.EndsWith("z") || numeric.EndsWith("zb"))
|
||||
// multiplier = ZettaByte;
|
||||
// else if (numeric.EndsWith("zi") || numeric.EndsWith("zib"))
|
||||
// multiplier = ZittiByte;
|
||||
// else if (numeric.EndsWith("y") || numeric.EndsWith("yb"))
|
||||
// multiplier = YottaByte;
|
||||
// else if (numeric.EndsWith("yi") || numeric.EndsWith("yib"))
|
||||
// multiplier = YittiByte;
|
||||
|
||||
return multiplier;
|
||||
}
|
||||
@@ -138,9 +144,18 @@ namespace SabreTools.Core.Tools
|
||||
if (value.StartsWith("0x"))
|
||||
value = value.Substring(2);
|
||||
|
||||
// If we have a negative value
|
||||
if (value.StartsWith("-"))
|
||||
value = value.Substring(1);
|
||||
|
||||
// If the value has a multiplier
|
||||
if (DetermineMultiplier(value) > 1)
|
||||
value = value.TrimEnd(['k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ']);
|
||||
|
||||
// If the value is empty after trimming
|
||||
if (value.Length == 0)
|
||||
return false;
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
return value.All(c => char.IsAsciiHexDigit(c) || c == '.' || c == ',');
|
||||
#else
|
||||
|
||||
@@ -80,8 +80,14 @@ namespace SabreTools.Core.Tools
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all unicode-specific chars from a string
|
||||
/// Remove all Unicode-specific chars from a string
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// "Unicode characters" here means any characters outside of the
|
||||
/// Extended ASCII (0x00 to 0xFF) set. This is just a simple
|
||||
/// way of filtering out characters that won't work on all
|
||||
/// supported platforms.
|
||||
/// </remarks>
|
||||
public static string RemoveUnicodeCharacters(string? input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
@@ -151,9 +157,9 @@ namespace SabreTools.Core.Tools
|
||||
private static string? NormalizeHashData(string? hash, int expectedLength)
|
||||
{
|
||||
// If we have a known blank hash, return blank
|
||||
if (string.IsNullOrEmpty(hash))
|
||||
if (hash == null)
|
||||
return null;
|
||||
else if (hash == "-" || hash == "_")
|
||||
else if (hash == string.Empty || hash == "-" || hash == "_")
|
||||
return string.Empty;
|
||||
|
||||
// Check to see if it's a "hex" hash
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
using SabreTools.Core.Tools;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Test.Core
|
||||
{
|
||||
public class UtiltiesTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData("null", null)]
|
||||
[InlineData("0b00001", null)]
|
||||
[InlineData("12345", 12345L)]
|
||||
[InlineData("00000000012345", 12345L)]
|
||||
[InlineData("10h", null)]
|
||||
[InlineData("0x10", 16L)]
|
||||
[InlineData(" 12345 ", 12345L)]
|
||||
public void CleanLongTest(string? input, long? expected)
|
||||
{
|
||||
long? actual = NumberHelper.ConvertToInt64(input);
|
||||
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 GetDepotPathTest(string? 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, false)]
|
||||
[InlineData("", false)]
|
||||
[InlineData("no-extension", false)]
|
||||
[InlineData("invalid.ext", false)]
|
||||
[InlineData("INVALID.EXT", false)]
|
||||
[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
using SabreTools.Core.Filter;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Test.Filter
|
||||
{
|
||||
public class FilteringTests
|
||||
{
|
||||
[Fact]
|
||||
public void PassesFiltersDatItemFilterPass()
|
||||
{
|
||||
// Setup filter
|
||||
var filter = new FilterRunner(["rom.name:foo", "item.name:foo"]);
|
||||
|
||||
// Setup DatItem
|
||||
var datItem = CreateDatItem();
|
||||
|
||||
// Run filters
|
||||
bool actual = datItem.PassesFilter(filter);
|
||||
Assert.True(actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PassesFiltersDatItemFilterFail()
|
||||
{
|
||||
// Setup filter
|
||||
var filter = new FilterRunner(["rom.name:bar", "item.name:bar"]);
|
||||
|
||||
// Setup DatItem
|
||||
var datItem = CreateDatItem();
|
||||
|
||||
// Run filters
|
||||
bool actual = datItem.PassesFilter(filter);
|
||||
Assert.False(actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PassesFiltersMachineFilterPass()
|
||||
{
|
||||
// Setup filter
|
||||
var filter = new FilterRunner(["machine.name:bar"]);
|
||||
|
||||
// Setup DatItem
|
||||
var datItem = CreateDatItem();
|
||||
|
||||
// Run filters
|
||||
bool actual = datItem.PassesFilter(filter);
|
||||
Assert.True(actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PassesFiltersMachineFilterFail()
|
||||
{
|
||||
// Setup filter
|
||||
var filter = new FilterRunner(["machine.name:foo"]);
|
||||
|
||||
// Setup DatItem
|
||||
var datItem = CreateDatItem();
|
||||
|
||||
// Run filters
|
||||
bool actual = datItem.PassesFilter(filter);
|
||||
Assert.False(actual);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a consistent DatItem for testing
|
||||
/// </summary>
|
||||
private static DatItem CreateDatItem()
|
||||
{
|
||||
var machine = new Machine();
|
||||
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "bar");
|
||||
machine.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, "bar");
|
||||
|
||||
var rom = new Rom();
|
||||
rom.SetName("foo");
|
||||
rom.SetFieldValue<Machine>(DatItem.MachineKey, machine);
|
||||
|
||||
return rom;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using System;
|
||||
using SabreTools.Core.Filter;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Test.Filter
|
||||
{
|
||||
public class PopulationTests
|
||||
{
|
||||
[Fact]
|
||||
public void PopulateFilterRunnerEmptyListTest()
|
||||
{
|
||||
// Setup the list
|
||||
string[]? filters = [];
|
||||
|
||||
// Setup the filter runner
|
||||
var filterRunner = new FilterRunner(filters);
|
||||
|
||||
// Check the filters
|
||||
Assert.NotNull(filterRunner.Filters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PopulateFilterMachineFieldTest()
|
||||
{
|
||||
// Setup the list
|
||||
string[] filters =
|
||||
[
|
||||
"machine.name:foo",
|
||||
"machine.name!:bar",
|
||||
];
|
||||
|
||||
// Setup the filter
|
||||
var filter = new FilterRunner(filters);
|
||||
|
||||
// Check the filters
|
||||
Assert.Equal("machine.name", filter.Filters[0].Key.ToString());
|
||||
Assert.Equal("foo", filter.Filters[0].Value);
|
||||
Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
|
||||
|
||||
Assert.Equal("machine.name", filter.Filters[1].Key.ToString());
|
||||
Assert.Equal("bar", filter.Filters[1].Value);
|
||||
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PopulateFilterDatItemFieldTest()
|
||||
{
|
||||
// Setup the list
|
||||
string[] filters =
|
||||
[
|
||||
"rom.name:foo",
|
||||
"datitem.name!:bar"
|
||||
];
|
||||
|
||||
// Setup the filter
|
||||
var filter = new FilterRunner(filters);
|
||||
|
||||
// Check the filters
|
||||
Assert.Equal("rom.name", filter.Filters[0].Key.ToString());
|
||||
Assert.Equal("foo", filter.Filters[0].Value);
|
||||
Assert.Equal(Operation.Equals, filter.Filters[0].Operation);
|
||||
|
||||
Assert.Equal("item.name", filter.Filters[1].Key.ToString());
|
||||
Assert.Equal("bar", filter.Filters[1].Value);
|
||||
Assert.Equal(Operation.NotEquals, filter.Filters[1].Operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SabreTools.Reports", "Sabre
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SabreTools.Test", "SabreTools.Test\SabreTools.Test.csproj", "{5B4E67D5-F4DA-4750-8FE2-04D08E343791}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Core.Test", "SabreTools.Core.Test\SabreTools.Core.Test.csproj", "{33CF1613-2190-4987-8BE3-73AF196327D3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -108,6 +110,14 @@ Global
|
||||
{5B4E67D5-F4DA-4750-8FE2-04D08E343791}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5B4E67D5-F4DA-4750-8FE2-04D08E343791}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{5B4E67D5-F4DA-4750-8FE2-04D08E343791}.Release|x64.Build.0 = Release|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{33CF1613-2190-4987-8BE3-73AF196327D3}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user