Remove "S7" from some class names

This commit is contained in:
Peter Butzhammer
2024-02-09 12:33:24 +01:00
parent dd0af70262
commit 4701a224a7
17 changed files with 142 additions and 142 deletions

View File

@@ -0,0 +1,25 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class ConvertBothWays : ConverterTestBase
{
[TestCaseSource(nameof(GetValidTestCases))]
public void Convert(ConverterTestCase tc)
{
//Arrange
var buffer = new byte[tc.VariableAddress.BufferLength];
var write = CreateWriteMethod(tc);
var read = CreateReadMethod(tc);
//Act
write.Invoke(null, [buffer, tc.Value, tc.VariableAddress]);
var result = read.Invoke(null, [buffer, tc.VariableAddress]);
//Assert
result.ShouldBe(tc.Value);
}
}

View File

@@ -0,0 +1,83 @@
using System.Reflection;
using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx.Tests.ValueConverterTests;
internal abstract class ConverterTestBase
{
protected static readonly IVariableNameParser Parser = new VariableNameParser();
public static MethodInfo CreateReadMethod(ConverterTestCase tc)
{
var convertMi = typeof(ValueConverter).GetMethod(nameof(ValueConverter.ReadFromBuffer));
var convert = convertMi!.MakeGenericMethod(tc.Value.GetType());
return convert;
}
public static MethodInfo CreateWriteMethod(ConverterTestCase tc)
{
var writeMi = typeof(ConverterTestBase).GetMethod(nameof(WriteToBuffer));
var write = writeMi!.MakeGenericMethod(tc.Value.GetType());
return write;
}
public static IEnumerable<ConverterTestCase> GetValidTestCases()
{
yield return new ConverterTestCase(true, "DB99.bit5.4", [0x10]);
yield return new ConverterTestCase(false, "DB99.bit5.4", [0x00]);
yield return new ConverterTestCase((byte) 18, "DB99.Byte5", [0x12]);
yield return new ConverterTestCase((short) 4660, "DB99.Int5", [0x12, 0x34]);
yield return new ConverterTestCase((short) -3532, "DB99.Int5", [0xF2, 0x34]);
yield return new ConverterTestCase((ushort) 4660, "DB99.UInt5", [0x12, 0x34]);
yield return new ConverterTestCase((ushort) 62004, "DB99.UInt5", [0xF2, 0x34]);
yield return new ConverterTestCase(305419879, "DB99.DInt5", [0x12, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(-231451033, "DB99.DInt5", [0xF2, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(305419879u, "DB99.UDInt5", [0x12, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(4063516263u, "DB99.UDInt5", [0xF2, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(1311768394163015151L, "DB99.LInt5", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(-994074615050678801L, "DB99.LInt5", [0xF2, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(1311768394163015151uL, "DB99.ULInt5", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(17452669458658872815uL, "DB99.ULInt5", [0xF2, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(0.25f, "DB99.Real5", [0x3E, 0x80, 0x00, 0x00]);
yield return new ConverterTestCase(0.25, "DB99.LReal5", [0x3F, 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
yield return new ConverterTestCase(new byte[] {0x12, 0x34, 0x56, 0x67}, "DB99.Byte5.4", [0x12, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase("ABCD", "DB99.String10.4", [0x04, 0x04, 0x41, 0x42, 0x43, 0x44]);
yield return new ConverterTestCase("ABCD", "DB99.String10.6", [0x06, 0x04, 0x41, 0x42, 0x43, 0x44, 0x00, 0x00]);
yield return new ConverterTestCase("ABCD", "DB99.WString10.4", [0x00, 0x04, 0x00, 0x04, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44]);
yield return new ConverterTestCase("ABCD", "DB99.WString10.6", [0x00, 0x06, 0x00, 0x04, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00]);
yield return new ConverterTestCase("ABCD", "DB99.Byte5.4", [0x41, 0x42, 0x43, 0x44]);
yield return new ConverterTestCase(true, "DB99.DBx0.0", [0x01]);
yield return new ConverterTestCase(false, "DB99.DBx0.0", [0x00]);
yield return new ConverterTestCase(true, "DB99.DBx0.4", [0x10]);
yield return new ConverterTestCase(false, "DB99.DBx0.4", [0]);
yield return new ConverterTestCase((byte) 18, "DB99.DBB0", [0x12]);
yield return new ConverterTestCase((short) 4660, "DB99.INT0", [0x12, 0x34]);
yield return new ConverterTestCase((short) -3532, "DB99.INT0", [0xF2, 0x34]);
yield return new ConverterTestCase(305419879, "DB99.DINT0", [0x12, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(-231451033, "DB99.DINT0", [0xF2, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(1311768394163015151uL, "DB99.dul0", [0x12, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(17452669458658872815uL, "DB99.dul0", [0xF2, 0x34, 0x56, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
yield return new ConverterTestCase(new byte[] {0x12, 0x34, 0x56, 0x67}, "DB99.DBB0.4", [0x12, 0x34, 0x56, 0x67]);
yield return new ConverterTestCase(0.25f, "DB99.D0", [0x3E, 0x80, 0x00, 0x00]);
}
/// <summary>
/// This helper method exists, since I could not manage to invoke a generic method
/// accepring a Span&lt;T&gt; as parameter.
/// </summary>
public static void WriteToBuffer<TValue>(byte[] buffer, TValue value, VariableAddress address)
{
ValueConverter.WriteToBuffer(buffer, value, address);
}
public record ConverterTestCase(object Value, string Address, byte[] Data)
{
public VariableAddress VariableAddress => Parser.Parse(Address);
public override string ToString() => $"{Value.GetType().Name}, {Address}: {Value}";
}
}

View File

@@ -0,0 +1,51 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class ReadFromBuffer : ConverterTestBase
{
[TestCaseSource(nameof(GetValidTestCases))]
[TestCaseSource(nameof(GetAdditinalReadTestCases))]
public void Read(ConverterTestCase tc)
{
//Arrange
var convert = CreateReadMethod(tc);
//Act
var result = convert.Invoke(null, [tc.Data, tc.VariableAddress]);
//Assert
result.ShouldBe(tc.Value);
}
public static IEnumerable<ConverterTestCase> GetAdditinalReadTestCases()
{
yield return new ConverterTestCase(true, "DB0.DBx0.4", [0x1F]);
yield return new ConverterTestCase(false, "DB0.DBx0.4", [0xEF]);
yield return new ConverterTestCase("ABCD", "DB0.string0.6", [0x04, 0x04, 0x41, 0x42, 0x43, 0x44, 0x00, 0x00]); // Length in address exceeds PLC string length
}
[TestCase((char) 18, "DB0.DBB0", new byte[] {0x12})]
public void UnsupportedType<T>(T template, string address, byte[] data)
{
//Arrange
var variableAddress = Parser.Parse(address);
//Act
Should.Throw<UnsupportedS7TypeException>(() => ValueConverter.ReadFromBuffer<T>(data, variableAddress));
}
[TestCase(123, "DB12.DINT3", new byte[] {0x01, 0x02, 0x03})]
[TestCase((short) 123, "DB12.INT3", new byte[] {0xF2})]
[TestCase("ABC", "DB0.string0.6", new byte[] {0x01, 0x02, 0x03})]
public void BufferTooSmall<T>(T template, string address, byte[] data)
{
//Arrange
var variableAddress = Parser.Parse(address);
//Act
Should.Throw<ArgumentException>(() => ValueConverter.ReadFromBuffer<T>(data, variableAddress));
}
}

View File

@@ -0,0 +1,53 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class WriteToBuffer : ConverterTestBase
{
[TestCaseSource(nameof(GetValidTestCases))]
[TestCaseSource(nameof(GetAdditinalWriteTestCases))]
public void Write(ConverterTestCase tc)
{
//Arrange
var buffer = new byte[tc.VariableAddress.BufferLength];
var write = CreateWriteMethod(tc);
//Act
write.Invoke(null, [buffer, tc.Value, tc.VariableAddress]);
//Assert
buffer.ShouldBe(tc.Data);
}
public static IEnumerable<ConverterTestCase> GetAdditinalWriteTestCases()
{
yield return new ConverterTestCase("aaaaBCDE", "DB0.string0.4", [0x04, 0x04, 0x61, 0x61, 0x61, 0x61]); // Length in address exceeds PLC string length
yield return new ConverterTestCase("aaaaBCDE", "DB0.WString0.4", [0x00, 0x04, 0x00, 0x04, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61]); // Length in address exceeds PLC string length
}
[TestCase(18, "DB0.DInt12", 3)]
[TestCase(0.25f, "DB0.Real1", 3)]
[TestCase("test", "DB0.String1.10", 9)]
public void BufferToSmall<T>(T input, string address, int bufferSize)
{
//Arrange
var variableAddress = Parser.Parse(address);
var buffer = new byte[bufferSize];
//Act
Should.Throw<ArgumentException>(() => ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
[TestCase((char) 18, "DB0.DBB0")]
public void UnsupportedType<T>(T input, string address)
{
//Arrange
var variableAddress = Parser.Parse(address);
var buffer = new byte[variableAddress.BufferLength];
//Act
Should.Throw<UnsupportedS7TypeException>(() => ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
}