Improve WriteToBuffer implementation and tests

This commit is contained in:
Peter Butzhammer
2024-02-08 16:45:48 +01:00
parent 3c592c6d46
commit 1001303b8c
4 changed files with 140 additions and 84 deletions

View File

@@ -24,23 +24,23 @@ internal class ReadFromBuffer : ConverterTestBase
{
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.10", [0x04, 0x04, 0x41, 0x42, 0x43, 0x44]); // Length in address exceeds PLC string length
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})]
[TestCase((ushort) 3532, "DB0.INT0", new byte[] {0xF2, 0x34})]
[TestCase(0.25, "DB0.D0", new byte[] {0x3E, 0x80, 0x00, 0x00})]
public void Invalid<T>(T template, string address, byte[] data)
public void UnsupportedType<T>(T template, string address, byte[] data)
{
//Arrange
var variableAddress = Parser.Parse(address);
//Act
Should.Throw<InvalidOperationException>(() => S7ValueConverter.ReadFromBuffer<T>(data, variableAddress));
Should.Throw<UnsupportedS7TypeException>(() => S7ValueConverter.ReadFromBuffer<T>(data, variableAddress));
}
[TestCase(3532, "DB0.DINT0", new byte[] {0xF2, 0x34})]
public void Argument<T>(T template, string address, byte[] data)
[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);

View File

@@ -1,13 +1,13 @@
using NUnit.Framework;
using Sharp7.Rx.Interfaces;
using Shouldly;
namespace Sharp7.Rx.Tests.S7ValueConverterTests;
[TestFixture]
internal class WriteToBuffer:ConverterTestBase
internal class WriteToBuffer : ConverterTestBase
{
[TestCaseSource(nameof(GetValidTestCases))]
[TestCaseSource(nameof(GetAdditinalWriteTestCases))]
public void Write(ConverterTestCase tc)
{
//Arrange
@@ -21,15 +21,33 @@ internal class WriteToBuffer:ConverterTestBase
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>(() => S7ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
[TestCase((char) 18, "DB0.DBB0")]
[TestCase(0.25, "DB0.D0")]
public void Invalid<T>(T input, string address)
public void UnsupportedType<T>(T input, string address)
{
//Arrange
var variableAddress = Parser.Parse(address);
var buffer = new byte[variableAddress.BufferLength];
//Act
Should.Throw<InvalidOperationException>(() => S7ValueConverter.WriteToBuffer<T>(buffer, input, variableAddress));
Should.Throw<UnsupportedS7TypeException>(() => S7ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
}