Implement MatchesType

This commit is contained in:
Peter Butzhammer
2024-02-08 17:54:15 +01:00
parent fdc25d2817
commit 829dee14af
4 changed files with 77 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework;
using Sharp7.Rx.Extensions;
using Sharp7.Rx.Interfaces;
using Sharp7.Rx.Tests.S7ValueConverterTests;
using Shouldly;
namespace Sharp7.Rx.Tests.S7VariableAddressTests;
@@ -11,19 +11,61 @@ public class MatchesType
{
static readonly IS7VariableNameParser parser = new S7VariableNameParser();
public void Supported(Type type, string address)
private static readonly IReadOnlyList<Type> typeList = new[]
{
Check(type, address, true);
typeof(byte),
typeof(byte[]),
typeof(bool),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(string),
typeof(int[]),
typeof(float[]),
typeof(DateTime[]),
typeof(object),
};
[TestCaseSource(nameof(GetValid))]
public void Supported(TestCase tc) => Check(tc.Type, tc.Address, true);
[TestCaseSource(nameof(GetInvalid))]
public void Unsupported(TestCase tc) => Check(tc.Type, tc.Address, false);
public static IEnumerable<TestCase> GetValid()
{
return
ConverterTestBase.GetValidTestCases()
.Select(tc => new TestCase(tc.Value.GetType(), tc.Address));
}
public IEnumerable<TestCase> GetValid()
public static IEnumerable<TestCase> GetInvalid()
{
yield return new TestCase(typeof(bool), "DB0.DBx0.0");
yield return new TestCase(typeof(short), "DB0.INT0");
yield return new TestCase(typeof(int), "DB0.DINT0");
yield return new TestCase(typeof(long), "DB0.DUL0");
yield return new TestCase(typeof(ulong), "DB0.DUL0");
return
ConverterTestBase.GetValidTestCases()
.DistinctBy(tc => tc.Value.GetType())
.SelectMany(tc =>
typeList.Where(type => type != tc.Value.GetType())
.Select(type => new TestCase(type, tc.Address))
)
// Explicitly remove some valid combinations
.Where(tc => !(
(tc.Type == typeof(string) && tc.Address == "DB99.Byte5") ||
(tc.Type == typeof(string) && tc.Address == "DB99.Byte5.4") ||
(tc.Type == typeof(byte[]) && tc.Address == "DB99.Byte5")
))
;
}
@@ -36,5 +78,8 @@ public class MatchesType
variableAddress.MatchesType(type).ShouldBe(expected);
}
public record TestCase(Type Type, string Address);
public record TestCase(Type Type, string Address)
{
public override string ToString() => $"{Type.Name} {Address}";
}
}